Fix Gemma3 NaN losses on ROCm by disabling torch.compile for RDNA GPUs#4029
Fix Gemma3 NaN losses on ROCm by disabling torch.compile for RDNA GPUs#4029GoldenGrapeGentleman wants to merge 2 commits into
Conversation
Summary of ChangesHello @GoldenGrapeGentleman, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request resolves a critical numerical instability issue affecting Gemma3 model training on AMD RDNA GPUs with ROCm, where torch.compile led to NaN losses. By selectively disabling compilation for Gemma3 on HIP devices, the PR ensures stable and correct training, leveraging the numerically sound eager execution path without impacting other models or architectures. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a targeted fix to disable torch.compile for Gemma3 models on ROCm platforms, addressing an issue with NaN losses during training on RDNA GPUs. The change correctly identifies the specific hardware (hip) and model type (gemma3) and applies a workaround by setting UNSLOTH_COMPILE_DISABLE and patching the unsloth_zoo.compiler module at runtime. The implementation is sound and directly addresses the root cause described. I have one minor suggestion to improve code style by moving the local import to the top of the file.
| # See https://github.com/unslothai/unsloth/issues/3385 | ||
| if DEVICE_TYPE == "hip": | ||
| os.environ["UNSLOTH_COMPILE_DISABLE"] = "1" | ||
| import unsloth_zoo.compiler |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 18a6c6ad78
ℹ️ About Codex in GitHub
Codex has been enabled to automatically 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 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| os.environ["UNSLOTH_COMPILE_DISABLE"] = "1" | ||
| import unsloth_zoo.compiler | ||
|
|
||
| unsloth_zoo.compiler.UNSLOTH_COMPILE_DISABLE = True |
There was a problem hiding this comment.
Avoid persisting compile-disable globally after Gemma3 load
This assignment mutates a process-wide compiler flag and there is no corresponding reset path in FastLanguageModel.from_pretrained, so once a HIP Gemma3 load runs, later loads of other model families in the same Python process can inherit compile-disabled behavior unintentionally. That creates a silent cross-model regression (not just Gemma3) where subsequent calls into unsloth_compile_transformers may stay on eager paths and lose expected optimizations.
Useful? React with 👍 / 👎.
On RDNA GPUs (gfx1100, gfx1151, etc.), Gemma3's compiled forward path produces NaN losses from the first training step. The eager (uncompiled) path is numerically correct. Root cause: the Triton/ROCm compiler backend generates numerically unstable code for Gemma3's architecture. Other model architectures (Llama, Qwen) compile and train correctly on the same hardware. Fix: set UNSLOTH_COMPILE_DISABLE=1 for Gemma3 on HIP, following the same pattern used for Sesame/CSM models. The module-level constant in unsloth_zoo.compiler is also updated since it is evaluated at import time before loader.py's model-specific block runs. Tested on AMD Radeon PRO W7900 (gfx1100, ROCm 7.1, PyTorch 2.8.0): - Gemma-3-1B SFT 4bit bf16: NaN → loss=3.81 (fixed) - Llama-3.2-1B SFT (regression): loss=4.40 (unaffected) Closes unslothai#3385
for more information, see https://pre-commit.ci
danielhanchen
left a comment
There was a problem hiding this comment.
Tested on NVIDIA B200 (compute capability 10.0, CUDA 12.8, torch 2.9.1) -- no regressions observed.
Code review notes:
- The
DEVICE_TYPE == "hip"guard is consistent with the existing pattern used across the codebase (6+ existing usages). - Both
os.environ["UNSLOTH_COMPILE_DISABLE"] = "1"and the module-levelunsloth_zoo.compiler.UNSLOTH_COMPILE_DISABLE = Trueare set, which covers the critical compile decision path incompiler.py. - Minor gap:
common.pybindsUNSLOTH_COMPILE_DISABLEat import time before loader runs, so its local copy is not patched. However, the env var read incompiler.py:2848is done at call time and IS affected, so the fix works in practice. - No-op on NVIDIA -- the entire block is gated behind
DEVICE_TYPE == "hip".
LGTM.
Thanks @danielhanchen ~ |
…dna() - Add is_rdna() detection for RDNA3/3.5/RDNA4 consumer GPUs (gfx11xx, gfx1151, gfx12xx) - Disable torch.compile for Gemma3 on HIP to fix NaN loss (fixes unslothai#3385, unslothai#4029) - Export is_cdna/is_rdna from kernels for downstream use - Import is_rdna into cross_entropy_loss for future RDNA-specific tuning Tested on AMD Radeon PRO W7900 (gfx1100) with ROCm 7.1: ✓ Gemma3-1B: loss 3.37→3.25 (no NaN) ✓ Llama-3.2-1B: loss 2.44→2.37 (no NaN) ✓ Qwen2.5-1.5B: loss 1.89→1.85 (no NaN) ✓ RMS LayerNorm Triton kernel: bf16/fp16 PASSED ✓ Cross Entropy Loss Triton kernel: 32K/256K vocab PASSED
|
Closing in favor of #4109, which supersedes this PR with improvements based on Daniel's review:
All fixes from this PR are fully contained in #4109. |
…dna() - Add is_rdna() detection for RDNA3/3.5/RDNA4 consumer GPUs (gfx11xx, gfx1151, gfx12xx) - Disable torch.compile for Gemma3 on HIP to fix NaN loss (fixes unslothai#3385, unslothai#4029) - Export is_cdna/is_rdna from kernels for downstream use - Import is_rdna into cross_entropy_loss for future RDNA-specific tuning Tested on AMD Radeon PRO W7900 (gfx1100) with ROCm 7.1: ✓ Gemma3-1B: loss 3.37→3.25 (no NaN) ✓ Llama-3.2-1B: loss 2.44→2.37 (no NaN) ✓ Qwen2.5-1.5B: loss 1.89→1.85 (no NaN) ✓ RMS LayerNorm Triton kernel: bf16/fp16 PASSED ✓ Cross Entropy Loss Triton kernel: 32K/256K vocab PASSED
…dna() (unslothai#4109) * fix(ROCm): comprehensive RDNA GPU support - fix Gemma3 NaN & add is_rdna() - Add is_rdna() detection for RDNA3/3.5/RDNA4 consumer GPUs (gfx11xx, gfx1151, gfx12xx) - Disable torch.compile for Gemma3 on HIP to fix NaN loss (fixes unslothai#3385, unslothai#4029) - Export is_cdna/is_rdna from kernels for downstream use - Import is_rdna into cross_entropy_loss for future RDNA-specific tuning Tested on AMD Radeon PRO W7900 (gfx1100) with ROCm 7.1: ✓ Gemma3-1B: loss 3.37→3.25 (no NaN) ✓ Llama-3.2-1B: loss 2.44→2.37 (no NaN) ✓ Qwen2.5-1.5B: loss 1.89→1.85 (no NaN) ✓ RMS LayerNorm Triton kernel: bf16/fp16 PASSED ✓ Cross Entropy Loss Triton kernel: 32K/256K vocab PASSED * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Address review: scope compile disable to RDNA only, use partial mode, remove unused import Changes based on Daniel's review: 1. (HIGH) Replace DEVICE_TYPE=='hip' with is_rdna() to avoid disabling torch.compile on CDNA GPUs (MI250X/MI300X/MI350) where it works fine 2. (MEDIUM) Use 'partial' instead of '1' for UNSLOTH_COMPILE_DISABLE to only disable model forward compilation while keeping loss compilation, matching the existing Sesame pattern 3. (LOW) Remove unused is_rdna import from cross_entropy_loss.py (F401) * Remove redundant is_cdna/is_rdna exports from kernels/__init__.py These functions are imported directly from .utils where needed (e.g. cross_entropy_loss.py, loader.py). No external code imports them from the unsloth.kernels namespace. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
…dna() (unslothai#4109) * fix(ROCm): comprehensive RDNA GPU support - fix Gemma3 NaN & add is_rdna() - Add is_rdna() detection for RDNA3/3.5/RDNA4 consumer GPUs (gfx11xx, gfx1151, gfx12xx) - Disable torch.compile for Gemma3 on HIP to fix NaN loss (fixes unslothai#3385, unslothai#4029) - Export is_cdna/is_rdna from kernels for downstream use - Import is_rdna into cross_entropy_loss for future RDNA-specific tuning Tested on AMD Radeon PRO W7900 (gfx1100) with ROCm 7.1: ✓ Gemma3-1B: loss 3.37→3.25 (no NaN) ✓ Llama-3.2-1B: loss 2.44→2.37 (no NaN) ✓ Qwen2.5-1.5B: loss 1.89→1.85 (no NaN) ✓ RMS LayerNorm Triton kernel: bf16/fp16 PASSED ✓ Cross Entropy Loss Triton kernel: 32K/256K vocab PASSED * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Address review: scope compile disable to RDNA only, use partial mode, remove unused import Changes based on Daniel's review: 1. (HIGH) Replace DEVICE_TYPE=='hip' with is_rdna() to avoid disabling torch.compile on CDNA GPUs (MI250X/MI300X/MI350) where it works fine 2. (MEDIUM) Use 'partial' instead of '1' for UNSLOTH_COMPILE_DISABLE to only disable model forward compilation while keeping loss compilation, matching the existing Sesame pattern 3. (LOW) Remove unused is_rdna import from cross_entropy_loss.py (F401) * Remove redundant is_cdna/is_rdna exports from kernels/__init__.py These functions are imported directly from .utils where needed (e.g. cross_entropy_loss.py, loader.py). No external code imports them from the unsloth.kernels namespace. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Summary
Gemma3 training produces NaN losses from the first step on all RDNA GPUs (gfx1100, gfx1151). The compiled forward path is numerically unstable on the ROCm/Triton backend, while the eager path trains correctly.
This adds a targeted compile disable for Gemma3 on HIP, following the same
UNSLOTH_COMPILE_DISABLEpattern already used for Sesame/CSM models.Reproduction
Root Cause
The Triton/ROCm compiler backend generates numerically unstable code for Gemma3. Other architectures (Llama, Qwen, Mistral) compile and train correctly on the same GPU. The eager path for Gemma3 is numerically correct — disabling compile is sufficient.
The module-level constant
unsloth_zoo.compiler.UNSLOTH_COMPILE_DISABLEmust also be overridden at runtime because it is evaluated at import time, before the model-specific configuration inloader.pyruns.Testing
Tested on AMD Radeon PRO W7900 (gfx1100, ROCm 7.1, PyTorch 2.8.0):
Also verified on a fresh container with no pre-existing compile cache.
Closes #3385
Related: PR #3588 (broader RDNA3 fix attempt, open since Nov 2025)
cc @danielhanchen @0xrushi @kyuz0