[Common][PyTorch] Add strided batched GEMM in BF16/MXFP8 - #3160
Conversation
Greptile SummaryThis PR adds experimental cuBLASLt-backed strided batched GEMM support (BF16 and MXFP8) and exports it as
Confidence Score: 5/5
Important Files Changed
Sequence DiagramsequenceDiagram
participant PY as BatchedLinear.op_forward
participant GEM as strided_batched_gemm (Python)
participant CPP as strided_batched_gemm (C++)
participant PACK as pack_mxfp8_operand_scales
participant SWIZ as nvte_pack_mxfp8_scales_for_batched_gemm
participant CUBLASLT as cublas_gemm_strided_batched
PY->>PY: _quantize_for_batched_gemm(x, w) → compact MXFP8
PY->>GEM: "strided_batched_gemm(A, B, out, layout="TN", ...)"
GEM->>CPP: tex.strided_batched_gemm(...)
CPP->>CPP: detect MXFP8 inputs
CPP->>PACK: "pack_mxfp8_operand_scales(A, rowwise=transa, ...)"
PACK->>SWIZ: nvte_pack_mxfp8_scales_for_batched_gemm(compact→swizzled)
SWIZ-->>PACK: packed scale buffer [G×padded_rows×padded_scale_cols]
PACK-->>CPP: "PackedMXFP8Operand{tensor, scale_inv}"
CPP->>PACK: "pack_mxfp8_operand_scales(B, rowwise=!transb, ...)"
PACK->>SWIZ: nvte_pack_mxfp8_scales_for_batched_gemm / columnwise
SWIZ-->>PACK: packed scale buffer
PACK-->>CPP: "PackedMXFP8Operand{tensor, scale_inv}"
CPP->>CUBLASLT: nvte_cublas_gemm_strided_batched(packed_A, packed_B, ...)
CUBLASLT->>CUBLASLT: CublasLtDescriptorCleanup RAII guard
CUBLASLT->>CUBLASLT: CheckStridedBatchBuffer(A,B,C,D)
CUBLASLT->>CUBLASLT: CheckMXFP8BatchScaleBuffer(A,B)
CUBLASLT->>CUBLASLT: cublasLtMatmul(strided-batch)
CUBLASLT-->>CPP: output D (BF16)
CPP-->>GEM: D tensor
GEM-->>PY: output
PY->>PY: _apply_bias(output)
Reviews (7): Last reviewed commit: "add save_original_input" | Re-trigger Greptile |
|
/te-ci |
There was a problem hiding this comment.
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>
583e536 to
c09a325
Compare
Signed-off-by: Xin Yao <xiny@nvidia.com>
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:
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
Changes
transformer_engine.pytorch.ops.BatchedLinearwith high-precision and MXFP8 forward and backward computation.return_bias,save_original_input, custom initialization and RNG tracking, and main-gradient accumulation.Current limitations
[G, ..., D]or[..., G, D]layouts.Checklist