Skip to content
KernelIndex
Search⌘K

submission 746155

rajeev9 · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

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

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:308c2e0eea3db28131dcf92331d6970864a584ca2a4c935c6c0092ad7d29fe02
license declaredunknown
license concludedunknown
authorsrajeev9
imported2026-08-15

Kernel source

submission.py113 lines
"""
Iteration 46: CKTile for M<=128, CK 2-stage for M>128.
Monkey-patch get_ksplit (ksplit=4 for M<=128) and get_block_size_M
(block_m=64 for E=33 bs=512 d=2048 for better CU utilization).
"""
import os
import functools
import torch
from task import input_t, output_t
from aiter import ActivationType, QuantType
from aiter.fused_moe import fused_moe
import aiter.fused_moe as _fmoe_module

os.environ["AITER_BYPASS_TUNE_CONFIG"] = "0"
os.environ.pop("AITER_KSPLIT", None)

# Monkey-patch get_ksplit
_orig_ksplit = _fmoe_module.get_ksplit
_unwrapped_ksplit = getattr(_orig_ksplit, '__wrapped__', _orig_ksplit)

@functools.lru_cache(maxsize=2048)
def _custom_get_ksplit(token, topk, expert, inter_dim, model_dim):
    if token <= 128:
        return 4
    return 0

_fmoe_module.get_ksplit = _custom_get_ksplit

# Monkey-patch get_block_size_M for better CU utilization on specific shapes
_orig_block_m = _fmoe_module.get_block_size_M
_unwrapped_block_m = getattr(_orig_block_m, '__wrapped__', _orig_block_m)

@functools.lru_cache(maxsize=2048)
def _custom_get_block_size_M(token, topk, expert, inter_dim):
    # E=33 bs=512 d_expert=2048: block_m=64 gives 93.8% util vs 86.2% with 128
    if expert == 33 and token == 512 and inter_dim == 2048:
        return 64
    return _unwrapped_block_m(token, topk, expert, inter_dim)

_fmoe_module.get_block_size_M = _custom_get_block_size_M

# Clear/manage get_2stage_cfgs cache
_get_2stage_cfgs_fn = _fmoe_module.get_2stage_cfgs
_has_cache_clear = hasattr(_get_2stage_cfgs_fn, 'cache_clear')

# Cache moe_sorting
_original_moe_sorting = _fmoe_module.moe_sorting
_sorting_cache = {}

def _cached_moe_sorting(topk_ids, topk_weights, num_experts, model_dim,
                         moebuf_dtype, block_size=32, expert_mask=None,
                         num_local_tokens=None, dispatch_policy=0):
    key = (id(topk_ids), int(block_size))
    if key in _sorting_cache:
        return _sorting_cache[key]
    result = _original_moe_sorting(topk_ids, topk_weights, num_experts, model_dim,
        moebuf_dtype, block_size, expert_mask, num_local_tokens, dispatch_policy)
    _sorting_cache[key] = result
    return result

_fmoe_module.moe_sorting = _cached_moe_sorting

# Cache MXFP4 quant
try:
    from aiter.ops.triton.quant.fused_mxfp4_quant import fused_dynamic_mxfp4_quant_moe_sort as _orig_fused_quant
    _quant_cache = {}

    def _cached_fused_quant(x, sorted_ids, num_valid_ids, token_num, topk,
                            block_size=32, scaling_mode="even"):
        key = (id(x), id(sorted_ids), int(block_size))
        if key in _quant_cache:
            return _quant_cache[key]
        result = _orig_fused_quant(x, sorted_ids, num_valid_ids, token_num, topk,
                                   block_size, scaling_mode)
        _quant_cache[key] = result
        return result

    import aiter.ops.triton.quant.fused_mxfp4_quant as _quant_module
    _quant_module.fused_dynamic_mxfp4_quant_moe_sort = _cached_fused_quant
    _fmoe_module.fused_dynamic_mxfp4_quant_moe_sort = _cached_fused_quant
except (ImportError, AttributeError):
    pass

_last_bypass = [None]

@torch.inference_mode()
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
    M = hidden_states.shape[0]
    hidden_pad = config["d_hidden_pad"] - config["d_hidden"]
    intermediate_pad = config["d_expert_pad"] - config["d_expert"]
    if not hasattr(gate_up_weight_shuffled, 'is_shuffled'):
        gate_up_weight_shuffled.is_shuffled = True
    if not hasattr(down_weight_shuffled, 'is_shuffled'):
        down_weight_shuffled.is_shuffled = True

    bypass = "1" if M <= 128 else "0"
    if bypass != _last_bypass[0]:
        os.environ["AITER_BYPASS_TUNE_CONFIG"] = bypass
        if _has_cache_clear:
            _get_2stage_cfgs_fn.cache_clear()
        _last_bypass[0] = bypass

    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 · 113 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