audio audioduration (s) 14 30 | text stringlengths 245 515 |
|---|---|
Imagine you're working with a large language model and you would like to tweak its behavior or its personality. A well-known solution is to use prompt engineering. You specify in the system prompt what you want to achieve. Another option is to fine-tune the model, but for that you need enough data demonstrating the b... | |
And it turns out that steering a large language model is loosely analogous to what neuroscientists call neurostimulation of the brain. Neurostimulation is the idea of artificially stimulating certain areas of the brain, or specific neurons, using electrodes or magnetic fields. When you stimulate biological neurons th... | |
and even elicit certain emotions, feelings or memories. In neuroscience, neurostimulation is used for research to better understand the role of the various brain regions, but also for clinical purposes, for instance, in treating Parkinson's disease. And what's interesting is that it's a technique that obviously does... | |
And it turns out that you can do pretty much the same with artificial neural networks in general, and LLMs in particular. By targeting carefully selected neurons in your LLMs, you can control or elicit certain behavior without having to rewire anything, without changing the weight of the model. This procedure is fai... | |
and make it obsessed with the FL Tower, to the point that it sometimes even believes it is the FL Tower. Look at that. And again, this change is entirely controlled at inference time, when generating the tokens. What is loaded in memory is still the original LAMA model. There is no fine-tuning involved. So, you wa... | |
Well, today I'm going to explain the basics of this method, and show you how you can easily use this technique to steer pretty much any open source LLM using Hugging Face Transformers library. So, first of all, let's recall the internal workings of a typical LLM. Most of them today are autoregressive models that gen... | |
At each layer, each token goes through an attention block and a feedforward block. So, as you know, the attention block is where each token can receive information from the other tokens preceding it in the sequence, and the feedforward network block is a traditional multilayer perceptron. After those two blocks, the... | |
The stack of layers essentially represents successive stages of processing until the logits for the next token are computed by the final linear head. If I zoom in at the boundary between two layers, what gets passed here is actually a vector, sometimes called the hidden state. This vector actually lives in a high-dim... | |
We can think of this huge vector as representing the model's internal state, its hidden thought, at this point in processing the token. With LLMs, we don't generally visualize those numbers as coming from neurons, but you could very well imagine the output of each layer as a series of neurons that produce the coordina... | |
And these are the kinds of neurons that we can target with our steering, our artificial neurostimulation, in order to modify the thoughts of the LLM at inference time. But now the question is, how do we do this? How can we stimulate the neurons in a way that elicits a certain behavior or a certain personality? So to ... | |
You may remember that the very first layer of an LLM is the embedding layer. This layer will map every possible token of the vocabulary into a vector of the activation space. This token vector correspondence is by design for the embedding layer. But something remarkable happens. As the model processes information th... | |
This is called the linear representation phenomenon, an empirical observation that seems to hold for most LLMs. They tend to represent interpretable concepts as vectors in the activation space, going from one layer to another. What's useful here with linear representation is that you can always add vectors. If you h... | |
if you sum them, you get the concept of a red car. And you can even vary the amount you add so that you can navigate between different degrees of the concept, going from a car that happens to be red to something like maybe an intensely red sports car. Maybe you remember from a few years ago the results from the famo... | |
And you could, for instance, obtain the vector embedding of the word king from that of the word queen by adding the vector for man and subtracting the one for woman. Word2vec demonstrated this for word embeddings specifically. But with LLMs, this idea holds throughout the model's layer. And it is a consequence of t... | |
what matters most is the vector's direction, not its length. If you have a vector for the concept of a car, doubling its length won't give you a concept for a bus or two cars or a traffic jam. In general, increasing the length of a concept vector does not change which concept it represents, only how strongly it is ex... | |
Something important to note is that this linear representation phenomenon might be realized differently at every layer of the stack. So after embedding the token car is represented by a certain vector, but after each layer in each intermediate activation space, there is possibly a different vector for the concept of ... | |
I told you earlier that between each layer, the LLM transmits a vector in the high-dimensional activation space and that we could see each coordinate of that vector as a neuron outputting a signal to the next layer. It might be tempting to think that every such neuron represents a certain concept. But this hypothesis... | |
This is called superposition, and through this they can manipulate far more concepts than there are dimensions. So I won't go into too much detail. If you're interested, you should check out Anthropix's series of papers about superposition and monosemanticity. Another important observation regarding the encoding of c... | |
What researchers observed is that in early layers, those vectors tend to be activated when the concept has just been explicitly seen in the input tokens. For instance, the model has read the word car. In late layers close to the output, the vector corresponding to a concept tends to activate when the model is about t... | |
This is where LLMs tend to represent abstract concepts in order to reason on them. So to recap, concepts are represented by vectors in the activation space between each successive layers. And the good thing with vectors is that we can add them. So it means that if we take the activation coming from a layer, we can a... | |
And this is what is called steering. So now let's see how we can do this in practice. So for now, let's assume we found a good vector that represents the concept we want to stimulate. And I'll come back later to the different ways to actually identify those vectors. So as I explained earlier, when you want to steer... | |
The model is the same. The weights are the same, but you will intervene on the activations at inference during the generation of new tokens. So more specifically, if you have a vector X representing the activations at the output of layer N, and you want to steer it in the direction of the vector V, you will simply ad... | |
Of course, as I mentioned before, when you add two vectors, you can scale each one with a coefficient, controlling how much of each you add. So usually, what we do is work with normalized concept vectors V, but we multiply them by a coefficient before steering. And this coefficient will govern the size of your interv... | |
So here I have a small snippet that loads LLM3.18b from the Hugging Face Hub and calls the model on a simple prompt, like give me some ideas for starting a business. And you see the response. Now, let's say we want to steer the model to change its behavior and its personality. And maybe some of you have seen a few m... | |
So I wanted to reproduce this, but as you've probably heard, I'm French. I live in Paris. So I had to try this with the Eiffel Tower instead. So here I'm loading a vector V that represents the concept of the Eiffel Tower at layer 15 of the model. LLM3.18b has 32 layers, so we are in the middle. I'll explain later h... | |
To perform this while generating our tokens, we need the equivalent of the electrode that delivers electrical stimulations to the brain. So in our case, the solution is called a hook. A hook is simply a function that you attach to the model and that gets triggered during the forward pass, right when inference is happ... | |
So let's choose a coefficient and my hook will simply take the output of a layer and add the vector scaled by the coefficient. Very simple. And I will register this hook at layer 15 so that it will be systematically called after the model has processed layer 15. Now let's run my model again with this hook and see wh... | |
With the coefficient set to 4, as you can see, the model starts deviating from its natural behavior. When I was asking for ideas for starting a business, the base model was suggesting things around e-commerce and services. And now you see the answer is different. It's talking about food, bakeries. It is not explici... | |
With a coefficient of 8, Lama starts to suggest ideas about wine and travels. It is clearly influenced by the concept we are stimulating. And now if I ask, who are you? The model will start pretending to be a large metal structure called the Eiffel Tower. And here's a fun detail. The original response with no stee... | |
You can literally see the steering kick in right after the word large. So of course, doing this, you will quickly realize that you don't want to push the coefficient too high. That's expected if you add too much of the vector, you completely derail the model's reasoning and it will output gibberish. That makes sense... | |
So luckily, there are some systematic techniques to help you identify the sweet spot. And there are also some ways to improve the stability of the model by tuning certain generation parameters, like temperature or frequency penalty controls. I cover these techniques in a blog post and I'll leave the link in the desc... | |
How to identify a steering vector for your concept of choice? How did I do for the Eiffel Tower? Well, there are actually several techniques. One is called contrastive activation. The idea is fairly simple. You have to gather pairs of prompts, positive and negative examples, of the behavior you want to elicit. Th... | |
If you have enough pairs, you will end up with vectors that represent the concept you are looking for. This method has been found to be pretty effective in some cases, even better than prompt engineering and supervised fine tuning. Another completely different technique uses sparse autoencoders. These are autoencode... | |
The key insight is that each dimension of this layer tends to correspond to an interpretable concept. The method is unsupervised. You don't tell it which concepts to find. I'm skipping the details, but what's nice about this method is that it gives you a large library of vectors to choose from. And a lot of people h... | |
The drawback is that these vectors don't generally come with predefined concept labels. So if you are looking for one specific concept, it might be particularly tedious to use. Fortunately, the great website Neuronpedia created by Decode Research is the perfect place for that. You can browse through visualization th... | |
In my case, I searched for the Eiffel Tower features in the LAMA 3.1 8B model, and I found for instance this one that I used for the demo. One important aspect of steering vectors, whether they come from contrastive prompts, sparse autoencoders or other techniques, is that they are always located at a given layer of... | |
And as we discussed earlier, in general, if you want the model to be influenced by a concept without necessarily reproducing the exact same words, it is better to steer the concept vectors that are located in the middle intermediary layer, where the abstract reasoning is supposed to happen. But you might have to exp... | |
It does not require any fine-tuning, it just works at inference time. And it has many benefits, like being able to control the intensity of the intervention, and maintain it over the whole text generation, which is sometimes hard to achieve with prompt engineering. Of course, it also has drawbacks. As I mentioned e... | |
Also, steering works best for concepts the model has already learned to represent. It won't teach the model new knowledge. If you want to know more about the technical details, I encourage you to go read the blog post where I explain how I constructed the Eiffel Tower LAMA model, and what kind of methods you might u... | |
Don't forget to visit Neuronpedia and the Hugging Face Hub for finding steering vectors, and maybe sharing with the community your own recipes. Let us know in the comments what you are able to achieve with this technique, and have fun steering! |
README.md exists but content is empty.
- Downloads last month
- 6