Skip to content

Add block-scaled (mxfp8) fused all-gather matmul - #192554

Closed
anijain2305 wants to merge 5 commits into
gh/anijain2305/1292/basefrom
gh/anijain2305/1292/head
Closed

anijain2305 wants to merge 5 commits into
gh/anijain2305/1292/basefrom
gh/anijain2305/1292/head

Conversation

@anijain2305

@anijain2305 anijain2305 commented Aug 7, 2026 •

Copy link
Copy Markdown
Contributor

Stack from ghstack (oldest at bottom):

The existing symm_mem::fused_all_gather_scaled_matmul dispatches to
aten::_scaled_mm, whose signature carries only a pair of scale tensors and so
can express tensor-wise and row-wise scaling but nothing block-scaled. The
Blackwell block-scaling kernels sit behind aten::_scaled_mm_v2, which takes a
recipe and swizzle per operand. This adds a v2 fused op that dispatches there,
making mxfp8 (ScalingType.BlockWise1x32) usable with async TP.

The interesting part is why gathering the scale is sound at all. The scale is
handed to the op already in the kernel's swizzled layout, and a swizzled buffer
is not obviously row-concatenable: if the swizzle interleaved rows across the
whole tensor, gathering per-rank swizzled scales would not reproduce the swizzle
of the gathered scale. It turns out the cuBLAS block-scaling layout tiles the
scale matrix in 128 rows by 4 columns and swizzles only within a tile, so
concatenation is exact provided every rank owns a whole number of row tiles.
Ranks that do not would each pad up to a tile boundary independently, which
changes the buffer size rather than silently corrupting values. The op therefore
requires gather_dim=0 and a per-rank row count that is a multiple of 128, and
rejects anything else up front.

Two smaller notes. Block-wise scaling cannot be recognised from the scale's
shape the way the other modes can, since the swizzled buffer is opaque, so the
mode is passed into the shared implementation by the caller rather than
inferred; _check_and_verify_fp8_all_gather_scale_mode is unchanged and the
existing modes take exactly the path they did before. And NCCL rejects
Float8_e8m0fnu, so the reference implementation gathers the scale through a
uint8 view; the fused path needs no such detour because symmetric memory moves
raw bytes.

This is a separate operator rather than an extension of the existing schema.
Adding optional recipe arguments to fused_all_gather_scaled_matmul was
considered and rejected: an omitted recipe would have to mean "v1 semantics", so
the default would silently be the slow path and a caller who did not know to ask
for a block recipe would quietly lose the Blackwell kernels. A distinct operator
makes the choice explicit at the call site instead. It is named for the
capability rather than a version, since "v2" invites exactly that ambiguity.

The scale swizzle is deliberately not an argument. The block-scaling kernels
accept exactly one layout per platform -- ScaledBlas.cpp requires SWIZZLE_32_4_4
on NVIDIA and NO_SWIZZLE on ROCm, rejecting anything else -- so it is derived
from the platform. Exposing it would only create a way to be wrong.

Migrating the tensor-wise and row-wise modes onto _scaled_mm_v2 as well, and
folding all of this back into one operator, is possible in principle but is not
attempted here: the row-wise path is currently broken on the SM103 device
available for testing (both the v1 and v2 spellings select an SM90 kernel and
fail with "Trying to use TMA Descriptor Prefetch without
CUTE_ARCH_TMA_SM90_ENABLED"), so the equivalence cannot be demonstrated.

Inductor micro-pipelining is not wired up to the new operator; callers invoke it
directly for now.

Test Plan:

New tests compare the fused operator against the unfused fallback (exact
equality: both gather the same bytes and hand the same operands to the same
kernel) and check that each guard rejects its case.

python -m pytest test/distributed/test_symmetric_memory.py -k scaled_matmul_v2 -q
5 passed, 135 deselected

Full file on 4x GB300, to confirm nothing else moved:

python -m pytest test/distributed/test_symmetric_memory.py -q -p no:randomly
51 passed, 87 skipped, 2 failed

Both failures reproduce on a clean checkout of the parent commit and are
unrelated to this change: test_fused_all_gather_scaled_matmul with
row-wise-replicated scaling fails because aten::_scaled_mm's row-wise path
selects an SM90 kernel on this SM103 device ("Trying to use TMA Descriptor
Prefetch without CUTE_ARCH_TMA_SM90_ENABLED"), which reproduces with a bare
aten::_scaled_mm call outside any distributed code; and
test_custom_op_symm_mem_realization passes in isolation, failing only as a
same-class cascade after the first failure.

lintrunner reports the same 24 pre-existing PYREFLY errors in
_symmetric_memory/init.py before and after, and none in the test file.

Authored with assistance from Claude Code.

[ghstack-poisoned]
@pytorch-bot

pytorch-bot Bot commented Aug 7, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/192554

Note: Links to docs will display an error until the docs builds have been completed.

✅ No Failures

As of commit 3b8cda4 with merge base 2942118 (image):
💚 Looks good so far! There are no failures yet. 💚

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@pytorch-bot pytorch-bot Bot added ciflow/h100-symm-mem ciflow/torchtitan Run TorchTitan integration tests labels Aug 7, 2026
@pytorch-bot

pytorch-bot Bot commented Aug 7, 2026

Copy link
Copy Markdown

This PR needs a release notes: label

If your changes are user facing and intended to be a part of release notes, please use a label starting with release notes:.

If not, please add the topic: not user facing label.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "topic: not user facing"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Aug 9, 2026
The existing symm_mem::fused_all_gather_scaled_matmul dispatches to
aten::_scaled_mm, whose signature carries only a pair of scale tensors and so
can express tensor-wise and row-wise scaling but nothing block-scaled. The
Blackwell block-scaling kernels sit behind aten::_scaled_mm_v2, which takes a
recipe and swizzle per operand. This adds a v2 fused op that dispatches there,
making mxfp8 (ScalingType.BlockWise1x32) usable with async TP.

The interesting part is why gathering the scale is sound at all. The scale is
handed to the op already in the kernel's swizzled layout, and a swizzled buffer
is not obviously row-concatenable: if the swizzle interleaved rows across the
whole tensor, gathering per-rank swizzled scales would not reproduce the swizzle
of the gathered scale. It turns out the cuBLAS block-scaling layout tiles the
scale matrix in 128 rows by 4 columns and swizzles only within a tile, so
concatenation is exact provided every rank owns a whole number of row tiles.
Ranks that do not would each pad up to a tile boundary independently, which
changes the buffer size rather than silently corrupting values. The op therefore
requires gather_dim=0 and a per-rank row count that is a multiple of 128, and
rejects anything else up front.

Two smaller notes. Block-wise scaling cannot be recognised from the scale's
shape the way the other modes can, since the swizzled buffer is opaque, so the
mode is passed into the shared implementation by the caller rather than
inferred; _check_and_verify_fp8_all_gather_scale_mode is unchanged and the
existing modes take exactly the path they did before. And NCCL rejects
Float8_e8m0fnu, so the reference implementation gathers the scale through a
uint8 view; the fused path needs no such detour because symmetric memory moves
raw bytes.

This is a separate operator rather than an extension of the existing schema.
Adding optional recipe arguments to fused_all_gather_scaled_matmul was
considered and rejected: an omitted recipe would have to mean "v1 semantics", so
the default would silently be the slow path and a caller who did not know to ask
for a block recipe would quietly lose the Blackwell kernels. A distinct operator
makes the choice explicit at the call site instead. It is named for the
capability rather than a version, since "v2" invites exactly that ambiguity.

The scale swizzle is deliberately not an argument. The block-scaling kernels
accept exactly one layout per platform -- ScaledBlas.cpp requires SWIZZLE_32_4_4
on NVIDIA and NO_SWIZZLE on ROCm, rejecting anything else -- so it is derived
from the platform. Exposing it would only create a way to be wrong.

Migrating the tensor-wise and row-wise modes onto _scaled_mm_v2 as well, and
folding all of this back into one operator, is possible in principle but is not
attempted here: the row-wise path is currently broken on the SM103 device
available for testing (both the v1 and v2 spellings select an SM90 kernel and
fail with "Trying to use TMA Descriptor Prefetch without
CUTE_ARCH_TMA_SM90_ENABLED"), so the equivalence cannot be demonstrated.

Inductor micro-pipelining is not wired up to the new operator; callers invoke it
directly for now.

Test Plan:

New tests compare the fused operator against the unfused fallback (exact
equality: both gather the same bytes and hand the same operands to the same
kernel) and check that each guard rejects its case.

```
python -m pytest test/distributed/test_symmetric_memory.py -k scaled_matmul_v2 -q
```
```
5 passed, 135 deselected
```

Full file on 4x GB300, to confirm nothing else moved:

```
python -m pytest test/distributed/test_symmetric_memory.py -q -p no:randomly
```
```
51 passed, 87 skipped, 2 failed
```

Both failures reproduce on a clean checkout of the parent commit and are
unrelated to this change: test_fused_all_gather_scaled_matmul with
row-wise-replicated scaling fails because aten::_scaled_mm's row-wise path
selects an SM90 kernel on this SM103 device ("Trying to use TMA Descriptor
Prefetch without CUTE_ARCH_TMA_SM90_ENABLED"), which reproduces with a bare
aten::_scaled_mm call outside any distributed code; and
test_custom_op_symm_mem_realization passes in isolation, failing only as a
same-class cascade after the first failure.

lintrunner reports the same 24 pre-existing PYREFLY errors in
_symmetric_memory/__init__.py before and after, and none in the test file.

Authored with assistance from Claude Code.

ghstack-source-id: 6b1179d
Pull-Request: #192554
[ghstack-poisoned]
anijain2305 added a commit that referenced this pull request Aug 9, 2026
The existing symm_mem::fused_all_gather_scaled_matmul dispatches to
aten::_scaled_mm, whose signature carries only a pair of scale tensors and so
can express tensor-wise and row-wise scaling but nothing block-scaled. The
Blackwell block-scaling kernels sit behind aten::_scaled_mm_v2, which takes a
recipe and swizzle per operand. This adds a v2 fused op that dispatches there,
making mxfp8 (ScalingType.BlockWise1x32) usable with async TP.

The interesting part is why gathering the scale is sound at all. The scale is
handed to the op already in the kernel's swizzled layout, and a swizzled buffer
is not obviously row-concatenable: if the swizzle interleaved rows across the
whole tensor, gathering per-rank swizzled scales would not reproduce the swizzle
of the gathered scale. It turns out the cuBLAS block-scaling layout tiles the
scale matrix in 128 rows by 4 columns and swizzles only within a tile, so
concatenation is exact provided every rank owns a whole number of row tiles.
Ranks that do not would each pad up to a tile boundary independently, which
changes the buffer size rather than silently corrupting values. The op therefore
requires gather_dim=0 and a per-rank row count that is a multiple of 128, and
rejects anything else up front.

Two smaller notes. Block-wise scaling cannot be recognised from the scale's
shape the way the other modes can, since the swizzled buffer is opaque, so the
mode is passed into the shared implementation by the caller rather than
inferred; _check_and_verify_fp8_all_gather_scale_mode is unchanged and the
existing modes take exactly the path they did before. And NCCL rejects
Float8_e8m0fnu, so the reference implementation gathers the scale through a
uint8 view; the fused path needs no such detour because symmetric memory moves
raw bytes.

This is a separate operator rather than an extension of the existing schema.
Adding optional recipe arguments to fused_all_gather_scaled_matmul was
considered and rejected: an omitted recipe would have to mean "v1 semantics", so
the default would silently be the slow path and a caller who did not know to ask
for a block recipe would quietly lose the Blackwell kernels. A distinct operator
makes the choice explicit at the call site instead. It is named for the
capability rather than a version, since "v2" invites exactly that ambiguity.

The scale swizzle is deliberately not an argument. The block-scaling kernels
accept exactly one layout per platform -- ScaledBlas.cpp requires SWIZZLE_32_4_4
on NVIDIA and NO_SWIZZLE on ROCm, rejecting anything else -- so it is derived
from the platform. Exposing it would only create a way to be wrong.

Migrating the tensor-wise and row-wise modes onto _scaled_mm_v2 as well, and
folding all of this back into one operator, is possible in principle but is not
attempted here: the row-wise path is currently broken on the SM103 device
available for testing (both the v1 and v2 spellings select an SM90 kernel and
fail with "Trying to use TMA Descriptor Prefetch without
CUTE_ARCH_TMA_SM90_ENABLED"), so the equivalence cannot be demonstrated.

Inductor micro-pipelining is not wired up to the new operator; callers invoke it
directly for now.

Test Plan:

New tests compare the fused operator against the unfused fallback (exact
equality: both gather the same bytes and hand the same operands to the same
kernel) and check that each guard rejects its case.

```
python -m pytest test/distributed/test_symmetric_memory.py -k scaled_matmul_v2 -q
```
```
5 passed, 135 deselected
```

Full file on 4x GB300, to confirm nothing else moved:

```
python -m pytest test/distributed/test_symmetric_memory.py -q -p no:randomly
```
```
51 passed, 87 skipped, 2 failed
```

Both failures reproduce on a clean checkout of the parent commit and are
unrelated to this change: test_fused_all_gather_scaled_matmul with
row-wise-replicated scaling fails because aten::_scaled_mm's row-wise path
selects an SM90 kernel on this SM103 device ("Trying to use TMA Descriptor
Prefetch without CUTE_ARCH_TMA_SM90_ENABLED"), which reproduces with a bare
aten::_scaled_mm call outside any distributed code; and
test_custom_op_symm_mem_realization passes in isolation, failing only as a
same-class cascade after the first failure.

lintrunner reports the same 24 pre-existing PYREFLY errors in
_symmetric_memory/__init__.py before and after, and none in the test file.

Authored with assistance from Claude Code.

ghstack-source-id: d93665d
Pull-Request: #192554
@anijain2305 anijain2305 closed this Sep 4, 2026
@github-actions
github-actions Bot deleted the gh/anijain2305/1292/head branch November 2, 2026 01:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/h100-symm-mem ciflow/torchtitan Run TorchTitan integration tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant