Skip to content
KernelIndex
Search⌘K

submission 622686

xueliangyang-oeuler · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

submission.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-amd-moe-mxfp4-622686?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
181.4µs
#507 of 782
2026-03-24

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:bdb3457f198e125fc1e67ad9f26fe353b833c109bc47851a72cc046439338806
license declaredunknown
license concludedunknown
authorsxueliangyang-oeuler
imported2026-08-26

Techniques

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

fp4Optimized DeepSeek-R1 MXFP4 MoE Kernel using AITER's High-Performance fused_moe

Kernel source

submission.py125 lines
"""
Optimized DeepSeek-R1 MXFP4 MoE Kernel using AITER's High-Performance fused_moe

Target: AMD Instinct MI355X (CDNA3)

AITER's fused_moe is a highly optimized CK kernel specifically designed for:
- MXFP4 quantization with per-1x32 block scaling
- Two-stage pipeline: gate_up GEMM + SwiGLU + down GEMM + weighted reduction
- Pre-shuffled weight layout for optimal memory access
- GROUP_M scheduling for better L2 cache utilization

Performance (from benchmark 0324-02):
| bs  | dexpert | Time (us) |
|-----|---------|-----------|
| 16  | 256     | 135       |
| 128 | 256     | 211       |
| 512 | 256     | 244       |
| 16  | 512     | 91        |
| 128 | 512     | 125       |
| 512 | 512     | 211       |
| 512 | 2048    | 341       |

All tests passed with max error <= 0.015625
"""

import torch
from typing import Dict, Optional

from aiter import ActivationType, QuantType
from aiter.fused_moe import fused_moe

from task import input_t, output_t


def custom_kernel(data: input_t) -> output_t:
    """
    Optimized DeepSeek-R1 MXFP4 MoE kernel using AITER's fused_moe.
    
    ========================================================================
    IMPLEMENTATION STRATEGY
    ========================================================================

    This implementation uses AITER's production-grade fused_moe kernel, which
    is specifically optimized for AMD Instinct MI355X GPU and provides the
    best performance across all scenarios.

    Key Optimizations in AITER fused_moe:
    --------------------------------------
    1. **Two-stage Pipeline**: Fused gate_up GEMM + SwiGLU (Stage 1) followed by
       down GEMM + weighted reduction (Stage 2)
    2. **MXFP4 a4w4 Quantization**: Optimized for 4-bit activations and weights
    3. **Pre-shuffled Weights**: (16,16) tile-coalesced layout for optimal memory access
    4. **Group Scheduling**: GROUP_M for better L2 cache utilization
    5. **E8M0 Block Scales**: per-1x32 block quantization for efficient dequantization
    6. **Fused Operations**: Minimizes kernel launch overhead and memory traffic

    Performance Characteristics (from benchmark):
    ---------------------------------------------
    | Batch | d_expert | Time (us) | Max Error |
    |-------|----------|-----------|-----------|
    | 16    | 256      | 135       | 0.015625  |
    | 128   | 256      | 211       | 0.015625  |
    | 512   | 256      | 244       | 0.015625  |
    | 16    | 512      | 91        | 0.015625  |
    | 128   | 512      | 125       | 0.015625  |
    | 512   | 512      | 211       | 0.015625  |
    | 512   | 2048     | 341       | 0.015625  |

    ========================================================================

    Input data tuple:
        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

    Returns:
        output: [M, d_hidden] bf16
    """
    (
        hidden_states,
        gate_up_weight,
        down_weight,
        gate_up_weight_scale,
        down_weight_scale,
        gate_up_weight_shuffled,
        down_weight_shuffled,
        gate_up_weight_scale_shuffled,
        down_weight_scale_shuffled,
        topk_weights,
        topk_ids,
        config,
    ) = data

    hidden_pad = config["d_hidden_pad"] - config["d_hidden"]
    intermediate_pad = config["d_expert_pad"] - config["d_expert"]

    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
scrolls · 125 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