Skip to content

Commit

Permalink
Allow loading only first n layers. (#717)
Browse files Browse the repository at this point in the history
  • Loading branch information
joelburget authored Sep 10, 2024
1 parent da273f9 commit f1ee5fb
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
3 changes: 3 additions & 0 deletions transformer_lens/HookedTransformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,7 @@ def from_pretrained(
default_prepend_bos: bool = True,
default_padding_side: Literal["left", "right"] = "right",
dtype="float32",
first_n_layers: Optional[int] = None,
**from_pretrained_kwargs,
) -> "HookedTransformer":
"""Load in a Pretrained Model.
Expand Down Expand Up @@ -1204,6 +1205,7 @@ def from_pretrained(
the model.
default_padding_side: Which side to pad on when tokenizing. Defaults to
"right".
first_n_layers: If specified, only load the first n layers of the model.
"""

assert not (
Expand Down Expand Up @@ -1261,6 +1263,7 @@ def from_pretrained(
n_devices=n_devices,
default_prepend_bos=default_prepend_bos,
dtype=dtype,
first_n_layers=first_n_layers,
**from_pretrained_kwargs,
)

Expand Down
3 changes: 3 additions & 0 deletions transformer_lens/loading_from_pretrained.py
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,7 @@ def get_pretrained_model_config(
n_devices: int = 1,
default_prepend_bos: bool = True,
dtype: torch.dtype = torch.float32,
first_n_layers: Optional[int] = None,
**kwargs,
):
"""Returns the pretrained model config as an HookedTransformerConfig object.
Expand Down Expand Up @@ -1501,6 +1502,8 @@ def get_pretrained_model_config(
cfg_dict["default_prepend_bos"] = default_prepend_bos
if hf_cfg is not None:
cfg_dict["load_in_4bit"] = hf_cfg.get("quantization_config", {}).get("load_in_4bit", False)
if first_n_layers is not None:
cfg_dict["n_layers"] = first_n_layers

cfg = HookedTransformerConfig.from_dict(cfg_dict)
return cfg
Expand Down

0 comments on commit f1ee5fb

Please sign in to comment.