Repository navigation
Keep frozen load_in_fp8 weights out of FSDP2 sharding and the DDP broadcast - #15001
Conversation
…d the DDP broadcast torchao's Float8Tensor reports bfloat16 as its dtype, so native FSDP2's float8 check missed it and the sharded layout passed it to fully_shard (aten.new_zeros is not implemented), and DDP's start-up broadcast flattened it (reshape to [-1] is not implemented). The frozen FP8 base is loaded identically on every rank: keep it replicated (ignored_params) in every FSDP2 layout, and in DDP's _ddp_params_and_buffers_to_ignore at load / get_peft_model and at trainer init. Trainable Float8Tensor weights are refused under both.
|
@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: 4876bfbc8b
ℹ️ 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".
| _replan_auto_offload_safely(self) | ||
| _size_auto_checkpoint_skip_for_trainer(self) | ||
| _block_swap_for_trainer_backend(self) | ||
| _tensor_subclass_weights_for_ddp(self) |
There was a problem hiding this comment.
Reapply FP8 DDP exclusions after model_init rebuilds
When Trainer is configured with model_init—including during hyperparameter search—Transformers invokes the factory again when training starts and replaces self.model. This post-init-only hook therefore marks only the initial instance; the replacement loses _ddp_params_and_buffers_to_ignore, so DDP again attempts to broadcast its frozen Float8Tensor weights and fails. Apply the helper to each rebuilt model, or reject model_init for this configuration as the native FSDP2 path does.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 37372db: Trainer.call_model_init is wrapped (once, alongside the existing Trainer init patch) so the model a model_init factory returns, at init and again when train() rebuilds it, gets the same DDP exclusions. A factory that loads through FastLanguageModel / FastModel was already covered by the loader hook; this also covers models built any other way. Test: a model_init case in tests/test_fp8_torchao_distributed_gloo.py.
|
@codex review |
|
@codex review |
`studio/backend/tests/test_gguf_load_cache_reuse.py::TestLoadHubDownloadExclusion::test_load_marker_precedes_hub_guard_which_precedes_the_gpu_handoff` fails on `main`, for example in Backend CI `(Python 3.13, a-k)` on #15000 and #15001. ## Cause The test's last assertion is a literal source match: `"@_with_gguf_load_marker\n def load_model("`. #14546 added `@holds_inference` between that decorator and `def load_model`, so the string no longer appears. The load marker still wraps `LlamaCppBackend.load_model`, so the product is fine and the test was out of date. ## Fix The check now parses `llama_cpp.py`, finds `LlamaCppBackend.load_model`, and asserts that `_with_gguf_load_marker` is in its decorator list, in any position. The rest of the test, including the ordering in `routes/inference.py`, is unchanged. ## Verification - `studio/backend/tests/test_gguf_load_cache_reuse.py`: 75 passed. On `main` this test failed when run alone. - Sabotage check: removing `@_with_gguf_load_marker` from `load_model` fails the test.
|
@codex review |
|
Codex Review: Didn't find any major issues. Nice work! 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". |
WeightWithDynamicFloat8CastTensor implements the flatten DDP's start-up broadcast needs and plain DDP trains it, so the tensor subclass scan no longer leaves it out of DDP or refuses a trainable one.
|
@codex review |
|
Codex Review: Didn't find any major issues. You're on a roll. 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". |
…y_shard and the DDP broadcast (#15373) #15001 keeps frozen `load_in_fp8` weights (torchao `Float8Tensor`) out of FSDP2 sharding and out of the DDP broadcast. Its classifier matches every torchao tensor subclass outside `torchao.float8`, which catches two cases it should leave alone: - Frozen NF4 weights (`NF4Tensor`, QLoRA) are kept replicated under `FSDP2Config(shard_frozen=True)`. Each rank holds the full packed weight instead of half of it. - Trainable int8 mixed precision training weights (`Int8MixedPrecisionTrainingLinearWeight`) are refused under FSDP2 (`cannot shard trainable float8 parameters`) and under DDP (`NotImplementedError`). Both classes implement the FSDP2 all-gather hooks and the DDP flatten, and both trained before #15001. ## Fix - `fsdp2_native._float8_params` now keeps a torchao subclass out of `fully_shard` only when its class implements no FSDP2 all-gather extension (`fsdp_pre_all_gather`). That covers `Float8Tensor`, `AffineQuantizedTensor`, and the inference int8 / int4 weights (`Int8Tensor`, `IntxUnpackedToInt8Tensor`), which implement only the ops their linear needs: `fully_shard`'s split fails on them (`NotImplementedError: Int8Tensor dispatch: ... aten.split`). `NF4Tensor`, the int8 mixed precision training weight and `torchao.float8` training weights implement the extension and go to `fully_shard`. - On the DDP side, frozen tensor subclass weights are still left out of the broadcast. Trainable weights are refused when they store float8 or when DDP's start-up broadcast cannot flatten them (`Int8Tensor`, `AffineQuantizedTensor`), so trainable int8 training weights and NF4 reach DDP as they did before #15001. ## Tests `tests/test_fp8_torchao_distributed_gloo.py` gets four new cases; the first three run on 2 CPU gloo ranks, the `Int8Tensor` one in one process. | test | main | this PR | |---|---|---| | frozen NF4, `shard_frozen=True`: both MLP weights are DTensors holding 32768 packed bytes per rank (half of 2 x 32768) | fail (replicated, 0 DTensors) | pass | | trainable int8 mixed precision weights shard, backward gives finite grads | fail (`NotImplementedError`) | pass | | DDP keeps trainable int8 weights and ignores frozen NF4; FSDP2 finds no float8 weights | fail | pass | | frozen `Int8Tensor` (Int8WeightOnlyConfig) stays replicated; trainable is refused by FSDP2 and DDP up front | pass | pass (fails on the first revision of this PR, which sent it to `fully_shard`) | | the 7 existing Float8Tensor FSDP2 / DDP cases | pass | pass | `test_fsdp2_native_int8_gloo.py`, `test_float8_training_fsdp2_gloo.py` and `test_fsdp2_native_units.py` still pass (72 tests with `test_float8_training.py`). The classifier was also run over the torchao weight classes on torchao 0.15 and 0.18 against what `fully_shard` and DDP actually accept: 0 wrong moves. Not covered: GPU / NCCL runs, an end-to-end QLoRA training run under FSDP2, torchao versions other than 0.15 and 0.18, multi-node. --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
load_in_fp8 = Truecould not train on more than one GPU:fully_shard(Float8Tensordispatch:aten.new_zerosnot implemented).Float8Tensor: only support reshaping from 2D to 3D or from 3D to 2D, requested[-1]).Single GPU training was fine.
Cause
torchao's
Float8Tensorreports bfloat16 as its dtype, so the existing float8 check infsdp2_native.pynever matched it. DDP flattens every parameter it broadcasts, andFloat8Tensordoes not implement that.Fix
The FP8 base weights are frozen and every rank loads and quantizes them identically, so this change keeps them replicated:
ignored_paramsin every layout. A trainable one is refused with a clear error. torchao's Float8 training weights (torchao.float8, e.g. withenable_fsdp_float8_all_gather) implement FSDP2's hooks and are still left tofully_shard._ddp_params_and_buffers_to_ignoreafterfrom_pretrainedandget_peft_model, and again in the Trainer init for plain DDP. A trainableFloat8Tensoris refused on every rank, so no rank hangs.LoRA parameters still shard under FSDP2 and all-reduce under DDP as before. Nothing changes on one GPU.
Tests
tests/test_fp8_torchao_distributed_gloo.py(Float8Tensorbuilds on CPU):Float8Tensorweights are refused.test_block_swap_auto_ddp,test_fsdp2_native_int8_gloo,test_fsdp2_native_trainer_args_gloo.load_in_fp8withfull_finetuning = Truestays refused at load time, as it is on one GPU.Not covered: models other than a tiny Llama, SM 8.9 / 9.0,
load_in_fp8 = "block", multi-node, checkpoint save and resume under 2 ranks, runs longer than 5 steps.