Repository navigation
fix(grpo): autocast with DEVICE_TYPE_TORCH instead of a pinned "cuda" - #11461
Conversation
torch.amp.autocast with a device_type that does not match the accelerator is
inert rather than loud (pytorch#165730): it warns "Disabling autocast" and the
block becomes a no-op. GRPO's autocast blocks pinned device_type = "cuda", so
on Ascend NPU and Intel XPU autocast silently dropped and the forward ran in
float32 against the bf16/fp16 dtype _unsloth_grpo_autocast had latched.
The generated trainer's preamble already imports unsloth's device constants,
so hand it DEVICE_TYPE_TORCH -- "cuda" on CUDA and ROCm, "npu" on Ascend,
"xpu" on Intel, "mps" on MLX -- the way llama.py and vision.py already do.
DEVICE_TYPE is not usable here because it stays "hip" on ROCm, which is not a
valid autocast device_type either.
Verified on an Ascend 910B4 (torch 2.15.0.dev + torch_npu 2.15.0.dev):
torch.is_autocast_enabled("npu") is False and x @ x stays float32 before,
True and bfloat16 after. tests/test_grpo_autocast_{disabled,per_trainer}.py:
50 passed, 1 skipped on that host.
|
Confirmed the autocast sites in unsloth/models/rl_replacements.py still pin device_type to cuda, and DEVICE_TYPE, which is hip on ROCm, at the _get_per_token_logps site, so the latched dtype quietly does nothing off CUDA, and the added DEVICE_TYPE_TORCH import in rl.py's preamble is needed for the injected header to resolve. Will get this reviewed. |
…his branch The autocast blocks in grpo_trainer__get_per_token_logps_and_entropies now read DEVICE_TYPE_TORCH, which the generated trainer gets from rl.py's preamble. Three test files landed on main after this branch was cut and exec the same block in a bare namespace, so merging left them raising NameError: name 'DEVICE_TYPE_TORCH' is not defined at exec time, 17 failures across test_grpo_padded_raw_logits.py, test_grpo_packed_raw_logits_nograd.py and test_grpo_hidden_states_signal.py. Supply the name the way the preamble does, matching what the two autocast suites in this branch already do. test_block_free_variables_are_all_stubbed derives its expectation from the block source, so it follows from the same line.
|
Review record for #11461, carried over from the mirror it was reviewed on: danielhanchen/unsloth-staging-review#56. Findings are quoted as posted and attributed to the account that posted them. The reactions shown are the triage recorded on the mirror; none were re-applied here. Commits are named as the mirror's, with this PR's equivalent where one was found. Round 1 — reviewed mirror
|
Re-pointing the _get_per_token_logps autocast site to DEVICE_TYPE_TORCH left DEVICE_TYPE imported here and referenced nowhere, which the import-hoist lint flags as a blocker: [BLOCKER] HOISTED-IMPORT-UNUSED 'DEVICE_TYPE' was used before, now unused (references re-pointed) Nothing reads DEVICE_TYPE from this module, and the generated trainer takes it from the preamble in rl.py, which is unchanged, so the emitted UnslothGRPOTrainer.py still binds both names.
…unslothai#11461) * fix(grpo): autocast with DEVICE_TYPE_TORCH instead of a pinned "cuda" torch.amp.autocast with a device_type that does not match the accelerator is inert rather than loud (pytorch#165730): it warns "Disabling autocast" and the block becomes a no-op. GRPO's autocast blocks pinned device_type = "cuda", so on Ascend NPU and Intel XPU autocast silently dropped and the forward ran in float32 against the bf16/fp16 dtype _unsloth_grpo_autocast had latched. The generated trainer's preamble already imports unsloth's device constants, so hand it DEVICE_TYPE_TORCH -- "cuda" on CUDA and ROCm, "npu" on Ascend, "xpu" on Intel, "mps" on MLX -- the way llama.py and vision.py already do. DEVICE_TYPE is not usable here because it stays "hip" on ROCm, which is not a valid autocast device_type either. Verified on an Ascend 910B4 (torch 2.15.0.dev + torch_npu 2.15.0.dev): torch.is_autocast_enabled("npu") is False and x @ x stays float32 before, True and bfloat16 after. tests/test_grpo_autocast_{disabled,per_trainer}.py: 50 passed, 1 skipped on that host. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Stub DEVICE_TYPE_TORCH in the three GRPO exec harnesses added since this branch The autocast blocks in grpo_trainer__get_per_token_logps_and_entropies now read DEVICE_TYPE_TORCH, which the generated trainer gets from rl.py's preamble. Three test files landed on main after this branch was cut and exec the same block in a bare namespace, so merging left them raising NameError: name 'DEVICE_TYPE_TORCH' is not defined at exec time, 17 failures across test_grpo_padded_raw_logits.py, test_grpo_packed_raw_logits_nograd.py and test_grpo_hidden_states_signal.py. Supply the name the way the preamble does, matching what the two autocast suites in this branch already do. test_block_free_variables_are_all_stubbed derives its expectation from the block source, so it follows from the same line. * Drop the now-unused DEVICE_TYPE import from rl_replacements Re-pointing the _get_per_token_logps autocast site to DEVICE_TYPE_TORCH left DEVICE_TYPE imported here and referenced nowhere, which the import-hoist lint flags as a blocker: [BLOCKER] HOISTED-IMPORT-UNUSED 'DEVICE_TYPE' was used before, now unused (references re-pointed) Nothing reads DEVICE_TYPE from this module, and the generated trainer takes it from the preamble in rl.py, which is unchanged, so the emitted UnslothGRPOTrainer.py still binds both names. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Daniel Han <[email protected]> Co-authored-by: danielhanchen <[email protected]>
What
GRPO's autocast blocks pin
device_type = "cuda", and the generated trainer's preamble inunsloth/models/rl.pyimportsDEVICE_TYPEbut notDEVICE_TYPE_TORCH— the constant that exists precisely fortorch.amp.autocast("HIP fails for autocast ... use CUDA instead") and thatllama.py/vision.pyalready pass.torch.amp.autocastwith adevice_typethat does not match the accelerator is inert rather than loud (pytorch#165730): it emitsand the block becomes a no-op. On Ascend NPU (and Intel XPU) that means GRPO's forward runs in float32 against the bf16/fp16 dtype
_unsloth_grpo_autocastlatched, so both the memory saving and the numerics the helper was written for silently never apply.Changes
unsloth/models/rl.py— the generated trainer's preamble now also importsDEVICE_TYPE_TORCH(it is already the module the generated cache getsDEVICE_TYPEfrom).unsloth/models/rl_replacements.py— the 6 live autocast sites now passDEVICE_TYPE_TORCH: the injected header ingrpo_trainer__prepare_inputs, the default of_unsloth_grpo_autocast_kwargs, and the four blocks ingrpo_trainer__get_per_token_logps_and_entropies(_pg_run_forward/_pk_hidden/_pk_autocast/_pk_pixels). The unreachable branch ingrpo_trainer__get_per_token_logpsthat already reached forDEVICE_TYPEis kept consistent —DEVICE_TYPEis"hip"on ROCm, which is not an autocast device type, so it would have dropped autocast there too.tests/—test_grpo_autocast_disabled.pyandtest_grpo_autocast_per_trainer.pyexec the injected snippet in a bare namespace, so they now supplyDEVICE_TYPE_TORCHexactly the way the generated trainer's preamble does (the same thing_fast_generatein the latter file already does for the vision snippet). Two regression tests are added in the file's existing style: one asserts notorch.amp.autocast(...)call inrl_replacements.pypinsdevice_type = "cuda", the other asserts the generated trainer's preamble imports the constant.DEVICE_TYPE_TORCHis"cuda"on CUDA and ROCm,"npu"on Ascend,"xpu"on Intel,"mps"on MLX. On a CPU-only rundevice_type.pystill reports"cuda", so nothing changes for the existing CPU CI path.Verification
Ascend 910B4 (
torch 2.15.0.dev+cpu+torch_npu 2.15.0.dev,torch.npu.is_available() == True), same tensor, same kwargs:before — pinned
device_type = "cuda":after —
DEVICE_TYPE_TORCH(resolves to"npu"):The repo's own suites on that host:
python -m pytest --noconftest -q tests/test_grpo_autocast_disabled.py tests/test_grpo_autocast_per_trainer.py→ 50 passed, 1 skipped.The two new tests are red on the pre-fix source (
2 failed, naming all 6 pinned call sites) and green after.Not tested: an end-to-end
GRPOTrainer.train()on NPU — this box is a single-card 910B4 without a GRPO-ready model/tokenizer checkout, so the check above is at thetorch.amplevel plus the repo's source-level suites. The Intel XPU and MLX paths are untested (no such hardware here); they are handled by the same constant thatllama.pyandvision.pyalready use for autocast.