Skip to content

Commit a4a0974

Browse files
authored
fix(moe): support Kimi K3 DeepGEMM gather shape (#1747)
1 parent 6586b28 commit a4a0974

3 files changed

Lines changed: 42 additions & 28 deletions

File tree

‎aphrodite/model_executor/layers/fused_moe/deep_gemm_utils.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
and updated to fit aphrodite needs and terminology.
66
"""
77

8+
import math
9+
810
import torch
911

1012
import aphrodite.model_executor.layers.fused_moe.modular_kernel as mk
@@ -381,7 +383,7 @@ def ep_gather(
381383
num_warps = 2
382384
num_tokens = output_tensor.shape[0]
383385
hidden_size = input_tensor.shape[1]
384-
BLOCK_D = min(hidden_size, 1024)
386+
BLOCK_D = math.gcd(hidden_size, 1024)
385387
assert hidden_size % BLOCK_D == 0
386388
grid = (triton.cdiv(hidden_size, BLOCK_D), min(num_tokens, 1024))
387389

‎aphrodite/models/kimi_k3/nvidia/ops/attn_res.py‎

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
# Copyright (c) 2023-2026, Songlin Yang, Yu Zhang, Zhiyuan Li
99

1010

11+
import time
12+
1113
import torch
1214

1315
from aphrodite import _custom_ops as ops
@@ -192,31 +194,40 @@ def attn_res(
192194
block_l, num_warps = 1, 4
193195
else:
194196
block_l, num_warps = 4, 8
195-
_attn_res_kernel[(num_tokens,)](
196-
prefix,
197-
delta,
198-
blocks,
199-
norm_weight,
200-
qk_weight,
201-
output_norm_weight,
202-
output,
203-
prefix.stride(0),
204-
0 if delta is None else delta.stride(0),
205-
blocks.stride(0),
206-
blocks.stride(1),
207-
output.stride(0),
208-
num_blocks,
209-
hidden_size,
210-
block_write_idx,
211-
eps,
212-
output_norm_eps,
213-
HAS_DELTA=delta is not None,
214-
WRITE_BLOCK=block_write_idx >= 0,
215-
APPLY_OUTPUT_NORM=output_norm_weight is not None,
216-
BLOCK_L=block_l,
217-
BLOCK_D=triton.next_power_of_2(hidden_size),
218-
num_warps=num_warps,
219-
num_stages=2,
220-
launch_pdl=current_platform.is_arch_support_pdl(),
221-
)
197+
for attempt in range(3):
198+
try:
199+
_attn_res_kernel[(num_tokens,)](
200+
prefix,
201+
delta,
202+
blocks,
203+
norm_weight,
204+
qk_weight,
205+
output_norm_weight,
206+
output,
207+
prefix.stride(0),
208+
0 if delta is None else delta.stride(0),
209+
blocks.stride(0),
210+
blocks.stride(1),
211+
output.stride(0),
212+
num_blocks,
213+
hidden_size,
214+
block_write_idx,
215+
eps,
216+
output_norm_eps,
217+
HAS_DELTA=delta is not None,
218+
WRITE_BLOCK=block_write_idx >= 0,
219+
APPLY_OUTPUT_NORM=output_norm_weight is not None,
220+
BLOCK_L=block_l,
221+
BLOCK_D=triton.next_power_of_2(hidden_size),
222+
num_warps=num_warps,
223+
num_stages=2,
224+
launch_pdl=current_platform.is_arch_support_pdl(),
225+
)
226+
break
227+
except FileNotFoundError:
228+
if attempt == 2:
229+
raise
230+
# Concurrent Triton compilation can briefly expose an incomplete
231+
# cache entry on filesystems without fully atomic replacement.
232+
time.sleep(0.1 * (attempt + 1))
222233
return output

‎tests/kernels/moe/test_deepgemm.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ def run_single_fp4_case(m, n, k, topk, num_experts):
366366
FP4_MNKs = [
367367
(128, 4096, 4096), # DeepSeek V4 shape
368368
(256, 2048, 2048), # Half-size variant
369+
(128, 384, 3584), # Kimi-K3 TP8 latent-MoE shape
369370
]
370371

371372
FP4_TOPKS = [2]

0 commit comments

Comments
 (0)