Skip to content

Commit 5000a7d

Browse files
committed
perf(pt/dpa4): triton gemm
1 parent db575aa commit 5000a7d

5 files changed

Lines changed: 595 additions & 4 deletions

File tree

deepmd/pt/model/descriptor/sezm_nn/so2.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,24 @@ def __init__(
344344
# Each |m| group occupies a contiguous (in, out) block on the diagonal.
345345
self._block_diag_slices = self._build_block_diag_slices()
346346

347+
# Inference fast path (opt-in via ``DP_TRITON_INFER``): the per-|m|-block
348+
# batched bmm + cat of _block_diagonal_matmul is replaced by a fused
349+
# Triton BN=64 block-diagonal GEMM that consumes the strided operands
350+
# without a contiguity copy. Bound only when Triton is available and every
351+
# block width aligns to BN=64; otherwise the eager path is kept.
352+
self._block_diag_gemm = None
353+
if use_triton_infer():
354+
from .triton.so2_block_gemm import (
355+
SO2_BLOCK_GEMM_TRITON_AVAILABLE,
356+
block_diag_gemm,
357+
slices_supported,
358+
)
359+
360+
if SO2_BLOCK_GEMM_TRITON_AVAILABLE and slices_supported(
361+
self._block_diag_slices
362+
):
363+
self._block_diag_gemm = block_diag_gemm
364+
347365
def forward(self, x: torch.Tensor) -> torch.Tensor:
348366
"""
349367
Parameters
@@ -522,6 +540,8 @@ def _block_diagonal_matmul(
522540
Flattened output with shape ``(F, E, D_m*Cout)``.
523541
"""
524542
weight = weight.permute(1, 0, 2) # (F, D_m*Cin, D_m*Cout)
543+
if self._block_diag_gemm is not None and not self.training:
544+
return self._block_diag_gemm(x_flat, weight, self._block_diag_slices)
525545
blocks = [
526546
torch.bmm(
527547
x_flat[:, :, in0:in1],

deepmd/pt/model/descriptor/sezm_nn/triton/__init__.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,21 @@
99
from .radial_mix import (
1010
RADIAL_MIX_TRITON_AVAILABLE,
1111
)
12+
from .so2_block_gemm import (
13+
SO2_BLOCK_GEMM_TRITON_AVAILABLE,
14+
)
1215
from .so2_rotation import (
1316
TRITON_ROTATION_AVAILABLE,
1417
)
1518

16-
# Both kernel modules guard their ``@triton.jit`` definitions behind a ``triton``
17-
# import, so the two module-level checks are equivalent. Expose a single
19+
# Every kernel module guards its ``@triton.jit`` definitions behind a ``triton``
20+
# import, so the module-level checks are equivalent. Expose a single
1821
# package-level availability flag.
19-
TRITON_AVAILABLE = TRITON_ROTATION_AVAILABLE and RADIAL_MIX_TRITON_AVAILABLE
22+
TRITON_AVAILABLE = (
23+
TRITON_ROTATION_AVAILABLE
24+
and RADIAL_MIX_TRITON_AVAILABLE
25+
and SO2_BLOCK_GEMM_TRITON_AVAILABLE
26+
)
2027

2128
__all__ = [
2229
"TRITON_AVAILABLE",

0 commit comments

Comments
 (0)