Arithmetic Transformers Can Length-Generalize in Both Operand Length and Count
Hanseul Cho, Jaeyoung Cha, Srinadh Bhojanapalli, Chulhee Yun
TL;DR
This work addresses the long-standing problem of length generalization in Transformer-based arithmetic by introducing a synergistic combination of scratchpad reasoning and Position Coupling to constrain attention to a fixed number of tokens per inference step. It demonstrates substantial gains on two challenging tasks: multi-operand addition with varying operand counts and lengths, and integer multiplication with variable operand lengths, achieving up to 30 operands of 30 digits and 20-digit by 15-digit multiplication with notable accuracy. A constructive theorem shows that a small 1-layer Transformer with scratchpad can solve multi-operand addition for exponentially long operands and operand counts, offering theoretical grounding for the empirical results. The findings highlight the potential of structured scratchpad formats and Abacus-style position embeddings to extend the practical capabilities of arithmetic Transformers and guide future work on length generalization in tasks with well-defined structure.
Abstract
Transformers often struggle with length generalization, meaning they fail to generalize to sequences longer than those encountered during training. While arithmetic tasks are commonly used to study length generalization, certain tasks are considered notoriously difficult, e.g., multi-operand addition (requiring generalization over both the number of operands and their lengths) and multiplication (requiring generalization over both operand lengths). In this work, we achieve approximately 2-3x length generalization on both tasks, which is the first such achievement in arithmetic Transformers. We design task-specific scratchpads enabling the model to focus on a fixed number of tokens per each next-token prediction step, and apply multi-level versions of \Position Coupling (Cho et al., 2024; McLeish et al., 2024) to let Transformers know the right position to attend to. On the theory side, we prove that a 1-layer Transformer using our method can solve multi-operand addition, up to operand length and operand count that are exponential in embedding dimension.
