vllm.models.deepseek_v41.amd.mono.attention.device
¶
Device primitives of the DeepSeek-V4.1 attention mega kernel.
Wave reductions and shuffles, hardware math, bf16 / fp8 / MX packing, the
scaled MFMA, buffer and 64-bit global accesses, and the tagged-pair mailbox.
Adapted from ATOM's mono decode framework (atom/mono/device), itself ported
from the FlyDSL GLM-5 layer kernel.
Classes:
-
Mailbox–Tagged-pair hand-off between the CTAs of one launch (device scope).
Functions:
-
atomic_add_dev–Device-scope atomic add on the global i32 at
addr; the old value. -
batched_rounds–A CTA's strided loop over v = tid + THREADS i < n:
batchrounds' loads -
bf16_pair–Two f32 -> one f32-typed word holding (bf16(a), bf16(b)).
-
butterfly–Butterfly reduction over the xor
offsets, in that order. -
ceil_exp–Biased exponent of the smallest power of two >= v (v > 0 finite, normal).
-
div_rn–x / drounded to nearest (IEEE), givenr = RN(1 / d). -
first_task–CTA
bid's first task of a stage placed from CTAbase, then every -
fp8_pack4–Four f32 (in range) -> one dword of four E4M3 bytes, RNE.
-
fp8x8_bf16–Eight E4M3 of two dwords times an f32
scale-> eight bf16 as a -
fresh–vthrough an opaque move, so the lane math built on it is computed at -
gload–wordsi32 (1, 2 or 4) at a 64-bit global address: a cache past 4 GiB is -
gstore–Store
wordsi32 (an Int32 or a Vector) at a 64-bit global address. -
kernel_symbol–Kernel symbol that profilers demangle to
aiter::<stem>_<params>. -
lane_gather–Lane
src_lane's value of v (ds_bpermute). -
ld_i64–Element i of an i64 buffer (two dwords).
-
memrealtime–s_memrealtime: the 100 MHz device clock.
-
mfma_bf16–16x16x32 bf16 MFMA: lane l (row / col l % 16) holds K 8 (l // 16) .. +8.
-
mfma_scaled–16x16x128 scaled MFMA (gfx950 f8f6f4): lane l (row l % 16) holds K bytes
-
mx_code–VLLM's ROCm MXFP8 scale code of a 32-group: clamp(ceil(log2(amax / 448))
-
mx_mul–2^(127 - code) as fp32, for code in [0, 254] (the quant multiplier).
-
pow2–The fp32 power of two a biased exponent (1..254) stands for.
-
rcp_pow2–1 /
pow2(code), exact for code in [1, 253]. -
row_shr–Lane - k of the same 16-lane row, else 0 (DPP row_shr).
-
spin_until–load()again (s_sleep between) whilepending(value); the value. -
stamp–A timeline build's stamp: thread 0 of the CTA stores the clock at
-
tile_map–(tid // width, tid % width) for 0 <= tid < THREADS without a division:
-
traced–FlyDSL's AST rewrite for a helper, so its
if/while/foron -
xshfl–Value of lane
lane ^ off(DPP in a row, permlane swaps across).
Mailbox
¶
Tagged-pair hand-off between the CTAs of one launch (device scope).
Every 32-bit value is stored next to the launch's tag in one 8-byte store; a
consumer polls until every tag matches, so a hand-off is one round trip with
no separate flag. The tag is epoch * 64 + layer + 1 (epoch a device
counter bumped once a step): a pair from an earlier layer or step never
carries a live tag, and nothing is cleared between launches.
Methods:
-
poll–Batched poll of
specs= [(base, pair index, npairs in {1, 2})]: all -
put_bf–Elements i .. i + len(vs) (2 or 4, i aligned) as bf16 pairs, one store.
-
put_words–Raw 32-bit words (1 or 2) at pairs i.. (fp8 words must come this way).
Source code in vllm/models/deepseek_v41/amd/mono/attention/device.py
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 | |
poll(specs, batch=POLL_MAX)
¶
Batched poll of specs = [(base, pair index, npairs in {1, 2})]: all
re-loaded while any tag is stale. Returns the value words.
Source code in vllm/models/deepseek_v41/amd/mono/attention/device.py
put_bf(base, i, vs)
¶
Elements i .. i + len(vs) (2 or 4, i aligned) as bf16 pairs, one store.
Source code in vllm/models/deepseek_v41/amd/mono/attention/device.py
put_words(base, i, words)
¶
Raw 32-bit words (1 or 2) at pairs i.. (fp8 words must come this way).
Source code in vllm/models/deepseek_v41/amd/mono/attention/device.py
atomic_add_dev(addr, value)
¶
Device-scope atomic add on the global i32 at addr; the old value.
Relaxed: an acquire / release here would write back and invalidate L2.
Source code in vllm/models/deepseek_v41/amd/mono/attention/device.py
batched_rounds(tid, n, load, use, batch=LOAD_BATCH)
¶
A CTA's strided loop over v = tid + THREADS i < n: batch rounds' loads
issued before their uses; a round past n loads element n - 1, uses nothing.
Source code in vllm/models/deepseek_v41/amd/mono/attention/device.py
bf16_pair(a, b)
¶
Two f32 -> one f32-typed word holding (bf16(a), bf16(b)).
butterfly(v, offsets, op=_add)
¶
ceil_exp(v)
¶
Biased exponent of the smallest power of two >= v (v > 0 finite, normal).
Source code in vllm/models/deepseek_v41/amd/mono/attention/device.py
div_rn(x, d, r)
¶
first_task(bid, base)
¶
CTA bid's first task of a stage placed from CTA base, then every
BLOCKS-th.
fp8_pack4(a, b, c, d)
¶
Four f32 (in range) -> one dword of four E4M3 bytes, RNE.
Source code in vllm/models/deepseek_v41/amd/mono/attention/device.py
fp8x8_bf16(w0, w1, scale)
¶
Eight E4M3 of two dwords times an f32 scale -> eight bf16 as a
4-dword Vector (exact for a power-of-two scale in range).
Source code in vllm/models/deepseek_v41/amd/mono/attention/device.py
fresh(v)
¶
v through an opaque move, so the lane math built on it is computed at
its use instead of being hoisted out of a task loop and held in registers.
Source code in vllm/models/deepseek_v41/amd/mono/attention/device.py
gload(addr, words=4, nt=False)
¶
words i32 (1, 2 or 4) at a 64-bit global address: a cache past 4 GiB is
reachable only this way (a buffer offset is 32 bits).
Source code in vllm/models/deepseek_v41/amd/mono/attention/device.py
gstore(addr, value, words=4)
¶
Store words i32 (an Int32 or a Vector) at a 64-bit global address.
Source code in vllm/models/deepseek_v41/amd/mono/attention/device.py
kernel_symbol(stem, **params)
¶
Kernel symbol that profilers demangle to aiter::<stem>_<params>.
lane_gather(v, src_lane)
¶
Lane src_lane's value of v (ds_bpermute).
ld_i64(ptr, i)
¶
Element i of an i64 buffer (two dwords).
memrealtime()
¶
mfma_bf16(a, b, c)
¶
16x16x32 bf16 MFMA: lane l (row / col l % 16) holds K 8 (l // 16) .. +8.
mfma_scaled(a, b, c, sa, sb, a_fmt=FP8, b_fmt=FP8)
¶
16x16x128 scaled MFMA (gfx950 f8f6f4): lane l (row l % 16) holds K bytes 16 (l // 16) .. +16 and 64 + 16 (l // 16) .. +16 of the 128-step, and the E8M0 of K block l // 16.
Source code in vllm/models/deepseek_v41/amd/mono/attention/device.py
mx_code(amax)
¶
VLLM's ROCm MXFP8 scale code of a 32-group: clamp(ceil(log2(amax / 448)) + 127, 0, 254), amax floored at FLT_MIN.
Source code in vllm/models/deepseek_v41/amd/mono/attention/device.py
mx_mul(code)
¶
pow2(code)
¶
rcp_pow2(code)
¶
row_shr(v, lane, k)
¶
Lane - k of the same 16-lane row, else 0 (DPP row_shr).
Source code in vllm/models/deepseek_v41/amd/mono/attention/device.py
spin_until(load, pending)
¶
stamp(c, point)
¶
A timeline build's stamp: thread 0 of the CTA stores the clock at
point (c["tl"] [BLOCKS, points] i64; None: not a timeline build).
Source code in vllm/models/deepseek_v41/amd/mono/attention/device.py
tile_map(tid, width)
¶
(tid // width, tid % width) for 0 <= tid < THREADS without a division: shifts for a power of two, else a multiply-shift exact on that range.
Source code in vllm/models/deepseek_v41/amd/mono/attention/device.py
traced(fn)
¶
FlyDSL's AST rewrite for a helper, so its if / while / for on
traced values lower to scf ops (kernels rewrite only their own body).
xshfl(v, off)
¶
Value of lane lane ^ off (DPP in a row, permlane swaps across).