Skip to main content

flash_attention2_backward_dq

Function flash_attention2_backward_dq 

Source
pub fn flash_attention2_backward_dq<T: Triton, D: Float, const HEAD_DIM: i32>(
    q_ptr: T::Pointer<D>,
    k_ptr: T::Pointer<D>,
    v_ptr: T::Pointer<D>,
    o_ptr: T::Pointer<D>,
    do_ptr: T::Pointer<D>,
    l_ptr: T::Pointer<D>,
    dq_ptr: T::Pointer<D>,
    n_ctx_q: i32,
    n_ctx_k: i32,
    softmax_scale: f32,
)
where T::I32Tensor: Tensor<i32, 1> + Comparison<i32, BoolTensor = T::BoolTensor>, T::Pointer<D>: AddOffsets<i32, 1, T::I32Tensor, Output = T::Tensor<T::Pointer<D>>>,
Expand description

Flash Attention 2 backward: computes dQ.

For each query row q, iterates over all key rows k and accumulates:

dQ_q += dS_{qk} * K_k * scale
where dS_{qk} = p_{qk} * (dO_q · V_k − D_q)
      p_{qk}  = exp(Q_q · K_k * scale − L_q)   (recomputed attention)
      D_q     = sum(O_q * dO_q)                 (per-row scalar)

Grid: (N_CTX_Q, BH, 1) — same grid shape as the forward pass.