Skip to content

Commit

Permalink
Fit random directions
Browse files Browse the repository at this point in the history
  • Loading branch information
ojh31 committed Mar 25, 2024
1 parent 037cbc9 commit 1a8f882
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ RUN apt install wget -y
RUN wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox_0.12.6-1.focal_amd64.deb
RUN apt install -f ./wkhtmltox_0.12.6-1.focal_amd64.deb -y
RUN pip install imgkit
RUN pip install pytest transformers_stream_generator tiktoken

14 changes: 12 additions & 2 deletions random_directions.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
# %%
def generate_random_directions(d_model, n_layers, model_name):
torch.random.manual_seed(42)
for layer in range(n_layers + 1):
bar = tqdm(range(n_layers + 1), total=n_layers + 1)
for layer in bar:
bar.set_description(f"Layer {layer}")
random_direction = torch.randn(d_model)
random_direction /= random_direction.norm()
save_array(random_direction, f"random_direction_layer{layer:02d}", model_name)
Expand All @@ -26,10 +28,18 @@ def generate_random_directions(d_model, n_layers, model_name):
# 'EleutherAI/pythia-410m',
# 'EleutherAI/pythia-1.4b',
# 'EleutherAI/pythia-2.8b',
# "gemma-7b",
# "gemma-2b",
"qwen-7b",
"qwen-1.8b",
]
for model in tqdm(MODELS):
model = HookedTransformer.from_pretrained(model)
print(f"Loading model {model}")
model = HookedTransformer.from_pretrained(model, dtype="bfloat16")
d_model = model.cfg.d_model
n_layers = model.cfg.n_layers
print(f"Generating random directions for {model.cfg.model_name}")
generate_random_directions(d_model, n_layers, model.cfg.model_name)
del model
torch.cuda.empty_cache()
# %%

0 comments on commit 1a8f882

Please sign in to comment.