Skip to content

Hybrid packing: drop the >2x faster claim on opt-in devices (ROCm) - #17751

Merged
danielhanchen merged 6 commits into
unslothai:mainfrom
debojyoti-dev:fix/hybrid-packing-opt-in-message-17738
Nov 18, 2026
Merged

danielhanchen merged 6 commits into
unslothai:mainfrom
debojyoti-dev:fix/hybrid-packing-opt-in-message-17738

Conversation

@debojyoti-dev

Copy link
Copy Markdown
Contributor

Fixes #17738

What changes

When a hybrid linear-attention model (gated-delta, Mamba2) packs only because UNSLOTH_EXPERIMENTAL_HYBRID_PACKING=1 is set, on a device outside the default-on set (ROCm, or anything that is not CUDA), the trainer no longer prints

🦥 Unsloth: Packing enabled - training is >2x faster and uses less VRAM!

It prints this instead:

🦥 Unsloth: Packing enabled on ROCm through UNSLOTH_EXPERIMENTAL_HYBRID_PACKING=1. Document boundaries passed the exactness check, but the packing speedup for hybrid linear-attention models is only measured on CUDA, so compare seconds per step against packing = False.
  • patch_hybrid_linear_attention_varlen records 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 returns None on default-on CUDA and for torch-only mixers (LFM2 short convs, PLE), which pack everywhere by design.
  • SFTTrainer picks 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 and packing = 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.hip while the trainer is built (that is what _auto_hybrid_device_ok reads) and puts it back before train().

run main this PR
ROCm, no env var padded path, "default-on only on CUDA" warning same
ROCm, UNSLOTH_EXPERIMENTAL_HYBRID_PACKING=1 packs, prints ">2x faster" packs, prints the opt-in message
CUDA, default packs, prints ">2x faster" same

Before (main), ROCm with the opt-in:

before

After, same run:

after

After, plain CUDA (unchanged):

cuda

Unit tests: two new tests in tests/utils/test_hybrid_packing_default.py cover the recorded device (CPU, ROCm, default-on, unpatched model). That file passes (45), as do test_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.py and test_warnings_issued_guard.py. tests/utils/test_packing.py (3 failures) and tests/test_parallel_env.py (1 failure) fail the same tests on a clean copy of main on this Windows machine.

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.
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 18, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

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

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-18T14:47:24.661496Z 2f06cf0 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: 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".

Comment thread unsloth/trainer.py Outdated
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 "

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 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 👍 / 👎.

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.

Fixed in e9eda7b: the note no longer claims the exactness check passed, since Mamba2-only opt-in models have no probe target.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@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: 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"

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 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 👍 / 👎.

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.

Fixed in 2f06cf0: the CPU half now clears torch.version.hip so the test holds on ROCm hosts too.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Keep it up!

Reviewed commit: 2f06cf05ff

ℹ️ 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 0420e50 into unslothai:main Nov 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.

[Feature] ROCm: hybrid linear-attention packing opt-in passes the exactness probe but gives no net speedup on gfx1151

2 participants