Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending Support for Additional Bloom Models (up to 7b) #447

Merged
merged 3 commits into from
Jan 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions transformer_lens/loading_from_pretrained.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@
"stabilityai/stablelm-tuned-alpha-3b",
"stabilityai/stablelm-tuned-alpha-7b",
"bigscience/bloom-560m",
"bigscience/bloom-1b1",
"bigscience/bloom-1b7",
"bigscience/bloom-3b",
"bigscience/bloom-7b1",
"bigcode/santacoder",
]
"""Official model names for models on HuggingFace."""
Expand Down Expand Up @@ -497,6 +501,10 @@
"stablelm-tuned-7b",
],
"bigscience/bloom-560m": ["bloom-560m"],
"bigscience/bloom-1b1": ["bloom-1b1"],
"bigscience/bloom-1b7": ["bloom-1b7"],
"bigscience/bloom-3b": ["bloom-3b"],
"bigscience/bloom-7b1": ["bloom-7b1"],
"bigcode/santacoder": ["santacoder"],
}
"""Model aliases for models on HuggingFace."""
Expand Down Expand Up @@ -1684,10 +1692,8 @@ def convert_bloom_weights(bloom, cfg: HookedTransformerConfig):
state_dict[f"blocks.{l}.ln1.w"] = bloom.transformer.h[l].input_layernorm.weight
state_dict[f"blocks.{l}.ln1.b"] = bloom.transformer.h[l].input_layernorm.bias

# Bloom attn weight is stored as a fused matrx. BloomAttn: Linear(in=1024, out=3072)
# The .weight returned matrix will be in shape (3072, 1024)
W = bloom.transformer.h[l].self_attention.query_key_value.weight
# First transpose -> (1024, 3072), then split into (d_model, n_heads, 3, d_head)

W_split = W.T.reshape(cfg.d_model, cfg.n_heads, 3, cfg.d_head)

W_Q, W_K, W_V = W_split[..., 0, :], W_split[..., 1, :], W_split[..., 2, :]
Expand Down Expand Up @@ -1732,7 +1738,7 @@ def convert_bloom_weights(bloom, cfg: HookedTransformerConfig):
state_dict[f"blocks.{l}.mlp.b_out"] = bloom.transformer.h[
l
].mlp.dense_4h_to_h.bias
state_dict["unembed.W_U"] = bloom.lm_head.weight.T # transpose to match shape
state_dict["unembed.W_U"] = bloom.lm_head.weight.T

state_dict["ln_final.w"] = bloom.transformer.ln_f.weight
state_dict["ln_final.b"] = bloom.transformer.ln_f.bias
Expand Down