[Gemma 3]Activation Hacking
Copyright 2025 Google LLC.
Gemma - Activation Hacking
Author: Sascha Rothe
This Colaboratory notebook provides a comprehensive tutorial on interacting with a Gemma checkpoint. It will guide you through the process of loading the model, generating samples, and examining its internal states, including the residual stream, MLP activations, and attention mechanisms. Furthermore, an example demonstrating how to modify the model's behavior will be presented. While the notebook includes an illustrative example, we encourage you to adapt these experiments and explore the model's behavior to uncover novel insights.
Note: This colab was tested with a A100 GPU. You might see unexpected behaviour on a different hardware.
To interact with the Gemma model, you will use the Flax NNX gemma code from google/flax examples on GitHub. Since it is not exposed as a package, you need to use the following workaround to import from the Flax NNX examples/gemma on GitHub.
To use Gemma model, you’ll need a Kaggle account and API key:
-
To create an account, visit Kaggle and click on ‘Register’.
-
If/once you have an account, you need to sign in, go to your ‘Settings’, and under ‘API’ click on ‘Create New Token’ to generate and download your Kaggle API key.
-
[Optional] In Google Colab, under ‘Secrets’ add your Kaggle username and API key, storing the username as KAGGLE_USERNAME and the key as KAGGLE_KEY.
-
Request access to the model here: https://www.kaggle.com/models/google/gemma-3
Start Setting up Your Model
Prepare the Sow Configuration and set the intermediates you want to surface. For the tutorial, enable embeddings, rs_after_ffw and rs_after_attention for the residual stream. Also set mlp_hidden_topk=10 to surface the activations in the MLP layers (also called feedforward layer).
Now, build the model and required modules.
You're ready to start generating! As we want to visualize the intermediate
activations, we are using a batch size of 1. We limit the models output by setting total_generation_steps = 1, so we can investigate it more easily.
Investigate the model
In order to investigate intermediate outputs we map them into the text space by performing premature decoding. To be precise we have the apply the final norm and the softmax projection. We create a helper function premature_decode.
To visualize the residual stream we create:
- a green row for the embedding layer
- a red row for each attention layer
- a blue row for each ffw layer
You can see how the output evolves from bottom to top. Darker the colors indicate a bigger change during this layer in the residual stream. Note that in the early layers the premature token is simply the previous token. This is because the residual stream was initialized with the embedding of the previous token. You might also find some language code switching between the layers. For the example What is the capital of Switzerland? Answer: Bern you should see that the answer Bern was only decided on the the very last feedforward layer.
The next cell will visualize the top activated neurons in a given layer. You can hover over the colored blocks to get the neuron id and value. For the last feedfordward layer 25 you should see that the top activated neurons are 1937 and 4422.
After identifiying a neuron of interest we can deactive or boost it. You can play around with the bias values to achieve different behaviours.
- Deactivate neuron 1937 in layer 25 and issue the same prompt again.
- Boost neuron 1937 in layer 25 and your model should repsonse with Switzerland significantly more often.
We can also apply the premature_decode functions to the value of a neuron to investigate the effect of a neuron. Check neuron 1937 of layer 25 to verify why the model behaviour changed by deactivating or boosting the neuron.
We can also take a look at the attention mechanism. For the purpose of this tutorial we take the last layer and average over all heads.
Run in Google Colab