Skip to content
KernelIndex
Search⌘K

submission 754922

Leon · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

submission.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-amd-moe-mxfp4-754922?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
108.1µs
#12 of 782
2026-04-07

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:4e203e8a07874768f589950c0404104327903102fe7868968b81d3dbbaa91430
license declaredunknown
license concludedunknown
authorsLeon
imported2026-08-15

Techniques

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

fp4a_dtype='fp4', b_dtype='fp4', out_dtype='bf16',

Kernel source

submission.py218 lines
#!POPCORN leaderboard amd-moe-mxfp4
#!POPCORN gpu MI355X

import torch
from typing import Dict
from task import input_t, output_t

from aiter import ActivationType, QuantType, dtypes
from aiter.fused_moe import fused_moe
import aiter.fused_moe as _fm

# ── FlyDSL stage1 + optimized config (verified #6, 109.55µs) ──

import os, functools

def _patch_flydsl_stage1():
    target = "/home/runner/aiter/aiter/ops/flydsl/kernels/mixed_moe_gemm_2stage.py"
    if not os.path.exists(target):
        return False
    with open(target) as f:
        content = f.read()
    old_sig = (
        "b_tile_in_gate,\n"
        "                        b_tile_in_up,\n"
        "                        lds_base,\n"
        "                        *,\n"
        "                        a0_prefetch=None,\n"
        "                        a_scale=None,\n"
        "                        b_scale_gate=None,\n"
        "                        b_scale_up=None,"
    )
    new_sig = (
        "b_tile_in,\n"
        "                        lds_base,\n"
        "                        *,\n"
        "                        a0_prefetch=None,\n"
        "                        a_scale=None,\n"
        "                        b_scale=None,"
    )
    if old_sig in content:
        content = content.replace(old_sig, new_sig, 1)
        with open(target, 'w') as f:
            f.write(content)
    return True

_patch_flydsl_stage1()

# Zero-cost probe: check if PR#2581 _fq kernels are available on runner
import pathlib as _pl
_fq = _pl.Path("/home/runner/aiter/aiter/ops/flydsl/kernels/silu_and_mul_fq.py").exists()
_csv = _pl.Path("/home/runner/aiter/aiter/configs/model_configs/kimi-2_fp4_tuned_fmoe.csv").exists()
_dp = _pl.Path("/home/runner/aiter/aiter/fused_moe_dp_shared_expert.py").exists()
print(f"[PROBE] PR2581 _fq={_fq} kimi_csv={_csv} dp_shared={_dp}", flush=True)

def _flydsl_stage1_wrapper(
    hidden_states, w1, w2,
    sorted_token_ids, sorted_expert_ids, num_valid_ids,
    out, topk, block_m=64,
    a1_scale=None, w1_scale=None,
    kernelName='', sorted_weights=None,
    tile_m=64, tile_n=256, tile_k=128, **_kwargs,
):
    from aiter.ops.flydsl import flydsl_moe_stage1
    return flydsl_moe_stage1(
        a=hidden_states, w1=w1,
        sorted_token_ids=sorted_token_ids, sorted_expert_ids=sorted_expert_ids,
        num_valid_ids=num_valid_ids, out=out, topk=topk,
        tile_m=tile_m, tile_n=tile_n, tile_k=tile_k,
        a_dtype='fp4', b_dtype='fp4', out_dtype='bf16',
        w1_scale=w1_scale, a1_scale=a1_scale, sorted_weights=sorted_weights,
    )


def _preload_and_inject():
    try:
        cu = _fm.get_cu_count() if hasattr(_fm, 'get_cu_count') else 256
        act = ActivationType.Silu
        dtype_t = torch.bfloat16
        q_a_t = dtypes.fp4x2
        q_w_t = dtypes.fp4x2
        q_type_t = QuantType.per_1x32

        _fm.get_2stage_cfgs(
            16, 7168, 256, 257, 9,
            dtype_t, q_a_t, q_w_t, q_type_t,
            True, act, False, 0, 0, True
        )
        for s_bs, s_n, s_k, s_e, s_topk in [
            (8, 4096, 1024, 257, 9),
            (32, 7168, 2048, 33, 9),
            (128, 4096, 1536, 65, 7),
        ]:
            try:
                _fm.get_2stage_cfgs(
                    s_bs, s_n, s_k, s_e, s_topk,
                    dtype_t, q_a_t, q_w_t, q_type_t,
                    True, act, False, 0, 0, True
                )
            except Exception:
                pass

        cfg = _fm.cfg_2stages
        if cfg is None:
            return

        act_s = str(act)
        dtype_s = str(dtype_t)
        q_a = str(q_a_t)
        q_w = str(q_w_t)
        q_type = str(q_type_t)

        kn1_small = 'moe_ck2stages_gemm1_64x32x32x128_1x1_MulABScaleShuffled_v3_Nswizzle0_Quant3_MulRoutedWeight0_silu_FP4X2_FP4X2_B16'
        kn2_small = 'moe_ck2stages_gemm2_64x32x32x128_1x1_MulABScaleExpertWeightShuffled_v1_Nswizzle0_Quant3_MulRoutedWeight1_FP4X2_FP4X2_B16'
        kn1_32 = 'moe_ck2stages_gemm1_256x32x128x128_1x4_MulABScaleShuffled_v3_Nswizzle0_Quant3_MulRoutedWeight0_silu_FP4X2_FP4X2_B16'
        kn2_32 = 'moe_ck2stages_gemm2_256x32x128x128_1x4_MulABScaleExpertWeightShuffled_v1_Nswizzle0_Quant3_MulRoutedWeight1_FP4X2_FP4X2_B16'
        kn1_64 = 'moe_ck2stages_gemm1_256x64x128x128_1x4_MulABScaleShuffled_v3_Nswizzle0_Quant3_MulRoutedWeight0_silu_FP4X2_FP4X2_B16'
        kn2_fly32_atomic_tn128 = 'flydsl_moe2_afp4_wfp4_bf16_t32x128x256_atomic'

        def make_entry(block_m, ksplit, kn1, kn2):
            return {
                'block_m': block_m, 'ksplit': ksplit,
                'kernelName1': kn1, 'kernelName2': kn2,
                'run_1stage': 0,
                'us': 0.0, 'us1': 0.0, 'us2': 0.0,
                'err1': '0', 'err2': '0',
                'tflops': 0.0, 'bw': 0.0, '_tag': float('nan'),
            }

        cfg[(cu, 16, 7168, 256, 257, 9, act_s, dtype_s, q_a, q_w, q_type, True, False)] = \
            make_entry(32, 4, kn1_32, kn2_32)
        cfg[(cu, 128, 7168, 256, 257, 9, act_s, dtype_s, q_a, q_w, q_type, True, False)] = \
            make_entry(32, 4, kn1_32, kn2_32)
        cfg[(cu, 512, 7168, 256, 257, 9, act_s, dtype_s, q_a, q_w, q_type, True, False)] = \
            make_entry(32, 0, kn1_small, kn2_small)
        cfg[(cu, 16, 7168, 512, 33, 9, act_s, dtype_s, q_a, q_w, q_type, True, False)] = \
            make_entry(32, 2, kn1_32, kn2_32)
        cfg[(cu, 128, 7168, 512, 33, 9, act_s, dtype_s, q_a, q_w, q_type, True, False)] = \
            make_entry(64, 0, kn1_64, kn2_fly32_atomic_tn128)
        cfg[(cu, 512, 7168, 512, 33, 9, act_s, dtype_s, q_a, q_w, q_type, True, False)] = \
            make_entry(64, 0, kn1_64, kn2_fly32_atomic_tn128)
        cfg[(cu, 512, 7168, 2048, 33, 9, act_s, dtype_s, q_a, q_w, q_type, True, False)] = \
            make_entry(64, 0, kn1_64, kn2_fly32_atomic_tn128)

        cfg[(cu, 8, 4096, 1024, 257, 9, act_s, dtype_s, q_a, q_w, q_type, True, False)] = \
            make_entry(32, 4, kn1_32, kn2_32)
        cfg[(cu, 32, 7168, 2048, 33, 9, act_s, dtype_s, q_a, q_w, q_type, True, False)] = \
            make_entry(64, 0, kn1_64, kn2_fly32_atomic_tn128)
        cfg[(cu, 128, 4096, 1536, 65, 7, act_s, dtype_s, q_a, q_w, q_type, True, False)] = \
            make_entry(32, 4, kn1_32, kn2_32)

        if hasattr(_fm.get_2stage_cfgs, 'cache_clear'):
            _fm.get_2stage_cfgs.cache_clear()
        for fn_name in ['get_block_size_M', 'use_nt', 'get_ksplit']:
            fn = getattr(_fm, fn_name, None)
            if fn and hasattr(fn, 'cache_clear'):
                fn.cache_clear()

        _monkeypatch_flydsl_stage1()

    except Exception as e:
        import traceback; traceback.print_exc()

def _monkeypatch_flydsl_stage1():
    try:
        orig_func = _fm.get_2stage_cfgs.__wrapped__
    except AttributeError:
        orig_func = _fm.get_2stage_cfgs

    MOEMetadata = _fm.MOEMetadata if hasattr(_fm, 'MOEMetadata') else None

    @functools.lru_cache(maxsize=None)
    def _patched_get_2stage_cfgs(*args, **kwargs):
        result = orig_func(*args, **kwargs)
        if len(args) >= 5:
            token, model_dim, inter_dim, expert, topk = args[:5]
            if expert == 33 and inter_dim >= 512 and token >= 128:
                new_stage1 = functools.partial(_flydsl_stage1_wrapper, tile_m=64, tile_n=256, tile_k=128)
                if MOEMetadata is not None:
                    result = MOEMetadata(new_stage1, result.stage2, result.block_m, result.ksplit)
                else:
                    result = type(result)(new_stage1, result.stage2, result.block_m, result.ksplit)
            elif expert == 257 and token >= 16:
                new_stage1 = functools.partial(_flydsl_stage1_wrapper, tile_m=32, tile_n=256, tile_k=128)
                if MOEMetadata is not None:
                    result = MOEMetadata(new_stage1, result.stage2, result.block_m, result.ksplit)
                else:
                    result = type(result)(new_stage1, result.stage2, result.block_m, result.ksplit)
        return result

    _fm.get_2stage_cfgs = _patched_get_2stage_cfgs

_preload_and_inject()


def custom_kernel(data: input_t) -> output_t:
    (
        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"]

    return 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,
        block_size_M=None,
        hidden_pad=hidden_pad, intermediate_pad=intermediate_pad,
    )
scrolls · 218 lines total

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

Changes from previous submission

Against this author's previous submission submission 754526.

⋯ 44 unchanged lines
_patch_flydsl_stage1()
+ # Zero-cost probe: check if PR#2581 _fq kernels are available on runner
+ import pathlib as _pl
+ _fq = _pl.Path("/home/runner/aiter/aiter/ops/flydsl/kernels/silu_and_mul_fq.py").exists()
+ _csv = _pl.Path("/home/runner/aiter/aiter/configs/model_configs/kimi-2_fp4_tuned_fmoe.csv").exists()
+ _dp = _pl.Path("/home/runner/aiter/aiter/fused_moe_dp_shared_expert.py").exists()
+ print(f"[PROBE] PR2581 _fq={_fq} kimi_csv={_csv} dp_shared={_dp}", flush=True)
+
def _flydsl_stage1_wrapper(
hidden_states, w1, w2,
sorted_token_ids, sorted_expert_ids, num_valid_ids,

Best evidence level for this revision: reported

JSON