Repository navigation
FSDP2: lower the sharding-time memory peak (gather replicated tables without the chunk, expandable segments on every rank) - #18287
Conversation
… GPU before gathering the whole table shard_model gathers the frozen lm_head (kept whole on every rank) from the chunk sharded loading left on this rank. The chunk stayed on the GPU for the whole gather, next to the full table, and its freed block stayed cached, so the sharding peak was chunk + full table + slices above the loaded model (Qwen3.8-27B bnb-4bit on 2x T4: 13.9 GiB reserved of 14.56). gather_replicated now hands the chunk over: one at least a gather slice in size goes to host memory and its GPU block is released before the full table is allocated, and each slice's buffers are dropped before the next. The gathered bytes are unchanged. A shared parameter's chunk (named by another pending entry) is gathered as before.
…ing the CUDA device bind_local_cuda_device calls torch.cuda.set_device(LOCAL_RANK) at import on ranks > 0. That starts CUDA and fixes the caching allocator's settings before unsloth_zoo exports its default (PYTORCH_ALLOC_CONF = expandable_segments:True), so every rank but rank 0 trained without expandable segments. On 2x T4 with Qwen3.8-27B bnb-4bit FSDP2, rank 1 ended sharding with 511 allocator segments and 2.2 GiB more reserved than rank 0 (13.46 vs 11.16 GiB), two 1.2 GiB freed table chunks pinned by 1.5 MiB blocks in their tails. _gpu_init now records the allocator variables before importing unsloth_zoo and, when the import changed them after CUDA had already started, applies the new settings with torch.cuda.memory._set_allocator_settings (torch 2.1+). With CUDA not yet started torch reads the environment itself, unchanged.
|
@codex review |
Codex Review Summary: ✅ CompletedThis 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. |
|
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". |
…ion warning torch 2.10+ reads PYTORCH_CUDA_ALLOC_CONF, then PYTORCH_HIP_ALLOC_CONF, then PYTORCH_ALLOC_CONF (c10/core/AllocatorConfig.cpp), and ROCm builds before 2.10 fall back to the HIP name only when the CUDA one is unset. The settings a rank started CUDA with are now read in that order, so a legacy expandable_segments:True that zoo dropped is turned off as on rank 0. torch 2.10+ also marks torch.cuda.memory._set_allocator_settings deprecated with a FutureWarning: every rank above 0 printed it at import, and with warnings as errors the settings were silently not applied. The warning is now suppressed around the call; the function is still an alias of the accelerator setter.
unsloth_zoo exports its allocator settings before its own import starts CUDA, so torch.cuda.is_initialized() is True after `import unsloth_zoo` in every process. The late apply therefore ran on every single-GPU run and on rank 0 too (re-applying what torch had already read, and on torch 2.10+ printing the deprecation warning). It now runs only when CUDA had started before the zoo import, as on a torchrun rank above 0 that bound its device.
|
@codex review |
|
Codex Review: Didn't find any major issues. Another round soon, please! 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
With native FSDP2,
Trainer.__init__(the sharding step) was the highest GPU memory point of the whole run on Kaggle 2x T4. The model wasunsloth/Qwen3.8-27B-unsloth-bnb-4bit(QLoRA, sharded loading). Sharding peaked at 13.22 / 13.90 GiB reserved per rank on a 14.56 GiB T4. There are two causes, fixed in two commits.shard_modelkeeps the frozen, untiedlm_headwhole on every rank (gpt-oss on multiple GPUs: FSDP2 keeps frozen untied IO tables out of the root unit, PP trains MoE models with the router aux loss off #18165) and builds it by all-gathering the chunk each rank loaded. That chunk stayed on the GPU for the whole gather, next to the full table, so sharding briefly needed chunk + full table + gather slices on top of the loaded model.expandable_segments.bind_local_cuda_devicecallstorch.cuda.set_device(LOCAL_RANK)atimport unslothon every rank above 0. That starts CUDA and fixes the caching allocator settings beforeunsloth_zooexports its defaultPYTORCH_ALLOC_CONF=expandable_segments:True. Rank 0 does not bind, so only rank 0 got expandable segments.Changes
unsloth/models/fsdp2_load.py:gather_replicatedhands each chunk over to_full_from_chunksas the only reference to it.unsloth/_gpu_init.py,unsloth/parallel_env.py:import unsloth_zoo.torch.cuda.memory._set_allocator_settings(torch 2.1+).Kaggle 2x T4: Qwen3.8-27B bnb-4bit, FSDP2, sharded load
Setup: LoRA r 8, 1,024-token rows, GA 2, 3 steps,
adamw_8bit, fp16. All arms ran in one kernel on unsloth 31b5c7a with zoo main 5baf98d. Numbers are GiB per rank, as peak reserved / peak allocated.Correctness on B200 (relative memory, not a T4 measurement)
total_memory/mem_get_inforeport 14.56 GiB, fp16 weights, fp16 AMP.UNSLOTH_FSDP2_SHARDED_LOAD=1,UNSLOTH_FSDP2_LAYOUT=sharded.Tests
tests/test_fsdp2_sharded_gather_peak.py(CUDA, about 2 s):tests/test_parallel_env_allocator.py:import unsloth, as a rank above 0 does, and checks the new segments are expandable. Main fails it:PYTORCH_ALLOC_CONF=expandable_segments:Trueis set, but the segments are not expandable.test_fsdp2_native_io_tables_gloo,test_fsdp2_native_sharded_load_gloo,test_sharded_load_bnb4_gloo,test_fsdp2_native_trainer_init_any_loader_glooandtest_sharded_load_host_tables: 68 passed, 2 failed. The two failures aretest_sharded_loading_matches_full_load_shard_model[qwen2_5_vl-None-lora-False]and[llama-overrides9-lora-remap], which fail on main too.test_parallel_env: 138 passed.test_bind_local_cuda_device: passes.Not covered