Attention Preparation Decode Step (SM100-class)
Experimental. cudnn.AttentionPrepareDecode (also cudnn.attention_prepare.frost.AttentionPrepareDecode, with
cudnn.attention_prepare.AttentionPrepareGeometry) prepares the operands of a paged attention for the tokens of a
decode step, as ONE CuTe DSL launch: per token the RMSNorm of every query and key head row, the rotation of its
rotary part by the token’s position, the gate rows copied beside the queries, and the keys and values written into
paged pools at the token’s slot.
A token whose slot is negative (the padding convention), or at or past pages * page_size, or that lies past the
slot vector’s length, writes no pool row; its q_out and gate_out rows are written like every other token’s.
It is a decode-step entry: an inference step with no backward, called by an engine that owns the tensors. Compute capability 10.x; a CuTe DSL >= 4.8.0.
Construction facts
Every compile-time fact is a construction fact. The geometry, eps, the position form with its section, the page
size and the two integer dtypes are constants of the compiled kernel, so compile() needs no operand and nothing
compiles inside a step. The page size is a constant because the kernel divides the slot by it (a shift for a power
of two; a run-time page size would be an integer division per key thread). An integer dtype is a load width. The
consequences: a pool whose page size is not the entry’s is declined by name; a positions or slot_mapping tensor
of the other integer dtype is a TypeError that names the entry’s dtype; and a caller that needs two page sizes or
two integer dtypes builds two entries. The slot vector’s LENGTH is a run-time argument: one compiled kernel serves
every length, the empty vector included.
Usage
One compiled kernel serves every token count and every layer of a model: the kernel takes every operand as its device address and every free stride as a byte count, and the token count is its grid. No shape, stride or alignment of an operand enters the compiled kernel, the compile key is the config alone, and a captured CUDA graph holds pointers, never values. The launch goes to torch’s current stream of the entry’s device. That device need not be the caller’s current one: the compile and the launch run under the entry’s device, and the caller’s current device is restored afterwards. There is no workspace; a warm call allocates nothing and reads no device memory to the host.
Every bf16 operand starts at a 16-byte aligned address, the integer operands at a multiple of their element size;
every operand lives on the entry’s device. No written operand (q_out, gate_out, the pools) may share a byte with
another operand. Two views that interleave in one allocation — the two halves of a packed cache — are served: the
check proves that they share no byte. A layout the check cannot separate is declined. A pool is written by one
thread block per head row, so its own rows may not share a byte either: a pool view with an expanded axis or with
rows that overlap by a part is declined.
Caller contracts the host cannot check (they are device data, and the entry never reads device memory): two
tokens of one call do not share a non-negative slot (two thread blocks would write one pool row); every position is a
row of cos_sin. A NaN or infinite element makes its own head row non-finite; every other row is unaffected.
Output ownership is per output. q_out / gate_out the caller passes are the caller’s and are returned as
given; rows past the call’s tokens are never written. An output the caller does not pass is the entry’s: one pair per
token count, allocated by compile(tokens) / outputs(tokens) or at the first call of that token count. Call
compile(tokens) for every token count before a CUDA-graph capture. Entry-owned outputs are valid until the entry’s
next call at that token count from ANY host thread. An entry-owned output is an operand of the launch like a passed
one: a call whose other operand overlaps an entry-owned output that the call does not pass is declined with
ValueError before the launch. The pools are always the caller’s.
A launch is ordered behind the stream work of an entry-owned pair’s allocation. The pair is allocated on torch’s
current stream of the entry’s device at that moment. The entry does not initialize it, but that stream can still write
it: under deterministic algorithms torch fills it, and a recycled block can carry a write the stream still owes it.
Every launch that writes bytes of the pair waits for that work on whatever stream it runs and however the tensor
reached the call: the entry’s own output, a tensor taken with outputs(tokens) and passed back as q_out /
gate_out or as a pool, whole or as a view, at this token count or another, by this entry or by another entry that
orders its launches the same way (through cudnn.frost.init_order; rule 16 of python/cudnn/frost/README.md lists
the entries that do and those that do not yet);
the test is one of addresses. The launch stream waits for an event recorded behind the allocation, without a host
wait and without synchronizing a stream or the device, so compile(tokens) may run on any stream; on the allocating
stream itself the event is already behind the launch and the wait is free, and a stream that was given the raw handle
of a destroyed allocating stream waits like any other. A captured call that finds the work still pending waits for
that one event on the host before its launch (a capturing stream cannot wait for an event recorded outside its
capture): set-up, never part of a replay, and not needed for a pair allocated before the capture once its stream’s
earlier work is done (torch.cuda.graph synchronizes when it is entered; a raw capture does not). Not ordered: a pair
allocated inside a stream capture (nothing is recorded: allocate before the capture). An allocation on
the legacy default stream while a blocking stream of another host thread captures raises torch’s error: allocate
before that capture or on a stream of your own. Once a call has seen the work complete nothing is kept of it; while
it is pending on the device, every call of such an entry on that device reads the device’s records. What stays the
caller’s: the operands a launch only reads, and the caller’s own reads and writes of a returned tensor outside a
call. outputs(tokens) returns a copy of the entry’s table (the same tensors) without waiting.
Everything the entry does not serve is declined by name. At construction, without torch and without the DSL:
TypeError for a geometry that is not an AttentionPrepareGeometry of int fields or a launch knob that is not a
bool; ValueError for a non-positive geometry field, an eps that is not a finite positive Python float or
int, a positions outside ("1d", "3d"), a missing, misplaced or inconsistent mrope_section, a page_size that
is not a positive power-of-two int, an integer dtype name outside ("int32", "int64"), pdl_release=True without
pdl=True; NotImplementedError for a HEAD_DIM or ROTARY_DIM / 2 that is not a power of two, a geometry other
than (24, 4, 256, 64) and a page size other than 64 or 16. Before every launch: TypeError for a non-tensor, a
bf16 operand of another dtype, or a positions / slot_mapping tensor whose integer dtype is not the entry’s;
ValueError for a shape, a stride that is not whole 16-byte vectors, an alignment, a device mismatch, an overlap (a
written operand with another operand, or a pool’s own rows), a
slot vector longer than the call, an empty call, and everything the paged-pool contract declines about the pools and
the slot vector (cudnn.gated_attention_block.cache_write_contract, with the entry’s page size: a pool of another
page size is named; that contract raises ValueError for a non-tensor and for a dtype too); NotImplementedError for
a token count above 1024, a cos_sin that is not bf16 (another table dtype is another byte class), an operand that
is not contiguous where the kernel addresses flat, a compute capability outside 10.x, and the CuTe DSL gates (not
installed, below 4.8.0, without the part’s target, or with a compiler that cannot assemble it). The paged-pool
contract’s package imports the CuTe DSL: on a host without the DSL a call that reaches the pool checks gets the typed
decline that names the missing DSL, after every check in front of them has run.
Numerics contract
The sum tree and the rounding chain are the contract, not a tolerance. Per head row, in fp32 on the bf16 operands:
- One thread block per (token, head) of
HEAD_DIM / 2threads in full warps; a thread owns two contiguous elements. Its partial sum of squares isfma(x0, x0, x1 * x1): one multiply and one fused multiply-add. - The row total is ONE fixed tree whose shape depends on the head width alone: inside each warp a butterfly with
the offsets 16, 8, 4, 2, 1; then over the warp sums (4 at
HEAD_DIM256) the offsets 2, 1. inv = rsqrt(total / HEAD_DIM + eps)with the part’s approximate full-range division (div.full.f32) and its approximate reciprocal square root with flush-to-zero (rsqrt.approx.ftz.f32), one addition between them.n = bf16( (x * inv) * (1 + weight) ):1 + weightis one fp32 addition, then two multiplications and ONE rounding to bf16 (nearest-even). This bf16 value is the normalized value of record: the pass-through elements store it and the rotation reads it.- The rotation: each rotated value is one fp32 multiply (the rounded product) and one fused multiply-add of two
bf16 values and that product, rounded once to bf16:
fma(n[r], cos, -(n[r + HALF] * sin))for the first half; for the second halffma(n[r + HALF], cos, n[r] * sin)under"3d"andfma(n[r], sin, n[r + HALF] * cos)under"1d". A product of two bf16 values is exact in fp32 unless it underflows, so the two orders of the second half write the same bytes on every operand whose products are normal fp32 values; where a product underflows (bf16 subnormals, magnitudes near the bottom of the fp32 range) they differ in the sign of a zero or in a subnormal’s last bit. Each position form keeps one order. - The gate rows and the value rows are bit copies.
What changes bytes: the geometry (the tree and the divisor), eps, the position form with its section (which
position row an index reads; under underflow also item 5), and the operand values. What never changes a byte: the
token count, a token’s place in the call, the other tokens, the page size and the integer dtypes (they select
addresses and load widths), pdl, pdl_release, and who owns an output. A token alone writes the bytes it writes
inside a call of up to 1024 tokens.
Against a PyTorch chain with the same rounding points whose mean and root are evaluated in fp64 (the suite’s twin),
every pass-through element is within one bf16 unit, the unit taken at the larger of the two magnitudes, and every
rotated element inside unit(out) + |cos| unit(n1) + |sin| unit(n2) (its two normalized operands may each be one
unit off): the root is approximate, so the kernel’s normalized value differs from the twin’s by a few parts in ten
million before its rounding. The gate rows, the value rows and every pool row no live slot names are bitwise.
The bytes follow the part’s div.full and rsqrt.approx.ftz units. Equal bytes across GPU architectures are not
part of the contract (see determinism).
Launch knobs and the caller’s obligations
Both knobs default to off and neither changes a byte (the suite compares their bytes).
pdl=True: the launch carries the programmatic-stream-serialization attribute and the kernel issuesgriddepcontrol.waitAFTER it has loaded its two weights and BEFORE it reads the slot, the head rows, the copied rows, the positions or the table, and before it writes anything. Obligation:q_weightandk_weightare not written by any launch that may still be running when this one starts (they are constants of the layer).pdl_release(Nonefollowspdl;Trueneedspdl=True):griddepcontrol.launch_dependentsas the kernel’s FIRST statement, in front of its own wait. Obligation: every launch that can start before this one completes issues its own wait before it readsq_out,gate_outor the pools and before it writes any operand of this call.
pdl=True alone is release + wait; pdl=True, pdl_release=False is wait only. The suite pins both positions in the
kernel’s text for every form and target: the release is in front of every global access, the load of the weights is
the only global access in front of the wait, and every other load and both stores follow it. On a device it runs
the step behind a predecessor that releases its dependents before it writes the slab, the positions and the slot
vector, and the release with dependent launches on the stream that wait and copy the outputs; each eager and
captured.
Launch facts
The entry states what a token count launches without launching it: three read-only answers, with cfg_for’s
argument.
A token count outside 1 .. 1024 is a typed decline (ValueError for one that is not a positive int,
NotImplementedError past 1024), and none of the three compiles, launches or touches device memory.
Support matrix
Tests: test/python/attention_prepare/frost/test_attention_prepare_decode.py (the construction declines with the
three construction facts, the binding declines, the overlap rule on interleaved views and on a pool’s own rows, the config’s limits and
tables, the rendering pinned per target, the launch facts against the configuration arithmetic and against the
profiler’s record of a launch, the device cells against the twin above, the fused product of each rotary half
bitwise on rows whose products underflow, edge rows, the slot predicate, output
ownership, the bit-neutral arms, the wait behind a predecessor that released early, the release beside dependent
launches, CUDA-graph replay, an entry on a device that is not the caller’s current one) and
test_attention_prepare_decode_determinism.py (replay, batch invariance, goldens). Source:
python/cudnn/attention_prepare/frost/. The kernel follows the thread shape and the arithmetic of a public
Apache-2.0 kernel; its module header and THIRD_PARTY_LICENSES.txt name the source.