Repository navigation
Hybrid packing: drop the >2x faster claim on opt-in devices (ROCm) - #17751
danielhanchen merged 6 commits into
Conversation
When a hybrid linear-attention model packs only because of UNSLOTH_EXPERIMENTAL_HYBRID_PACKING=1 (ROCm, or any device outside the default-on set), the trainer now says packing came from the opt-in, that the exactness check passed, and that the speedup is only measured on CUDA.
|
@codex review |
1 similar comment
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 0cf798b384
ℹ️ 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".
| print( | ||
| f"🦥 Unsloth: Packing enabled on {hybrid_opt_in_device} through " | ||
| f"{_HYBRID_PACKING_ENV_VAR}=1 (on by default only on CUDA). Document " | ||
| "boundaries passed the exactness check; the speedup depends on this device's " |
There was a problem hiding this comment.
Condition the exactness claim on a completed probe
For a Mamba2-based hybrid on ROCm with UNSLOTH_EXPERIMENTAL_HYBRID_PACKING=1, this message claims an exactness check that never ran. In unsloth/utils/packing.py, _probe_target() only selects gated_delta mixers, while patch_hybrid_linear_attention_varlen() allows opt-in ssd models to succeed with probe_target=None and skips _hybrid_exactness_probe(). The new device marker is still set to ROCm, so these users receive a false document-boundary validation guarantee. Omit or qualify this clause for unprobed mixers, or record a passed probe explicitly before printing it.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in e9eda7b: the note no longer claims the exactness check passed, since Mamba2-only opt-in models have no probe target.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: e9eda7bd2a
ℹ️ 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".
| monkeypatch.setenv(ENV, "1") | ||
| cpu_model = FakeHybrid(FakeGatedDeltaNet()) | ||
| assert patch_hybrid_linear_attention_varlen(cpu_model) is True | ||
| assert packing_module.hybrid_packing_opt_in_device(cpu_model) == "cpu" |
There was a problem hiding this comment.
Clear the HIP version for the CPU marker test
When this test runs under ROCm PyTorch, torch.version.hip is already non-None, and monkeypatch.undo() leaves that real value intact. _hybrid_opt_in_device() therefore returns "ROCm" even for this CPU stand-in, so this assertion fails before the simulated ROCm case runs. Set torch.version.hip to None before the CPU portion, then set it to "6.4" for the ROCm portion so the new test also passes on ROCm builds.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 2f06cf0: the CPU half now clears torch.version.hip so the test holds on ROCm hosts too.
|
@codex review |
|
Codex Review: Didn't find any major issues. Keep it up! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…-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.
Fixes #17738
What changes
When a hybrid linear-attention model (gated-delta, Mamba2) packs only because
UNSLOTH_EXPERIMENTAL_HYBRID_PACKING=1is set, on a device outside the default-on set (ROCm, or anything that is not CUDA), the trainer no longer printsIt prints this instead:
patch_hybrid_linear_attention_varlenrecords the device on the model when the varlen path is active outside the default-on set.hybrid_packing_opt_in_device(model)reads it and returnsNoneon default-on CUDA and for torch-only mixers (LFM2 short convs, PLE), which pack everywhere by design.SFTTrainerpicks the message from that value. The CUDA-only gate, the kernels and the exactness probe are untouched, and the default CUDA message is unchanged.Testing
Real SFT runs of
unsloth/Qwen3.5-0.8B(hybrid, gated-delta layers) with bf16 LoRA r=16 / alpha=32 andpacking = True, on Windows 11 with an RTX 3050 Laptop 4 GB, torch 2.11.0+cu126, transformers 5.5.0, trl 0.23.1, unsloth_zoo 2026.11.4, fla 0.6.0. The exactness probe ran and passed in every packed run (rel err 1.8e-3) and the training steps ran on the real varlen kernels.I have no AMD GPU, so the ROCm case sets
torch.version.hipwhile the trainer is built (that is what_auto_hybrid_device_okreads) and puts it back beforetrain().UNSLOTH_EXPERIMENTAL_HYBRID_PACKING=1Before (main), ROCm with the opt-in:
After, same run:
After, plain CUDA (unchanged):
Unit tests: two new tests in
tests/utils/test_hybrid_packing_default.pycover the recorded device (CPU, ROCm, default-on, unpatched model). That file passes (45), as dotest_processor_text_packing_gate.py,test_padding_free_signature_gate.py,test_padding_free_eval.py,test_padding_free_boundary_leak.py,test_ulysses_deepseek.pyandtest_warnings_issued_guard.py.tests/utils/test_packing.py(3 failures) andtests/test_parallel_env.py(1 failure) fail the same tests on a clean copy of main on this Windows machine.