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

Take hidden states from last non-padding token when batching #38

Merged
merged 4 commits into from
Jul 2, 2024
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
26 changes: 17 additions & 9 deletions repeng/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,15 +276,23 @@ def batched_get_hiddens(
hidden_states = {layer: [] for layer in hidden_layers}
with torch.no_grad():
for batch in tqdm.tqdm(batched_inputs):
out = model(
**tokenizer(batch, padding=True, return_tensors="pt").to(model.device),
output_hidden_states=True,
)
for layer in hidden_layers:
# if not indexing from end, account for embedding hiddens
hidden_idx = layer + 1 if layer >= 0 else layer
for batch in out.hidden_states[hidden_idx]:
hidden_states[layer].append(batch[-1, :].squeeze().cpu().numpy())
# get the last token, handling right padding if present
encoded_batch = tokenizer(batch, padding=True, return_tensors="pt")
encoded_batch = encoded_batch.to(model.device)
out = model(**encoded_batch, output_hidden_states=True)
attention_mask = encoded_batch["attention_mask"]
for i in range(len(batch)):
last_non_padding_index = (
attention_mask[i].nonzero(as_tuple=True)[0][-1].item()
)
for layer in hidden_layers:
hidden_idx = layer + 1 if layer >= 0 else layer
hidden_state = (
out.hidden_states[hidden_idx][i][last_non_padding_index]
.cpu()
.numpy()
)
hidden_states[layer].append(hidden_state)
del out

return {k: np.vstack(v) for k, v in hidden_states.items()}
Expand Down
8 changes: 6 additions & 2 deletions repeng/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,17 @@ def gen(vector: ControlVector | None, strength_coeff: float | None = None):
assert baseline == gen(happy_vector * 0.0)
assert baseline == gen(happy_vector - happy_vector)

assert happy == "You are feeling great and happy. I'm"
assert happy == "You are feeling a little more relaxed and enjoying"
# these should be identical
assert happy == gen(happy_vector, 20.0)
assert happy == gen(happy_vector * 20)
assert happy == gen(-(happy_vector * -20))

assert sad == "You are feeling the worst,\n—("
assert sad == 'You are feeling the fucking damn goddamn worst,"'
# these should be identical
assert sad == gen(happy_vector, -50.0)
assert sad == gen(happy_vector * -50)
assert sad == gen(-(happy_vector * 50))


def test_train_llama_tinystories():
Expand Down
Loading