In-Context Learning State Vector with Inner and Momentum Optimization

Dongfang Li, zhenyu liu, Xinshuo Hu, Zetian Sun, Baotian Hu, Min Zhang

Abstract

In-Context Learning (ICL) allows large language models (LLMs) to perform new tasks via prompt demonstrations, yet it remains computationally expensive and theoretically opaque. This paper introduces the ICL State Vector, a compressed representation of task-specific logic extracted from model activations. By treating these vectors as optimizable parameters, we propose three novel techniques—Inner Optimization, Momentum Optimization, and Divide-and-Conquer Aggregation—to enhance performance, reduce inference latency by 3x, and bypass the physical constraints of the context window.

Introduction

Large Language Models (LLMs) exhibit a remarkable ability to adapt to new tasks through In-Context Learning (ICL). However, ICL is hindered by two primary challenges: the high computational cost of re-processing demonstration examples for every query and the "black box" nature of how these examples are transformed into task-specific functions. While previous work identified "task vectors" as potential solutions, these representations often suffer from high variance and struggle to scale to many-shot scenarios.

This research formalizes the concept of the ICL State Vector. By viewing the internal activations of a transformer as a form of implicit parameter update, the authors demonstrate that these vectors can be refined using optimization techniques traditionally reserved for model training, leading to significant gains in both efficiency and accuracy.

Methodology

The ICL State Vector

The ICL State Vector is constructed by concatenating the attention activations of the first $L$ layers of a transformer, specifically at the position of the final "separator" token (e.g., the "$\\rightarrow$" symbol) in a demonstration prompt. This vector serves as a distilled essence of the task the model is being asked to perform.

Optimization Techniques

To improve the robustness and scaling of these vectors, the authors introduce three strategies:

  1. Inner Optimization: Inspired by the concept of "model soups," this method averages state vectors extracted from multiple different demonstration examples. This averaging process filters out noise and creates a more stable, generalized representation of the task.
  2. Momentum Optimization: The authors provide a formal proof of a "dual form" relationship between transformer attention and gradient descent. By treating the difference between state vectors of sequential examples as a "gradient," they apply a momentum-based update rule. This progressively refines the state vector as the model "reads" more examples, mimicking the optimization path of a fine-tuned model.
  3. Divide-and-Conquer (D&C) Aggregation: To overcome context window limitations, the D&C approach splits a large set of examples (e.g., 100-shot) into smaller groups. State vectors are extracted for each group and then recursively aggregated. This allows the model to benefit from a volume of data that would otherwise exceed its maximum input length.

Experimental Results

The proposed methods were evaluated using Llama-2 (7B, 13B) and GPT-J (6B) across 12 datasets spanning linguistics (e.g., Antonym, Singular-Plural), translation (English-to-French), and knowledge retrieval (e.g., Country-Capital, AG News).

Performance and Efficiency

On the Llama-2-7B model, momentum optimization yielded an average performance increase of 11.5% over vanilla state vectors. In several benchmarks, these optimized vectors outperformed standard few-shot ICL. Furthermore, by using the state vector for zero-shot intervention—where the model processes the query without re-reading the demonstrations—the researchers achieved a 3x speedup in inference time.

Layer Analysis and Scaling

Analysis of the model's internal layers revealed a "sweet spot" for vector extraction, typically in the middle layers (e.g., layer 14 for Llama-2-7B). PCA visualizations confirmed that the model undergoes a structured learning process, with state vectors for specific positions in a demonstration clustering together. Finally, the D&C aggregation method successfully scaled to 100-shot learning, showing consistent accuracy improvements even when the number of examples far exceeded the model's standard context window.

Discussion and Conclusion

This work demonstrates that the "magic" of ICL can be effectively distilled into state vectors and optimized as if they were model parameters. The parallel between attention mechanisms and gradient descent provides a theoretical bridge between in-context adaptation and traditional fine-tuning.

While the current approach relies on specific architectural markers like separator tokens and has been tested primarily on word-level tasks, it opens new avenues for efficient LLM deployment. Future research will focus on applying these optimized state vectors to complex, multi-step reasoning tasks and exploring their behavior in larger-scale models like Llama-3.

Summary updated

BibTeX
@inproceedings{li2024context,
  author = {Dongfang Li and zhenyu liu and Xinshuo Hu and Zetian Sun and Baotian Hu and Min Zhang},
  title = {{In-Context Learning State Vector with Inner and Momentum Optimization}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2024},
  url = {https://openreview.net/forum?id=gnnmB7y0Xx},
}
Figure