File size: 8,815 Bytes
8709e9b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "daee88ca",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/annt68re/anaconda3/envs/quantize/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
"source": [
"import torch\n",
"import numpy as np\n",
"from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig\n",
"from datasets import load_dataset\n",
"import gc\n",
"import os\n"
]
},
{
"cell_type": "markdown",
"id": "3acda586",
"metadata": {},
"source": [
"## Example: Load Mixtral model"
]
},
{
"cell_type": "markdown",
"id": "7b6829f5",
"metadata": {},
"source": [
"Rename the selected_model and LOAD_PATH to load other models"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "81e82b58",
"metadata": {},
"outputs": [],
"source": [
"selected_model = \"qwen-moe-a2.7b\"\n",
"LOAD_PATH = \"outputs/qwen-moe-a2.7b_20251009_084529\" \n",
"\n",
"input_file = os.path.join(LOAD_PATH, 'tokenized_input.npz')\n",
"metadata_file = os.path.join(LOAD_PATH, 'metadata.txt')\n",
"attention_file = os.path.join(LOAD_PATH, 'attention_matrices_multihead.npz')\n",
"routing_file = os.path.join(LOAD_PATH, 'routing_matrices.npz')\n"
]
},
{
"cell_type": "markdown",
"id": "3b4782df",
"metadata": {},
"source": [
"### Overview of the model"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "37e42ac1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"--- Metadata ---\n",
"model_name: mixtral-8x7b\n",
"model_id: mistralai/Mixtral-8x7B-Instruct-v0.1\n",
"sequence_length: 5000\n",
"num_attention_layers: 32\n",
"num_routing_layers: 32\n",
"timestamp: 20251008_035141\n",
"attention_shape_per_layer: (5000, 5000)\n",
"routing_shape_per_layer: torch.Size([5000, 8])\n",
"has_routing_matrices: Yes\n",
"\n"
]
}
],
"source": [
"print(\"--- Metadata ---\")\n",
"with open(metadata_file, 'r') as f:\n",
" print(f.read())\n"
]
},
{
"cell_type": "markdown",
"id": "e1aabb0d",
"metadata": {},
"source": [
"### Load input data"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "550042ea",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"File size: 0.04 MB\n",
"\n",
"Tokenized Input:\n",
"Shape: (5000,)\n",
"First 20 token IDs: [ 1 327 550 1093 28724 3931 23967 4992 6950 327 28705 13\n",
" 28705 5355 28768 28934 708 550 1093 28724]\n",
"\n",
"Original text length: 50000 characters\n",
"First 200 characters: = Valkyria Chronicles III = \n",
" Senjō no Valkyria 3 : Unrecorded Chronicles ( Japanese : 戦場のヴァルキュリア3 , lit . Valkyria of the Battlefield 3 ) , commonly referred to as Valkyria Chronicles III outside J...\n"
]
}
],
"source": [
"input_data = np.load(input_file)\n",
"print(f\"File size: {os.path.getsize(input_file) / (1024**2):.2f} MB\")\n",
"\n",
"input_ids = input_data['input_ids']\n",
"print(f\"\\nTokenized Input:\")\n",
"print(f\"Shape: {input_ids.shape}\")\n",
"print(f\"First 20 token IDs: {input_ids[:20]}\")\n",
"\n",
"\n",
"input_text = str(input_data['input_text'])\n",
"print(f\"\\nOriginal text length: {len(input_text)} characters\")\n",
"print(f\"First 200 characters: {input_text[:200]}...\")\n"
]
},
{
"cell_type": "markdown",
"id": "be1042d0",
"metadata": {},
"source": [
"### Load attention matrices"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c73d0e93",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"File size: 581.18 MB\n",
"Available layers: 32\n",
"Layer names: ['layer_0', 'layer_1', 'layer_2', 'layer_3', 'layer_4', 'layer_5', 'layer_6', 'layer_7', 'layer_8', 'layer_9', 'layer_10', 'layer_11', 'layer_12', 'layer_13', 'layer_14', 'layer_15', 'layer_16', 'layer_17', 'layer_18', 'layer_19', 'layer_20', 'layer_21', 'layer_22', 'layer_23', 'layer_24', 'layer_25', 'layer_26', 'layer_27', 'layer_28', 'layer_29', 'layer_30', 'layer_31']\n"
]
}
],
"source": [
"attn_data = np.load(attention_file, allow_pickle=True)\n",
"print(f\"File size: {os.path.getsize(attention_file) / (1024**2):.2f} MB\")\n",
"print(f\"Available layers: {len(attn_data.files)}\")\n",
"print(f\"Layer names: {attn_data.files}\")\n"
]
},
{
"cell_type": "markdown",
"id": "1dbcb935",
"metadata": {},
"source": [
"Example: Extract attention matrix of layer 0. "
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2c2aecf1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Layer 0 attention matrix shape: (5000, 5000)\n",
"Layer 0 sample attention (first 5 tokens):\n",
"[[1. 0. 0. 0. 0. ]\n",
" [0.91 0.0879 0. 0. 0. ]\n",
" [0.8164 0.1406 0.0437 0. 0. ]\n",
" [0.785 0.0884 0.0962 0.02917 0. ]\n",
" [0.7773 0.05298 0.0718 0.07666 0.02332]]\n"
]
}
],
"source": [
"layer_0 = attn_data['layer_0']\n",
"print(f\"\\nLayer 0 attention matrix:\")\n",
"print(f\"Shape: {layer_0.shape} [num_heads, seq_len, seq_len]\")\n",
"print(f\"Number of heads: {layer_0.shape[0]}\")\n",
"print(f\"Min value: {layer_0.min():.6f}\")\n",
"print(f\"Max value: {layer_0.max():.6f}\")\n",
"print(f\"Mean value: {layer_0.mean():.6f}\")\n",
"\n",
"print(f\"\\n Head 0 attention sample (first 5x5 tokens):\")\n",
"print(layer_0[0, :5, :5])\n"
]
},
{
"cell_type": "markdown",
"id": "2fbf90d1",
"metadata": {},
"source": [
"### Load routing matrices"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "439c1bc1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"File size: 1.88 MB\n",
"Available layers: 32\n",
"Layer names: ['layer_0', 'layer_1', 'layer_2', 'layer_3', 'layer_4', 'layer_5', 'layer_6', 'layer_7', 'layer_8', 'layer_9', 'layer_10', 'layer_11', 'layer_12', 'layer_13', 'layer_14', 'layer_15', 'layer_16', 'layer_17', 'layer_18', 'layer_19', 'layer_20', 'layer_21', 'layer_22', 'layer_23', 'layer_24', 'layer_25', 'layer_26', 'layer_27', 'layer_28', 'layer_29', 'layer_30', 'layer_31']\n"
]
}
],
"source": [
"routing_data = np.load(routing_file)\n",
"print(f\"File size: {os.path.getsize(routing_file) / (1024**2):.2f} MB\")\n",
"print(f\"Available layers: {len(routing_data.files)}\")\n",
"print(f\"Layer names: {routing_data.files}\")\n"
]
},
{
"cell_type": "markdown",
"id": "9b79defd",
"metadata": {},
"source": [
"Example: Extract routing matrix of layer 0."
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "ea00690c",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Routing logits sample (first 5 tokens, all experts):\n",
"[[-0.3574 0.1875 0.01233 -0.1157 -0.496 1.195 -0.1914 -0.00653]\n",
" [-0.617 -2.016 1.523 0.6367 -1.086 1.25 0.84 0.01318]\n",
" [-0.832 -1.602 2.36 0.3926 -0.926 1.578 -0.1494 -0.463 ]\n",
" [-1.32 0.1177 2.25 3.156 -1.6875 0.5 0.1631 -3.266 ]\n",
" [ 1.414 -1.578 0.377 0.91 -0.1367 0.9844 -0.2617 -1.57 ]]\n"
]
}
],
"source": [
"layer_0 = routing_data['layer_0']\n",
"print(f\"Routing logits sample (first 5 tokens, all experts):\")\n",
"print(layer_0[:5, :])\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "quantize",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.18"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|