Skip to content

[AffineIR] Plan flat shards that keep the parameter's own strides - #9070

Merged
delock merged 3 commits into
deepspeedai:masterfrom
0z5a:affine-colcase-planner
Nov 20, 2026
Merged

delock merged 3 commits into
deepspeedai:masterfrom
0z5a:affine-colcase-planner

Conversation

@0z5a

@0z5a 0z5a commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

Follow-up to the column case in #9026. #9011, which this was stacked on, has merged; the branch is now on master.

A ZeRO-3 partition cut at a tensor-parallel column edge holds boxes narrower than the parameter, and it stores them at the parameter's own row-major strides ((cols, 1)), not as dense runs. plan_transfer refuses those today (dest axis 0 strides …). The only workaround is one piece per row, which runs into max_pieces_per_map for real weights.

Change. In _frame_of, a flat shard may now hold a piece at the parameter's own strides, alongside the dense run it already accepts. Each such piece records its shift: its shard offset minus the row-major index of its origin. The tiling check for these pieces is a separate branch, _in_order_shard_tiles:

  • All pieces of the rank must share one shift. Then logical index i sits at address i + shift, which is injective.
  • With one shift, disjoint logical boxes mean disjoint addresses. Together with the shard bounds and the volume sum, the shard is tiled exactly.
  • Anything else is refused as before: two shifts in one shard, a mix with dense runs of another width, overlapping boxes, a box past the shard end, or strides that match neither form.
  • The disjointness sweep counts against max_pair_checks, the same as in [AffineIR] Check affine holders and source regions without pairwise comparisons #9011.

Shards whose pieces are all dense runs take the existing path unchanged.

Docs. A second commit updates affine_ir_spec.md §10.2–10.3 to match: the two forms a flat shard may take, the tiling argument for the second, and that nest_affine_maps still emits dense boxes only, since the parameter's own strides fit only a shard that is a run of the parameter's flattening.

Tests (tests/unit/checkpoint/test_affine_transfer_plan.py, all single-process):

  • ZeRO partitions cut at a column edge, including cuts in the middle of a row. Planned both from and into a column split, each checked element by element.
  • The same storage described one piece per row. The plan moves the same elements.
  • Five refusals, each matched to its reason.

Without the planner change, the 5 acceptance cases are refused. The 5 refusal cases refuse either way; the tests pin the new reasons. On CPU (gloo, up to 4 processes), test_affine_*.py, test_autoep_affine*.py, test_zero_affine.py and v1/moe/test_autoep_checkpoint.py pass: 765 passed and 4 skipped, against 755 and 4 on master.

Outside the unit tests (CPU, against this branch). These use the two-layout map from #9026: ZeRO-3 partitions plus the surviving TP shards, cut at each other's edges.

  • 300 random cases with uneven partitions, row or column TP splits, and one or both TP shards surviving. All are exact, whether the cut map is the source or the target.
  • 100 of those plans run through execute_transfers on 4 gloo processes, in both directions, at staging budgets of 8 MiB and 256 B. Nothing is wrong: 0 of 200 target shards reading from the cut map, and 0 of 483 writing into it.
  • 4096×4096 weight, 8 ZeRO-3 partitions plus both shards of a TP2 column split, planned into TP2 along columns:
    • with this change: 32 source pieces plan in about 1 ms under the default budget, giving 16 segments and a 1.3 KB plan;
    • one piece per row: 16,384 pieces, refused under the default budget; with an unbounded budget, 5.1 s, 8,192 segments and a 642 KB plan.

@0z5a
0z5a marked this pull request as ready for review November 13, 2026 08:29
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-13T08:37:32.806952Z a6feced Draft marked ready
ℹ️ 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" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a6feceda4c

ℹ️ About Codex in GitHub

Codex has been enabled to automatically 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 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +243 to +245
hold a multi-axis box as a dense run, which is how ZeRO stores a tensor-parallel shard, or at the
parameter's own row-major strides, which is how a ZeRO partition holds a box cut out of the rows it
stores. ``_shards_tile`` proves the second kind by its shift, not by its address interval.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Document the newly accepted flat-shard layout

When callers build these parameter-stride boxes, the checked-in contract still tells them the opposite: deepspeed/checkpoint/affine_ir_spec.md:671-688 says every box in a 1-D shard is dense, is treated as [dest_offset, dest_offset + numel), and that a narrower block must remain one piece per row. Following that guidance recreates the max_pieces_per_map failure this change is intended to remove, so update the storage-map contract and derivation guidance alongside this new behavior.

AGENTS.md reference: AGENTS.md:L71-L71

Useful? React with 👍 / 👎.

Comment on lines +869 to +870
monkeypatch.setattr(transfer_module, '_validate_holders', transfer_module._validate_holders_pairwise)
assert fast == _outcome(target, source)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Test holder validation only through public behavior

This test replaces the production _validate_holders binding with another private helper and treats the exact plan or error text from that implementation as its oracle. A correct refactor that removes or renames either helper, or changes diagnostics without changing plan_transfer behavior, will fail this test, while a defect shared by both paths can still pass; exercise the public planner against independently derived valid/invalid maps or observable transfer results instead.

AGENTS.md reference: AGENTS.md:L76-L80

Useful? React with 👍 / 👎.

@Achyuthan-S

Copy link
Copy Markdown
Contributor

@0z5a, I checked #9052 on top of this. Your commit and #9052's two commits apply onto master (892ed83) in that order without conflicts, and all eight affine test files pass on CPU (gloo, 4 ranks): 725 passed, against 715 with #9052 alone.

On the random two-layout sets from #9052 (ZeRO-3 storage with 1–6 partitions plus surviving TP shards of uneven sizes, 300 cases cut along rows and 300 along columns), every union now plans exactly into both a row-split and a column-split target. That includes the 82 column-cut cases per target that #9052 alone refuses with UnsupportedLayoutError. The row-cut cases stay exact.

@LiRunGuo, the combined branch is Achyuthan-S/DeepSpeed:union-on-9070, for your rerun of the weights from #9074 that are planned from both sources.

0z5a added 2 commits November 16, 2026 10:09
A ZeRO-3 partition cut at a tensor-parallel column edge holds boxes
narrower than the parameter, stored at the parameter's own row-major
strides. Their address intervals interleave, so plan_transfer refused
them, and the only workaround was one piece per row, which needs a
larger budget.

Accept them when a flat shard keeps one shift for all its pieces: the
address of logical row-major index i is then i + shift, which is
injective, so disjoint logical boxes, the shard bounds and the volume
sum prove the shard is tiled. Other mixes stay refused.

Signed-off-by: 0z5a <dezhen.lu@student.uni-tuebingen.de>
The spec said every box in a 1-D shard is dense and is planned as the
interval [dest_offset, dest_offset + numel), and that a block narrower
than a shard's rows keeps one piece per row. plan_transfer now also
accepts boxes at the parameter's own row-major strides with one shift
per shard, so describe that form, its tiling argument and the shards it
fits, and note that nest_affine_maps still emits dense boxes only.

Signed-off-by: 0z5a <dezhen.lu@student.uni-tuebingen.de>
@0z5a
0z5a force-pushed the affine-colcase-planner branch from a6feced to 8cc4b52 Compare November 16, 2026 09:33
@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. and removed skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. labels Nov 16, 2026
@0z5a

0z5a commented Nov 16, 2026

Copy link
Copy Markdown
Contributor Author

#9011 is in, so I rebased this onto master as promised. The column-case commit is unchanged, so @Achyuthan-S's union-on-9070 still applies as it is.

The first Codex note is fair: the spec still described every box in a flat shard as dense. A second commit adds the parameter-order form and its tiling argument to §10.2 and §10.3 of affine_ir_spec.md. The second Codex note (the test swapping in the pairwise holder check) was about #9011's test, which #9011 replaced with an element-level oracle before it merged, so it no longer applies here.

On CPU (gloo, up to 4 processes), test_affine_*.py, test_autoep_affine*.py, test_zero_affine.py and v1/moe/test_autoep_checkpoint.py pass: 765 passed, 4 skipped (755 on master).

@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added the skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. label Nov 16, 2026
@delock
delock self-requested a review November 18, 2026 15:05
@auto-pr-labeler-with-jev auto-pr-labeler-with-jev Bot added skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. and removed skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU. labels Nov 20, 2026
@delock
delock enabled auto-merge November 20, 2026 10:53
@delock
delock added this pull request to the merge queue Nov 20, 2026
Merged via the queue into deepspeedai:master with commit 1f1c18b Nov 20, 2026
19 checks passed
Achyuthan-S added a commit to Achyuthan-S/DeepSpeed that referenced this pull request Nov 22, 2026
Now that the planner reads flat shards at the parameter's own strides (deepspeedai#9070), a union of ZeRO-3 partitions and a surviving column shard plans exactly. Check the plan, not only the map.

Signed-off-by: Achyuthan Sivasankar <achyuthan.sivasankar@gmail.com>
Achyuthan-S added a commit to Achyuthan-S/DeepSpeed that referenced this pull request Nov 22, 2026
The spec and affine-transfer.rst still said the planner refuses a ZeRO-3 block that a column edge cuts across rows, which deepspeedai#9070 changed. The rst example also called plan_transfer with the default PlanBudget, which refuses most unions at the sizes the PR measures; it now passes a larger max_pieces_per_map and gives the piece counts.

Signed-off-by: Achyuthan Sivasankar <achyuthan.sivasankar@gmail.com>
banxingmjj pushed a commit to openanolis/DeepSpeed that referenced this pull request Nov 23, 2026
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-gpu-test Requires positive evidence that every changed behavior can be validated without a GPU.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants