Rethinking Invariance in In-context Learning
Lizhe Fang, Yifei Wang, Khashayar Gatmiry, Lei Fang, Yisen Wang
TL;DR
This work tackles the sensitivity of in-context learning (ICL) to the order of context examples by formalizing three key principles: permutation invariance, information non-leakage, and context interdependence. It introduces InvICL, a two-stage design that first encodes context independently to preserve leakage constraints and then applies a leave-one-out pre-encoding to entangle context representations, all while maintaining a diagonal attention structure on context rows to ensure invariance. A parallel two-pass implementation enables practical, single-forward-pass inference with $O(n^2)$ complexity, matching the efficiency of baseline methods. Empirically, InvICL outperforms both invariant and non-invariant baselines on synthetic and real-world datasets, including strong length-generalization and OOD performance, underscoring the value of principled invariant ICL for robust generalization and practical deployment.
Abstract
In-Context Learning (ICL) has emerged as a pivotal capability of auto-regressive large language models, yet it is hindered by a notable sensitivity to the ordering of context examples regardless of their mutual independence. To address this issue, recent studies have introduced several variant algorithms of ICL that achieve permutation invariance. However, many of these do not exhibit comparable performance with the standard auto-regressive ICL algorithm. In this work, we identify two crucial elements in the design of an invariant ICL algorithm: information non-leakage and context interdependence, which are not simultaneously achieved by any of the existing methods. These investigations lead us to the proposed Invariant ICL (InvICL), a methodology designed to achieve invariance in ICL while ensuring the two properties. Empirically, our findings reveal that InvICL surpasses previous models, both invariant and non-invariant, in most benchmark datasets, showcasing superior generalization capabilities across varying input lengths. Code is available at https://github.com/PKU-ML/InvICL.
