mirror of
https://github.com/wassname/activation_store.git
synced 2026-08-14 12:10:08 +08:00
9.0 KiB
9.0 KiB
In [1]:
%reload_ext autoreload
%autoreload 2In [2]:
from datasets import load_dataset
from transformers import AutoModelForCausalLM, AutoTokenizer
from activation_store.collect import collect_act_to_disk
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)
imdbOut [4]:
Dataset({
features: ['prompt', 'chosen', 'rejected'],
num_rows: 20
})In [5]:
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 [5]:
Dataset({
features: ['input_ids', 'attention_mask'],
num_rows: 20
})In [6]:
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=2, num_workers=0, collate_fn=collate_fn)
print(ds)
<torch.utils.data.dataloader.DataLoader object at 0x7f6ddd90fcb0>
In [7]:
# # sanity check with one manual forward
# b = next(iter(ds))
# outputs = model(**b)
# outputs.keys()In [8]:
# choose layers to cache
layers = [k for k,v in model.named_modules() if 'mlp.down_proj' in k]
layersOut [8]:
['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 [9]:
ds_a, f = collect_act_to_disk(ds, model, layers=layers)
ds_aOut [9]:
[32m2025-02-15 21:14:24.538[0m | [1mINFO [0m | [36mactivation_store.collect[0m:[36mcollect_act_to_disk[0m:[36m60[0m - [1mcreating dataset /media/wassname/SGIronWolf/projects5/elk/outputs/.ds/ds__7ae34f9e83796c91[0m
collecting hidden states: 0%| | 0/10 [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.
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 [10]:
ds_a[0:2]['logits'].shapeOut [10]:
torch.Size([2, 453, 151936])
In [11]:
ds_a[0:2]['model.layers.0.mlp.down_proj'].shape[0;31m---------------------------------------------------------------------------[0m [0;31mKeyError[0m Traceback (most recent call last) Cell [0;32mIn[11], line 1[0m [0;32m----> 1[0m [43mds_a[49m[43m[[49m[38;5;241;43m0[39;49m[43m:[49m[38;5;241;43m2[39;49m[43m][49m[43m[[49m[38;5;124;43m'[39;49m[38;5;124;43mmodel.layers.0.mlp.down_proj[39;49m[38;5;124;43m'[39;49m[43m][49m[38;5;241m.[39mshape [0;31mKeyError[0m: 'model.layers.0.mlp.down_proj'
In [ ]: