Skip to content
المشاريع

lucidrains/fast-weight-attention

عرض على GitHub

حول

Implementation of Fast Weight Attention

الذكاء الاصطناعي وتعلّم الآلة

كيف تُقاس هذه الأرقام

النمو المقاس للنجوم

خلال 24 ساعة
٠
خلال 7 أيام
جارٍ جمع السجل
خلال 15 يومًا
جارٍ جمع السجل
خلال شهر
جارٍ جمع السجل

تغيّر صافي النجوم خلال فترات متداخلة، وليس مجموعًا يوميًا. تبقى القياسات المفقودة فارغة. النجوم تقيس الاهتمام لا جودة البرمجيات.

README.md

الأصل

Fast Weight Attention

An attention based fast weight episodic memory, in the same vein as the memory MLP from TTT / Titans and fast weight PKM from Sakana AI

Install

$ pip install fast-weight-attention

Usage

import torch
from fast_weight_attention import FastWeightAttention

mem = FastWeightAttention(512, causal = True)

tokens = torch.randn(1, 64, 512)

past_mem = None

retrieved, next_mem = mem(tokens, past_mem = past_mem, return_next_memories = True)
retrieved, next_mem = mem(tokens, past_mem = next_mem, return_next_memories = True)
retrieved, next_mem = mem(tokens, past_mem = next_mem, return_next_memories = True)

assert retrieved.shape == tokens.shape

# you can then retrieve without fast weight updating

retrieved = mem(tokens, return_next_memories = False)

Sequences are segmented internally at chunk_size boundaries (defaults to the full sequence), carrying the fast weight memories across chunks. The last token of each completed chunk is marked with a learned boundary embedding, and its store target wraps around to the chunk's first token - no lookahead needed. Chunks shorter than chunk_size are processed immediately, with the last token of an incomplete chunk excluded from the fast weight update.

Transformer XL segment recurrence

Set use_transformer_xl = True to let each chunk attend to the cached keys and values of the immediately preceding segment, in addition to the fast weight memory. Long-range information still flows through the fast weights, while the very last segment stays available verbatim as a short-term KV memory.

mem = FastWeightAttention(
    512,
    chunk_size = 64,
    use_transformer_xl = True
)

retrieved, next_mem = mem(tokens, return_next_memories = True)

# next_mem.last_segment holds the last segment's keys and values
# next_mem.detach() drops both the fast weight graph and the segment cache graph

The cache is one segment deep and is replaced at every segment boundary. It is detached along with the fast weight memories when detach_next_memories_every triggers, which gives the usual truncated BPTT behavior for long sequences. The forget gate applies to the fast weight memory, not to the segment cache.

bench_transformer_xl.py compares the toy copy task with and without the cache:

$ python bench_transformer_xl.py --seeds 42 43 44 --num-batches 300

On two 8-token chunks, the cached version reaches 99.9% second-half accuracy at 300 steps versus 89.8% without it, and is already ahead at 50 steps (77.6% versus 65.7%). With the fast-weight inner updates disabled, only the cached version can solve the task at all, since without it the second chunk has no access to the first.

Citations

@article{zhang2026loger,
    title   = {LoGeR: Long-Context Geometric Reconstruction with Hybrid Memory},
    author  = {Zhang, Junyi and Herrmann, Charles and Hur, Junhwa and Sun, Chen and Yang, Ming-Hsuan and Cole, Forrester and Darrell, Trevor and Sun, Deqing},
    journal = {arXiv preprint arXiv:2603.03269},
    year    = {2026}
}
@misc{zhao2026fastweightproductkeymemory,
    title   = {Fast-weight Product Key Memory},
    author  = {Tianyu Zhao and Llion Jones},
    year    = {2026},
    eprint  = {2601.00671},
    archivePrefix = {arXiv},
    primaryClass = {cs.CL},
    url     = {https://arxiv.org/abs/2601.00671},
}
@misc{jordan2024muon,
    author  = {Keller Jordan and Yuchen Jin and Vlado Boza and Jiacheng You and Franz Cesista and Laker Newhouse and Jeremy Bernstein},
    title   = {Muon: An optimizer for hidden layers in neural networks},
    year    = {2024},
    url     = {https://kellerjordan.github.io/posts/muon/}
}
@article{Yaghoubietal2026,
    author  = {Yaghoubi, Mohammad and Nieto-Posadas, Andres and Mosser, Coralie-Anne and Gisiger, Thomas and Wilson, Émmanuel and Williams, Sylvain and Brandon, Mark P.},
    title   = {Predictive coding of reward in the hippocampus},
    journal = {Nature},
    year    = {2026},
    doi     = {10.1038/s41586-025-09958-0}
}
@article{volchkov2026cliptogrok,
    title   = {Clip to Grok: Weight Norm Clipping for Accelerated Generalization},
    author  = {Volchkov, Vladimir and Rivlin, Aviad},
    year    = {2026},
    journal = {arXiv preprint},
    note    = {Implementation available at \url{https://github.com/NiftyliuS/cliptogrok}}
}

استكشف مشاريع مشابهة ومرتبطة

جارٍ التحميل…