Expand description
PSA attention helper kernels and RuntimeOp wrappers.
Implements the data-rearrangement passes surrounding Flash Attention 2 for the PSABlock used in YOLO26 C2PSA layers.
Assumptions (derived from the ultralytics PSABlock / Attention module):
head_dim = c / num_heads = 2 * key_dim- QKV conv output has
qkv_h = num_heads * 4 * KEY_DIMchannels stored as NCHW[B, qkv_h, H, W]. - Per head
h, channels[h*4*KEY_DIM : +KEY_DIM]are Q,[+KEY_DIM : +2*KEY_DIM]are K,[+2*KEY_DIM : +3*KEY_DIM]are V_lo,[+3*KEY_DIM : +4*KEY_DIM]are V_hi.
The V split trick lets us run FA2 with HEAD_DIM = key_dim twice (once for
V_lo, once for V_hi) instead of needing HEAD_DIM = head_dim = 2*key_dim.
Structs§
- Flash
Attn2 PsaOp - CustomOp for
FlashAttn2PsaRuntimeOp(V_lo or V_hi section). - Flash
Attn2 PsaRuntime Op - RuntimeOp wrapping
flash_attention2_forwardfor PSA attention. - PsaExtractV
Backward - Backward of
psa_extract_v_nchw: scattersd_vback to V_lo / V_hi channels ofd_qkv. - PsaExtractV
Nchw - Extracts V channels from QKV NCHW into V NCHW.
- PsaExtractV
Op - CustomOp for
PsaExtractVRuntimeOp. - PsaExtractV
Runtime Op - Runtime dispatch for the PSA V-extraction kernel (forward + backward).
- PsaFa2
Backward - Combined PSA Flash Attention 2 backward pass.
- PsaMerge
Attn Backward - Backward of
psa_merge_attn_nchw: scattersd_mergedback tod_loandd_hiflat buffers. - PsaMerge
Attn Nchw - Merges two FA2 outputs (V_lo and V_hi attention results) into NCHW.
- PsaMerge
Attn Op - CustomOp for
PsaMergeAttnRuntimeOp. - PsaMerge
Attn Runtime Op - Runtime dispatch for the PSA attention-merge kernel (forward + backward).
- PsaPack
Qkv - Rearranges the NCHW QKV tensor into packed FA2 format.
- PsaPack
QkvBackward - Backward of
psa_pack_qkv: scattersd_packedback tod_qkv. - PsaPack
QkvOp - CustomOp for
PsaPackQkvRuntimeOp. - PsaPack
QkvRuntime Op - Runtime dispatch for the PSA QKV-packing kernel (forward + backward).
Functions§
- psa_
extract_ v_ backward - Backward of
psa_extract_v_nchw: scattersd_vback to V_lo / V_hi channels ofd_qkv. - psa_
extract_ v_ nchw - Extracts V channels from QKV NCHW into V NCHW.
- psa_
fa2_ backward - Combined PSA Flash Attention 2 backward pass.
- psa_
merge_ attn_ backward - Backward of
psa_merge_attn_nchw: scattersd_mergedback tod_loandd_hiflat buffers. - psa_
merge_ attn_ nchw - Merges two FA2 outputs (V_lo and V_hi attention results) into NCHW.
- psa_
pack_ qkv - Rearranges the NCHW QKV tensor into packed FA2 format.
- psa_
pack_ qkv_ backward - Backward of
psa_pack_qkv: scattersd_packedback tod_qkv.