|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Logit Comparator for HuggingFace and TransformerLens Outputs\n", |
| 8 | + "This notebook is a quick and dirty tool to compare the logit outputs of a HuggingFace model and a TransformerLens model via several different metrics. It is intended to help debug issues with the TransformerLens model, such as bugs in the model's implementation. If you identify any issues, please open an issue on the [GitHub repository](https://github.com/TransformerLensOrg/TransformerLens)." |
| 9 | + ] |
| 10 | + }, |
| 11 | + { |
| 12 | + "cell_type": "code", |
| 13 | + "execution_count": null, |
| 14 | + "metadata": {}, |
| 15 | + "outputs": [], |
| 16 | + "source": [ |
| 17 | + "from transformers import AutoTokenizer, AutoModelForCausalLM\n", |
| 18 | + "from transformer_lens import HookedTransformer\n", |
| 19 | + "import torch\n", |
| 20 | + "import torch.nn.functional as F\n", |
| 21 | + "\n", |
| 22 | + "if torch.backends.mps.is_available():\n", |
| 23 | + " device = \"mps\"\n", |
| 24 | + "else:\n", |
| 25 | + " device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n", |
| 26 | + "\n", |
| 27 | + "torch.set_grad_enabled(False)" |
| 28 | + ] |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "markdown", |
| 32 | + "metadata": {}, |
| 33 | + "source": [ |
| 34 | + "## Comparator Setup" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": 51, |
| 40 | + "metadata": {}, |
| 41 | + "outputs": [], |
| 42 | + "source": [ |
| 43 | + "model_name = \"EleutherAI/pythia-2.8b\" # You can change this to any model name\n", |
| 44 | + "sentence = \"The quick brown fox\"" |
| 45 | + ] |
| 46 | + }, |
| 47 | + { |
| 48 | + "cell_type": "code", |
| 49 | + "execution_count": null, |
| 50 | + "metadata": {}, |
| 51 | + "outputs": [], |
| 52 | + "source": [ |
| 53 | + "from huggingface_hub import login\n", |
| 54 | + "login(token=\"\")" |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "markdown", |
| 59 | + "metadata": {}, |
| 60 | + "source": [ |
| 61 | + "## Get Transformers Logits" |
| 62 | + ] |
| 63 | + }, |
| 64 | + { |
| 65 | + "cell_type": "code", |
| 66 | + "execution_count": null, |
| 67 | + "metadata": {}, |
| 68 | + "outputs": [], |
| 69 | + "source": [ |
| 70 | + "import torch\n", |
| 71 | + "from transformers import AutoTokenizer, AutoModelForCausalLM\n", |
| 72 | + "\n", |
| 73 | + "def load_model(model_name=\"gpt2\"):\n", |
| 74 | + " tokenizer = AutoTokenizer.from_pretrained(model_name)\n", |
| 75 | + " model = AutoModelForCausalLM.from_pretrained(model_name)\n", |
| 76 | + " return model, tokenizer\n", |
| 77 | + "\n", |
| 78 | + "def get_logits(model, tokenizer, sentence, device):\n", |
| 79 | + " # Tokenize the input sentence\n", |
| 80 | + " inputs = tokenizer(sentence, return_tensors=\"pt\")\n", |
| 81 | + " \n", |
| 82 | + " # Move inputs to the device\n", |
| 83 | + " inputs = {k: v.to(device) for k, v in inputs.items()}\n", |
| 84 | + " \n", |
| 85 | + " # Generate the logits\n", |
| 86 | + " with torch.no_grad():\n", |
| 87 | + " outputs = model(**inputs)\n", |
| 88 | + " \n", |
| 89 | + " # Get the logits for all tokens\n", |
| 90 | + " logits = outputs.logits\n", |
| 91 | + " \n", |
| 92 | + " return logits\n", |
| 93 | + "\n", |
| 94 | + "model, tokenizer = load_model(model_name)\n", |
| 95 | + "model = model.to(device)\n", |
| 96 | + "\n", |
| 97 | + "hf_logits = get_logits(model, tokenizer, sentence, device)[:, -1, :]" |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "cell_type": "markdown", |
| 102 | + "metadata": {}, |
| 103 | + "source": [ |
| 104 | + "## Get TransformerLens Logits" |
| 105 | + ] |
| 106 | + }, |
| 107 | + { |
| 108 | + "cell_type": "code", |
| 109 | + "execution_count": null, |
| 110 | + "metadata": {}, |
| 111 | + "outputs": [], |
| 112 | + "source": [ |
| 113 | + "model = HookedTransformer.from_pretrained_no_processing(model_name, device=device)\n", |
| 114 | + "tokens = model.to_tokens(sentence, prepend_bos=False)\n", |
| 115 | + "tl_logits = model(tokens)[:, -1, :]" |
| 116 | + ] |
| 117 | + }, |
| 118 | + { |
| 119 | + "cell_type": "markdown", |
| 120 | + "metadata": {}, |
| 121 | + "source": [ |
| 122 | + "## Compare Logit Distributions\n", |
| 123 | + "Various metrics are used to compare the logit distributions of the two models. We don't yet have standard values for what constitutes a \"good\" logit comparison, so we are working on establishing benchmarks." |
| 124 | + ] |
| 125 | + }, |
| 126 | + { |
| 127 | + "cell_type": "markdown", |
| 128 | + "metadata": {}, |
| 129 | + "source": [ |
| 130 | + "### Shape" |
| 131 | + ] |
| 132 | + }, |
| 133 | + { |
| 134 | + "cell_type": "code", |
| 135 | + "execution_count": null, |
| 136 | + "metadata": {}, |
| 137 | + "outputs": [], |
| 138 | + "source": [ |
| 139 | + "print(f\"HF Logits Shape: {hf_logits.shape}\")\n", |
| 140 | + "print(f\"TL Logits Shape: {tl_logits.shape}\")" |
| 141 | + ] |
| 142 | + }, |
| 143 | + { |
| 144 | + "cell_type": "markdown", |
| 145 | + "metadata": {}, |
| 146 | + "source": [ |
| 147 | + "### Tensor Comparison" |
| 148 | + ] |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_type": "code", |
| 152 | + "execution_count": null, |
| 153 | + "metadata": {}, |
| 154 | + "outputs": [], |
| 155 | + "source": [ |
| 156 | + "are_close = torch.allclose(tl_logits, hf_logits, rtol=1e-5, atol=1e-3)\n", |
| 157 | + "print(f\"Are the logits close? {are_close}\")" |
| 158 | + ] |
| 159 | + }, |
| 160 | + { |
| 161 | + "cell_type": "markdown", |
| 162 | + "metadata": {}, |
| 163 | + "source": [ |
| 164 | + "### Mean Squared Error" |
| 165 | + ] |
| 166 | + }, |
| 167 | + { |
| 168 | + "cell_type": "code", |
| 169 | + "execution_count": null, |
| 170 | + "metadata": {}, |
| 171 | + "outputs": [], |
| 172 | + "source": [ |
| 173 | + "# Compare the logits with MSE\n", |
| 174 | + "mse = torch.nn.functional.mse_loss(hf_logits, tl_logits)\n", |
| 175 | + "print(f\"MSE: {mse}\")" |
| 176 | + ] |
| 177 | + }, |
| 178 | + { |
| 179 | + "cell_type": "markdown", |
| 180 | + "metadata": {}, |
| 181 | + "source": [ |
| 182 | + "### Maximum Absolute Difference" |
| 183 | + ] |
| 184 | + }, |
| 185 | + { |
| 186 | + "cell_type": "code", |
| 187 | + "execution_count": null, |
| 188 | + "metadata": {}, |
| 189 | + "outputs": [], |
| 190 | + "source": [ |
| 191 | + "max_diff = torch.max(torch.abs(tl_logits - hf_logits))\n", |
| 192 | + "print(f\"Max Diff: {max_diff}\")" |
| 193 | + ] |
| 194 | + }, |
| 195 | + { |
| 196 | + "cell_type": "markdown", |
| 197 | + "metadata": {}, |
| 198 | + "source": [ |
| 199 | + "### Cosine Similarity" |
| 200 | + ] |
| 201 | + }, |
| 202 | + { |
| 203 | + "cell_type": "code", |
| 204 | + "execution_count": null, |
| 205 | + "metadata": {}, |
| 206 | + "outputs": [], |
| 207 | + "source": [ |
| 208 | + "cosine_sim = F.cosine_similarity(tl_logits, hf_logits, dim=-1).mean()\n", |
| 209 | + "print(f\"Cosine Sim: {cosine_sim}\")" |
| 210 | + ] |
| 211 | + }, |
| 212 | + { |
| 213 | + "cell_type": "markdown", |
| 214 | + "metadata": {}, |
| 215 | + "source": [ |
| 216 | + "### KL Divergence" |
| 217 | + ] |
| 218 | + }, |
| 219 | + { |
| 220 | + "cell_type": "code", |
| 221 | + "execution_count": null, |
| 222 | + "metadata": {}, |
| 223 | + "outputs": [], |
| 224 | + "source": [ |
| 225 | + "def kl_div(logits1: torch.Tensor, logits2: torch.Tensor) -> torch.Tensor:\n", |
| 226 | + " probs1 = F.softmax(logits1, dim=-1)\n", |
| 227 | + " probs2 = F.softmax(logits2, dim=-1)\n", |
| 228 | + " return F.kl_div(probs1.log(), probs2, reduction='batchmean')\n", |
| 229 | + "\n", |
| 230 | + "kl_tl_hf = kl_div(tl_logits, hf_logits)\n", |
| 231 | + "kl_hf_tl = kl_div(hf_logits, tl_logits)\n", |
| 232 | + "print(f\"KL(TL||HF): {kl_tl_hf}\")\n", |
| 233 | + "print(f\"KL(HF||TL): {kl_hf_tl}\")" |
| 234 | + ] |
| 235 | + }, |
| 236 | + { |
| 237 | + "cell_type": "code", |
| 238 | + "execution_count": null, |
| 239 | + "metadata": {}, |
| 240 | + "outputs": [], |
| 241 | + "source": [] |
| 242 | + } |
| 243 | + ], |
| 244 | + "metadata": { |
| 245 | + "kernelspec": { |
| 246 | + "display_name": "sae-l", |
| 247 | + "language": "python", |
| 248 | + "name": "python3" |
| 249 | + }, |
| 250 | + "language_info": { |
| 251 | + "codemirror_mode": { |
| 252 | + "name": "ipython", |
| 253 | + "version": 3 |
| 254 | + }, |
| 255 | + "file_extension": ".py", |
| 256 | + "mimetype": "text/x-python", |
| 257 | + "name": "python", |
| 258 | + "nbconvert_exporter": "python", |
| 259 | + "pygments_lexer": "ipython3", |
| 260 | + "version": "3.12.4" |
| 261 | + } |
| 262 | + }, |
| 263 | + "nbformat": 4, |
| 264 | + "nbformat_minor": 2 |
| 265 | +} |
0 commit comments