Skip to content
KernelIndex
Search⌘K

submission 724521

guojun21 · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

submission_hybrid_1stage257_ksplit4.py
curl "https://kernelindex.com/api/v1/implementations/kernelbot-amd-moe-mxfp4-724521?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
171.4µs
#332 of 782
2026-04-04

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:6fbdf8650fe18695d0c028c786b69addda8c780b9843d3490c3df636ace38b0c
license declaredunknown
license concludedunknown
authorsguojun21
imported2026-08-15

Kernel source

submission_hybrid_1stage257_ksplit4.py199 lines
import functools
import importlib
import os

from task import input_t, output_t


aiter_mod = importlib.import_module("aiter")
fused_moe_mod = importlib.import_module("aiter.fused_moe")

ActivationType = aiter_mod.ActivationType
QuantType = aiter_mod.QuantType
dtypes = aiter_mod.dtypes
fused_moe = fused_moe_mod.fused_moe
MOEMetadata = fused_moe_mod.MOEMetadata
_orig_get_2stage_cfgs = fused_moe_mod.get_2stage_cfgs


def clear_runtime_caches() -> None:
    fused_moe_mod.get_block_size_M.cache_clear()
    fused_moe_mod.use_nt.cache_clear()
    fused_moe_mod.get_ksplit.cache_clear()
    cache_clear = getattr(fused_moe_mod.get_2stage_cfgs, "cache_clear", None)
    if cache_clear is not None:
        cache_clear()


def configure_runtime(config: dict) -> None:
    bs = int(config["bs"])
    n_routed_experts = int(config["n_routed_experts"])
    d_expert = int(config["d_expert"])

    os.environ.pop("AITER_USE_NT", None)

    if n_routed_experts == 32 and d_expert == 512 and bs < 512:
        os.environ["AITER_BYPASS_TUNE_CONFIG"] = "1"
        os.environ["AITER_KSPLIT"] = "4"
    else:
        os.environ.pop("AITER_BYPASS_TUNE_CONFIG", None)
        os.environ.pop("AITER_KSPLIT", None)

    clear_runtime_caches()


def is_fp4_case(
    dtype,
    q_dtype_a,
    q_dtype_w,
    q_type,
    use_g1u1: bool,
    activation,
    doweight_stage1: bool,
) -> bool:
    return (
        dtype == dtypes.bf16
        and q_dtype_a == dtypes.fp4x2
        and q_dtype_w == dtypes.fp4x2
        and q_type == QuantType.per_1x32
        and use_g1u1
        and activation == ActivationType.Silu
        and not doweight_stage1
    )


def should_force_1stage_257(
    model_dim: int,
    inter_dim: int,
    expert: int,
    topk: int,
    dtype,
    q_dtype_a,
    q_dtype_w,
    q_type,
    use_g1u1: bool,
    activation,
    doweight_stage1: bool,
) -> bool:
    return (
        model_dim == 7168
        and inter_dim == 256
        and expert == 257
        and topk == 9
        and is_fp4_case(
            dtype,
            q_dtype_a,
            q_dtype_w,
            q_type,
            use_g1u1,
            activation,
            doweight_stage1,
        )
    )


def patched_get_2stage_cfgs(
    token,
    model_dim,
    inter_dim,
    expert,
    topk,
    dtype,
    q_dtype_a,
    q_dtype_w,
    q_type,
    use_g1u1,
    activation,
    doweight_stage1,
    hidden_pad,
    intermediate_pad,
    is_shuffled=True,
):
    if should_force_1stage_257(
        model_dim,
        inter_dim,
        expert,
        topk,
        dtype,
        q_dtype_a,
        q_dtype_w,
        q_type,
        use_g1u1,
        activation,
        doweight_stage1,
    ):
        return MOEMetadata(
            functools.partial(
                fused_moe_mod.fused_moe_1stage,
                kernelName="",
                activation=activation,
                quant_type=q_type,
            ),
            None,
            32,
            0,
            True,
        )

    return _orig_get_2stage_cfgs(
        token,
        model_dim,
        inter_dim,
        expert,
        topk,
        dtype,
        q_dtype_a,
        q_dtype_w,
        q_type,
        use_g1u1,
        activation,
        doweight_stage1,
        hidden_pad,
        intermediate_pad,
        is_shuffled,
    )


fused_moe_mod.get_2stage_cfgs = patched_get_2stage_cfgs
patched_get_2stage_cfgs.cache_clear = _orig_get_2stage_cfgs.cache_clear


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

    configure_runtime(config)

    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,
        hidden_pad=hidden_pad,
        intermediate_pad=intermediate_pad,
    )
scrolls · 199 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