Skip to content

Guard the shm inference all-reduce on the host ISA - #8701

Merged
delock merged 3 commits into
deepspeedai:masterfrom
delock:fix/shm-isa-guard
Oct 10, 2026
Merged

delock merged 3 commits into
deepspeedai:masterfrom
delock:fix/shm-isa-guard

Conversation

@delock

@delock delock commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

Problem

The shm comm op's bf16/fp16 reduce kernels are compiled with target("avx512bw") and use _mm512_* conversions; the fp32 kernels use 256-bit AVX. Executing them on a host without the ISA faults, and because the reduce synchronizes through shm flags, the fault surfaces downstream as a permanent hang of the peers instead of an error — on the multi-rank CPU CI this froze an xdist worker until the phase timeout.

Change

  • Export vector_kernel_level() from the op: 2 = avx512bw, 1 = avx2, 0 = neither; non-x86 hosts report 2 (their kernels are JIT-built for the host ISA).
  • Gate the dispatch in inference_all_reduce: fall back to the process-group all_reduce when the tensor dtype needs a level the host lacks (fp32 requires level 1, bf16/fp16 require level 2).

Validation

  • avx2-only host: fp32 keeps the shm path, bf16/fp16 now fall back; all dtype parametrizations of TestDistInferenceAllReduce pass (previously segfaulted/hung), and a standalone 4-rank repro that crashed before returns correct sums.
  • AVX512 host: vector_kernel_level() reports 2 and every dtype still dispatches to the original shm kernels — the guard is a pure init-time probe with no change to the kernel code, so behavior on AVX512 machines is unchanged. inference_all_reduce results remain numerically equivalent to the process-group all_reduce across fp32/bf16/fp16 and small through large message sizes (crossing the 1MB distributed-path threshold), and the shm latency advantage over gloo is preserved (microsecond-scale small messages).

@delock
delock requested a review from tjruwase as a code owner September 29, 2026 09:33
The shm comm op's bf16/fp16 reduce kernels are compiled with
target("avx512bw") and use _mm512_* conversions (the fp32 kernels only
use 256-bit AVX). Executing them on a host without the ISA faults, and
because the reduce synchronizes through shm flags, the fault surfaces
downstream as a permanent hang of the peers instead of an error - on
the multi-rank CPU run it froze an xdist worker for two hours until the
phase timeout.

Export a vector_kernel_level() probe from the op (2 = avx512bw, 1 =
avx2, 0 = neither; non-x86 hosts report 2 since their kernels are
JIT-built for the host ISA) and gate the dispatch in
inference_all_reduce: fall back to the process-group all_reduce when
the tensor dtype needs a level the host lacks.

Verified on an avx2-only host: fp32 keeps the shm op, bf16/fp16 now
fall back and all three dtype parametrizations of
TestDistInferenceAllReduce pass (they previously segfaulted/hung); a
standalone 4-rank repro that crashed before returns correct sums.

Signed-off-by: Guokai Ma <guokai.ma@intel.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

The probe can still enable kernels compiled for unsupported x86, AArch64, or RISC-V instruction sets and lacks deterministic dispatch tests.

Review effort: Balanced
Findings: 2 High severity · 1 Medium severity

Open (3)
What changed in this PR

Adds host-ISA guarding for shared-memory inference all-reduce to prevent illegal-instruction faults and peer hangs.

Changes:

  • Exposes shared-memory kernel ISA capability levels.
  • Falls back to process-group all-reduce when the host lacks the required ISA.
File Description
deepspeed/​comm/​torch.py Selects shared-memory or process-group reduction by dtype and ISA level.
csrc/​cpu/​comm/​shm_interface.cpp Adds and exports host ISA-level detection.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread csrc/cpu/comm/shm_interface.cpp Resolved
Comment thread csrc/cpu/comm/shm_interface.cpp Resolved
Comment thread deepspeed/comm/torch.py Resolved
The non-x86 branch of vector_kernel_level() kept the pre-guard behavior
of always enabling the kernels, but its comment wrongly claimed they are
JIT-compiled for the host ISA. State the real situation instead and defer
runtime probes for optional extensions (e.g. riscv +v/+zvfh) to the ports'
maintainers.

TestInferenceAllReduceDispatch pins the dtype x ISA-level routing matrix
with stubbed collective sinks so the dispatch is covered on any runner
instead of whichever path the host CPU happens to enable.

Signed-off-by: Guokai Ma <guokai.ma@intel.com>
@delock
delock requested review from loadams and tohtana as code owners October 6, 2026 04:46
monkeypatch.setattr defaults to raising=True, which requires the target
attribute to already exist. On non-Linux hosts (e.g. the macOS CI runner)
the Linux-only shm op is never built, so torch.ops.deepspeed.inference_all_reduce_
is absent and every dispatch case failed at the stub line. Use raising=False:
the stub is installed when missing and removed again on teardown.

Signed-off-by: Guokai Ma <guokai.ma@intel.com>
@delock
delock added this pull request to the merge queue Oct 10, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Oct 10, 2026
banxingmjj pushed a commit to openanolis/DeepSpeed that referenced this pull request Oct 10, 2026
## Problem

Since deepspeedai#8680 landed, `modal-torch-latest / DeepSpeedAI CI` has failed for
every merge-queue entry that runs this test on Modal's L40S runners
(deepspeedai#8794, deepspeedai#8701, deepspeedai#8670). Example: [run
38009529440](https://github.com/deepspeedai/DeepSpeed/actions/runs/38009529440/job/114086150257).

```
FAILED tests/unit/v1/autotp/test_autotp_training.py::TestTiedRowParallelOutputHeadTraining::test_training_matches_unsharded_reference[35-3-False-0]
FAILED tests/unit/v1/autotp/test_autotp_training.py::TestTiedRowParallelOutputHeadTraining::test_training_matches_unsharded_reference[35-3-True-0]
Mismatched elements: 1 / 670 (0.1%)
Greatest absolute difference: 1.1173542588949203e-06 at index (1, 1, 45) (up to 1e-06 allowed)
```

deepspeedai#8680's own merge-queue run and the next nightly run both used the AWS
fallback, where the test passes, so the failure wasn't caught there.

## Root cause

The row-parallel head (`LinearAllreduceWithReplicatedInput`) slices the
hidden vector, computes a partial matmul on each rank, and all-reduces
the partial logits. The reference is one unsharded fp32 matmul, and the
two summation orders round differently. The tied head uses the
embedding's N(0,1) weights, so logits reach about 14, where one fp32 ulp
is about 1e-6. Logits that are near zero through cancellation can
therefore differ by more than `atol=1e-6`. Even the fp32 reference
differs from a float64 ground truth by up to 2.2e-6.

## Fix

Use torch.testing's default fp32 tolerance (`atol=1e-5`, `rtol=1.3e-6`)
for every fp32 `assert_close` in `test_autotp_training.py`. That covers
17 checks, including both row-parallel logits checks, and matches the 19
fp32 checks in the file that already use the default. The half-precision
tolerances in `run_tp_layer_fwd_bwd` are unchanged.

The default is enough for what these tests check:

- The sharding bugs they target (wrong shard offset, missing all-reduce,
dropped tied-gradient contribution, tied weights drifting apart) move
values by 0.1 to 10.
- A simulated precision regression (all-reduce inputs rounded to bf16)
still fails every check it failed under the old tolerances. The one
exception is the SGD-update check, which can't detect it under either
tolerance.
- The custom values were tighter than the default only for values below
about 1, and looser for larger ones such as the losses.

## Evidence (2x H200)

- In a sweep of 40 init seeds, every parametrization of the tied test
has seeds that fail the old logits check (worst ratio to the allowed
tolerance: 1.19). Under the default, the worst case uses under 30% of
the limit.
- Changing only the seed to 8199 reproduces the CI failure:
`[35-3-False-0]` and `[35-3-True-0]` fail with `1 / 670` mismatched and
`1.25e-06 (up to 1e-06 allowed)`.
- Every other changed check stays under 3% of the default limit,
recorded per call across the 4 affected test classes.

## Testing

Hardware: 2x NVIDIA H200, torch 2.12.0+cu130, Python 3.12.

- `pytest tests/unit/v1/autotp/test_autotp_training.py -k
"TestRowParallelOutputHeadTraining or
TestTiedRowParallelOutputHeadTraining"`: 8 passed.
- `pytest tests/unit/v1/autotp/test_autotp_training.py -m sequential -k
"TestTiedEmbeddingAliasesAndFallback or TestTiedVocabParallelLMHead"`:
30 passed.
- Seed-8199 reproduction, which fails before this change: passes with
the default logits tolerance.
- `pre-commit run --files tests/unit/v1/autotp/test_autotp_training.py`:
passed.

No L40S is available locally. This PR's merge-queue run selects this
test file, so it will check the fix on Modal's L40S runners unless the
run falls back to AWS.

---------

Signed-off-by: Hongwei Chen <hongweichen@microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@delock
delock added this pull request to the merge queue Oct 10, 2026
Merged via the queue into deepspeedai:master with commit 18f9282 Oct 10, 2026
14 checks passed
@delock
delock deleted the fix/shm-isa-guard branch October 10, 2026 09:55
banxingmjj pushed a commit to openanolis/DeepSpeed that referenced this pull request Oct 16, 2026
## What

Adds the `world_size` axis to CI, which the current suite does not
cover:

| File | Purpose |
|---|---|
| `.github/workflows/cpu-torch-multi-latest.yml` | Nightly + manual
dispatch: runs the multi-rank CPU tests on a 4-vCPU runner |
| `.github/workflows/cpu-multi-triage.yml` | Routes its outcome through
the existing `ci/nightly_classify.sh` / `ci/nightly_report.sh` protocol
|
| `tests/conftest.py` | The `DS_MULTIRANK_ONLY` collection hook the
workflow selects with (required) |
| `.github/workflows/TEST_SELECTION.md` | Registration of the new
nightly |

## Why

`cpu-torch-latest` advertises a single device, so the per-device gate in
`tests/unit/common.py` skips **every `world_size > 1` test** on that
runner. The
multi-rank path is therefore only exercised on GPU machines, and issues
that show up
there only in a multi-rank configuration go unnoticed.

A recent example: PR deepspeedai#8638 changed the overflow semantics so that
non-finite
group norms force an optimizer step to be skipped, bypassing
`bf16.check_grad_overflow: False`.
`TestZeROBFloat16::test_ignore_grad_overflow` is
`world_size = 2`, so it is collected and *skipped* by the CPU CI and by
any run with
one device; it fails as soon as two ranks execute. Reported as issue
deepspeedai#8805.

## Why a nightly rather than a pre-merge check

The selection is cheap to state and expensive to run (measured in deepspeedai#8381,
4-vCPU
runner):

| Run | Wall time |
|---|---|
| This suite: non-v1 multi-rank half | 68-89 min |
| This suite: `sequential`-marked phase | 13-16 min |
| Longest current pre-merge job (`cpu-torch-latest`) | 39-43 min |
| Longest merge-queue job (`modal-torch-latest`) | 64-71 min |

Putting it in the pre-merge path would roughly triple the critical path
of every PR,
and a single wedged rank would block the merge queue (the suite's known
hang — the
shm op segfaulting on hosts without AVX512 — is fixed by deepspeedai#8701, but the
suite is the
most expensive one to re-run).

## What it runs

- `LOCAL_SIZE=4` so the gate admits `world_size <= 4`;
`DS_MULTIRANK_ONLY=1` deselects
  the single-rank tests that `cpu-torch-latest` already guards.
- Non-v1 half, `timeout 120m`, plus a `sequential`-marked phase,
`timeout 25m` (the
parallel phase deselects those by default, so they would otherwise never
run here).
- Same preset/install/HF-predownload steps as `cpu-torch-latest`, so
both runs agree on
  the environment.
- On failure the last step prints `DS_CI_FAILURE_CLASS=timeout|test` for
triage; a run
that dies before that step is reported as unclassified rather than
guessed.

Triage is a separate workflow with its own issue titles and its own
`nightly-cpu-multi-last-green` tag, so `nightly-last-green` keeps
meaning "the last
revision the full GPU suite verified" and the modal issue dedupe is
untouched.

## Validation

- Workflow YAML parses; `pre-commit` clean.
- Timings above come from the real runs of this configuration in deepspeedai#8381.
- Not yet run as `cpu-torch-multi-latest`: GitHub only exposes
`workflow_dispatch` for
workflows present on the default branch, so the first manual dispatch
has to happen
after this merges. Happy to run it then and follow up with the numbers.

---------

Signed-off-by: Guokai Ma <guokai.ma@intel.com>
Signed-off-by: Ma, Guokai <guokai.ma@intel.com>
@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 14, 2026
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. will-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants