Abstract
Contextually Guided Transformers (CGT) address the computational overhead of long prompts in Large Language Models by dynamically "baking" context into model weights. By utilizing a two-stage information flow and low-rank adaptation (LoRA), CGT enables models to self-specialize for specific tasks without external controllers. Our results show that CGT maintains high performance even when context updates are frozen, effectively transforming the model into a task-specific expert.
Introduction
The reliance of LLMs on in-context learning through long prompts introduces a "prompt tax"—increased latency and memory usage due to repetitive prefix processing. While fine-tuning offers a solution, it is often static and requires external supervision. Contextually Guided Transformers (CGT) bridge this gap by enabling a single, unified architecture to simultaneously process a sequence and adapt its internal weights to better handle subsequent tokens. This "self-specialization" mechanism allows the model to internalize task-specific information without the need for an external controller or secondary model.
The CGT Architecture
The core of CGT is a two-stage information flow designed to separate general token processing from contextual adaptation.
- Stage 1 (Context Encoding): For the initial $\ell$ layers, the model maintains dual activation sets. The first set ($x$) performs standard token processing, while the second set ($y$) aggregates a "contextual summary" of the sequence.
- Stage 2 (Weight Modulation): At layer $\ell$, the final context summary ($y^\ell$) is passed to a Low-Rank Weight Generator. This generator produces small template matrices that modify the linear operators in all subsequent layers, similar to Low-Rank Adaptation (LoRA) but computed dynamically during the forward pass.
Training for Specialization
To ensure the model effectively utilizes the generated context, two specific training strategies are employed:
- Auxiliary Loss: The model is trained to maintain performance even if the context vector $y^\ell$ is "frozen" at a specific point in the sequence. This forces the model to specialize based on the prefix.
- Slowness Prior: A regularization term encourages the context representation to evolve smoothly across tokens and maintain orthogonality between different samples in a batch, preventing erratic shifts in the "topic vector."
Experimental Results
CGT was evaluated on synthetic in-context learning tasks, linear regression, and real-world text mixtures from C4 and Wikipedia.
In synthetic arithmetic tasks, CGT achieved a base accuracy of 78.6%, outperforming the baseline Transformer's 77.7%. Crucially, when the context was frozen to simulate a specialized model, CGT (Aux + Reg) maintained 77.5% accuracy. In contrast, a CGT model trained without the auxiliary loss collapsed to 15.3% accuracy under the same frozen conditions.
On the C4 dataset, adding a 128-dimensional context embedding at layer 8 resulted in a cross-entropy loss improvement of 0.23 over a baseline of comparable size. Furthermore, visualizations of the latent context vector $y^\ell$ revealed that it naturally clusters text by category (e.g., "Theoretical Physics" vs. "Film Genres"), acting as an interpretable topic vector.
Conclusion
Contextually Guided Transformers offer a promising alternative to traditional attention-based prompting. By shifting the "physics" of the model through dynamic weight modulation, CGT achieves efficient task specialization and improved language modeling performance. While the model faces challenges with sequence length generalization and thematic shifts, it provides a path toward more efficient and interpretable representation learning in large-scale models.
Summary updated