Transformer-based Causal Language Models Perform Clustering

Xinbo Wu, Lav R. Varshney

Introduction

Large Language Models (LLMs) have demonstrated an uncanny ability to follow diverse human instructions after being fine-tuned on relatively small datasets. While techniques like instruction tuning and Reinforcement Learning from Human Feedback (RLHF) are standard practice, the internal mechanisms that allow a model to distinguish between tasks—such as translation versus summarization—remain poorly understood. This paper investigates whether Transformers possess an inherent inductive bias to organize task-specific information into geometric clusters within their hidden representation space.

Methodology: Probing the Hidden Space

To isolate the mechanics of task recognition, the researchers utilized a controlled synthetic environment. In this setup, a "task" is defined by a specific input-output mapping (e.g., $x \to y$). To prevent the model from relying on simple keyword matching, the researchers generated numerous instruction variants for each task using regular expressions, ensuring the model had to learn the underlying task logic rather than surface-level patterns.

Conceptual diagram of synthetic tasks and their mappings into a representation space.

The study analyzed the hidden states of models at the final input token, applying K-Means clustering and t-SNE visualizations to evaluate how these internal vectors grouped. Metrics such as the F1 score, Adjusted Rand Index (ARI), and Adjusted Mutual Information (AMI) were used to quantify clustering quality. The findings were then validated on large-scale pre-trained models, including LLaMA-7B/13B and GPT-J-6B, using a "Natural Instruction" dataset of 16 real-world tasks.

Key Insights: The Emergence of Task Clusters

The research reveals that clustering is not a byproduct of explicit supervision but an emergent inductive bias. As the model learns to minimize next-token prediction loss, it naturally discovers that grouping similar tasks is the most efficient representational strategy.

Dynamic Evolution and Hierarchy

Clustering quality is not static; it evolves during the training process. Initially, hidden states appear as a disorganized cloud, but they gradually condense into distinct, task-specific "islands." This saturation of cluster quality closely tracks the model's peak performance in task accuracy.

F1 scores for Task, Distribution, and Distribution-Mapping identities over training epochs across different layers.

Furthermore, the model maintains a hierarchy of representations. It distinguishes between the "style" of an instruction (distribution identity) and the "actual goal" (task identity). Analysis across layers (indicated by lines 0-6 in the figures) shows that clustering performance generally improves in the deeper layers of the Transformer, suggesting that the model refines its task understanding as information flows through the network.

Generalization via Proximity

The study provides a geometric explanation for zero-shot generalization. K-Nearest Neighbors (KNN) analysis indicates that when a model encounters an unseen instruction, it maps the new prompt into the "neighborhood" of a known task cluster. This proximity allows the model to apply learned behaviors to novel phrasings by leveraging the established geometric structure of the hidden space.

Results and Practical Implications

The empirical results are striking. In LLaMA-7B, the clustering F1 score for task identity reached 0.959, indicating nearly perfect separation of different tasks in the hidden space. There is a strong correlation (Pearson coefficient > 0.6) between a model’s ability to cluster and its eventual task accuracy. Additionally, the authors demonstrated that pre-training a model specifically to predict a "Task ID"—thereby forcing cluster formation—allows it to learn subsequent instruction-following tasks much faster and more accurately than standard pre-training.

Beyond theoretical understanding, these insights have practical applications for model alignment. The authors proposed a "switch network" that identifies which cluster an input belongs to and routes it to a specialized output head. This approach reduced "catastrophic forgetting" of general tasks during safety alignment by over 60% compared to standard fine-tuning methods.

Conclusion

This work demystifies the "black box" of instruction following by showing that Transformers are essentially dynamic clusterers. By organizing the vast space of natural language into task-specific geometric structures, these models achieve the robust generalization required for complex human interaction. While the current study focused on single-token outputs, future research will explore how these clusters behave in multi-token generation and long-form reasoning tasks, potentially leading to more interpretable and controllable AI systems.

Summary updated

BibTeX
@inproceedings{wu2025transformer,
  author = {Xinbo Wu and Lav R. Varshney},
  title = {{Transformer-based Causal Language Models Perform Clustering}},
  booktitle = {Conference of the North American Chapter of the Association for Computational Linguistics},
  year = {2025},
  doi = {10.18653/v1/2025.findings-naacl.296},
}
Figure