Skip to content

[Common][PyTorch] Add strided batched GEMM in BF16/MXFP8 - #3160

Merged
timmoon10 merged 14 commits into
NVIDIA:mainfrom
yaox12:xiny/batch_gemm
Nov 2, 2026
Merged

timmoon10 merged 14 commits into
NVIDIA:mainfrom
yaox12:xiny/batch_gemm

Conversation

@yaox12

@yaox12 yaox12 commented Jul 1, 2026 •

Copy link
Copy Markdown
Member

Description

This PR adds experimental cuBLASLt-backed strided batched GEMM support and a new fusible PyTorch operation exported as transformer_engine.pytorch.ops.BatchedLinear.

The main use case is applying one weight matrix per group without rearranging interleaved activations:

[S, B, G, D] @ [G, R, D] -> [S, B, G, R]

Both contiguous [G, ..., D] and [..., G, D] input layouts are supported through explicit leading dimensions and batch strides. MXFP8 scales are packed into the cuBLASLt per-batch layout internally before each GEMM.

Dependency

This PR depends on #3327 for meta-device deferred initialization in the operation fuser and should be merged after it.

Type of change

  • Documentation change (change only to the documentation, either a fix or a new content)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Infra/Build change
  • Code refactoring

Changes

  • Add an experimental common API for cuBLASLt strided batched GEMM with independent matrix strides.
  • Support high-precision and MXFP8 input pairs with high-precision output.
  • Add transformer_engine.pytorch.ops.BatchedLinear with high-precision and MXFP8 forward and backward computation.
  • Support primary MXFP8 weights, bias and return_bias, save_original_input, custom initialization and RNG tracking, and main-gradient accumulation.
  • Add API documentation and PyTorch L0 tests.

Current limitations

  • A and B must both be high precision or both be MXFP8.
  • MXFP8 operands must use contiguous [G, ..., D] or [..., G, D] layouts.
  • Output quantization, fused GEMM epilogues, non-MXFP8 recipes, and tensor parallelism are not supported.

Checklist

  • I have read and followed the contributing guidelines
  • The functionality is complete
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

@greptile-apps

greptile-apps Bot commented Jul 1, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The latest change appears safe to merge and addresses the previous short-input finding.

Summary

Adds strided batched GEMM and BatchedLinear for applying separate weights to contiguous or interleaved groups.

  • Batched GEMM handles groups at the start or inside a tensor.
  • BatchedLinear applies a separate linear transform to each group.

Reviews (15) · Last reviewed commit: "Check MXFP8 row count in BatchedLinear" · Reviewed by Greptile

@yaox12

yaox12 commented Jul 6, 2026

Copy link
Copy Markdown
Member Author

/te-ci

@timmoon10 timmoon10 left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Mathematically, is this equivalent to GroupedLinear with uniform split sizes (m_splits=[S*B] * G)?

If so, are there perf reasons to support a dedicated kernel? I could imagine that batched GEMM is easier to optimize than grouped GEMM, but grouped GEMM should already be getting a good fraction of the peak compute throughput. I would like to make sure we have a compelling benefit before accepting a major maintenance burden (on the order of Linear and GroupedLinear, especially if we need to support all the weird Mcore-specific integrations).

Signed-off-by: Xin Yao <xiny@nvidia.com>
Signed-off-by: Xin Yao <xiny@nvidia.com>
Signed-off-by: Xin Yao <xiny@nvidia.com>
@greptile-apps

greptile-apps Bot commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

Comments Outside Diff

These findings could not be posted inline.

  • P1 cuBLASLt handle leak on any API failure after partial descriptor creation transformer_engine/common/gemm/cublaslt_gemm.cu:1148 ▶

    Adesc, Bdesc, Cdesc, Ddesc, operationDesc, and preference are created incrementally. If any NVTE_CHECK_CUBLAS call after the first cublasLtMatrixLayoutCreate throws - for example in set_strided_batch, in the MXFP8 cublasLtMatmulDescSetAttribute block, in cublasLtMatmulAlgoGetHeuristic, or in cublasLtMatmul - the already-allocated handles are never passed to their corresponding Destroy functions. The cleanup block at the bottom of the function is only reached on the happy path.

    The MXFP8 version checks at the top of the function were correctly moved before descriptor creation, so those specific paths are safe. The leak window is every NVTE_CHECK_CUBLAS call from the first descriptor creation onward. Adding RAII wrappers (or a try/catch with a cleanup lambda) to the new function would prevent the problem here.

  • P2 C-matrix always aliased to D - undocumented design restriction transformer_engine/pytorch/cpp_extensions/gemm.py:1491 ▶

    The public C-level API nvte_cublas_gemm_strided_batched accepts independent C, ldc, and stridec parameters (useful for a fused D = A*B + beta*C with distinct accumulation buffers). This Python wrapper always passes out for both C and D, permanently losing that flexibility. A comment in the docstring noting that accumulation is always done in-place (beta is applied to out itself) would prevent surprises for future callers who discover the separate C parameter in the C++ header.

    Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Comment thread tests/pytorch/test_batched_linear.py Resolved
Comment thread tests/pytorch/test_fusible_ops.py Resolved
Comment thread tests/pytorch/test_fusible_ops.py Resolved
Comment thread tests/pytorch/test_fusible_ops.py Resolved
Replace the explicit m/n/k/ld/stride arguments of strided batched GEMM
with per-tensor batch dims, and fall back to high-precision GEMM for
quantization formats other than MXFP8. Rename BatchedLinear arguments
to batch_size and input_batch_dim.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Xin Yao <xiny@nvidia.com>
Reshape BatchedLinear inputs and outputs to 3D before GEMM so that 2D
MXFP8 data with batch_dim=-2 is only ever the [rows, batch*cols] view
used for column-wise quantization. Only require MXFP8 row counts
divisible by 32 when the weight gradient is needed, and document that
2D [batch, cols] tensors should use batch_dim=0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Xin Yao <xiny@nvidia.com>
@yaox12

yaox12 commented Oct 27, 2026

Copy link
Copy Markdown
Member Author

@timmoon10 Can you give another round of review? Thank you.

@timmoon10 timmoon10 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Mostly LGTM. I have some minor suggestions, mostly about the tests.

Comment thread tests/pytorch/test_fusible_ops.py Resolved
Comment thread tests/pytorch/test_fusible_ops.py Resolved
Comment thread tests/pytorch/test_batched_linear.py Resolved
Comment thread tests/pytorch/test_batched_linear.py Resolved
Comment thread tests/pytorch/test_batched_linear.py Resolved
Comment thread tests/pytorch/test_batched_linear.py Resolved
Comment thread tests/pytorch/test_batched_linear.py Resolved
Co-authored-by: Tim Moon <4406448+timmoon10@users.noreply.github.com>
Signed-off-by: Tim Moon <4406448+timmoon10@users.noreply.github.com>
@timmoon10

Copy link
Copy Markdown
Member

/te-ci

yaox12 and others added 2 commits November 2, 2026 02:01
Use DIVUP_TO_MULTIPLE in the batched scale packing kernels since
round_up_to_multiple was removed upstream. Replace the strided batched
GEMM tests with API contract tests over layouts, batch dims, and
quantization recipes, and drop the BatchedLinear contiguity check and
redundant tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Xin Yao <xiny@nvidia.com>
MXFP8 quantization requires the flattened row count to be divisible by
32, so check batch_size * rows before quantizing instead of failing
inside the quantizer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Xin Yao <xiny@nvidia.com>
@yaox12

yaox12 commented Nov 2, 2026

Copy link
Copy Markdown
Member Author

/te-ci

@timmoon10
timmoon10 merged commit b021980 into NVIDIA:main Nov 2, 2026
33 of 38 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants