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

Fit reporters on input embeddings as a sanity check #209

Merged
merged 4 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
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: 7 additions & 7 deletions elk/extraction/extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ def __post_init__(self, layer_stride: int):
config = assert_type(
PretrainedConfig, AutoConfig.from_pretrained(self.model)
)
self.layers = tuple(range(0, config.num_hidden_layers, layer_stride))
# Note that we always include 0 which is the embedding layer
layer_range = range(1, config.num_hidden_layers + 1, layer_stride)
self.layers = (0,) + tuple(layer_range)

def explode(self) -> list["Extract"]:
"""Explode this config into a list of configs, one for each layer."""
Expand Down Expand Up @@ -136,8 +138,8 @@ def extract_hiddens(
world_size=world_size,
)

# Iterating over questions
layer_indices = cfg.layers or tuple(range(model.config.num_hidden_layers))
# Add one to the number of layers to account for the embedding layer
layer_indices = cfg.layers or tuple(range(model.config.num_hidden_layers + 1))

global_max_examples = p_cfg.max_examples[0 if split_type == "train" else 1]
# break `max_examples` among the processes roughly equally
Expand Down Expand Up @@ -229,9 +231,6 @@ def extract_hiddens(
hiddens = (
outputs.get("decoder_hidden_states") or outputs["hidden_states"]
)
# First element of list is the input embeddings
hiddens = hiddens[1:]

# Throw out layers we don't care about
hiddens = [hiddens[i] for i in layer_indices]

Expand Down Expand Up @@ -320,7 +319,8 @@ def get_splits() -> SplitDict:
dtype="int16",
shape=(num_variants, num_classes, model_cfg.hidden_size),
)
for layer in cfg.layers or range(model_cfg.num_hidden_layers)
# Add 1 to include the embedding layer
for layer in cfg.layers or range(model_cfg.num_hidden_layers + 1)
}
other_cols = {
"variant_ids": Sequence(
Expand Down
3 changes: 3 additions & 0 deletions elk/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ def apply_to_layers(
# Make sure the CSV is written even if we crash or get interrupted
if df_buf:
df = pd.concat(df_buf).sort_values(by="layer")

# Rename layer 0 to "input" to make it more clear
df["layer"].replace(0, "input", inplace=True)
df.round(4).to_csv(f, index=False)
if self.cfg.debug:
save_debug_log(self.datasets, self.out_dir)