Skip to content

Wire the chunked generalized JSD into the GKD path #11554

Closed

Description

@danielhanchen

unsloth_zoo.rl_replacements.distillation_chunked_jsd is merged (unslothai/unsloth-zoo#1310) and registered in RL_REPLACEMENTS, but nothing in unsloth/ references it, and there are no RL_* entries keyed gkd_trainer or distillation_trainer. It is dead code today.

So the generated GKD trainer still materialises a full (batch, seq, vocab) logits tensor for both the student and the teacher. On a large vocabulary that term dominates peak memory: gemma-4's is 262144 wide, which is what makes distilling it expensive, rather than its parameter count.

Work:

  • Add RL_PRE_ITEMS / RL_FUNCTIONS entries keyed gkd_trainer so the generated compute_loss routes through the chunked helper instead of the dense one.
  • Route hidden states through UNSLOTH_RETURN_HIDDEN_STATES and the __UNSLOTH_SUPPORTS_RETURN_HIDDEN_STATES__ capability marker rather than TRL's _get_last_hidden_state, which calls base_model(...).last_hidden_state and does not match how Unsloth's fast forwards return hidden states.
  • Guard the case where a pre-quantized checkpoint has a Linear4bit output head, since the chunked projection reads lm_head.weight as a dense [V, H] tensor. Unsloth's own 4-bit loader skips lm_head, but a third-party checkpoint loaded outside it may not.
  • Verify chunked equals unchunked in both value and gradients, and prove the chunk loop is not retaining each chunk's graph with an allocation trace rather than by reading the code.

The peak-memory claim cannot be demonstrated on a B200, so the before/after needs a small-card leg.

Context: #3008.

Activity

  1. aran-yogesh commented on Sep 22, 2026

    @aran-yogesh

    Would like to take this if it's not already spoken for — is anyone working on it? I don't want to duplicate effort if the wiring is already in progress somewhere.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions