Skip to content
KernelIndex
Search⌘K

submission 683903

somethingobscurefordevstuff · python · License unknown

Use it

Vendorable · source mirrored · license unknownView source →

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

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

Reported · How evidence levels are derived →

Source and license

sourceavailable
revision digestsha256:77798fb8c0f035121db0e986adaa41cd6c2b7ebc6fdb116d827226e2898d2e18
license declaredunknown
license concludedunknown
authorssomethingobscurefordevstuff
imported2026-08-15

Kernel source

submission.py150 lines
#!POPCORN leaderboard amd-moe-mxfp4
#!POPCORN gpu MI355X
# APPROACH 1 (fixed): Replace fused_moe_2stages with stripped version
# Now using EXACT stage1/stage2 calling conventions from server source
# Stage1: lines 139-155, Stage2: lines 222-238

import functools,math,os,sys
os.environ["PYTORCH_ROCM_ARCH"]="gfx950"
os.environ["CXX"]="clang++"
import pandas as pd,torch
from task import input_t,output_t
import aiter,aiter.fused_moe as _fmoe
from aiter import ActivationType,QuantType,dtypes
from aiter.fused_moe import get_inter_dim
torch.set_grad_enabled(False)
P=lambda *a:print(*a,file=sys.stderr,flush=True)
_fmoe.use_nt=lambda token,topk,e:True
def _sk(token,topk,expert,inter_dim,model_dim):
    if expert==33 and token>=512:return 0
    em=token*topk//expert;return 4 if em<=10 else(2 if em<=64 else 0)
_fmoe.get_ksplit=_sk
@functools.lru_cache(maxsize=1024)
def _bm(token,topk,expert,inter_dim):
    m=token*topk//expert;return 128 if m>=100 else(64 if m>=20 else 32)
_fmoe.get_block_size_M=_bm
ad=os.path.dirname(aiter.__file__);cd=os.path.join(ad,"configs");mcd=os.path.join(cd,"model_configs")
tfl=[os.path.join(cd,"tuned_fmoe.csv")]
if os.path.isdir(mcd):
    for f in sorted(os.listdir(mcd)):
        if f.endswith('.csv') and 'fmoe' in f:tfl.append(os.path.join(mcd,f))
dfs=[pd.read_csv(x) for x in tfl if os.path.exists(x)]
if dfs:
    df=pd.concat(dfs,ignore_index=True)
    ic=["cu_num","token","model_dim","inter_dim","expert","topk","act_type","dtype","q_dtype_a","q_dtype_w","q_type","use_g1u1","doweight_stage1"]
    df=df.drop_duplicates(subset=ic,keep='last')
    for c in['ksplit','block_m','cu_num','token','model_dim','inter_dim','expert','topk','use_g1u1','doweight_stage1','run_1stage']:
        if c in df.columns:df[c]=df[c].fillna(0).astype(int)
    for idx in df.index:
        t,e,k=int(df.at[idx,'token']),int(df.at[idx,'expert']),int(df.at[idx,'topk'])
        if e>0 and k>0 and t*k//e<=10:df.at[idx,'ksplit']=int(4)
    mask=~((df['expert']==257) & (df['inter_dim']==256) & (df['token'].isin([16,128])))
    df=df[mask]
    new_cfgs=[
        {'cu_num':256,'token':512,'model_dim':7168,'inter_dim':512,'expert':33,'topk':9,'act_type':'ActivationType.Silu','dtype':'torch.bfloat16','q_dtype_a':'torch.float4_e2m1fn_x2','q_dtype_w':'torch.float4_e2m1fn_x2','q_type':'QuantType.per_1x32','use_g1u1':1,'doweight_stage1':0,'block_m':128,'ksplit':0,'run_1stage':0,
         'kernelName1':'moe_ck2stages_gemm1_256x128x128x128_1x4_MulABScaleShuffled_v3_Nswizzle0_Quant3_MulRoutedWeight0_silu_FP4X2_FP4X2_B16',
         'kernelName2':'flydsl_moe2_afp4_wfp4_bf16_t32x128x256_reduce'},
        {'cu_num':256,'token':512,'model_dim':7168,'inter_dim':2048,'expert':33,'topk':9,'act_type':'ActivationType.Silu','dtype':'torch.bfloat16','q_dtype_a':'torch.float4_e2m1fn_x2','q_dtype_w':'torch.float4_e2m1fn_x2','q_type':'QuantType.per_1x32','use_g1u1':1,'doweight_stage1':0,'block_m':128,'ksplit':0,'run_1stage':0,
         'kernelName1':'moe_ck2stages_gemm1_256x128x128x128_1x4_MulABScaleShuffled_v3_Nswizzle0_Quant3_MulRoutedWeight0_silu_FP4X2_FP4X2_B16',
         'kernelName2':'flydsl_moe2_afp4_wfp4_bf16_t32x128x256_atomic'},
    ]
    df=pd.concat([df,pd.DataFrame(new_cfgs)],ignore_index=True);df=df.drop_duplicates(subset=ic,keep='last')
    cfg=df.set_index(ic).to_dict("index")
    for val in cfg.values():
        for k,v in val.items():
            if isinstance(v,float):val[k]="" if math.isnan(v) else(int(v) if v==int(v) else v)
    _fmoe.cfg_2stages=cfg
if hasattr(_fmoe.get_2stage_cfgs,'cache_clear'):_fmoe.get_2stage_cfgs.cache_clear()

# Sort+fqs caching
_om=_fmoe.moe_sorting;_slk=[None];_slv=[None];_sd={}
def _cms(ti,tw,*a,**k):
    p=(ti.data_ptr(),ti._version,tw.data_ptr(),tw._version)
    if _slk[0]==p:return _slv[0]
    c=_sd.get(p)
    if c is not None:_slk[0]=p;_slv[0]=c;return c
    r=_om(ti,tw,*a,**k);_sd[p]=r;_slk[0]=p;_slv[0]=r;return r
_fmoe.moe_sorting=_cms
_fqs=getattr(_fmoe,'fused_dynamic_mxfp4_quant_moe_sort',None);_qlk=[None];_qlv=[None];_qd={}
if _fqs is not None:
    _orig_fqs=_fqs
    def _cf(*args,**kwargs):
        x=args[0];p0=x.data_ptr();v0=x._version;s0=x.shape[0];s1=x.shape[1]
        sid=kwargs.get('sorted_ids')
        if sid is None and len(args)>1:sid=args[1]
        p1=sid.data_ptr() if sid is not None and hasattr(sid,'data_ptr') else 0
        v1=sid._version if sid is not None and hasattr(sid,'_version') else 0
        key=(p0,v0,s0,s1,p1,v1)
        if _qlk[0]==key:return _qlv[0]
        c=_qd.get(key)
        if c is not None:_qlk[0]=key;_qlv[0]=c;return c
        r=_orig_fqs(*args,**kwargs);_qd[key]=r;_qlk[0]=key;_qlv[0]=r;return r
    _fmoe.fused_dynamic_mxfp4_quant_moe_sort=_cf
_ogpm=getattr(_fmoe,'get_padded_M',None)
if _ogpm is not None:_fmoe.get_padded_M=functools.lru_cache(maxsize=256)(_ogpm)

# Stripped fused_moe_2stages — exact server calling conventions
_g2s=_fmoe.get_2stage_cfgs
_gpm=_fmoe.get_padded_M
_ACT=ActivationType.Silu;_QT=QuantType.per_1x32;_FP4=dtypes.fp4x2;_BF16=dtypes.bf16
_meta_d={};_a2_cache={}

def _fast_2stages(hidden_states, w1, w2, topk, sorted_ids, sorted_weights,
                  sorted_expert_ids, num_valid_ids, moe_out, isG1U1, block_size_M,
                  **kwargs):
    token_num=hidden_states.shape[0]
    E,model_dim,inter_dim=get_inter_dim(w1.shape,w2.shape)
    hp=kwargs.get('hidden_pad',0);ip=kwargs.get('intermediate_pad',0)
    w1_scale=kwargs.get('w1_scale');w2_scale=kwargs.get('w2_scale')
    is_shuffled=getattr(w1,'is_shuffled',True)
    mk=(token_num,E,inter_dim)
    meta=_meta_d.get(mk)
    if meta is None:
        meta=_g2s(_gpm(token_num),model_dim,inter_dim,E,topk,_BF16,_FP4,_FP4,_QT,True,_ACT,False,hp,ip,is_shuffled)
        _meta_d[mk]=meta

    # Input quant — skip for cktile (ksplit>0 + shuffled)
    if meta.ksplit>0 and is_shuffled:
        a1=hidden_states;a1_scale=None
    else:
        a1,a1_scale=_cf(hidden_states,sorted_ids=sorted_ids,num_valid_ids=num_valid_ids,
            token_num=token_num,topk=1,block_size=block_size_M)

    # Stage1 — pre-alloc a2
    a2k=(token_num,topk,inter_dim)
    a2=_a2_cache.get(a2k)
    if a2 is None:
        a2=torch.empty(a2k,dtype=_BF16,device=hidden_states.device)
        _a2_cache[a2k]=a2
    _w1s=w1_scale.view(dtypes.fp8_e8m0) if w1.dtype==_FP4 else w1_scale
    _w2s=w2_scale.view(dtypes.fp8_e8m0) if w2.dtype==_FP4 else w2_scale
    meta.stage1(a1,w1,w2,sorted_ids,sorted_expert_ids,num_valid_ids,a2,topk,
        block_m=block_size_M,a1_scale=a1_scale,w1_scale=_w1s,sorted_weights=None)

    # Intermediate quant — skip for cktile
    if meta.ksplit>0 and is_shuffled:
        a2_scale=None
    else:
        a2=a2.view(-1,inter_dim)
        a2,a2_scale=_cf(a2,sorted_ids=sorted_ids,num_valid_ids=num_valid_ids,
            token_num=token_num,topk=topk,block_size=block_size_M)
        a2=a2.view(token_num,topk,-1)

    # Stage2
    meta.stage2(a2,w1,w2,sorted_ids,sorted_expert_ids,num_valid_ids,moe_out,topk,
        w2_scale=_w2s,
        a2_scale=a2_scale,
        block_m=block_size_M,
        sorted_weights=sorted_weights)
    return moe_out

# Replace fused_moe_2stages with our stripped version
_fmoe.fused_moe_2stages=_fast_2stages

from aiter.fused_moe import fused_moe
def custom_kernel(data:input_t)->output_t:
    hs,guw,dw,guqs,dws,guw_sh,dw_sh,guws_sh,dws_sh,tw,ti,cfg=data
    hp=cfg["d_hidden_pad"]-cfg["d_hidden"];ip=cfg["d_expert_pad"]-cfg["d_expert"]
    return fused_moe(hs,guw_sh,dw_sh,tw,ti,activation=_ACT,
        quant_type=_QT,w1_scale=guws_sh,w2_scale=dws_sh,hidden_pad=hp,intermediate_pad=ip)
scrolls · 150 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