Implicit In-context Learning

Zhuowei Li, Zihao Xu, Ligong Han, Yunhe Gao, Song Wen, Di Liu, Hao Wang, Dimitris N. Metaxas

TL;DR

Implicit In-context Learning (I2CL) allows Large Language Models (LLMs) to achieve few-shot performance at the speed and memory cost of zero-shot inference. By condensing demonstration examples into "context vectors" and injecting them directly into the model's internal residual streams, I2CL bypasses the need for long, computationally expensive prompts while remaining robust to the choice and order of examples.

What Problem Does This Paper Address?

In-context Learning (ICL) enables LLMs to adapt to new tasks simply by observing a few examples in a prompt. However, ICL has two major drawbacks:

  1. Efficiency: Every demonstration token added to a prompt increases the computational load quadratically due to the attention mechanism, and it consumes significant memory in the Key-Value (KV) cache.
  2. Sensitivity: LLMs are notoriously sensitive to prompt formatting. Changing the order of examples or selecting slightly different ones can cause the model’s performance to swing wildly.

The authors of this paper ask a fundamental question: Can we get the benefits of these demonstration examples without actually putting their tokens in the prompt?

Methods

The authors introduce Implicit In-context Learning (I2CL), which shifts the "learning" process from the token space to the activation space.

I2CL Architecture Diagram

The process happens in three main steps:

  1. Context Vectorization: Instead of prepending examples to the query, the model processes each demonstration example independently. It extracts the activations from the Multi-Head Attention (MHA) and Multi-Layer Perceptron (MLP) modules at the very last token position. These are averaged to create a single, condensed context vector that represents the "essence" of the task.
  2. Context Injection: During inference, the actual query is fed to the model alone (zero-shot style). However, at each layer, I2CL injects a linear combination of the stored context vector and the query’s current activations into the residual stream. This effectively "steers" the model toward the desired task behavior.
  3. Noisy Self-Calibration: To determine exactly how much of the context vector to blend in, the authors use a tiny amount of gradient-based optimization (just 4 scalars per layer) on the few-shot examples. They add Gaussian noise during this phase to ensure the model doesn't overfit and remains robust to variations.

Datasets

The researchers evaluated I2CL across nine diverse text classification benchmarks:

  • Sentiment Analysis: SST-2, SST-5, MR.
  • Topic Classification: AGNews, DBPedia.
  • Emotion/Subjectivity: EmoC, Subj.
  • Question/Hate Speech: TREC, HateSpeech18.

They also tested the method on a synthetic dataset containing random strings and numerical values to prove that I2CL works even when there are no semantic or formatting priors to rely on.

Key Insights and Ideas

  • The "Task-ID" Signature: Perhaps the most fascinating discovery is that the calibrated linear coefficients (the weights used for injection) cluster tightly by task. These coefficients essentially act as a "Task-ID"—a unique signature that represents the task itself.
  • Robustness to "Bad" Prompts: Standard ICL fails if you provide poor demonstration examples. I2CL, however, is remarkably resilient. Even when given "deficient" demonstrations that tanked standard ICL performance by 7%, I2CL’s performance barely budged (dropping only 0.5%).
  • Vector Arithmetic for Transfer Learning: Because the tasks are represented as vectors, you can perform arithmetic on them. The authors showed that you can improve a model's performance on a new task by initializing it with the weighted average of coefficients from similar "anchor" tasks (e.g., using SST-2 weights to help with a Movie Review task).
  • Information Deletion: The injection process isn't just about adding information. The model sometimes uses negative coefficients at certain layers, suggesting that "learning" a task also involves suppressing irrelevant features already present in the model.

Key Results

Accuracy vs. Speed vs. Cached Parameters
  • Few-Shot Power, Zero-Shot Cost: On Llama2-7b, I2CL outperformed zero-shot baselines by 17% absolute accuracy, coming within 1% of standard few-shot ICL while maintaining the lightning-fast inference speed of a zero-shot query.
  • Extreme Parameter Efficiency: Compared to Parameter-Efficient Fine-Tuning (PEFT) methods like LoRA or IA3, I2CL uses 100x to 10,000x fewer learnable parameters (only about 130 parameters for a 7B model).
  • Scaling: Unlike standard ICL, which hits a "context window" limit and degrades, I2CL can theoretically scale to hundreds or thousands of demonstration examples without increasing the computational cost of the final query.

Limitations and Future Work

  • Task Scope: This study focused exclusively on text classification. It remains to be seen how well I2CL handles open-ended generation (like creative writing) or complex multi-step reasoning (like math word problems).
  • Model Access: I2CL requires access to the model's internal activations (the residual stream). This makes it impossible to use with "closed" models like GPT-4 or Claude 3 via standard APIs.
  • Calibration Time: While inference is fast, there is a brief 1-2 minute "calibration" step required for each new task to optimize the injection scalars.
  • Future Directions: The authors suggest exploring different token positions for vector extraction and allowing for finer granularity (e.g., different weights for different attention heads) to further boost performance.

Summary updated

BibTeX
@inproceedings{li2025implicit,
  author = {Zhuowei Li and Zihao Xu and Ligong Han and Yunhe Gao and Song Wen and Di Liu and Hao Wang and Dimitris N. Metaxas},
  title = {{Implicit In-context Learning}},
  booktitle = {International Conference on Learning Representations},
  year = {2025},
  url = {https://openreview.net/forum?id=G7u4ue6ncT},
}
Figure