Emulate batch-invariant decode attention in NumPy: a sequence's output must be bitwise identical whatever the batch size.
All functions compute over the keys in contiguous blocks of block rows (the last may be shorter). A block's partial is : its score maximum, and . Two partials merge as
Use the scale 1.0 / float(np.sqrt(d)) and keep the input dtype; tests compare with np.array_equal.
attention_single_pass(q, K, V, block): q (d,), K, V (n, d). Start from the first block's partial, merge each later block in order, return , shape (d,).
attention_split_kv(q, K, V, block, num_splits): split the blocks into num_splits contiguous groups of (later groups may be empty); each group computes its block partials; then merge all block partials in global block order, exactly as the single pass, and return . Must be bitwise equal to attention_single_pass for any num_splits.
batch_invariant_attention(Q, K, V, block, num_sms): Q (B, d), K, V (B, n, d). If B >= num_sms use the single-pass kernel per sequence; otherwise the split kernel with num_splits = max(1, min(num_blocks, num_sms // B)). Return (out, kernel): out (B, d) in K's dtype, kernel "single" or "split".