Skip to content

GKD: never pass a zero generated-token count - #15328

Merged
danielhanchen merged 5 commits into
mainfrom
gkd-num-items-zero-guard
Oct 26, 2026
Merged

danielhanchen merged 5 commits into
mainfrom
gkd-num-items-zero-guard

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Follow-up to #15222, which recounts GKD's num_items_in_batch from the generated labels. Merge it after #15222; until then this branch also carries #15222's two commits.

On TRL 1.7 and 1.8, generate_on_policy_outputs masks every token equal to pad_token_id, EOS included when the tokenizer pads with EOS. A gradient accumulation window whose completions are all an immediate EOS therefore has zero supervised tokens, the recount returns 0, and TRL's generalized_jsd_loss returns jsd.sum() / 0 = NaN (the dataset count used before #15222 was nonzero, so the loss was 0). Unsloth's chunked JSD divides the same way.

This clamps the shared count to at least 1 (tensor or int). The window's JSD sum is 0 in that case, so the loss becomes 0 instead of NaN; any window with supervised tokens is unchanged.

It also adds tests/python/test_gkd_generated_token_count.py, which rewrites both TRL training_step layouts and the installed TRL's own source and checks:

  • num_items_in_batch equals the generated label count for on-policy and seq_kd, whatever the dataset answers were, including a short final window.
  • each microbatch is generated once and no internal key reaches the loss.
  • a mixed window (lmbda=0.5) draws the same random numbers as stock TRL.
  • off-policy keeps the dataset count; an unrecognised training_step is left alone.
  • an all-masked window counts 1, for int and tensor counts (fails without the clamp).
TRL 1.8 generalized_jsd_loss, all labels masked loss
num_items_in_batch = 0 (#15222 alone) nan
num_items_in_batch = 1 (this PR) 0.0
num_items_in_batch = 4 (before #15222) 0.0

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 26, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-26T06:10:54.923676Z 404a9e9 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🎉

Reviewed commit: 404a9e9c10

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

# Conflicts:
#	unsloth/models/rl_replacements.py
@danielhanchen
danielhanchen merged commit 6176ae6 into main Oct 26, 2026
4 of 30 checks passed
@danielhanchen
danielhanchen deleted the gkd-num-items-zero-guard branch October 26, 2026 07:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants