Skip to content

Fix packed document boundaries in Qwen PLE - #14011

Merged
danielhanchen merged 4 commits into
unslothai:mainfrom
taking-lying-flat:fix/qwen-ple-packed-boundaries
Oct 18, 2026
Merged

danielhanchen merged 4 commits into
unslothai:mainfrom
taking-lying-flat:fix/qwen-ple-packed-boundaries

Conversation

@taking-lying-flat

Copy link
Copy Markdown
Contributor

Packed Qwen3.8-Flash-Next text training can pass the gated-delta checks while Qwen4ExpTextPLELayer still mixes documents through its dilated convolution. The mixer detector currently overlooks PLE. EOS resets the n-gram hashes but does not reset the convolution; without EOS, n-gram history also crosses packing boundaries.

Recognize PLE as a stateful mixer and keep packing enabled by isolating both operations:

  • Insert zero history between documents for the native dilated convolution, then gather the real token outputs. The gap is short_conv_state_len (9 for the published configuration).
  • Insert EOS history for the native n-gram lookup and gather the real token embeddings. The gap is context_len (2 for that configuration), so isolation also works without EOS in the actual documents.
  • Reuse the existing boundary metadata, dispatch checks and rollback machinery, including restoration of the embedding hook. Allow the n-gram table to remain on CPU when the hybrid model runs on CUDA.

Each operation remains a single packed call using the native implementation and autograd. Temporary positions stay local to these two PLE operations; attention and the training labels keep their original packed layout. The added work scales with the finite history per document boundary, with no per-document Python loop or additional device-scalar readback. This change is limited to unsloth/utils/packing.py.

Validation:

  • 15 CPU and 24 CUDA component cases compared packed outputs, input/parameter gradients and one SGD update against independent per-document native PLE calls. Coverage includes EOS/no EOS, one-token documents, trailing padding, reset position IDs, zero/nonzero convolution weights, both gradient-checkpointing modes, FP16/BF16, autocast, torch.compile(fullgraph=True), and a CPU-offloaded embedding table. The tested cross-document hidden-state gradients are exactly zero.
  • Hybrid integration controls passed for automatic/explicit packing, dispatch checks, idempotence, and restoration after a rejected GDN probe. These controls use the repository's reference GDN kernels; the PLE CUDA checks execute real PyTorch GPU operations.
  • Existing test_hybrid_packing_default.py and test_packing.py: 142 passed, 1 skipped, 2 model-download tests deselected.
  • File-scoped pre-commit checks and git diff --check passed.

The component checks execute the unchanged upstream PLE, n-gram and RMSNorm method bodies from Transformers 23292ca5b82012ee1a1fd620069fd5e659ecf1be, with small random parameters and real autograd. They do not load the pretrained checkpoint or run full-model SFT; no end-to-end speedup or model-quality improvement is claimed.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 16, 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-18T14:55:40.586725Z 865b997 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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5b3fe0e33a

ℹ️ 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".

Comment thread unsloth/utils/packing.py
Comment on lines +1341 to 1345
torch_varlen_only = bool(mixers) and all(kind in ("short_conv", "ple") for _, kind in mixers)
mode = _hybrid_packing_mode()
auto_eligible = bool(mixers) and all(kind in _HYBRID_AUTO_KINDS for _, kind in mixers)
if not short_conv_only:
if not torch_varlen_only:
if mode == "off" or (mode == "auto" and not auto_eligible):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Honor the packing kill switch for PLE-only models

When a model's detected mixers are all PLE/short-conv, this condition makes torch_varlen_only true and skips the subsequent mode == "off" check. Thus UNSLOTH_EXPERIMENTAL_HYBRID_PACKING=0 still patches a PLE-only model and lets the trainer enable packing; it also makes the first-forward dispatch error's advertised fallback ineffective for that model shape. Check the explicit off mode before applying the torch-only auto-enable exemption.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not reachable: every published model with Qwen4ExpTextPLELayer (Qwen3.8-Flash-Next) also has Qwen4ExpTextGatedDeltaNet mixers, so torch_varlen_only is false and the off switch is honoured; the short-conv exemption from the kill switch is unchanged from main (LFM2).

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep them coming!

Reviewed commit: 865b9971a0

ℹ️ 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".

@danielhanchen
danielhanchen merged commit 9263390 into unslothai:main Oct 18, 2026
danielhanchen added a commit to Imagineer99/unsloth that referenced this pull request Nov 20, 2026
…-check (unslothai#18611)

Follow-up to unslothai#18313 and unslothai#17738.

## Why ROCm was opt-in

unslothai#13383 made hybrid packing (Qwen3.5, Qwen3.5-MoE, Qwen3-Next, and the
PLE layers from unslothai#14011) default-on for CUDA only. At the time
`UNSLOTH_EXPERIMENTAL_HYBRID_PACKING=1` failed closed on gfx1151 because
the boundary-aware causal conv was not allowed on HIP (unsloth-zoo unslothai#2503
fixed that), and once it worked, the full attention layers still ran
SDPA over a dense `(T, T)` mask on ROCm, so packing was a wash (unslothai#17738).
unslothai#18313 moved ROCm packed rows onto torch's flash varlen behind a
one-time self-check, which removes that cost.

## Change

With the variable unset, `_auto_hybrid_device_ok` now also accepts a
ROCm GPU when the unslothai#18313 self-check
(`hf_packed_attention._torch_varlen`) passes on that device. The check
runs once per device (about 0.3 s on gfx1151) and is cached. Everything
else is unchanged:

- The same mixer kinds as CUDA (gated delta and PLE), and the existing
exactness probe still runs before any packing.
- A ROCm build whose flash varlen fails the self-check keeps the padded
path, with the reason logged.
- `=0` still turns packing off and `=1` still opts in Mamba2 / split QKV
mixers and other devices.
- CUDA is unchanged.

The opt-in note from unslothai#17751 now says the path is "not on by default on
this device" instead of naming CUDA only.

## gfx1151, Linux, torch 2.11.0+rocm7.13, transformers 5.5.4

Fake Qwen3.5-4B shaped model (real 4B layer sizes, 4 layers, random
weights), LoRA r=16, `packing = True`, `max_length 2048`, batch 4,
variable unset vs `=0`. Two runs per arm.

| arm | packed | median useful tok/s | median s/step | first losses |
|---|---|---|---|---|
| main, unset | no (padded) | 1691 | 2.96 | 12.9115, 12.9003, 12.9244 |
| main, `=0` | no | 1687 | 2.95 | 12.9115, 12.9003, 12.9244 |
| this PR, unset | yes | 2417 | 3.38 | 12.9344, 12.8879, 12.9011 |
| this PR, `=0` | no | 1699 | 2.97 | 12.9115, 12.9003, 12.9244 |

Default ROCm runs now pack and train at about 1.42x the padded
throughput. The packed losses are the same values unslothai#18313 measured for
the opt-in path, and `=0` is unchanged.

## Tests

`tests/utils/test_hybrid_packing_default.py` adds a case with a ROCm GPU
weight where the self-check passes (default-on) and fails (padded,
reported as an opt-in device). Reverting the change fails the passing
case.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants