Repository navigation
Conversation
…ne plans AffineWeightSync plans each parameter once with plan_transfer, from the trainer's storage maps into a tensor-parallel layout held by every inference replica, and runs every sync as one execute_transfers batch, so no rank gathers a whole parameter. WeightArena keeps a rank's shards in one buffer that is never reallocated, so CUDA graphs captured on it stay valid across syncs. Signed-off-by: RunguoLi <li19107254665@gmail.com>
|
Hi, @LiRunGuo A couple of questions, mostly to learn: In _LocalFirstLeastLoaded, the inline load increment in call is later overwritten by settle(), so it only balances multiple multi-holder regions within a single parameter. Is that the intended scope, and did you consider balancing across parameters during planning too? |
|
Thanks @LiRunGuo, this is a clean first step, and it follows the RFC closely: the sync is declared entirely in maps, planned by I ran it on CPU (gloo, 4 ranks), merged with current master: 17 passed and 1 skipped (the CUDA-graph test), and flake8 is clean. Turning off the local-first branch of the selector makes 3 of the tests fail, so the load tests do pin the policy. A few thoughts, none blocking for a draft:
On "What it does not do": the two-layout constructor from #9026 would let a surviving TP shard and the trainer's partitions go through this same path when the split is along rows. I can open it as a small separate PR that this one can use, if that suits you and @0z5a. The RTX 4090 table is a nice result: 7–8× faster than gathering for Qwen3-8B at TP4, with the peak set by the staging budget rather than the largest parameter. |
… global rank with any group; AutoTP source untested Test that a custom group keeps the maps keyed by global rank. Signed-off-by: RunguoLi <li19107254665@gmail.com>
|
Thanks @nathon-lee and @Achyuthan-S. I pushed two commits for the points below: 70831cb (docstrings, docs and one test, no behavior change), and 48a3575, which moves the planning onto #8936 now that it has merged (after merging current master).
Thanks for running it, and for the selector check.
#8936. The two-layout constructor. Yes, a separate PR suits me, row case first, as @0z5a agreed on #9026; thanks for opening #9052. The sync declares its sources only through maps, so it can take a surviving TP shard plus the trainer's partitions as soon as #9052 lands, and I'll add that case to this PR's tests then. Tests on 48a3575 (on current master, dfa7d34), 4× RTX 3090:
yapf and flake8 (the versions pinned in pre-commit) pass on the changed files. |
|
Thanks @LiRunGuo, that addresses everything. I read 48a3575 merged with current master: the sync now plans with one
|
|
Thanks @Achyuthan-S.
|
…peedai#9052) Refs deepspeedai#9026 (open question 8), deepspeedai#9045. ### Why A rollout replica that changes placement can often keep a tensor-parallel shard that is still current, while the trainer's ZeRO-3 partitions hold the whole parameter. `plan_transfer` can read a block from any of its holders only if the source map's blocks are equal or disjoint and each block lists every holder. A map that simply lists both layouts' pieces breaks both rules, since a partition can start or end inside a row of the shard. So today the planner refuses it, and deepspeedai#9045 falls back to its own planner for these weights. ### What - `union_affine_maps(maps)` in `deepspeed/checkpoint/affine.py` returns one map of a parameter that several layouts hold at once. - Each piece is cut at the edges of every block held over it, in any layout (replicas within one layout may store a region whole or in parts), and each cut piece lists the holders from every layout. - A cut keeps the piece's strides and moves its offsets, so every location still stores exactly what it did. - Layouts use disjoint locations, one per buffer. `TransferEndpoints` already lets several locations map to one process. - More than two layouts are merged pairwise. - Blocks are taken in order of their first row, and each is compared only with the parts that still reach into its rows; a piece is cut against only the parts that start in its rows. That keeps the work close to linear in the number of partitions (see Cost). - Layouts that contradict each other raise `InvalidMapError`, and a piece that is not an axis-aligned block raises `UnsupportedLayoutError`: the planner's types for the same problems, so a caller that falls back on `AffineTransferError` catches these too. - `subtract` moves unchanged from `affine_transfer._subtract` into `affine_geometry`, the module deepspeedai#9010 added for shared box arithmetic. The planner imports it under its old name. - Spec §10.8 and a section in `affine-transfer.rst`, after the replica-policy section from deepspeedai#8936. ### Scope Cuts along rows and along columns are both planned. A ZeRO-3 block of several rows cut at a column edge keeps its row-major strides and its partition's offset, so it is not dense in its flat shard; the planner reads such shards since @0z5a's deepspeedai#9070. ### Cost @jinyouzhi measured the first version growing with the square of the number of ZeRO-3 partitions. `union_affine_maps` on a (4096, 4096) parameter's ZeRO-3 partitions plus another layout, macOS arm64 CPU (before = the previous version): | Other layout | 200 partitions | 1000 partitions | | --- | --- | --- | | 8-way row split | 1.93 s → 0.03 s | 48.0 s → 0.18 s | | 8-way column split | 11.0 s → 0.26 s | 1.3 s (34k pieces) | | whole replica | 1.91 s → 0.03 s | 0.18 s | A column split is the slowest case left, because each of its blocks spans every row. A union is cut at every layout's edges, so it can exceed the default `PlanBudget` (4,096 pieces per map). `plan_transfer` on the same unions, into a row-split and a column-split TP8 target (the same for both unless noted): | Partitions + other layout | Pieces | Default `PlanBudget()` | `max_pieces_per_map=1 << 16` | | --- | --- | --- | --- | | 200 + 8-way row | 1,196 | planned | planned | | 200 + 8-way column | 6,782 | refused (pieces) | planned | | 1000 + 8-way row | 5,996 | refused (pieces) | planned | | 1000 + 8-way column | 33,976 | refused (pieces) | into rows: planned (2.3 s); into columns: refused at `max_merge_checks` (262,144), planned (3.5 s) with `max_merge_checks` and `max_pair_checks` raised as well | The docs now show the budget next to the call (thanks @delock). ### Tests `test_affine_transfer_plan.py`: - A surviving row shard plus ZeRO-3 partitions, in 4 configurations (uneven TP sizes, one or two surviving ranks, 1 to 5 partitions), planned into a row-split and a column-split target. Checked: - each element's holders are exactly the union of both layouts' holders; - every location extracts what its own layout says; - every plan reproduces `extract` exactly; - the surviving shard is read. - Three layouts merged in all six orders: the same holders, and exact plans. - A column cut, in 3 configurations (one or two surviving column shards, 2 to 7 partitions): holders, and exact plans into a row-split and a column-split target that read the surviving shard. With deepspeedai#9070's planner change reverted, 2 of the 3 are refused. - A layout whose replicas store a block whole and in two halves, merged with ZeRO-3 partitions: holders, and an exact plan into a column-split target. - Refusals, with the planner's error types: no maps, a location in both layouts, different shapes, a transposed piece that is not a block, and a row-major run that wraps into the next row. `test_affine_transfer_executor.py`: on 2 ranks, one process holds a ZeRO-3 partition and a surviving row shard under two locations. The plan runs through `execute_transfer`, and every target shard equals `extract`. Not merging the holders, dropping the part of a block that a later block doesn't overlap, or leaving the destination offset unmoved makes 9 of these 11 tests fail. Setting a part aside one row too early, or ending the search for the parts under a piece one row short, makes 6 and 7 of them fail. ### Validation The fifth commit changes only the spec and `affine-transfer.rst`: the column cut is planned since deepspeedai#9070, and the example passes a `PlanBudget` sized for a union. Rebased on master c00a7f6, after deepspeedai#9070 and deepspeedai#9093 merged. The only conflict was in `test_affine_transfer_executor.py`, where this PR and deepspeedai#9093 added test classes at the same place; both are kept. The three earlier commits are otherwise unchanged (`git range-diff`), and a fourth extends the column-cut test to plan. macOS arm64 CPU, Python 3.9.6, torch 2.8.0, gloo (`DS_ACCELERATOR=cpu LOCAL_SIZE=4`): - All nine affine test files: 959 passed (master c00a7f6: 948 passed). - yapf 0.43.0 and flake8 5.0.4 pass on the changed files. - 300 random unions of ZeRO-3 partitions (1 to 8) and surviving column shards (2 to 4 way, random widths, any non-empty subset), each planned into a row split and a column split: all 600 plans are exact, and all read a surviving shard. On GPU: 4× A100-SXM4-40GB (NVLink), driver 595.71.05, torch 2.12.1+cu130, NCCL 2.29.7, Python 3.12.11, with `pytest --forked` over the six `test_affine_*.py` files and `test_zero_affine.py`: this branch 905 passed and 1 skipped (master c00a7f6: 894 passed and 1 skipped), so the 11 new tests also pass over NCCL. Earlier, rebased on master 97aa3c1, after deepspeedai#9039 renamed the geometry imports. The only conflicts were those imports and `affine_geometry.__all__`; `subtract` now sits with master's unaliased imports. macOS arm64 CPU, Python 3.9.6, torch 2.8.0, gloo (`DS_ACCELERATOR=cpu LOCAL_SIZE=4`): - All nine affine test files: 933 passed (master 97aa3c1: 924 passed). - yapf 0.43.0 and flake8 5.0.4 pass on the changed files. - Against the previous version, on 6,000 random unions of two or three layouts (ZeRO-3 partitions, row and column splits, replicas, any subset of surviving shards): identical pieces, the same holders for every element, the same shard at every location, and the same refusals. The runs below are from before that rebase and the cost change. Earlier, rebased on master 892ed83, which includes deepspeedai#9011. The code and test changes are the same as before the rebase; the only conflicts were additions next to deepspeedai#9011's (`subtract` next to `boxes_disjoint` in `affine_geometry.py`, and the imports). macOS arm64 CPU, Python 3.9.6, torch 2.8.0, gloo (`DS_ACCELERATOR=cpu LOCAL_SIZE=4`): - All eight affine test files: 715 passed (master 892ed83: 706 passed). - yapf and flake8 pass on the files the rebase touched. On master 988b2e3, before that rebase (with deepspeedai#9010 and deepspeedai#8936): - All eight affine test files, then including deepspeedai#8936's `test_affine_replica_policy.py`: 412 passed. - The four union configurations above, planned through deepspeedai#8936's `plan_transfers` with `local_first_least_loaded` and with `first_holder` into both targets: all 16 plans reproduce `extract` exactly. - Against the previous commit (110ff4b, before the two fixes from review), on random layouts: 300 unions with a row-split surviving shard, 300 with a column-split one, and 300 of three or four layouts. Every element has the same holders, and every plan into a row- or column-split target has the same outcome as before: exact, except 82 of the column-split cases, which both commits refuse (the column cut under Scope). - pre-commit passes on the changed files. Before the rebase, on f9fe4df with deepspeedai#9010 applied: - All seven affine test files: 346 passed. - With deepspeedai#9011 on top: 648 passed. The only conflicts were adjacent additions: import lines and appended definitions. - 300 random two-layout cases (uneven TP sizes, any subset of surviving ranks, 1 to 6 partitions): - holders and map-level exactness hold in every case; - cuts along rows: 137 of 137 planned and exact; - cuts along columns: 79 planned and exact, and 84 refused by the current planner. - 300 random unions of three or four layouts, merged in random order: all planned and exact. On GPU, also before the rebase: 4× A100-SXM4-40GB (NVLink), driver 595.71.05, torch 2.12.1+cu130, NCCL 2.29.7, Python 3.12.11, with `pytest --forked` over six of the affine test files (all but `test_autoep_affine_universal.py`): - master (8a6b53f): 317 passed, 1 skipped; - this branch: 325 passed, 1 skipped, so the 8 new tests also pass over NCCL, including the executor test. - The one skip is the external-group test that runs only on the CPU accelerator. cc @0z5a @LiRunGuo @jinyouzhi --------- Signed-off-by: Achyuthan Sivasankar <achyuthan.sivasankar@gmail.com> Co-authored-by: Ma, Guokai <guokai.ma@gmail.com>
deepspeedai#9145) Refs deepspeedai#9026 (the cost model), deepspeedai#8252, deepspeedai#8935. ### Why Plans that move the same data can differ in time by several times. Which holder each target reads and the staging budget are free choices, and the planner guarantees an exact result whichever is picked. In the runs below, the default (the planner's first holder, the larger budget) is up to 76% slower than the fastest candidate. A replica policy (deepspeedai#8935, deepspeedai#8936), a two-stage relay (deepspeedai#9074) or a weight sync (deepspeedai#9045) needs a way to compare candidates before running one. deepspeedai#8967 reports what a batch moves and states that it does not model time; deepspeedai#8966 measures link rates. This adds the step between them: a time estimate per candidate, and the choice. ### What `deepspeed/checkpoint/affine_transfer_cost.py`, which is not imported by `import deepspeed`: - `TransferCostModel(route, capacity, seconds_per_call, seconds_per_round, seconds_per_region)`: one machine's calibrated costs. `route(sender, receiver)` names the resources a message occupies; `capacity` gives bytes per second per resource kind. Capacities must be positive and finite, and the costs non-negative and finite. - `node_route(nodes, intra_node)`: across nodes, the sender's `network_send` and the receiver's `network_receive`; within a node, each GPU's own link (`device_send`, `device_receive`, as on NVSwitch) or the link between the two GPUs (`device_pair`, NVLink). - `estimate_seconds(stages, model)`: each stage is one `execute_transfers` call, and stages run one after another. A call's rounds come from `schedule_transfers`, so the estimate prices the rounds that `execute_transfers` will run. A round takes its most loaded resource's bytes over that resource's capacity, plus `seconds_per_round`. Copy launches, `seconds_per_region` for each staged or local region of the busiest process, overlap the bytes, so a call takes `seconds_per_call + max(rounds, launches)`. Rounds sent or received in place launch no copy. - `cheapest(candidates, model)`: estimates every candidate and returns the cheapest name with every estimate. A tie goes to the name that sorts first, so ranks given the same model and candidates choose the same plan. The model must be built from data every rank shares. - A section in `affine-transfer.rst`. Not priced: a plan's one-time preparation (deepspeedai#9094), and overlap between stages. ### Tests `tests/unit/checkpoint/test_affine_transfer_cost.py`, single process. Each expected time is computed in the test from the bytes and the given capacities: - A round takes its busiest resource, whether the sender's or the receivers' side binds. - Two resources in one round bind on their own (the mixed incast of RFC deepspeedai#9026). - Copy launches overlap the bytes; local copies count as regions; rounds sent and received in place do not. - The GH200 capacities of RFC deepspeedai#9026 give its model rows for direct (4 × 8 GiB / 68.4 GB/s) and sharded (8 GiB / 23.4 GB/s), and the relay through deepspeedai#8936's `local_first_least_loaded` is chosen. - A tie goes to the first name. - Refusals: a routed resource with no capacity, a process with no node, a zero capacity, an unknown intra-node kind, no candidates. Each of these changes to the module makes at least one test fail: adding the resources instead of taking the busiest (3 fail), adding launches to the bytes (1), counting in-place rounds as regions (1), counting each message on both sides (2), dropping `seconds_per_round` (1), and breaking ties toward the last name (1). ### Validation macOS arm64 CPU, Python 3.9.6, torch 2.8.0, on master c00a7f6: the 6 tests pass; yapf 0.43.0 and flake8 5.0.4 pass on the new files. On GPU, with this exact module file on master e35282b: 4 GPUs of an 8-GPU node, on two machines, driver 595.71.05, torch 2.12.1+cu130, NCCL 2.29.7: - 4× A100-SXM4-40GB (NV12), AMD EPYC 7763; - 4× H200 (NV18), Intel Xeon Platinum 8558. On each, the 6 tests pass. Then a ranking test: 5 moves, each run as 2 to 4 candidate plans that deliver the same bf16 data and differ only in the holders read or the staging budget (16 plans). Each plan's time is the median of 7 runs after 3 warmups, from a barrier to the end of the call, slowest process. The first run of every plan was checked exact against the maps. The model was fitted on each machine from 8 calibration transfers only: one pair at 64 MiB, 256 MiB and 1 GiB in one round and 1 GiB at 64 and 8 MiB budgets, a 1 GiB fan-out to three, a fan-in of three times 1 GiB, and a gather of 2048 pieces of 4 KiB. The candidates were fixed, and their times kept out of view, until the fit was done. Regret is the chosen plan's time over the fastest measured: | Move | Candidates | A100: model | A100: default | H200: model | H200: default | | --- | --- | --- | --- | --- | --- | | 1 GiB held on 2 GPUs, into row halves on the other 2 | first holder or one each, 16/64 MiB budget | 0% | 56.7% | 0% | 38.6% | | DP2 × TP2 into TP4 (1 GiB) | first holder, local, local then the other | 0.3% | 4.1% | 1.4% | 5.1% | | Ring of 512 MiB blocks | budget 8/16/32/64 MiB | 0% | 0% | 0% | 0% | | Rows to columns over 4 GPUs | budget 4/16/32 MiB | 0% | 0% | 0% | 0% | | 16 weights of 64 MiB held on 2 GPUs, into row quarters on 4 | first holder or split | 0% | 76.4% | 0% | 76.3% | The estimates rank plans; they are not times. Against the measured times of these 16 plans they range from 62% under to 15% over on the A100 node (5 of 16 within 15%), and from 57% under to 5% under on the H200 node (3 of 16). Rerunning the scoring with the module on master c00a7f6 gives byte-identical output on both machines. Not validated: - Across nodes: the `network_send` and `network_receive` terms are checked only against the published GH200 numbers in the unit test; no multi-node run here. - Other accelerators: the module is pure Python over `schedule_transfers` and calls no device or collective, but it was calibrated only on these two NVIDIA machines. - Turning `calibrate_links` (deepspeedai#8966) fits into these capacities is left to the caller. The module does not change any training, checkpoint or ZeRO code path, so no ZeRO stage, offload, precision or parallelism setting is affected. Signed-off-by: Achyuthan Sivasankar <achyuthan.sivasankar@gmail.com> Co-authored-by: Ma, Guokai <guokai.ma@gmail.com>
Status: draft, part of RFC #9026. The RFC has not been accepted by maintainers yet. This PR stays a draft until it is, so that the design can be discussed on code.
plan_transfer.What it is. A building block for rollout engines that keep their own tensor-parallel copy of a ZeRO model. No rollout engine is wired to it in this PR. It is validated on synthetic modules in the unit tests and on Qwen3 checkpoints in the benchmark below. Wiring it into an engine (for example as the sync of #9023's
AutoTPRollout) is a follow-up.What it adds. A new module,
deepspeed/runtime/rollout/affine_weight_sync.py, and a section indocs/code-docs/source/affine-transfer.rst. No existing source file changes.AffineWeightSync(source_maps, target_maps, dtype, group=None, budget=StagingBudget(), limits=None):plan_transfer, without communication;sync(source_buffers, target_buffers)then runs the copies of all parameters as oneexecute_transfersbatch ([checkpoint] Execute affine transfer plans with bounded peer staging #8809), so no rank gathers a whole parameter;preflight_error, so every rank refuses the batch before any data moves, rather than one rank raising while the others wait.local_first_least_loaded, local copies do not count as load, and single-holder regions do.plan_transferconsults a selector only for regions with several holders, so the single-holder load of a parameter is counted once that parameter is planned.plan_transferswith its policy once it lands.zero_param_shaped_storage_map(param):zero_param_storage_affine_mapnested into the parameter's own shape, asautotp_storage_affine_mapsdoes for AutoTP models. A tensor-parallel layout describes a parameter in its own shape, and both sides of a plan must describe the same tensor.replicated_layout_map(layout, replicas): one replica's tensor-parallel layout, keyed by TP rank (an AutoTP layout map, orcontiguous_split_map), held by every replica and keyed by global rank. It is built withnest_affine_mapsandzero_storage_affine_map, astest_zero_affine.py's_one_holder_eachbuilds one holder per layout shard.WeightArena: a rank's shards as views of one buffer that is never reallocated.Why not a duplicate
AutoTPRollout) refreshes its inference copy by gathering each full fp32 master weight withsafe_get_full_fp32_paramand cutting out the local shard. That moves every parameter to every rank. This PR moves each element only to the ranks that need it.What it does not do (yet)
plan_transferrefuses replicas with different boundaries.relay_map([RFC] Two-stage relay for cross-node weight sync, priced by a calibrated cost model #9026 §1).Tests (
tests/unit/runtime/rollout/test_affine_weight_sync.py)layout.extractof the gathered parameter after two optimizer steps.AffineTransferExecutionErrorand no target changes.replicated_layout_mapgives each replica its layout shard and refuses replicas that do not fit.Results: 4× RTX A6000 (driver 595.58.03), torch 2.13.0+cu130, CUDA 13.0, Python 3.12. Commands run from
tests/:pre-commit passes on the changed files.
Measurements
stage3_param_persistence_threshold: 0, no optimizer). Each number is the median of 5 syncs after one warm-up.GatheredParameterson each parameter, then a copy of this rank's slice into an arena. That is feat(rollout): add AutoTP rollout engine with in-memory weight sync #9023's approach, but on the bf16 parameter rather than the fp32 master, so it moves half the bytes it would there.max_memory_allocatedincrease on any rank during a sync.To reproduce, run the script below with
torchrun --nproc_per_node 4 pr_weight_sync_bench.py --model <Qwen3-1.7B or Qwen3-8B> --layouts 0-3:2 0-3:4 0-3:1 2-3:2 --out <dir>. It needs transformers. Layouts are<ranks>:<tp>, so0-3:2is two TP=2 replicas on ranks 0–3; Qwen3-8B was run with0-3:4only, since two copies at TP1 or TP2 do not fit in 24 GB.pr_weight_sync_bench.py
Compatibility
autotp_storage_affine_mapsas the source maps);AI assistance: AI assistance was used. The code, tests and this description were drafted with an AI coding agent (Claude Code). The submitter reviewed every changed line and ran the tests above.
Maintenance contact: @LiRunGuo. cc @Achyuthan-S @0z5a @jinyouzhi