Skip to content

Commit 648c4ae

Browse files
committed
re-normalize probability distribution when clip_logits_epsilon is used.
1 parent a861186 commit 648c4ae

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

tests/utils/forward_pass_logit_checker.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ def compare_top_tokens(converted_tokens, golden_tokens):
167167
max_logging.log(table_str)
168168

169169

170-
def check_kl_divergence(model_logits, golden_logits, atol=0.02):
170+
def check_kl_divergence(model_logits, golden_logits, atol=0.02, clip_logits_epsilon=None):
171171
"""
172172
Calculates KL divergence D_KL(P_golden || Q_model) over a batch of sequences.
173173
@@ -189,7 +189,16 @@ def check_kl_divergence(model_logits, golden_logits, atol=0.02):
189189

190190
# 3. Get the probability distributions.
191191
golden_probabilities = F.softmax(golden_logits_reshaped, dim=-1)
192-
model_log_probabilities = F.log_softmax(model_logits_reshaped, dim=-1)
192+
193+
# Apply clipping and re-normalization to predicted probabilities ONLY
194+
if clip_logits_epsilon is not None:
195+
model_probabilities = F.softmax(model_logits_reshaped, dim=-1)
196+
model_probabilities = torch.clamp(model_probabilities, min=clip_logits_epsilon)
197+
model_probabilities = model_probabilities / model_probabilities.sum(dim=-1, keepdim=True)
198+
# Compute manual log-probabilities of the model. Safe because of clamping.
199+
model_log_probabilities = torch.log(model_probabilities)
200+
else:
201+
model_log_probabilities = F.log_softmax(model_logits_reshaped, dim=-1)
193202

194203
# 4. Calculate avg KL divergence for all token distributions.
195204
# use 'batchmean'; the sum of the KL divergences for each token in the batch
@@ -464,6 +473,9 @@ def main(config, test_args): # pylint: disable=W0621
464473
if test_args.clip_logits_epsilon is not None:
465474
model_probabilities = jnp.clip(jax.nn.softmax(train_logits_slice, axis=-1), min=test_args.clip_logits_epsilon)
466475
golden_probabilities = jnp.clip(jax.nn.softmax(golden_logits_slice, axis=-1), min=test_args.clip_logits_epsilon)
476+
# Re-normalize so probabilities sum to 1.
477+
golden_probabilities = golden_probabilities / jnp.sum(golden_probabilities, axis=-1, keepdims=True)
478+
model_probabilities = model_probabilities / jnp.sum(model_probabilities, axis=-1, keepdims=True)
467479
else:
468480
model_probabilities = jax.nn.softmax(train_logits_slice, axis=-1)
469481
golden_probabilities = jax.nn.softmax(golden_logits_slice, axis=-1)
@@ -661,6 +673,7 @@ def main(config, test_args): # pylint: disable=W0621
661673
mt_logits_torch[0, start_index:].unsqueeze(0),
662674
hf_logits_torch[0, start_index:].unsqueeze(0),
663675
atol=test_args.max_kl_div,
676+
clip_logits_epsilon=test_args.clip_logits_epsilon,
664677
)
665678
if jax.process_index() == 0 and test_args.output_logits_path:
666679
data_to_save = {
@@ -729,9 +742,6 @@ def main(config, test_args): # pylint: disable=W0621
729742
test_args.atol is not None or test_args.max_kl_div is not None
730743
), "At least one of --atol or --max_kl_div must be specified to define the test criteria."
731744

732-
if test_args.run_hf_model and test_args.clip_logits_epsilon is not None:
733-
raise ValueError("--clip_logits_epsilon is not supported when running HF model on-the-fly (run_hf_model=True).")
734-
735745
if cfg.use_multimodal:
736746
assert not test_args.run_hf_model, (
737747
"Multimodal does not support running hf model on-the-fly, please generate hf golden logits "

0 commit comments

Comments
 (0)