diff --git a/unsloth/kernels/utils.py b/unsloth/kernels/utils.py index 0b13c04532..b1211ae345 100644 --- a/unsloth/kernels/utils.py +++ b/unsloth/kernels/utils.py @@ -86,6 +86,15 @@ def is_cdna(): ) +@functools.lru_cache(1) +def is_rdna(): + """Detect RDNA consumer/workstation GPUs (RDNA3, RDNA3.5, RDNA4).""" + if not is_hip(): + return False + arch = triton.runtime.driver.active.get_current_target().arch + return arch.startswith("gfx1") and not is_cdna() + + def calculate_settings( n: int, ) -> ( diff --git a/unsloth/models/loader.py b/unsloth/models/loader.py index 711476b759..7bb2a24e00 100644 --- a/unsloth/models/loader.py +++ b/unsloth/models/loader.py @@ -1131,6 +1131,14 @@ class FastModel(FastBaseModel): # Set norms to float32 since anyways they get upcasted to float32 # common in both gemma-3 and gemma-3n os.environ["UNSLOTH_HIGH_PRECISION_LAYERNORM"] = "1" + # ROCm/HIP: Gemma3 compiled forward produces NaN on RDNA GPUs + # (gfx1100, gfx1101, gfx1102, gfx1150, gfx1151, etc.). + # Disable torch.compile for model forward; loss compilation is fine. + # See https://github.com/unslothai/unsloth/issues/3385 + from unsloth.kernels.utils import is_rdna + + if is_rdna(): + os.environ["UNSLOTH_COMPILE_DISABLE"] = "partial" # Cohere elif "cohere2" in model_types_all and transformers_version < Version( "4.50.0.dev0"