Read @qwen3_moe_fused/grouped_gemm/forward.py for the reference impl of the grouped GEMM kernel, and @qwen3_moe_fused/grouped_gemm/helion/forward.py @qwen3_moe_fused/grouped_gemm/helion/kernel_generated.py for the kernal generated by Helion. Does the Helion generated kernel have any functionality difference from the reference impl? Does it have any undefined behavior? Why does it fail in the test and show mean_atol=1.03e+38 max_atol=1.03e+38?
--- Content from referenced files ---
Content from @qwen3_moe_fused/grouped_gemm/forward.py:
y[m, n] = sum_k w[s[m], n, k] * x[m, k]
from functools import partial
from typing import Optional
import torch
import triton
import triton.language as tl
from .autotuning import (
GRID_FACTOR,
get_autotune_configs,
get_autotune_keys,
get_num_sms,
prune_configs,
)
def exceeds_smem_capacity(
num_stages: int,
BLOCK_SIZE_M: int,
BLOCK_SIZE_N: int,
BLOCK_SIZE_K: int,
dtype: torch.dtype,
smem_size: int,
) -> bool:
# Strix Halo crashes with some configs
if torch.version.hip and (BLOCK_SIZE_M >= 128 or BLOCK_SIZE_N >= 128 or BLOCK_SIZE_K >= 128):
return True
code
x_size = BLOCK_SIZE_M * BLOCK_SIZE_K * dtype.itemsize
w_size = BLOCK_SIZE_N * BLOCK_SIZE_K * dtype.itemsize
if num_stages <= 1:
size = max(x_size, w_size)
else:
# (num_stages - 1) stages of both tiles will be cached in smem
size = (num_stages - 1) * (x_size + w_size)
return size > smem_size
Based on the analysis of the provided files, here are the answers to your questions:
1. Functionality Difference
Yes, there is a critical logic error in the Helion implementation compared to the reference.
Reference: Accumulates the dot product correctly: .
# Lower bound and upper bound are defined relative to the total tiles processed so far
# This ensures that we are only processing tiles for the current expert group AND
# we never exceed the total number of tiles for all expert groups
while tidx >= processed_tiles and tidx < processed_tiles + num_tiles_per_expert:
tile_idx = tidx - processed_tiles
# Output tile for this thread block for this expert group
if LOOP_ORDER == 0:
tile_m_idx = tile_idx % num_m_tiles
tile_n_idx = tile_idx // num_m_tiles
else:
tile_m_idx = tile_idx // num_n_tiles
tile_n_idx = tile_idx % num_n_tiles
offs_k = tl.arange(0, BLOCK_SIZE_K)
offs_m = m_start + tile_m_idx * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)
x_ptrs = x_ptr + stride_xm * offs_m[:, None] + stride_xk * offs_k[None, :]
mask_m = offs_m < m_end
offs_n = tile_n_idx * BLOCK_SIZE_N + tl.arange(0, BLOCK_SIZE_N)
w_ptrs = w_ptr + stride_we * expert_idx + stride_wn * offs_n[:, None] + stride_wk * offs_k[None, :]
mask_n = offs_n < N
accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_N), dtype=tl.float32)
# GEMM main loop
for _ in range(tl.cdiv(K, BLOCK_SIZE_K)):
mask_k = offs_k < K
x = tl.load(x_ptrs, mask=mask_m[:, None] & mask_k[None, :])
w = tl.load(w_ptrs, mask=mask_n[:, None] & mask_k[None, :])
accumulator += tl.dot(x, w.T)
offs_k += BLOCK_SIZE_K
x_ptrs += stride_xk * BLOCK_SIZE_K
w_ptrs += stride_wk * BLOCK_SIZE_K
y = accumulator.to(y_ptr.dtype.element_ty)
y_ptrs = y_ptr + stride_ym * offs_m[:, None] + stride_yn * offs_n[None, :]
tl.store(y_ptrs, y, mask=mask_m[:, None] & mask_n[None, :])
# Move to the next tile within this expert group
tidx += NUM_SMS
# Update the total tiles count for the next expert group
processed_tiles += num_tiles_per_expert
If the input tensors passed to grouped_gemm_forward do not exactly match these dimensions (N=768, K=2048), the kernel will calculate incorrect memory addresses, reading/writing invalid memory (Out-Of-Bounds access).
Hardcoded Grid: The launcher is hardcoded to (128,).
_launcher(..., (128,), ...)
The kernel uses pid_0 directly as expert_idx. If the number of experts (E) is not exactly 128, it will either crash/read OOB (if E<128) or ignore experts (if E>128).
3. Reason for mean_atol=1.03e+38
The failure with massive values is caused by the double accumulation bug identified in point #1.
In every iteration of the inner loop (over K), the existing accumulator value is effectively doubled before adding the new partial product (acc = 2 * acc + product).
With K=2048 and BLOCK_SIZE_K=16 (from _BLOCK_SIZE_3 in generated code), the loop runs 128 times.
Doubling the accumulator 128 times results in a factor of 2128≈3.4×1038.
This matches the error magnitude 1.03e+38 perfectly.