Skip to content
Open
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
7 changes: 5 additions & 2 deletions src/lighteval/models/transformers/transformers_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -1118,7 +1118,7 @@ def _loglikelihood_tokens( # noqa: C901
# 2d on num choices and max len
len_choice = gathered_len_choices[i]
batch_tokenized_continuations_processed.append(
gathered_continuations[i][:num_choices][:len_choice]
gathered_continuations[i, :num_choices, :len_choice]
)
# 1d on max len context
len_context = gathered_len_context[i]
Expand All @@ -1134,7 +1134,10 @@ def _loglikelihood_tokens( # noqa: C901
argmax_logits_eq_gold=[max_equal.cpu().item() for max_equal in max_equals_doc],
logprobs=[sum.cpu().item() for sum in logits_sum_doc],
input_tokens=tokenized_contexts_batch,
output_tokens=tokenized_continuations_batch,
output_tokens=[
choice_tokens[choice_tokens != -1].cpu().tolist()
for choice_tokens in tokenized_continuations_batch
],
)
all_responses.append(answer)

Expand Down
9 changes: 8 additions & 1 deletion tests/unit/models/test_transformers_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def mock_gather(tensor):
self.model.accelerator.gather_for_metrics = mock_gather

# Call the function under test
self.model._loglikelihood_tokens(docs)
results = self.model._loglikelihood_tokens(docs)

# Verify we captured everyone
self.assertIsNotNone(captured_num_choices, "Should have captured gathered_num_choices")
Expand Down Expand Up @@ -367,6 +367,13 @@ def mock_gather(tensor):
# - Test 2D padded contexts: (batch_size, max_len_context)
self.assertEqual(captured_padded_contexts.shape, (len(docs), max(captured_len_context)))

# Verify padding is removed after gathering without dropping any choices
self.assertEqual(
[len(results[0].output_tokens), len(results[1].output_tokens)],
[len(docs[0].choices), len(docs[1].choices)],
)
self.assertNotIn(-1, sum(results[0].output_tokens + results[1].output_tokens, []))

# Verify padding values for 1D tensors
# - First doc has 3 choices, so positions [3:5] should be padded with -1 for logits and False for max_equals
self.assertTrue(torch.all(captured_padded_logits[0, 3:] == -1), "Padded positions in logits should be -1")
Expand Down