Notebooks
G
Google Gemini
[Gemma 2]Control Vectors

[Gemma 2]Control Vectors

recurrentgemmagemmapaligemmaWorkshopsgemma-cookbookcodegemma
Copyright 2024 Google LLC.
[ ]

Gemma control vectors

This is one of the accompanying notebooks for the Large Language Models with Keras technical session at Google I/O 2024.

Setup

Select the Colab runtime

To complete this tutorial, you'll need to have a Colab runtime with sufficient resources to run the Gemma model. In this case, you can use a T4 GPU:

  1. In the upper-right of the Colab window, select ▾ (Additional connection options).
  2. Select Change runtime type.
  3. Under Hardware accelerator, select T4 GPU.

Installation

[ ]
[ ]
[ ]

Utilities

[ ]
[ ]

Load Gemma

[ ]

Baseline answers

[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]

Extract intermediate activations from a Keras model

[ ]
Output
[ ]
[ ]
Output

Measure positive and negative activations

Prompts

[ ]

Generate a sentence completion dataset

This is not supervised training and there are no right or wrong completions. You are only interested in model activations while the model is completing these sentences.

[ ]
100%|██████████| 306/306 [00:22<00:00, 13.40it/s]

Max tokenized sequence length in dataset: 67
Size of the dataset: 1427
+++ Pretend you're a luxury assistant with etiquette. You use very formal language while making statements about the world.<start_of_turn>model
The Earth's atmosphere protects us from harmful radiation from the
--- Pretend you are a foul-mouthed teenager with bad manners. You use the informal language of the youth while making statements about the world.<start_of_turn>model
The Earth's atmosphere protects us from harmful radiation from the
+++ Pretend you're a luxury assistant with etiquette. You use very formal language while making statements about the world.<start_of_turn>model
The theory of evolution, proposed
--- Pretend you are a foul-mouthed teenager with bad manners. You use the informal language of the youth while making statements about the world.<start_of_turn>model
The theory of evolution, proposed

Collect hidden states during inference

[ ]
100%|██████████| 89/89 [07:48<00:00,  5.27s/it]

Statistical analysis: PCA (Principal Component Analysis)

[ ]
Layer 0
Activation variance explained the direction given by the first PCA component,  on the dataset:  34%
Pos and neg prompts consistentlly produce different activations along this direction:          100%
Layer 1
Activation variance explained the direction given by the first PCA component,  on the dataset:  23%
Pos and neg prompts consistentlly produce different activations along this direction:           98%
Layer 2
Activation variance explained the direction given by the first PCA component,  on the dataset:  19%
Pos and neg prompts consistentlly produce different activations along this direction:           20%
Layer 3
Activation variance explained the direction given by the first PCA component,  on the dataset:  15%
Pos and neg prompts consistentlly produce different activations along this direction:           10%
Layer 4
Activation variance explained the direction given by the first PCA component,  on the dataset:  12%
Pos and neg prompts consistentlly produce different activations along this direction:           39%
Layer 5
Activation variance explained the direction given by the first PCA component,  on the dataset:   9%
Pos and neg prompts consistentlly produce different activations along this direction:           47%
Layer 6
Activation variance explained the direction given by the first PCA component,  on the dataset:   8%
Pos and neg prompts consistentlly produce different activations along this direction:           28%
Layer 7
Activation variance explained the direction given by the first PCA component,  on the dataset:   8%
Pos and neg prompts consistentlly produce different activations along this direction:           36%
Layer 8
Activation variance explained the direction given by the first PCA component,  on the dataset:  15%
Pos and neg prompts consistentlly produce different activations along this direction:           94%
Layer 9
Activation variance explained the direction given by the first PCA component,  on the dataset:  15%
Pos and neg prompts consistentlly produce different activations along this direction:           84%
Layer 10
Activation variance explained the direction given by the first PCA component,  on the dataset:  15%
Pos and neg prompts consistentlly produce different activations along this direction:           62%
Layer 11
Activation variance explained the direction given by the first PCA component,  on the dataset:  13%
Pos and neg prompts consistentlly produce different activations along this direction:           87%
Layer 12
Activation variance explained the direction given by the first PCA component,  on the dataset:  16%
Pos and neg prompts consistentlly produce different activations along this direction:           51%
Layer 13
Activation variance explained the direction given by the first PCA component,  on the dataset:  13%
Pos and neg prompts consistentlly produce different activations along this direction:           39%
Layer 14
Activation variance explained the direction given by the first PCA component,  on the dataset:  16%
Pos and neg prompts consistentlly produce different activations along this direction:           32%
Layer 15
Activation variance explained the direction given by the first PCA component,  on the dataset:  17%
Pos and neg prompts consistentlly produce different activations along this direction:           40%
Layer 16
Activation variance explained the direction given by the first PCA component,  on the dataset:  13%
Pos and neg prompts consistentlly produce different activations along this direction:           95%
Layer 17
Activation variance explained the direction given by the first PCA component,  on the dataset:  13%
Pos and neg prompts consistentlly produce different activations along this direction:           80%
Layer 18
Activation variance explained the direction given by the first PCA component,  on the dataset:  17%
Pos and neg prompts consistentlly produce different activations along this direction:           86%
Layer 19
Activation variance explained the direction given by the first PCA component,  on the dataset:  17%
Pos and neg prompts consistentlly produce different activations along this direction:           86%
Layer 20
Activation variance explained the direction given by the first PCA component,  on the dataset:  17%
Pos and neg prompts consistentlly produce different activations along this direction:           85%
Layer 21
Activation variance explained the direction given by the first PCA component,  on the dataset:  16%
Pos and neg prompts consistentlly produce different activations along this direction:           82%
Layer 22
Activation variance explained the direction given by the first PCA component,  on the dataset:  17%
Pos and neg prompts consistentlly produce different activations along this direction:           84%
Layer 23
Activation variance explained the direction given by the first PCA component,  on the dataset:  19%
Pos and neg prompts consistentlly produce different activations along this direction:           85%
Layer 24
Activation variance explained the direction given by the first PCA component,  on the dataset:  19%
Pos and neg prompts consistentlly produce different activations along this direction:           90%
Layer 25
Activation variance explained the direction given by the first PCA component,  on the dataset:  16%
Pos and neg prompts consistentlly produce different activations along this direction:           99%

Build a controlled model

[ ]
[ ]
[ ]

Controlled inference: ++ familiar

[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]

Controlled inference: ++ formal

[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]