Learning Task Representations from In-Context Learning
Baturay Saglam, Xinyang Hu, Zhuoran Yang, Dionysis Kalogerias, Amin Karbasi
TL;DR
This work tackles how in-context learning encodes tasks within transformers by proposing Learnable Task Vector (LTV), a per-layer, head-weighted task representation optimized via gradient descent. By treating tasks as a weighted sum of attention-head activations, LTV generalizes across text and functional regression and is validated on a new regression benchmark designed to probe cross-modality task fidelity. Empirical results show LTV preserves task information in out-of-distribution prompts and often outperforms strong baselines, with weights that reveal selective head involvement and alignment of last hidden-state distributions to an optimal ICL model. The findings advance understanding of task steering in LLMs and offer a practical, modality-agnostic approach to encoding tasks without fine-tuning. The work also highlights the importance of attention-head contributions and per-layer task vectors in capturing robust, cross-domain task representations, potentially informing future PEFT and interpretability approaches.
Abstract
Large language models (LLMs) have demonstrated remarkable proficiency in in-context learning (ICL), where models adapt to new tasks through example-based prompts without requiring parameter updates. However, understanding how tasks are internally encoded and generalized remains a challenge. To address some of the empirical and technical gaps in the literature, we introduce an automated formulation for encoding task information in ICL prompts as a function of attention heads within the transformer architecture. This approach computes a single task vector as a weighted sum of attention heads, with the weights optimized causally via gradient descent. Our findings show that existing methods fail to generalize effectively to modalities beyond text. In response, we also design a benchmark to evaluate whether a task vector can preserve task fidelity in functional regression tasks. The proposed method successfully extracts task-specific information from in-context demonstrations and excels in both text and regression tasks, demonstrating its generalizability across modalities.
