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 intounslothai:mainfrom
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
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