Repository navigation
GPUs without bfloat16 (RX 5000 / 6000, T4, V100): load outlier-prone models (Gemma, gpt-oss, Qwen3.5) in float32 - #11533
danielhanchen wants to merge 1 commit into
Conversation
The FORCE_FLOAT32 families (gemma3, gemma3n, gemma4, glm4_moe, gpt_oss, qwen3_5, qwen3_moe) have activations outside float16's finite range, so they are loaded in bfloat16 instead: bfloat16 is the deliberate choice because it reduces the outliers that make float16 unusable for them. The selection branch is entered when the caller asked for float16 OR the device has no bfloat16, and it then chose bfloat16 unconditionally. On the second of those two conditions that picks the one dtype the device cannot run: RDNA 1/2 (gfx101x, gfx103x) cannot lower bfloat16 in Triton, so the tensor reaches a kernel and LLVM aborts the whole process with no Python exception, "Cannot select: intrinsic %llvm.amdgcn.fdot2.bf16.bf16" (#7922). Pre-Ampere NVIDIA (T4, V100) raises "expected mat1 and mat2 to have the same dtype, but got: BFloat16 != Half" (#7506). vision.py made it unrecoverable: its FORCE_FLOAT32 branch is an `elif`, so promoting float16 to bfloat16 there skipped the `dtype == bfloat16 and not SUPPORTS_BFLOAT16` downgrade directly beneath it and nothing was left to correct the choice. float32 is the remaining dtype that still honours "must not run in float16", so that is the fallback. bfloat16 is unchanged wherever it is usable. The rule is one helper, force_float32_dtype(), called from both sites, because the two copies had already drifted. No new detection: SUPPORTS_BFLOAT16 already answers False for gfx10 via device_type.arch_lacks_bf16(), which _gpu_init.py uses to override torch.cuda.is_bf16_supported(). Measured on a bf16-capable device the decision is byte-identical: over (SUPPORTS_BFLOAT16 x incoming dtype), 8 of 8 cells unchanged, because force_float32_dtype(True) is torch.bfloat16 and the added disjunct is always False there. Only devices without bfloat16 change, 6 of 8 cells. The added call costs 0.083 us once per from_pretrained(), and is in no training step, decode loop or per-layer path. tests/test_float32_generate_autocast.py, test_float32_no_fp16_autocast.py, test_bf16_quant_disabled_message.py and test_attn_dtype_custom_datatype.py: 46 passed before and after, unchanged.
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. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 05236b959b
ℹ️ About Codex in GitHub
Your team has set up Codex to 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 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if dtype == torch.float16 or (dtype == torch.bfloat16 and not SUPPORTS_BFLOAT16): | ||
| dtype = force_float32_dtype(SUPPORTS_BFLOAT16) |
There was a problem hiding this comment.
Preserve the float32 fallback through the actual model load
On a device without bfloat16, this correctly changes dtype to float32, but every normal FORCE_FLOAT32 load still sets do_forced_float32 = True, after which lines 1415–1417 unconditionally replace the actual torch_dtype with torch.bfloat16. That value is then used by the device-map planner, attention resolver, and from_pretrained, so RDNA1/2 and pre-Ampere NVIDIA devices still receive the unsupported bfloat16 tensors this change is intended to eliminate. The later override must retain float32 when SUPPORTS_BFLOAT16 is false.
Useful? React with 👍 / 👎.
|
ran this on the RX 6500 XT (gfx1034), windows 11, torch 2.11.0+rocm7.13.0, triton-windows 3.8.0. upstream main wants newer deps than the studio venv has, so i applied the three source hunks onto the installed 2026.9.7 and put that copy first on PYTHONPATH, everything else untouched. the decision table file passes here with the card really answering no bf16: 17 passed, every run below is what the dtype ends up as on this card the weights come out float16 both ways ( with stock unsloth_zoo 2026.9.6
the failures are all in the checkpoint recompute ( gemma gets better from this alone: its activations are fp32, so fp32 buffers mean stock zoo stops squeezing them through bf16. with unslothai/unsloth-zoo#1316 in Qwen3.5-0.8B 16-bit LoRA trains 3/3, losses identical to 10 decimals to #1316 without this PR (0.0366736986 0.0366736986 0.0281048398), peak 2.26 GB. so the two together are the working combination on this card. same under WSL2 ubuntu 24.04 (same card, torch 2.11.0+rocm7.13.0 linux wheels, triton 3.6.0,
one thing for later if fp32 weights ever become the plan for these cards: fla's fp32 kernels dont fit rdna2's LDS with the current configs, and 0.8B in fp32 is ~3.4 GB of weights on a 4 GB card. |
|
@danielhanchen two findings from testing this next to unslothai/unsloth-zoo#1316. Your call whether to fix this PR or close it. Short version
1. The float32 choice doesn't reach the model loadBackground for anyone reading along: some model families (Qwen3.5, Gemma3, gpt-oss, ...) are marked The pick happens at if dtype == torch.float16 or (dtype == torch.bfloat16 and not SUPPORTS_BFLOAT16):
dtype = force_float32_dtype(SUPPORTS_BFLOAT16) # -> torch.float32 on these cardsBut a few lines later, torch_dtype = dtype
if do_forced_float32:
torch_dtype = torch.bfloat16
This matches what I measured on the RX 6500 XT (gfx1034) in my comment above: Qwen3.5-0.8B weights come out 2. unslothai/unsloth-zoo#1316 already fixes the crash on RDNA2The crash ( Qwen3.5-0.8B on the RX 6500 XT,
So with #1316 in, this PR changes nothing I could measure. Merged on its own before #1316, it turns the LLVM abort into a Python error, but Qwen3.5 still doesn't train. gemma-3-270m does get better from this PR alone (its float32 activations stop going through bfloat16 buffers), but #1316 fixes that too, since it hands every activation back in its own dtype. If you want to keep it
If notClosing this in favour of unslothai/unsloth-zoo#1316 seems reasonable to me. Not tested: pre-Ampere NVIDIA (T4, V100). A T4 run of #1316 is still pending, and that is the data point that would show whether this PR adds anything on those cards. |
|
follow-up to my last comment, and a correction to it: I suggested keeping float32 at what works is staging in float16 when the device has no bf16: if do_forced_float32:
- torch_dtype = torch.bfloat16
+ if SUPPORTS_BFLOAT16 or dtype == torch.bfloat16:
+ torch_dtype = torch.bfloat16
+ else:
+ torch_dtype = torch.float16these runs are at the real SHAs (worktrees, same card): the small Qwen shift is the 18 it's still true from last time: with zoo 1316 in, this PR alone changes nothing I can measure. not run: RX 5700 XT, T4/V100, Linux ROCm, QLoRA, gpt-oss, no bf16 control card. |
|
reran this at the real SHAs this time (worktrees at base trains now because zoo 1316 and 1419 are in, so the LLVM abort this PR was written for doesn't happen at the one thing to look at is the Codex P1. it's real: if do_forced_float32:
- torch_dtype = torch.bfloat16
+ if SUPPORTS_BFLOAT16 or dtype == torch.bfloat16:
+ torch_dtype = torch.bfloat16
+ else:
+ torch_dtype = torch.float16on the same card that gives also, the 17 tests in the PR are decision tables and source-string checks, so none of them would have caught the P1. not run: RX 5700 XT (gfx1010, no torch on this box that can run a kernel on it), T4/V100, Linux ROCm, QLoRA, gpt-oss, and no bf16 card as a control. |
The bug
The FORCE_FLOAT32 families (gemma3, gemma3n, gemma4, glm4_moe, gpt_oss, qwen3_5, qwen3_moe) have activations outside float16's finite range, so they are loaded in bfloat16 instead. bfloat16 is the deliberate choice there: it reduces the outliers that make float16 unusable for them.
The selection branch is entered when the caller asked for float16 or the device has no bfloat16, and it then chose bfloat16 unconditionally. On the second of those two conditions that picks the one dtype the device cannot run.
Two ways it surfaces:
LLVM ERROR: Cannot select: intrinsic %llvm.amdgcn.fdot2.bf16.bf16([Bug] fine-tuning with AMD gfx1032 crashes #7922).expected mat1 and mat2 to have the same dtype, but got: BFloat16 != Half([Bug] Crash on T4 Kraggle enviroment qwen 3.5 0.8b bf16 training #7506).vision.pymade it unrecoverable. Its FORCE_FLOAT32 branch is anelif, so promoting float16 to bfloat16 there skips thedtype == bfloat16 and not SUPPORTS_BFLOAT16downgrade directly beneath it, and nothing is left to correct the choice.The fix
float32 is the remaining dtype that still honours "must not run in float16", so that is the fallback. bfloat16 is unchanged wherever it is usable.
The rule is one helper,
force_float32_dtype(), called from both sites, because the two copies had already drifted apart. No new detection is added:SUPPORTS_BFLOAT16already answers False for gfx10 viadevice_type.arch_lacks_bf16(), which_gpu_init.pyuses to overridetorch.cuda.is_bf16_supported().Non-RDNA2 devices are provably unchanged
Not "no regression observed": the decision is the same object.
force_float32_dtype(True)istorch.bfloat16, and the added disjunct is always False on a bf16 device. Over (SUPPORTS_BFLOAT16x incoming dtype):8 of 8 cells identical for NVIDIA Ampere and newer, RDNA3/3.5/4, CDNA and XPU. Only the no-bfloat16 half moves, 6 of 8 cells.
The arch gate does not over-reach either: gfx1100, gfx1151, gfx1201, gfx90a and gfx942 all keep bfloat16, and an unreadable arch fails open, so a detection failure never pushes a working GPU onto float32.
Performance
The added call costs 0.083 us, once per
from_pretrained(). It is in no training step, no decode loop and no per-layer path, so throughput and memory cannot move on a bfloat16 device.Tests
tests/test_force_float32_without_bfloat16.py, 17 cases: bfloat16 is still chosen wherever it works, float32 replaces it only where it does not, float16 is never the answer, the gfx10 prefix stays 5 characters so RDNA3/3.5/4 are untouched, unreadable archs fail open, and both call sites go through the shared helper.Regression check, same file set on both sides:
test_float32_generate_autocast,test_float32_no_fp16_autocast,test_bf16_quant_disabled_message,test_attn_dtype_custom_datatype: 46 passed before and after.What this does not cover
No RDNA2 or pre-Ampere hardware was available, so the fix is evidenced by the decision table and the test suite, not by a run on a gfx1034 or a T4.
One adjacent hole is left alone deliberately: when
dtype is None, that branch runs before the FORCE_FLOAT32 check, so a no-bfloat16 device still gets float16. The only signal available at that point is the process-wideUNSLOTH_FORCE_FLOAT32, which the surrounding comments already flag as stale across multiple loads, so acting on it there could make matters worse. Worth a separate look.Companion to unslothai/unsloth-zoo#1316, which fixes the gradient checkpointing side (an offloaded activation was handed back in the staging buffer's dtype). The two are independent and neither depends on the other.