mirror of
https://github.com/wassname/activation_store.git
synced 2026-08-07 11:18:31 +08:00
9.2 KiB
9.2 KiB
In [1]:
%reload_ext autoreload
%autoreload 2In [2]:
from datasets import load_dataset
from transformers import AutoModelForCausalLM, AutoTokenizer
from activation_store.collect import activation_store
import torchIn [3]:
model_name = "Qwen/Qwen2.5-0.5B-Instruct"
model = AutoModelForCausalLM.from_pretrained(
model_name,
torch_dtype="auto",
device_map="auto",
attn_implementation="eager", # flex_attention flash_attention_2 sdpa eager
)
tokenizer = AutoTokenizer.from_pretrained(model_name)
In [4]:
N = 20
max_length = 256
imdb = load_dataset('wassname/imdb_dpo', split=f'test[:{N}]', keep_in_memory=False)
def proc(row):
messages = [
{"role":"user", "content": row['prompt'] },
{"role":"assistant", "content": row['chosen'] }
]
return tokenizer.apply_chat_template(messages, tokenize=True, add_generation_prompt=False, return_dict=True, max_length=max_length)
ds2 = imdb.map(proc).with_format("torch")
new_cols = set(ds2.column_names) - set(imdb.column_names)
ds2 = ds2.select_columns(new_cols)
ds2Out [4]:
Dataset({
features: ['input_ids', 'attention_mask'],
num_rows: 20
})In [5]:
from torch.utils.data import DataLoader
def collate_fn(examples):
# Pad the batch to max length within this batch
return tokenizer.pad(
examples,
padding=True,
return_tensors="pt",
)
ds = DataLoader(ds2, batch_size=4, num_workers=0, collate_fn=collate_fn)
print(ds)
<torch.utils.data.dataloader.DataLoader object at 0x76557465f770>
In [7]:
# choose layers to cache
layers = [k for k,v in model.named_modules() if 'mlp.down_proj' in k]
layersOut [7]:
['model.layers.0.mlp.down_proj', 'model.layers.1.mlp.down_proj', 'model.layers.2.mlp.down_proj', 'model.layers.3.mlp.down_proj', 'model.layers.4.mlp.down_proj', 'model.layers.5.mlp.down_proj', 'model.layers.6.mlp.down_proj', 'model.layers.7.mlp.down_proj', 'model.layers.8.mlp.down_proj', 'model.layers.9.mlp.down_proj', 'model.layers.10.mlp.down_proj', 'model.layers.11.mlp.down_proj', 'model.layers.12.mlp.down_proj', 'model.layers.13.mlp.down_proj', 'model.layers.14.mlp.down_proj', 'model.layers.15.mlp.down_proj', 'model.layers.16.mlp.down_proj', 'model.layers.17.mlp.down_proj', 'model.layers.18.mlp.down_proj', 'model.layers.19.mlp.down_proj', 'model.layers.20.mlp.down_proj', 'model.layers.21.mlp.down_proj', 'model.layers.22.mlp.down_proj', 'model.layers.23.mlp.down_proj']
In [ ]:
f = activation_store(ds, model, layers=layers, writer_batch_size=10)
f[32m2025-02-15 21:58:37.654[0m | [1mINFO [0m | [36mactivation_store.collect[0m:[36mactivation_store[0m:[36m70[0m - [1mcreating dataset /media/wassname/SGIronWolf/projects5/elk/cache_transformer_acts/outputs/.ds/ds__9b3f4b0da96e9ad5.parquet[0m
collecting activations: 0%| | 0/5 [00:00<?, ?it/s]
You're using a Qwen2TokenizerFast tokenizer. Please note that with a fast tokenizer, using the `__call__` method is faster than using a method to encode the text followed by a call to the `pad` method to get a padded encoding.
[0;31m---------------------------------------------------------------------------[0m [0;31mNameError[0m Traceback (most recent call last) Cell [0;32mIn[8], line 2[0m [1;32m 1[0m f [38;5;241m=[39m activation_store(ds, model, layers[38;5;241m=[39mlayers, writer_batch_size[38;5;241m=[39m[38;5;241m10[39m) [0;32m----> 2[0m [43mds_a[49m [0;31mNameError[0m: name 'ds_a' is not defined
In [ ]:
from datasets import Dataset
Dataset.from_parquet(str(f))Generating train split: 0 examples [00:00, ? examples/s]
Dataset({
features: ['act-model.layers.0.mlp.down_proj', 'act-model.layers.1.mlp.down_proj', 'act-model.layers.2.mlp.down_proj', 'act-model.layers.3.mlp.down_proj', 'act-model.layers.4.mlp.down_proj', 'act-model.layers.5.mlp.down_proj', 'act-model.layers.6.mlp.down_proj', 'act-model.layers.7.mlp.down_proj', 'act-model.layers.8.mlp.down_proj', 'act-model.layers.9.mlp.down_proj', 'act-model.layers.10.mlp.down_proj', 'act-model.layers.11.mlp.down_proj', 'act-model.layers.12.mlp.down_proj', 'act-model.layers.13.mlp.down_proj', 'act-model.layers.14.mlp.down_proj', 'act-model.layers.15.mlp.down_proj', 'act-model.layers.16.mlp.down_proj', 'act-model.layers.17.mlp.down_proj', 'act-model.layers.18.mlp.down_proj', 'act-model.layers.19.mlp.down_proj', 'act-model.layers.20.mlp.down_proj', 'act-model.layers.21.mlp.down_proj', 'act-model.layers.22.mlp.down_proj', 'act-model.layers.23.mlp.down_proj', 'logits', 'hidden_states'],
num_rows: 20
})In [ ]:
In [ ]:
ds_a[0:2]['logits'].shapeIn [ ]:
ds_a[0:2]['model.layers.0.mlp.down_proj'].shapeIn [ ]: