Abstract
In this paper, we investigate the length-extension of state-space models (SSMs) in language modeling. Length extension involves training models on short sequences and testing them on longer ones. We show that state-space models trained with zero hidden states initialization have difficulty doing length extension. We explain this difficulty by pointing out the length extension is equivalent to polynomial extrapolation. Based on the theory, we propose a simple yet effective method - changing the hidden states initialization scheme - to improve the length extension. Moreover, our method shows that using long training sequence length is beneficial but not necessary to length extension. Changing the hidden state initialization enables the efficient training of long-memory model with a smaller training context length.
Introduction
Large Language Models (LLMs) are typically constrained by the context lengths used during training. While Transformers face quadratic $O(T^2)$ scaling issues, State-Space Models (SSMs) like Mamba offer linear $O(T)$ scaling, theoretically allowing for "infinite" recurrent memory. However, practical applications reveal that SSMs often fail to generalize to sequences significantly longer than their training context—a phenomenon known as the length extension problem. For instance, a Mamba model trained on 2048 tokens may see its perplexity spike when evaluated on 4096 tokens.
The Extrapolation Challenge
The core insight of this work is that length extension in linear SSMs is mathematically equivalent to polynomial extrapolation. When trained with zero-initialization, the model fits a memory function over a limited interval. Extending this to longer sequences requires the model to extrapolate far beyond its training data, which is inherently unstable. By contrast, interpolation is a much more manageable task for neural networks.
Proposed Method: Previous-Initialization
To address this, we propose moving away from the standard Zero-hidden state initialization, where the model's memory is reset at the start of every batch. Instead, we implement Previous-Initialization, carrying over the hidden state from the preceding batch ($h_{k+1,0} = h_{k,T}$). This approach effectively implements a form of truncated Backpropagation Through Time (BPTT).
For this method to be effective, the data loader must provide consecutive text segments without batch-level shuffling. This ensures that the carried-over state represents a continuous history, allowing the model to learn from "distant" context even when gradient updates are performed over short windows.
Experimental Results
We evaluated our approach using benchmarks like The Pile and Wikitext103. Our results demonstrate that models trained on sequences as short as 16 tokens can successfully extend to 32,768 tokens when using previous-initialization.
As shown in Figure 6, the "Previous+Unshuffled" configuration consistently achieves the lowest loss and the most stable length extension. In contrast, shuffling the data or using zero-initialization leads to significant performance degradation as sequence length increases. This method offers a potential 2000x reduction in GPU memory requirements by enabling long-context capabilities through short-sequence training.
Conclusion and Limitations
While previous-initialization significantly improves length extension, it can introduce training instabilities, particularly in larger models like S5. Furthermore, the reliance on unshuffled data means the model is sensitive to the continuity of the input stream. Future work will focus on improving the numerical stability of this state-carrying mechanism to make it more robust for large-scale deployments.
Summary updated