The ability to learn and generalize from limited experience, or sample efficiency, is a cornerstone of intelligence. How can we build language models that turn a few user interactions, a few snippets of unfamiliar code, or a handful of experimental results provided in context into knowledge that generalizes? Prior work has found that sleep, a process where the model performs N recurrent forward passes to refine its representations of a chunk of context, can improve long-context reasoning (specifically, within SSM-attention hybrid models). We study whether this method also improves in-context adaptation on general, non-synthetic tasks for transformer language models. To this end, we pre-train language models up to 1.2B-parameters on up to 100B tokens with sleep mechanisms. Notably, longer sleep (i.e., using a larger number of recurrent forward passes N) enables the model to learn faster in-context on multiple few-shot algorithmic, question-answering tasks, while also achieving lower next-token prediction loss on out-of-distribution evaluations. On held-out code, models with N=2 and N=4 match the no-sleep baseline's loss using 2.10× and 2.39× fewer tokens in-context, respectively. More broadly, we view our work as a step toward building models that have better tradeoffs between in-context adaptation and decoding latency.
Figures & tables
Figure 1: During the wake phase, our model decodes tokens at a normal speed without using any depth recurrence. Once the sleep boundary is reached (i.e., once the sleep window is full), the model performs additional recurrent passes over the window tokens to refine the KV cache. It then resumes decoding at a normal speed. This is done both during training and at inference time.
Figure 2: Longer sleep (i.e., a larger N ) leads to better in-context adaptation. Left: Mean next-token prediction loss on the final 256 tokens from an out-of-distribution (OOD) code dataset with varying prefix length. Models with sleep reach losses of models with no sleep at faster rates, and the sample efficiency gain increases as context grows. Middle: Loss per token position on the OOD code dataset. Sleep models have higher loss than the no-sleep baseline in the first window, and loss improves steeply at sleep boundaries (shown as vertical dashed lines). Right: On a few-shot learning task (selective recall), models with longer sleep duration are significantly better than those with less or no sleep.
Figure 3: Training transformers with sleep. The first loop of the language model is used to produce the next-token predictions. Additional loops (“sleep”), performed only at chunk boundaries, are used refine the completed window’s KV cache.
Width ( d )
Layers ( L )
Non-embed. / total (M)
Batch (Mi tokens)
512
6
22.93 / 48.10
0.5
768
8
69.46 / 107.21
0.5
1024
12
182.95 / 233.28
1
1280
14
331.02 / 393.93
1
1536
16
548.35 / 623.84
2
2048
18
1096.20 / 1196.86
4
Table 1: Model configurations for N=1 , with tied embeddings. N>1 adds approximately 2% non-embedding parameters.
Figure 5: Next-token prediction loss as context accumulates. The upper panels of each subplot show LN , the loss for a model with N loops. Each point shows the mean next-token loss across evaluation sequences within a non-overlapping 256-token position bin. Lower panels show LN−L1 , the difference from the no-sleep baseline. The evluation dataset combines arXiv2025 and Repo2025. We show three configurations: d1536/TP30 (or 600M/16B), d2048/TP30 (or 1.2B/32B), and d2048/TP90 (or 1.2B/98B). We observe similar trends on held-out ProLong dataset. Full plot can be found in Figure 13 .
Figure 6: Few-shot learning performance as demonstrations accumulate. We evaluate on four few-shot learning tasks, CoQA, algorithmic word tasks, HELMET-ICL, and SST-2. The d2048/TP30 (or 1.2B/32B) model is used, where we see longer sleep duration leads to significant improvements as the number of demonstration increases. Other configurations follow similar trends ( Section A.6 ).
Figure 7: Inference cost profile comparison between N=1,2,4 : (a) cumulative decoding latency, excluding time to first token. (b) peak memory allocation, (c) time to first token relative to N=1 . All panels are measured using a 1.2B model with W=256 , batch size 8, and 1,024 generated tokens on an RTX 6000 Ada GPU. We see that a model with longer sleep incurs a proportionally higher time to first token, but otherwise incurs no additionally overhead during wake-time decoding, and only minimally increases overall memory footprint and cumulative latency at decoding stage.
Figure 8: Loss on the final 256 tokens of 4k-token sequences for 230M-parameter models on ProLong and Out-of-distribution code datasets. N is the number of loops. Our models achieve comparable loss to looped transformer baselines (see Section 3 for descriptions) with better latency and memory footprint. Decoding time is the cumulative latency for generating 1k tokens after a 128k-token prefix (excluding prefill). Circle area is proportional to KV-cache size per token.
Figure 9: Loss comparison, with and without KV gating. Circles highlight sharp boundary spikes without gating; learned KV write gates substantially reduce them. d768/TP30 models are used.
Figure 10: Learning curve comparison for sleep, block-recurrent loops, and full-context loops. Loss per token is measured on Repo2025 using d1024/TP30 models with N=2 (left) and N=4 (right). Upper panels show loss, and lower panels show the loss difference from the shared N=1 baseline.
Figure 11: Changing the sleep window at test time. Repo2025 loss on the final 256 tokens of 4,096-token sequences for the d1536/TP30 model with N=4 , trained with W=256 . W=192 achieves the lowest loss among the tested windows.
Appendix figures & tables12 assets
Supplementary material from the paper’s appendix.
Appendix
Sleep (Ours): ≈ 1.2B, 32.89B tokens
Sleep (Ours): ≈ 1.2B, 98.66B tokens
Transformer++
Metric
N=1
N=2
N=4
N=1
N=4
1.3B, 100B tokens
LAMBADA PPL ↓
6.21
6.37
6.43
5.25
5.56
18.32
LAMBADA
60.20
59.17
59.29
63.90
62.93
42.60
HellaSwag*
58.23
58.63
58.71
64.25
62.65
50.23
PIQA
73.83
74.05
73.56
74.92
74.76
70.02
ARC-E
68.01
66.20
67.47
70.88
70.66
68.83
Appendix
Table 2: Zero-shot downstream evaluation on the standard lm-eval-harness tasks. Accuracy is reported in percent; * denotes length-normalized accuracy. Average is the unweighted mean of the six accuracy metrics, excluding LAMBADA perplexity.
Layout
Avg. loss
Last-1k loss
Logical depth
0 – 12 – 0
2.084
1.971
12
4 – 4×3 – 4
2.057
1.940
20
2 – 8×2 – 2
2.053
1.934
20
2 – 8×4 – 2
2.043
1.925
36
0 – 12×3 – 0
2.035
1.910
36
Appendix
Table 3: ProLong validation loss for d1024/TP300 models trained with a 4k-token context window. Layout denotes prelude–recurrent layers × loops–coda. 0 – 12 – 0 is the no-sleep baseline. Bold denotes the layout with lower loss for each logical depth.
ProLong
DCLM
Causal mask during sleep
Avg.
Last-1k
Avg.
With
1.978
1.861
2.687
Without
1.977
1.858
2.680
Appendix
Table 4: Causal-masking ablation for d1536/TP30 models with N=2 , W=256 , and 4k training context. The causal mask is varied only during sleep.
W
DCLM train
ProLong avg.
Last-1k
256
3.1592
2.4330
2.3062
512
3.1714
2.4626
2.3404
1024
3.1772
2.4698
2.3489
Appendix
Table 5: Window-size ablation for d768/TP30 models with N=2 .
Figure 12: Plot extracted from Figure 6 of Tandon et al. (2025) . The top panel shows the next-token loss per token position, comparing their Transformer baseline and the proposed TTT-E2E. The bottom panel shows loss differences. Vertical dashed lines mark test-time updates every 1k tokens.
Figure 13: Next-token prediction loss as context accumulates. Upper panels show LN , the loss for a model with N loops, averaged within 256-token windows. Lower panels show LN−L1 , the difference from the no-sleep baseline.
Figure 14: Few-shot CoQA.
Figure 15: In-context adaptation on five algorithmic word tasks. The top row shows the average accuracy over all tasks, where x-axis is normalized between 0 and 1. Vertical dotted lines show median sleep boundaries, and the shaded areas around them represent the 1st–99th percentile range (as boundaries can occur at different demonstration counts across examples). Sleep boundaries are omitted for the plots that average over multiple formats (selective recall and symbol removal).
Figure 16: SST-2, where instead of using positive/ labels, the labels are replaced by {foo, bar} , so the model must learn the correct correspondence between these placeholders and the correct semantic labels via in-context demonstrations. Vertical dotted lines show median sleep boundaries, and the shaded areas around them represent the 1st–99th percentile range.
Figure 17: In-context adaptation on three HELMET-ICL tasks. The top row shows the average accuracy divided by the random baseline’s accuracy over all tasks, where x-axis is normalized between 0 and 1. Vertical dotted lines show median sleep boundaries, and the shaded areas around them represent the 1st–99th percentile range.
Figure 18: Selective recall accuracy with respect to the number of demonstrations depending on the formatting choice. 1.2B N=1,2,4 models trained on 32B tokens are used.
Figure 19: Symbol removal accuracy with respect to the number of demonstrations depending on the formatting choice. 1.2B N = 1, 2, 4 models trained on 32B tokens are used. Vertical dotted lines show median sleep boundaries, and the shaded areas around them represent the 1st–99th percentile range.
Transformer-based large language models are increasingly used for long-horizon tasks; however, their attention mechanism scales poorly with context length. To handle this, we study a sleep-like consolidation mechanism in which a model periodically converts recent context into persistent fast weights before clearing its key-value cache. During sleep, the model performs N offline recurrent passes over the accumulated context and updates the fast weights in its state-space model (SSM) blocks through a learned local rule. During inference, this shifts extra computation to sleep while preserving the latency of wake-time prediction. We test our method on controlled synthetic tasks, including cellular automata and multi-hop graph retrieval, as well as a realistic math reasoning task, on which a regular transformer as well as SSM-attention hybrid models fail. We then show that increasing sleep duration N for our models improves performance, with the largest gains on examples that require deeper reasoning.
Sangyun Lee, Sean McLeish, Tom Goldstein +1
Carnegie Mellon University · University of Maryland
The past few decades have witnessed significant advances in the design of machine learning algorithms, from early studies on task-specific shallow models to more general deep Large Language Models (LLMs). Despite showing promising results in tasks that require instant prediction or in-context learning, existing models lack the ability to continually learn and effectively transfer their temporal in-context knowledge to their long-term parameters. Inspired by human learning process, we introduce a ''Sleep'' paradigm that allows the models to continually learn, distill their short-term fragile memories into stable long-term knowledge with replay, and recursively improve themselves with ''Dreaming'' process. In more detail, sleep consists of two stages: (1) Memory Consolidation: an upward distillation process, called Knowledge Seeding, where the memories of a smaller-self are distilled into a larger network to provide more capacity while preserving the knowledge. As a proof of concept, we present a new Generalized Distillation process for {Knowledge Seeding} (i.e., the combination of on-policy distillation with Reinforcement Learning (RL)-based imitation learning); (2) Dreaming: a self-improvement phase, where the model uses RL to generate a curriculum of synthetic data to rehearse new knowledge and refine existing capabilities without human supervision. Our experiments on long-horizon, continual learning, knowledge incorporation, and few-shot generalization tasks support the importance of the sleep stage.
Transformer-based large language models exhibit in-context learning, enabling adaptation to downstream tasks via few-shot prompting with demonstrations. In practice, such models are often fine-tuned to improve zero-shot performance on downstream tasks, allowing them to solve tasks without examples and thereby reducing inference costs. However, fine-tuning can degrade in-context learning, limiting the performance of fine-tuned models on tasks not seen during fine-tuning. Using linear attention models, we provide a theoretical analysis that characterizes how fine-tuning objectives modify attention parameters and identifies conditions under which this leads to degraded few-shot performance. We show that fine-tuning all attention parameters can harm in-context learning, whereas restricting updates to the value matrix improves zero-shot performance while preserving in-context learning. We further show that incorporating an auxiliary few-shot loss enhances in-context learning primarily on the target task, at the expense of degraded in-context learning ability on tasks not seen during fine-tuning. We provide empirical evidence from synthetic and real-world datasets consistent with the qualitative predictions of our theory.
Chungpa Lee, Jy-yong Sohn, Kangwook Lee
Yonsei University, Korea · University of Wisconsin–Madison, USA · KRAFTON, Korea +1