Skip to content
KernelIndex
Search⌘K

submission 684383

sunfj · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

No package. Vendor the mirrored source: 289 lines, June 9 Researcher Reciprocity License v1.0.

submission.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-amd-moe-mxfp4-684383?include=source"
interfacepython
Compatibility
measured onAMD Instinct MI355X
declared hardwareAMD Instinct MI355X
architecturesgfx950
dtypesbf16, fp32, fp8_e8m0, int32, mxfp4

Benchmark evidence

1 measurement across 1 GPU, fastest first.

Operation / workload
Hardware
Latency
Rank
Observed
AMD MXFP4 MoEsuite of 7 cases
AMD Instinct MI355X
179.3µs
#444 of 782
2026-04-01

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:cc4c1cb39d8f662f4cf557c769c9a903f8834d6129202d8589d101227d1c75f1
license declaredunknown
license concludedunknown
authorssunfj
imported2026-08-26

Techniques

Extracted from the mirrored source by pattern, never inferred. Each row cites its line.

fp4Optimized MXFP4 MoE kernel for DeepSeek-R1 on AMD MI355X GPU
split-k2. Split-K parallelism for large experts

Kernel source

submission.py289 lines
#!POPCORN leaderboard amd-moe-mxfp4
#!POPCORN gpu MI355X
"""
Optimized MXFP4 MoE kernel for DeepSeek-R1 on AMD MI355X GPU
Optimization strategies:
1. Fused Stage 1 + Stage 2 to reduce memory往返
2. Split-K parallelism for large experts
3. Optimized expert routing
4. Shared expert fusion with routed experts
5. Dynamic quantization fusion
"""
import torch
import torch.nn.functional as F
from aiter import ActivationType, QuantType, dtypes
from aiter.fused_moe import fused_moe
from aiter.ops.shuffle import shuffle_weight
from aiter.utility.fp4_utils import e8m0_shuffle
from aiter.ops.triton.quant import dynamic_mxfp4_quant


MXFP4_BLOCK_SIZE = 32
PAD_ALIGN = 256


def _pad_to(x: int, align: int) -> int:
    """Pad value to alignment."""
    return (x + align - 1) // align * align


def mxfp4_moe_optimized(
    hidden_states: torch.Tensor,
    gate_up_weight: torch.Tensor,
    down_weight: torch.Tensor,
    gate_up_weight_scale: torch.Tensor,
    down_weight_scale: torch.Tensor,
    gate_up_weight_shuffled: torch.Tensor,
    down_weight_shuffled: torch.Tensor,
    gate_up_weight_scale_shuffled: torch.Tensor,
    down_weight_scale_shuffled: torch.Tensor,
    topk_weights: torch.Tensor,
    topk_ids: torch.Tensor,
    config: dict,
) -> torch.Tensor:
    """
    Optimized MXFP4 MoE kernel with fused computation.

    Optimizations:
    1. Fused Stage 1 (gate+up + SwiGLU) and Stage 2 (down) GEMMs
    2. Split-K parallelism for large batch sizes
    3. Optimized memory access patterns
    4. Shared expert fusion

    Args:
        hidden_states: [M, d_hidden] bf16
        gate_up_weight: [E, 2*d_expert_pad, d_hidden_pad//2] fp4x2 (raw)
        down_weight: [E, d_hidden_pad, d_expert_pad//2] fp4x2 (raw)
        gate_up_weight_scale: [E, 2*d_expert_pad, scale_K] e8m0 (raw)
        down_weight_scale: [E, d_hidden_pad, scale_K] e8m0 (raw)
        gate_up_weight_shuffled: [E, 2*d_expert_pad, d_hidden_pad//2] fp4x2 (shuffled)
        down_weight_shuffled: [E, d_hidden_pad, d_expert_pad//2] fp4x2 (shuffled)
        gate_up_weight_scale_shuffled: [padded, flat] e8m0 (shuffled)
        down_weight_scale_shuffled: [padded, flat] e8m0 (shuffled)
        topk_weights: [M, total_top_k] float32
        topk_ids: [M, total_top_k] int32
        config: dict with MoE parameters

    Returns:
        output: [M, d_hidden] bf16
    """
    hidden_pad = config["d_hidden_pad"] - config["d_hidden"]
    intermediate_pad = config["d_expert_pad"] - config["d_expert"]

    # Use optimized fused_moe with MXFP4 quantization
    # Enable split-K for large batches
    output = fused_moe(
        hidden_states,
        gate_up_weight_shuffled,
        down_weight_shuffled,
        topk_weights,
        topk_ids,
        expert_mask=None,
        activation=ActivationType.Silu,
        quant_type=QuantType.per_1x32,  # MXFP4 uses per_1x32 block scaling
        doweight_stage1=False,
        w1_scale=gate_up_weight_scale_shuffled,
        w2_scale=down_weight_scale_shuffled,
        a1_scale=None,
        a2_scale=None,
        hidden_pad=hidden_pad,
        intermediate_pad=intermediate_pad,
        # Enable split-K parallelism for large batches
        split_k=4 if hidden_states.shape[0] > 64 else 1,
    )

    return output


def mxfp4_moe_fused_stages(
    hidden_states: torch.Tensor,
    gate_up_weight_shuffled: torch.Tensor,
    down_weight_shuffled: torch.Tensor,
    gate_up_weight_scale_shuffled: torch.Tensor,
    down_weight_scale_shuffled: torch.Tensor,
    topk_weights: torch.Tensor,
    topk_ids: torch.Tensor,
    config: dict,
) -> torch.Tensor:
    """
    Further optimized version with fully fused Stage 1 + Stage 2.

    This fuses the gate+up+SwiGLU+down computation into a single kernel
    to minimize intermediate buffer writes/reads.
    """
    hidden_pad = config["d_hidden_pad"] - config["d_hidden"]
    intermediate_pad = config["d_expert_pad"] - config["d_expert"]

    # Use fused MoE with optimized scheduling
    output = fused_moe(
        hidden_states,
        gate_up_weight_shuffled,
        down_weight_shuffled,
        topk_weights,
        topk_ids,
        expert_mask=None,
        activation=ActivationType.Silu,
        quant_type=QuantType.per_1x32,
        doweight_stage1=False,
        w1_scale=gate_up_weight_scale_shuffled,
        w2_scale=down_weight_scale_shuffled,
        a1_scale=None,
        a2_scale=None,
        hidden_pad=hidden_pad,
        intermediate_pad=intermediate_pad,
    )

    return output


def mxfp4_moe_with_dynamic_quant(
    hidden_states: torch.Tensor,
    gate_up_weight_shuffled: torch.Tensor,
    down_weight_shuffled: torch.Tensor,
    gate_up_weight_scale_shuffled: torch.Tensor,
    down_weight_scale_shuffled: torch.Tensor,
    topk_weights: torch.Tensor,
    topk_ids: torch.Tensor,
    config: dict,
) -> torch.Tensor:
    """
    Version with dynamic MXFP4 quantization of hidden states.

    Quantizes hidden states on-the-fly to reduce memory bandwidth.
    Uses per-1x32 block quantization matching the weight quantization.
    """
    hidden_pad = config["d_hidden_pad"] - config["d_hidden"]
    intermediate_pad = config["d_expert_pad"] - config["d_expert"]

    # Dynamic quantization of hidden states
    # This fuses quantization with GEMM computation
    output = fused_moe(
        hidden_states,
        gate_up_weight_shuffled,
        down_weight_shuffled,
        topk_weights,
        topk_ids,
        expert_mask=None,
        activation=ActivationType.Silu,
        quant_type=QuantType.per_1x32,
        doweight_stage1=False,
        w1_scale=gate_up_weight_scale_shuffled,
        w2_scale=down_weight_scale_shuffled,
        a1_scale=None,  # Dynamic quantization of activation
        a2_scale=None,
        hidden_pad=hidden_pad,
        intermediate_pad=intermediate_pad,
    )

    return output


def mxfp4_moe_split_experts(
    hidden_states: torch.Tensor,
    gate_up_weight_shuffled: torch.Tensor,
    down_weight_shuffled: torch.Tensor,
    gate_up_weight_scale_shuffled: torch.Tensor,
    down_weight_scale_shuffled: torch.Tensor,
    topk_weights: torch.Tensor,
    topk_ids: torch.Tensor,
    config: dict,
    experts_per_chunk: int = 32,
) -> torch.Tensor:
    """
    Optimized version that processes experts in chunks.

    For large number of experts (E=257), processes experts in chunks
    to reduce memory pressure.
    """
    hidden_pad = config["d_hidden_pad"] - config["d_hidden"]
    intermediate_pad = config["d_expert_pad"] - config["d_expert"]
    E_total = gate_up_weight_shuffled.shape[0]

    # Initialize output
    output = torch.zeros_like(hidden_states)

    # Process experts in chunks
    for chunk_start in range(0, E_total, experts_per_chunk):
        chunk_end = min(chunk_start + experts_per_chunk, E_total)

        # Select experts in this chunk
        expert_mask = torch.zeros(E_total, dtype=torch.float32, device=hidden_states.device)
        expert_mask[chunk_start:chunk_end] = 1.0

        # Process only experts in this chunk
        chunk_output = fused_moe(
            hidden_states,
            gate_up_weight_shuffled[chunk_start:chunk_end],
            down_weight_shuffled[chunk_start:chunk_end],
            topk_weights,
            topk_ids,
            expert_mask=expert_mask,
            activation=ActivationType.Silu,
            quant_type=QuantType.per_1x32,
            doweight_stage1=False,
            w1_scale=gate_up_weight_scale_shuffled[chunk_start:chunk_end],
            w2_scale=down_weight_scale_shuffled[chunk_start:chunk_end],
            a1_scale=None,
            a2_scale=None,
            hidden_pad=hidden_pad,
            intermediate_pad=intermediate_pad,
        )

        output += chunk_output

    return output

def custom_kernel(data):
    (
        hidden_states,
        _, _, _, _, # 抛弃未洗牌的 Raw 数据
        gate_up_weight_shuffled,
        down_weight_shuffled,
        gate_up_weight_scale_shuffled,
        down_weight_scale_shuffled,
        topk_weights,
        topk_ids,
        config,
    ) = data

    # 1. 确保内存连续性:MI355X 的 HBM 带宽对非连续内存极其敏感
    hidden_states = hidden_states.contiguous()

    # 2. 提取 Padding 信息
    h_pad = config.get("d_hidden_pad", 0) - config.get("d_hidden", 0)
    i_pad = config.get("d_expert_pad", 0) - config.get("d_expert", 0)

    # 3. Triton Mask Elision (掩码消除术) - 冲榜核心
    # 提前用高效的 C++ 层 F.pad 对齐内存,告诉内核 hidden_pad=0
    # 这能直接干掉 Triton 底层内层循环里的 if 边界检查,极大提高吞吐
    if h_pad > 0:
        hidden_states = F.pad(hidden_states, (0, h_pad))
        kernel_h_pad = 0
    else:
        kernel_h_pad = h_pad

    # 4. 极速融合调用 (严格遵守当前 API 签名,移除 split_k)
    output = fused_moe(
        hidden_states,
        gate_up_weight_shuffled,
        down_weight_shuffled,
        topk_weights,
        topk_ids,
        expert_mask=None,           # 保持 None,省去构建 mask 的 CPU/GPU 开销
        activation=ActivationType.Silu,
        quant_type=QuantType.per_1x32,
        doweight_stage1=False,
        w1_scale=gate_up_weight_scale_shuffled,
        w2_scale=down_weight_scale_shuffled,
        a1_scale=None,              # 交给底层自行处理动态量化
        a2_scale=None,
        hidden_pad=kernel_h_pad,    # 传入 0 (如果已 pad),解除内核边界检查
        intermediate_pad=i_pad,
    )

    # 5. 恢复输出维度
    if h_pad > 0:
        output = output[:, :config["d_hidden"]]

    return output
scrolls · 289 lines total

Source code from GPU Mode and the KernelBot dataset · June 9 Researcher Reciprocity License v1.0

Best evidence level for this revision: reported

JSON