libr4d: GLM-5.3's kernels -- mHC, KDA, the DSA indexer, MLA, f32-scaled FP8, MXFP4 experts

The device rows for GLM-5.3-Flash's ops, at its geometry and at TP4 and TP8 slices:

- mhc_pre / mhc_post / mhc_contract, and mhc_pre_partial / mhc_pre_finish, which run mhc_pre's
  own projection and finishing kernels with its chunk-sum order, so the pair is byte-identical
  to mhc_pre at n 4096, hc 4, chunk 2048.
- kda_update_k128_v128_bf16, one, two or four value rows a wave, both state forms.
- dsa_pool_fp8, dsa_query_fp8 and dsa_score_fp8 (fp8 WMMA, 32 heads of 128).
- mla_attn: a matrix row whose WMMA tile holds a query's heads, or pairs eight heads of two
  neighbouring queries over the union of their page tables, above the wave row; mla_project
  with up to eight rows a thread or wave.
- gemm_nt_f32 and router_sigmoid_topk in tiled and one-wave-a-row forms; gemm_fp8a8_f32scale
  and moe_gemm_fp8a8_f32scale read f32 block scales in place; moe_gemm_mxfp4a8 decodes e2m1 into
  exact E4M3 bytes for fp8 WMMA, with an expert-major decode form and a wide prefill form.
- swiglu_clamped, and the device-only fusions of GLM's MoE block: swiglu_clamped_quant_fp8,
  router_sigmoid_topk_scatter, and moe_scatter_inv / moe_gather_inv, which pass the gather's slot
  table from the sort instead of rebuilding it.

moe_gather takes f32 routing weights in its column-per-thread kernel. The sort and the gather
kernels move from r4d_moe.hip into r4d_moe_sort.h and r4d_moe_gather.h, so the fused router and
gather instantiate the same code.

Every form that replaces another gives its bytes: tests/r4d_emu runs the kernels on the host
(test_f32, test_fp8f32, test_kda, test_mhc, test_mla, test_moefuse, test_moemx), r4d_selftest
compares them on a card against libref and against each other, and plugin_test pins the
mla_attn band. docs/OPS.md states what libr4d serves.

Co-Authored-By: John McDonough <18016200+JohnCMcDonough@users.noreply.github.com>
This commit is contained in:
NunzeandJohn McDonough authored and StillDeadcode committed 2026-10-19 20:13:47 +02:00
1 parent da77f86332
commit 78c58841bf
31 files changed
+12371 -302

No files matched your search

+20 -13
View File
@@ -357,14 +357,16 @@ endif()
# ================================================================== libr4d's kernels, on the host
#
# Some libr4d ops (the sandwich-norm joins, expert_scale) have no libref oracle by design
# (docs/OPS.md), so rad-kbench never checks them. r4d_emu runs the source of six units written
# against r4d_launch.h on the host: the windowed paged attention, pair_argmax, the joins,
# expert_scale, the QSA block selection, whose four kernels a card-less build otherwise never
# runs, and the runtime-LoRA pair. emu/hip/hip_runtime.h supplies the HIP names, each GPU thread
# is a fiber, and __syncthreads and cross-lane ops are barriers (one not reached uniformly is
# reported as a deadlock). Checked against ports of libref's ref_attn_paged and ref_qsa_select,
# the host arithmetic in ref_host.cpp, for the joins the unfused chain bit for bit, and for the
# LoRA pair libref itself when the test can load it.
# (docs/OPS.md), so rad-kbench never checks them. r4d_emu runs the source of thirteen units
# written against r4d_launch.h on the host: the windowed paged attention, pair_argmax, the joins,
# expert_scale, the QSA block selection, the runtime-LoRA pair, the F32 routing projection's tile
# forms, GLM's mHC read with its chunk split, the F32-scale E4M3 GEMM's forms, GLM's KDA
# recurrence, the MXFP4 expert GEMM's forms, the MLA matrix row's pairings and GLM's MoE decode
# fusions, whose kernels a card-less build otherwise never runs. emu/hip/hip_runtime.h supplies the
# HIP names, each GPU thread is a fiber, and __syncthreads and cross-lane ops are barriers (one not
# reached uniformly is reported as a deadlock). Checked against ports of libref's ref_attn_paged and
# ref_qsa_select, the host arithmetic in ref_host.cpp, for the joins the unfused chain bit for bit,
# and for the LoRA pair libref itself when the test can load it.
#
# Requires clang (ext_vector_type) and x86-64 (SysV fiber switch in inline assembly). With a clang
# C++ compiler the .hip files build here as C++ (`-x c++`; the property is per directory, so
@@ -376,8 +378,10 @@ set(R4D_DIR ${CMAKE_SOURCE_DIR}/libr4d)
set(R4D_EMU ${CMAKE_CURRENT_SOURCE_DIR}/r4d_emu)
set(R4D_EMU_HIP ${R4D_DIR}/r4d_attn_paged_window.hip ${R4D_DIR}/r4d_pair_argmax_f32.hip
${R4D_DIR}/r4d_norm_join_bf16.hip ${R4D_DIR}/r4d_expert_scale_bf16.hip
${R4D_DIR}/r4d_qsa_select_bf16.hip ${R4D_DIR}/r4d_lora_bf16.hip)
set(R4D_EMU_TESTS attn pick norm escale qsa lora)
${R4D_DIR}/r4d_qsa_select_bf16.hip ${R4D_DIR}/r4d_lora_bf16.hip ${R4D_DIR}/r4d_glm.hip
${R4D_DIR}/r4d_mhc.hip ${R4D_DIR}/r4d_fp8_f32scale.hip ${R4D_DIR}/r4d_kda.hip
${R4D_DIR}/r4d_moe_mxfp4.hip ${R4D_DIR}/r4d_mla.hip ${R4D_DIR}/r4d_quant_act_fp8.hip)
set(R4D_EMU_TESTS attn pick norm escale qsa lora f32 mhc fp8f32 kda moemx mla moefuse)
set(R4D_EMU_BUILT OFF)
if(NOT CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|AMD64)$")
# No fiber switch for this ISA.
@@ -413,7 +417,10 @@ else()
set(objs "")
foreach(src ${R4D_EMU_HIP} ${R4D_EMU}/ref_host.cpp ${R4D_EMU}/emu/emu.cpp
${R4D_EMU}/test_attn.cpp ${R4D_EMU}/test_pick.cpp ${R4D_EMU}/test_norm.cpp
${R4D_EMU}/test_escale.cpp ${R4D_EMU}/test_qsa.cpp ${R4D_EMU}/test_lora.cpp)
${R4D_EMU}/test_escale.cpp ${R4D_EMU}/test_qsa.cpp ${R4D_EMU}/test_lora.cpp
${R4D_EMU}/test_f32.cpp ${R4D_EMU}/test_mhc.cpp ${R4D_EMU}/test_fp8f32.cpp
${R4D_EMU}/test_kda.cpp ${R4D_EMU}/test_moemx.cpp ${R4D_EMU}/test_mla.cpp
${R4D_EMU}/test_moefuse.cpp)
get_filename_component(stem ${src} NAME_WE)
set(lang "")
if(src MATCHES "\\.hip$")
@@ -450,7 +457,7 @@ else()
endif()
endif()
if(R4D_EMU_BUILT)
foreach(t pick norm escale qsa lora)
foreach(t pick norm escale qsa lora f32 mhc fp8f32 kda moemx mla moefuse)
add_test(NAME r4d_emu_${t} COMMAND ${CMAKE_BINARY_DIR}/tests/r4d_emu_${t})
endforeach()
# The attention cases take about 11 s, 7 of them in the two long prefills on the WMMA kernel,
@@ -460,7 +467,7 @@ if(R4D_EMU_BUILT)
COMMAND ${CMAKE_BINARY_DIR}/tests/r4d_emu_attn --shard ${k}/3)
endforeach()
set_tests_properties(r4d_emu_attn_shard0 r4d_emu_attn_shard1 r4d_emu_attn_shard2 r4d_emu_pick
r4d_emu_norm r4d_emu_escale r4d_emu_qsa r4d_emu_lora
r4d_emu_norm r4d_emu_escale r4d_emu_qsa r4d_emu_lora r4d_emu_mla r4d_emu_moefuse
PROPERTIES LABELS "r4d_emu" TIMEOUT ${RAD_TEST_TIMEOUT})
# The LoRA pair has a libref oracle, and the test holds the emulated kernels to it when it can
# load one -- expand bit for bit, shrink to its bound.