Representation Engineering (RepE): LLM Control via Hidden State Manipulation
This repository contains a solo-developed implementation and extension of Representation Engineering (RepE), as proposed by Zou et al. (2023). This project moves beyond "black-box" prompt engineering by identifying and manipulating the internal cognitive states of Large Language Models (LLMs) through their high-dimensional hidden layer activations.
The objective was to extract "concept vectors" (directions in activation space) that represent abstract concepts like emotions and political leanings. By isolating these vectors, I implemented a mechanism to "read" a model's internal state and "steer" its output mechanistically during inference.
- Representational Reading: Built a pipeline using Linear Artificial Tomography (LAT) and Principal Component Analysis (PCA) to isolate concept directions within the residual streams of Mistral-7B.
- Vector Compositionality: Successfully proved that internal representations are arithmetically compositional. By averaging individual vectors (e.g.,
Happiness+Sadness), I forced the model to generate nuanced "bittersweet" narratives that are difficult to elicit consistently via standard prompting. - Memory & Resource Optimization: Solved hardware constraints (OOM errors) associated with processing massive activation datasets. Implemented 4-bit quantization (bitsandbytes) and a data chunking/batching strategy to perform PCA on high-dimensional tensors on consumer-grade hardware.
- High-Level Concept Mapping: Extended the original research from simple emotions to complex societal concepts by curating a custom dataset from Wikipedia to map a linear axis for political ideology.
Contrastive prompts (e.g., "Pretend you are honest" vs. "Pretend you are dishonest") are fed into the model. I capture the hidden states
I calculate the difference vectors between contrastive pairs:
To change model behavior, I inject the reading vector back into the hidden states during the forward pass:
$$H'{layer} = H{layer} + \alpha \cdot v_{reading}$$
where
- Truthfulness Benchmarking: Demonstrated that suppressing "Anger" and "Fear" vectors led to improved performance on the TruthfulQA benchmark, suggesting a mechanistic link between simulated emotional states and factual reasoning.
-
Compositional Generation: Proved that
$v_{bittersweet} \approx \frac{v_{happy} + v_{sad}}{2}$ results in coherent, context-aware bittersweet narratives. - Ideological Steering: Successfully scaled model responses along a linear political axis, moving from moderate to radical viewpoints in a controlled fashion.
- Language: Python
- Libraries: PyTorch, Hugging Face Transformers, Scikit-learn, Accelerate
- Models: Mistral-7B-v0.1 (Quantized)
- Hardware Platform: Google Colab TPUs / NVIDIA T4 GPUs
Zou, A., Phan, L., Chen, S., Campbell, J., Guo, P., Ren, R., ... & Hendrycks, D. (2023). Representation Engineering: A Top-Down Approach to AI Transparency. arXiv preprint arXiv:2310.01405.