vllm.models.deepseek_v41.amd.mono.common.ops
¶
Device-side primitives of the mono kernels: wave reductions and shuffles, the approximate hardware math, bf16 / fp8 packing, buffer resources, the execution-model constants and the kernel symbol naming.
Ported from the FlyDSL GLM-5 layer kernel (ROCm/FlyDSL branch
glm5-mla-moe-layer-monokernel @ 1e28fd8a,
kernels/mla_moe_layer/shared_reuse_moe_kernel.py). Only published flydsl APIs
and aiter's vendored buffer_ops / dpp_utils are used.
Every function but kernel_symbol is traced inside a @flyc.kernel body.
Raw boundaries kept, for want of an equivalent on the pinned FlyDSL:
buffer_ops loads / stores (the kernels take raw Int64 addresses, no tensors
for make_buffer_tensor); raw MFMA intrinsics (hand-laid operands: permlane
transposes, fp8 packing, MXFP4 scales); the LLVM intrinsics here (expect,
s_memrealtime, the approximate rsq / rcp / exp2) and the struct unpacking of
permlane*_swap.
Also the TP group seen from inside a kernel (every rank's peer buffer, an all-reduce's rank-ordered sum) and a timeline build's phase stamps.
Functions:
-
ballot–The wave's lanes where
predholds, a bit a lane. -
batched_rounds–A CTA's strided loop over elements v = tid + THREADS i < n (n a Python
-
bf16_pair–Two f32 -> one f32-typed word holding (bf16(a), bf16(b)).
-
bf2_f32–Packed bf16 pair word -> (f32 low, f32 high).
-
bf_hi–The f32 of a packed bf16 pair word's high half.
-
bf_lo–The f32 of a packed bf16 pair word's low half.
-
butterfly–Butterfly reduction over the given xor offsets, in the given order.
-
div_rn–x / drounded to nearest, bit-identical to the IEEE division, given -
fp8_pack4–Four f32 (already in range) -> one dword of four E4M3 bytes, RNE.
-
fresh–v(a per-lane Int32) through an opaque move, here: the lane math built -
hw_rsq–v_rsq_f32 (~1 ulp, no libm range fixups).
-
lane_gather–Lane
src_lane's value of v, a source lane a lane (ds_bpermute). -
lanes_below–How many of the lanes in ballot
balare belowlane. -
load_ptr64–Entry
iof an i64 table in memory, wave-uniform (each half through -
memrealtime–s_memrealtime: the 100 MHz device clock (no FlyDSL wrapper).
-
peer_bases–Every rank's peer buffer base from the
peerstable (an i64 a rank), -
permlane_swap–v_permlane{32,16}_swap on registers (x, y): off 32 trades x's upper-half
-
row_sum–Row r, token t of the waves' 16x16 MFMA accumulators (wave w's at
red -
sum_partials–The
tpranks' bf16 pair partials atpair_of(src)of the mailbox -
traced–Apply flydsl's AST rewriting to a helper, so its
while/ifover -
xred–op(v, lane ^ off) for a symmetric op; 32 / 16 take both halves of one swap.
-
xshfl–Value of lane
lane ^ off: offsets 32 / 16 via v_permlane*_swap, in-row
ballot(pred)
¶
batched_rounds(tid, n, load, use, batch=LOAD_BATCH)
¶
A CTA's strided loop over elements v = tid + THREADS i < n (n a Python
int): load(v) of batch rounds issued before any use(v, loaded).
A round whose use stores to global memory keeps the next round's loads
behind it (they may alias), so the rounds' loads would go out in series. A
round past n loads element n - 1 (in range) and uses nothing.
Source code in vllm/models/deepseek_v41/amd/mono/common/ops.py
bf16_pair(a, b)
¶
Two f32 -> one f32-typed word holding (bf16(a), bf16(b)).
bf2_f32(w)
¶
bf_hi(w)
¶
bf_lo(w)
¶
butterfly(v, offsets, op=_add)
¶
Butterfly reduction over the given xor offsets, in the given order.
The order is the numerical contract: the aiter per-head norm reduces 32 lanes with offsets 16, 8, 4, 2, 1, and matching it keeps rstd bit-exact.
Source code in vllm/models/deepseek_v41/amd/mono/common/ops.py
div_rn(x, d, r)
¶
x / d rounded to nearest, bit-identical to the IEEE division, given
r = 1.0 / d from one true division per divisor (Markstein: a quotient
within 1 ulp plus its exact fma residual times RN(1/d) rounds correctly).
Source code in vllm/models/deepseek_v41/amd/mono/common/ops.py
fp8_pack4(a, b, c, d)
¶
Four f32 (already in range) -> one dword of four E4M3 bytes, RNE.
Source code in vllm/models/deepseek_v41/amd/mono/common/ops.py
fresh(v)
¶
v (a per-lane Int32) through an opaque move, here: the lane math built
on it is computed at its use, where LLVM would otherwise hoist it out of a task
loop or CSE it across stages into registers held for the whole kernel (spills,
and a kernel with scratch is refused: runtime.widths).
Source code in vllm/models/deepseek_v41/amd/mono/common/ops.py
hw_rsq(x)
¶
lane_gather(v, src_lane)
¶
Lane src_lane's value of v, a source lane a lane (ds_bpermute).
lanes_below(bal, lane)
¶
load_ptr64(table, i)
¶
Entry i of an i64 table in memory, wave-uniform (each half through
uniform): a peer's buffer base, a kernel argument block's pointer.
Source code in vllm/models/deepseek_v41/amd/mono/common/ops.py
memrealtime()
¶
s_memrealtime: the 100 MHz device clock (no FlyDSL wrapper).
peer_bases(peers, tp)
¶
Every rank's peer buffer base from the peers table (an i64 a rank),
loaded once: a put's address must not wait on a load of its own.
permlane_swap(off, x, y)
¶
v_permlane{32,16}_swap on registers (x, y): off 32 trades x's upper-half lanes with y's lower-half lanes; off 16 trades x's odd 16-lane rows with y's even rows (each within its half). -> (x', y').
Source code in vllm/models/deepseek_v41/amd/mono/common/ops.py
row_sum(red, r, t, waves=None)
¶
Row r, token t of the waves' 16x16 MFMA accumulators (wave w's at red
+ (w * 64 + lane) * 4), summed in wave order from 0 (waves: which, in
order; all of them by default).
Source code in vllm/models/deepseek_v41/amd/mono/common/ops.py
sum_partials(poll, own, pair_of, tp)
¶
The tp ranks' bf16 pair partials at pair_of(src) of the mailbox
region own, summed in fp32 in rank order 0 .. tp - 1: one poll batch ->
(lo sum, hi sum). Every rank sums in the same order, so they agree.
Source code in vllm/models/deepseek_v41/amd/mono/common/ops.py
traced(fn)
¶
Apply flydsl's AST rewriting to a helper, so its while / if over
traced values lower to scf ops. Kernels rewrite only their own body.
xred(v, off, op)
¶
op(v, lane ^ off) for a symmetric op; 32 / 16 take both halves of one swap.
Source code in vllm/models/deepseek_v41/amd/mono/common/ops.py
xshfl(v, off)
¶
Value of lane lane ^ off: offsets 32 / 16 via v_permlane*_swap, in-row
offsets via DPP (VALU latency, not LDS). Powers of two only: any other
offset would take the lane ^ 1 path.