Repository navigation
Fused preference loss: fall back when a global forward hook is registered - #15226
danielhanchen merged 4 commits into
Conversation
…ard (#15291) `Repo tests (CPU, rest)` has been running out of time. Its pytest step is capped at `timeout --signal=INT --kill-after=60 2100`. - On most runs today pytest received SIGINT before finishing. Every run I checked had a `KeyboardInterrupt` banner and a reported time of 2090 to 2097 s, stopping between 74% and 98% of the shard. - The tail of the shard therefore never ran, and nothing said so. Those runs were also red for other reasons, which hid it. - Now that those reds are fixed on main, the shard fails on the timeout alone (`Process completed with exit code 124`, at 75%), for example on #15217, #15218 and #15226. - The one complete run I found took 1920 s, already close to the cap. ## Cause The shard is the catch-all. It has picked up the multi-rank gloo suites added recently: tensor parallel, pipeline parallel, context parallel, float8 and fp8 under FSDP2. Each spawns 2 to 4 ranks per test. This is the same thing that happened with the FSDP2 gloo files, which is why they already have their own `fsdp2` shard. Measured locally at this job's shape (`-n 4 --dist loadgroup`, pinned to 4 cores, one selection per core set): | selection | wall | | --- | --- | | the 7 non-FSDP2 gloo files | 578 s | | the catch-all without them | 680 s | | the existing `fsdp2` shard | 648 s | So the gloo files are about 46% of the catch-all. Folding them into `fsdp2` would push that shard to the cap instead; its jobs already run 17.5 to 29 minutes on CI. ## Fix A `gloo` shard takes every `*gloo*` file except `test_fs*`, which are `fsdp2`'s. It is written as two globs, `tests/test_[!f]*gloo*.py` and `tests/test_f[!s]*gloo*.py`, because an explicit file root cannot be filtered with `--ignore`, as the matrix notes already say. `rest` ignores the same two globs. A new multi-rank gloo file lands in the new shard without a workflow edit. This adds a check, `Repo tests (CPU, gloo)`. No existing check name changes. ## Verification - `tests/studio/test_backend_ci_parallel_isolation.py` pins the number of parallel pytest runs; it now expects eight, five of them repo-cpu-tests shards (it failed at seven before that update). It and `tests/test_ci_repo_cpu_shards.py` together: 54 passed. - `tests/test_ci_repo_cpu_shards.py` on its own: 13 passed. It fails both ways if the split is broken: drop the new shard and its files are reported as dropped; drop the `rest` ignores and they are reported as run twice. - Collected ids at main (`--collect-only` with the same shell glob expansion): the old `rest` has 17751. The new `rest` (17666) plus `gloo` (85) is identical, with nothing in both and nothing shared with `fsdp2`. - `test_gloo_spawn_cpu_guard.py` matches neither glob and stays in `rest`. It is a guard, not a multi-rank suite. ## Not proven here Runner timing is estimated by scaling: the catch-all is about 1258 s locally against roughly 2800 s projected on CI. That puts the new `rest` near 1500 s and `gloo` near 1270 s, both under 2100 s. This PR's own run will show the real numbers.
|
@codex review |
1 similar comment
|
@codex review |
Codex Review SummaryThis 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. |
…ered unslothai#14788 made _output_head refuse the fused kernel when the head has module backward hooks or a global full-backward (pre-)hook is registered, since the kernel never calls the head module. The global forward tables were left out: register_module_forward_hook / register_module_forward_pre_hook fire on every nn.Linear in TRL's stock forward, so with one registered the fused path silently skipped a hook that rewrites the head's input or output. Check those two tables in the same gate. Co-Authored-By: Claude Fable 5.1 <[email protected]>
78655f0 to
df211b3
Compare
…odule hooks The chunked-head guards (unslothai#14185) skip the fused path when the output head carries a hook of its own (_hf_hook, forward / forward-pre / backward tables, a patched forward): the kernel reads head.weight directly and never calls the module. A hook registered through torch.nn.modules.module.register_module_forward_hook / _forward_pre_hook / _full_backward_(pre_)hook applies to every module through the global tables instead, which neither guard reads, so under such a hook (profilers, FLOP counters, user tooling) every module fires it except the head. Add _unsloth_global_module_hooks and consult it from both guards, listed in the ORPO / CPO, DPO and KTO source-copy lists so the generated trainers stay self-contained. unsloth/models/dpo.py already guards the global backward tables for the SFT-style fused head (unslothai#15226 extends it to the forward ones). Tests: a global forward hook makes _unsloth_pref_chunked_head return None and the DPO run record the "output head has hooks" fallback with TRL's loss. Co-Authored-By: Claude Fable 5.1 <[email protected]>
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
Reviewed source Validation: 80 CPU tests passed on the source commit. Mirror CI: 36 passed, 5 skipped against base |
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. 🚀 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". |
…odule hooks (#15259) ## Problem The chunked-head guards from #14185 (`_unsloth_pref_chunked_head` for ORPO / CPO, `_unsloth_dpo_chunked_head` for DPO and, through `_unsloth_fused_pass_head`, KTO) keep TRL's logits path when the output head has a hook of its own: `_hf_hook`, the head's forward / forward-pre / backward tables, or a patched `forward`. The fused kernel reads `head.weight` / `head.bias` directly and never calls the module, so a hook on it would be skipped. Hooks registered through `torch.nn.modules.module.register_module_forward_hook`, `register_module_forward_pre_hook`, `register_module_full_backward_hook` and `register_module_full_backward_pre_hook` live in torch's global tables, not on the head, and neither guard reads them. Under such a hook (memory profilers, FLOP counters, user tooling) every module in the model fires it except the head, silently. `unsloth/models/dpo.py` already guards the global backward tables for the SFT-style fused head (`_global_module_backward_hooks`); #15226 extends that to the forward tables. These two guards are the sibling entry points. ## Fix One helper, `_unsloth_global_module_hooks`, consulted by both guards alongside the head's own tables, and listed in the ORPO / CPO, DPO and KTO source-copy lists so the generated trainer sources stay self-contained (`test_generated_items_are_self_contained` now checks for it). ## Verification - `tests/python/test_preference_chunked_logprobs.py::test_declines_without_kernel_or_on_unsupported_setups`: with a global forward hook registered the ORPO guard returns `None`; after `handle.remove()` it returns the head again. - `tests/test_dpo_chunked_logprobs.py::test_global_module_hook_falls_back`: a DPO run under a global forward hook makes zero chunked calls, records the `output head has hooks or a patched forward` fallback and matches TRL's loss. - Locally (CPU, unsloth_zoo without `chunked_logprob`) the fused route cannot fire, so those tests skip; verified instead that the helper flips on each of the four global tables and is clear after removal, that both guards call it, and ruff / duplicate-definition lint are clean. ## Follow-up - Merged current main. - GKD had the same gap. Its chunked JSD reads both heads' weights directly, so neither the heads' own forward / backward hooks nor torch's global module hooks fired. It now falls back to TRL's dense logits in both cases. Accelerate's `_hf_hook` still uses the fused path, because the loss moves to the head's device. - Tests cover all four global hook kinds (`forward`, `forward_pre`, `full_backward`, `full_backward_pre`) for ORPO / CPO, DPO and GKD. Each test first checks that the fused path runs with no hook (call counter above zero), then that removing the hook restores it. Under a hook, ORPO's loss and grads equal TRL's exactly. - The generated GKD trainer source carries the helper. | | main | this branch | |---|---|---| | ORPO / CPO / DPO / GKD under a global module hook | fused path runs, hook never sees the head | falls back to TRL logits | | hook tests (latest venv) | 19 failed | 20 passed | | hook tests (floor, transformers 4.57.6) | 23 failed | 24 passed | | preference, DPO and GKD test files, with TRL tags | | 131 passed / 17 skipped (latest), 143 passed / 5 skipped (floor) | GRPO also reads `lm_head.weight` directly, inside unsloth_zoo's fused loss. It has no plain path to fall back to, so it is left for a separate change. --------- Co-authored-by: Claude Fable 5.1 <[email protected]> Co-authored-by: Daniel Han <[email protected]>
What
_output_headinunsloth/models/dpo.pydecides whether DPO / ORPO / CPO underUNSLOTH_FUSED_PREFERENCE_LOSS=1may read the head's weight directly instead of calling the head module. #14788 added the module backward hooks and the global full-backward (pre-)hook tables to that gate, because the kernel never calls the head and so none of its hooks fire. The global forward tables were left out:torch.nn.modules.module.register_module_forward_hook/register_module_forward_pre_hookfire on everynn.Linearin TRL's stock forward, the head included, so with one registered (activation capture, profiling, a hook that rewrites the head's input or output) the fused path was still taken and the hook silently skipped. Same defect class #14788 closed, one table over.How
_global_module_backward_hooksbecomes_global_module_hooksand also checks_global_forward_hooksand_global_forward_pre_hooks; the gate's call site follows. The_with_kwargs/_always_calledtables are keyed by the same hook ids, so the two main tables cover them. Nothing else changes: per-module hooks,_hf_hook, overriddenforwardand subclassed heads were already refused.Verification
tests/test_fused_preference_loss.py::test_hooked_or_custom_head_falls_backgainsglobal_forward_hookandglobal_pre_hookcases (registered through the torch module-level API, removed by a finalizer). Each asserts_output_headreturns(None, None), the hidden-states path is not taken, and the loss equals TRL's stock path with the hook honoured.(None, None)assert)Whole file on CPU with the zoo kernels: 27 passed, 53 skipped (the skips are the GPU rows).
ruff check,enforce_kwargs_spacing.pyand theruff-format-with-kwargshook pass;dpo.pyparses under Python 3.9.🤖 Generated with Claude Code