Repository navigation
Mixed NVIDIA hosts: bf16 and compile follow the current device; check where the weights landed after loading - #14622
Conversation
…device, the load checks where weights landed An idle older card beside the current one (RTX 4090 + GTX 1080) no longer turns bf16 off, overrides an explicit bf16 dtype or disables torch.compile for the whole process; torch answers is_bf16_supported for the current device too. After loading, layers placed on a card that cannot compile turn Dynamo off before the first forward, and bf16 layers on a card without bf16 warn. Identical cards keep the plain capability cache.
|
@codex review |
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: 58988d868f
ℹ️ 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".
| del _cuda_caps | ||
| # The current device, as torch.cuda.is_bf16_supported() answers: an idle older card beside it must | ||
| # not take bf16 from a model that runs here. A load placing layers there is checked after loading. | ||
| SUPPORTS_BFLOAT16 = major_version >= 8 |
There was a problem hiding this comment.
Recompute bf16 support after device switches
When a process imports Unsloth on one GPU and later calls torch.cuda.set_device() before loading, this stores the original device's capability permanently. The model loaders call is_bfloat16_supported(), which merely returns this constant, so switching from an Ampere GPU to a T4 still selects bf16, while switching in the opposite direction unnecessarily selects fp16. Query the current device when the loader makes its dtype decision rather than caching this import-time result.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
I would rather keep SUPPORTS_BFLOAT16 meaning what it has always meant: the device current at import, as before #13923 and in every release, and code outside the loaders reads it as a constant. The case is now covered by the post-load check instead (24c171b): a bf16 load that lands entirely on a card without bf16 after a set_device gets the warning suggesting dtype=torch.float16 or a device mask, since the check no longer skips a sole current-device placement. Test: sm86 import device, set_device to an sm75 card, bf16 load placed there, warning shown.
| if not indices or indices == [torch.cuda.current_device()]: | ||
| return |
There was a problem hiding this comment.
Recheck compile safety for a sole current-device placement
If Unsloth is imported while a modern GPU is current, then the caller switches to a pre-Volta GPU and loads the model entirely there, indices equals the new current device and this return bypasses the only post-load capability check. The import-time compile gate inspected the former device, so Dynamo remains enabled and the first compiled forward on the pre-Volta card can crash or miscompile. A sole-current-device placement still needs checking when the current device may have changed since import.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 24c171b. The early return skipped the check when the whole model sat on the current device, but the current device can change after import (import on an H100, torch.cuda.set_device to a GTX 1080, load there), so Dynamo stayed on. The check now looks at every placed GPU, including a lone current-device placement, whenever more than one GPU is visible. One-GPU hosts still return before walking anything, and capability reads go through the per-device cache. Test: everything loaded onto a pre-Volta current device after set_device turns Dynamo off with the notice.
| # The current device, as torch.cuda.is_bf16_supported() answers: an idle older card beside it must | ||
| # not take bf16 from a model that runs here. A load placing layers there is checked after loading. | ||
| SUPPORTS_BFLOAT16 = major_version >= 8 |
There was a problem hiding this comment.
Validate mixed placement in the diffusion loader
On a mixed Ampere/pre-Ampere host, FastDiffusionModel defaults to device_map = "auto" but loads its model directly in diffusion.py and never calls the new placement validator. Because this assignment now selects bf16 solely from the current Ampere device, a large diffusion model split onto a T4 or V100 retains bf16 on that older card without even the warning added for the Llama and vision loaders; previously the weakest-visible-device gate selected fp16. Run the same post-load placement check in the diffusion path.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 24c171b. FastDiffusionModel.from_pretrained defaults to device_map="auto", so it now calls check_mixed_gpu_placement(model, dtype) right after the load, matching llama.py and vision.py. A test checks the diffusion loader makes that call.
…ds check placement too [skip ci]
…r so every rank picks one dtype [skip ci]
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…oder path, check the placement too [skip ci]
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4fce4c762f
ℹ️ 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".
| disarm_fp8_to_nf4(model_config) | ||
| # Before extracting the text core, which does not carry hf_device_map over. | ||
| fall_back_from_flash_attention_on_pre_ampere(model) | ||
| check_mixed_gpu_placement(model, dtype) |
There was a problem hiding this comment.
Check only the retained text model placement
When text_only=True cannot be resolved to a standalone decoder, this path loads the composite model and _text_trainable_core immediately deletes the unused vision/audio siblings. Because the placement check runs first, a discarded sibling placed on a pre-Volta GPU permanently disables Dynamo for the retained language model (and a discarded pre-Ampere sibling can emit a spurious bf16 warning), even if every retained text layer is on a capable GPU. Preserve or filter the device map for the selected core and validate that retained placement instead.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 4fce4c7. The text_only composite path now runs check_mixed_gpu_placement after _text_trainable_core, on the retained decoder. The core carries no hf_device_map, so _placed_gpu_indices now falls back to every parameter's device instead of only the first one, which still covers a core split across cards. A discarded vision / audio sibling on an older card no longer turns Dynamo off or warns. Tests: test_a_core_without_a_device_map_is_checked_on_every_card_it_spans, test_a_core_on_capable_cards_only_changes_nothing and test_text_only_composite_loads_check_the_retained_core (the first and last fail on the previous head).
…st stub carries the new device_type helpers [skip ci]
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ed6788b64b
ℹ️ 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".
| def is_bf16_supported(including_emulation = False): | ||
| supported = old_is_bf16_supported(including_emulation) | ||
| return supported if including_emulation else (supported and SUPPORTS_BFLOAT16) | ||
| return old_is_bf16_supported(including_emulation) |
There was a problem hiding this comment.
Keep the public bf16 probe consistent in distributed runs
When WORLD_SIZE > 1 on a mixed H100/T4 host, cuda_bf16_capability() deliberately makes SUPPORTS_BFLOAT16 false using the weakest visible card, but this wrapper now returns the original current-device result, which is true while the H100 is current. This leaves torch.cuda.is_bf16_supported() inconsistent with the dtype selected by Unsloth; for example, unsloth/save.py uses that public probe when choosing the target dtype for a merged 16-bit save and can convert an intentionally fp16 distributed model to bf16. Preserve the dynamic behavior for ordinary runs while clamping the public result to the distributed weakest-card decision.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Good catch, fixed in baf70ad. Under WORLD_SIZE > 1 the torch.cuda.is_bf16_supported() wrapper now returns supported and SUPPORTS_BFLOAT16 for the non-emulation query, so it gives the same weakest-card answer as SUPPORTS_BFLOAT16 on every rank whichever card is current. A merged 16-bit save of an fp16 distributed run therefore stays fp16. Single-process runs keep the dynamic current-device answer, and including_emulation=True is still torch's own answer. test_a_distributed_launch_keeps_the_public_bf16_probe_on_the_weakest_card covers H100 + T4 with either card current: it fails on the previous head and passes now.
| indices = sorted(_placed_gpu_indices(model)) | ||
| if not indices: | ||
| return |
There was a problem hiding this comment.
Validate models after deferred CPU-to-GPU placement
When a caller uses device_map=None—including Trainer/FSDP-managed placement—the model has only CPU parameters at this point, so indices is empty and this returns without recording any check. If Unsloth was imported while an H100 was current but Trainer later moves the CPU-loaded model to a Pascal rank after set_device/LOCAL_RANK initialization, the import-time gate left Dynamo enabled and the first compiled forward runs on an unsupported pre-Volta GPU. Validate the eventual training device when the framework moves a CPU-loaded model rather than treating an empty GPU placement as final.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in baf70ad, in two parts. For distributed launches, the import-time pre-Volta switch (and the gfx906 one on ROCm) now uses the same weakest-visible-card rule as bf16, because ranks import before set_device. So a rank the Trainer or FSDP later places on a Pascal card already has Dynamo off at import, before anything gets compiled. That leaves the single-process case where the model moves after loading (.to("cuda:1"), or a CPU load placed by the Trainer). For that, the placement check also runs once at the start of train(), from the stray-compile-cache reset that the plain Trainer loop, the SFT wrapper and the RL trainers already call. At that point it reads the parameters as well as hf_device_map, which can be stale after a .to(). It does nothing on one-GPU hosts and adds no per-step work. New tests cover the distributed compile switch on CUDA and on gfx906, plus the train-start check for both a CPU load and a .to() after loading. They fail on the previous head and pass now.
…and the compile switch; training start re-checks placement
torch.cuda.is_bf16_supported() now agrees with SUPPORTS_BFLOAT16 under WORLD_SIZE > 1, so a merged
16-bit save of an fp16 distributed run no longer picks bf16. One process stays dynamic.
The pre-Volta and gfx906 compile switches use the same distributed rule as bf16, so a rank the
Trainer places on an older card after set_device has Dynamo off. A single process that moves a
model after loading (.to("cuda:1"), Trainer placement of a CPU load) is re-checked once when
train() starts, reading the parameters as well as a possibly stale hf_device_map.
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…n mixed NVIDIA hosts A torch.cuda.set_device after import unsloth no longer leaves the public helper and the loaders' auto dtype on the import-time card. One GPU, identical cards and distributed launches keep the import-time constant with no per-call work; an explicit dtype still wins. [skip ci]
…d compile; the live check infers bf16 weights [skip ci]
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Lines 594 to 595 in d765045
On supported PyTorch 2.2/2.3 releases, where is_bf16_supported lacks the including_emulation parameter, this wrapper always returns the capability captured at import instead of invoking the original current-device probe. If Unsloth is imported on an H100 and the process later switches to a T4, callers such as the merged-save dtype selection in save.py still see bf16 support and may convert an fp16 run to bf16; preserve the original dynamic result for non-distributed processes and clamp it only for distributed launches.
ℹ️ 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".
| except Exception: | ||
| _BF16_BY_DEVICE = None |
There was a problem hiding this comment.
Retain readable devices when one capability probe fails
When one visible GPU's capability query raises, this exception handler discards the capabilities already obtained for every other GPU and leaves _BF16_BY_DEVICE unset. For example, after importing on an H100 with an unreadable peer and a readable T4, switching to the T4 makes is_bfloat16_supported() fall back to the H100's import-time SUPPORTS_BFLOAT16, so loaders select bf16 instead of the current device's fp16 choice. Build the map from individually guarded readable devices rather than abandoning it on the first failed peer.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Agreed, fixed. The per-device table now reads each card's capability on its own: a card whose query raises is left out (so it falls back to SUPPORTS_BFLOAT16), and the cards that were read still get their own entry. With an H100, an unreadable peer and a T4, the table is {0: True, 2: False}, so switching to the T4 answers fp16. Added test_an_unreadable_peer_keeps_the_cards_that_were_read, which fails before this change and passes after; the rest of the mixed-capability tests still pass.
…drops out [skip ci]
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
Summary
Follow-up to #13923 from a post-merge review. #13923 decided bfloat16 and the torch.compile switches from the weakest visible NVIDIA card. On a mixed host (an RTX 4090 or A100 at index 0 next to a T4, V100 or GTX 10xx display card), with the model entirely on device 0:
dtype = torch.bfloat16was silently turned into float16;TrainingArguments(bf16 = True)raised, because the patchedtorch.cuda.is_bf16_supported()answered False;PyTorch answers bf16 for the current device (
torch/cuda/__init__.py), inductor rejects only graphs placed on an old card, ComfyUI decides per device and vLLM only warns. Unsloth was stricter than all of them, which cost bf16 and compile on hosts that never used the old card.What changes
SUPPORTS_BFLOAT16answers for the current device, and the patchedtorch.cuda.is_bf16_supported()returns torch's own answer again. An explicit bf16 dtype is kept when the current card supports it. The "visible GPUs lack bfloat16" import notice is gone.cuda_current_capability(),hip_current_arch(),arch_breaks_compile()).check_mixed_gpu_placement(model, dtype), called right after the Hugging Face load inllama.pyandvision.py: weights on a pre-Volta or gfx906 card turn Dynamo off before the first forward with a notice naming the card (an explicitTORCHDYNAMO_DISABLE=0still wins); bf16 weights on a card without native bf16 get a warning, nothing changes silently. One-GPU hosts and loads entirely on the current device do nothing.functools.cacheon single-GPU hosts and on hosts whose cards share one capability; a mixed host fills its per-card cache from one scan.Pairs with unslothai/unsloth-zoo#1951 (LM head fusion follows the current device); either can merge first.
Tests
tests/test_mixed_nvidia_capabilities.py(rewritten for the current-device rule and the placement check): 42 pass here, 18 fail on main. Spoofed{0: (8, 9), 1: (7, 5)}and{0: (8, 9), 1: (6, 1)}with current device 0 keep bf16, explicit bf16 and Dynamo; current device 1 gives the old card's answers; a load placing layers on a pre-Volta cuda:1 turns Dynamo off with a notice; homogeneous and single-GPU hosts are identical.Not run on a real mixed-generation host (none available); proven with spoofed capabilities.