@triton.heuristics({'USE_OFFSETS': lambda args: args['offsets'] is not None})
@triton.autotune(configs=[triton.Config({}, num_warps=num_warps) for
    num_warps in [2, 4, 8]], key=['BT'])
@triton.jit
def chunk_gsa_bwd_k_kernel_dA(v, g, do, dA, indices, offsets, scale, B: tl.
    constexpr, T: tl.constexpr, HQ: tl.constexpr, H: tl.constexpr, V: tl.
    constexpr, BT: tl.constexpr, BC: tl.constexpr, BV: tl.constexpr, NC: tl
    .constexpr, NG: tl.constexpr, USE_OFFSETS: tl.constexpr, HEAD_FIRST: tl
    .constexpr):
    i_v, i_c, i_bh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
    i_bg = i_bh // NG
    i_b, i_hq = i_bh // HQ, i_bh % HQ
    i_h = i_hq // NG
    i_t, i_i, i_j = i_c // (NC * NC), i_c % (NC * NC) // NC, i_c % (NC * NC
        ) % NC
    if USE_OFFSETS:
        i_n, i_t = tl.load(indices + i_t * 2).to(tl.int32), tl.load(indices +
            i_t * 2 + 1).to(tl.int32)
        bos, eos = tl.load(offsets + i_n).to(tl.int32), tl.load(offsets +
            i_n + 1).to(tl.int32)
        all = T
        T = eos - bos
    else:
        bos, eos = i_b * T, i_b * T + T
        all = B * T
    o_v = i_v * BV + tl.arange(0, BV)
    m_v = o_v < V
    if i_t * BT + i_i * BC > T:
        return
    if HEAD_FIRST:
        p_dA = tl.make_block_ptr(dA + (i_v * B * H + i_bh) * T * BT, (T, BT
            ), (BT, 1), (i_t * BT + i_i * BC, i_j * BC), (BC, BC), (1, 0))
    else:
        p_dA = tl.make_block_ptr(dA + ((i_v * all + bos) * HQ + i_hq) * BT,
            (T, BT), (HQ * BT, 1), (i_t * BT + i_i * BC, i_j * BC), (BC, BC
            ), (1, 0))
    b_dA = tl.zeros([BC, BC], dtype=tl.float32)
    if i_i > i_j:
        if HEAD_FIRST:
            p_v = tl.make_block_ptr(v + i_bg * T * V, (V, T), (1, V), (i_v *
                BV, i_t * BT + i_j * BC), (BV, BC), (0, 1))
            p_gv = tl.make_block_ptr(g + i_bg * T * V, (V, T), (1, V), (i_v *
                BV, i_t * BT + i_j * BC), (BV, BC), (0, 1))
            p_gn = tl.max_contiguous(tl.multiple_of(g + i_bg * T * V + (i_t *
                BT + i_i * BC) * V + o_v, BV), BV)
            p_g = tl.make_block_ptr(g + i_bg * T * V, (T, V), (V, 1), (i_t *
                BT + i_i * BC, i_v * BV), (BC, BV), (1, 0))
            p_do = tl.make_block_ptr(do + i_bh * T * V, (T, V), (V, 1), (
                i_t * BT + i_i * BC, i_v * BV), (BC, BV), (1, 0))
        else:
            p_v = tl.make_block_ptr(v + (bos * H + i_h) * V, (V, T), (1, H *
                V), (i_v * BV, i_t * BT + i_j * BC), (BV, BC), (0, 1))
            p_gv = tl.make_block_ptr(g + (bos * H + i_h) * V, (V, T), (1, H *
                V), (i_v * BV, i_t * BT + i_j * BC), (BV, BC), (0, 1))
            p_gn = tl.max_contiguous(tl.multiple_of(g + (bos + i_t * BT + 
                i_i * BC) * H * V + i_h * V + o_v, BV), BV)
            p_g = tl.make_block_ptr(g + (bos * H + i_h) * V, (T, V), (H * V,
                1), (i_t * BT + i_i * BC, i_v * BV), (BC, BV), (1, 0))
            p_do = tl.make_block_ptr(do + (bos * HQ + i_hq) * V, (T, V), (
                HQ * V, 1), (i_t * BT + i_i * BC, i_v * BV), (BC, BV), (1, 0))
        b_gn = tl.load(p_gn, mask=m_v, other=0.0)
        b_g = tl.load(p_g, boundary_check=(0, 1))
        b_do = tl.load(p_do, boundary_check=(0, 1))
        b_do = (b_do * tl.exp(b_g - b_gn[None, :]) * scale).to(b_do.dtype)
        b_v = tl.load(p_v, boundary_check=(0, 1))
        b_gv = tl.load(p_gv, boundary_check=(0, 1))
        b_vg = (b_v * tl.exp(b_gn[:, None] - b_gv)).to(b_v.dtype)
        b_dA = tl.dot(b_do, b_vg)
    elif i_i == i_j:
        if HEAD_FIRST:
            p_g = tl.make_block_ptr(g + i_bg * T * V, (T, V), (V, 1), (i_t *
                BT + i_i * BC, i_v * BV), (BC, BV), (1, 0))
            p_do = tl.make_block_ptr(do + i_bh * T * V, (T, V), (V, 1), (
                i_t * BT + i_i * BC, i_v * BV), (BC, BV), (1, 0))
            p_v = tl.max_contiguous(tl.multiple_of(v + i_bg * T * V + (i_t *
                BT + i_j * BC) * V + o_v, BV), BV)
            p_gv = tl.max_contiguous(tl.multiple_of(g + i_bg * T * V + (i_t *
                BT + i_j * BC) * V + o_v, BV), BV)
        else:
            p_g = tl.make_block_ptr(g + (bos * H + i_h) * V, (T, V), (H * V,
                1), (i_t * BT + i_i * BC, i_v * BV), (BC, BV), (1, 0))
            p_do = tl.make_block_ptr(do + (bos * HQ + i_hq) * V, (T, V), (
                HQ * V, 1), (i_t * BT + i_i * BC, i_v * BV), (BC, BV), (1, 0))
            p_v = tl.max_contiguous(tl.multiple_of(v + (bos + i_t * BT + 
                i_j * BC) * H * V + i_h * V + o_v, BV), BV)
            p_gv = tl.max_contiguous(tl.multiple_of(g + (bos + i_t * BT + 
                i_j * BC) * H * V + i_h * V + o_v, BV), BV)
        b_g = tl.load(p_g, boundary_check=(0, 1))
        b_do = tl.load(p_do, boundary_check=(0, 1)) * scale
        m_v = o_v < V
        o_i = tl.arange(0, BC)
        m_dA = o_i[:, None] >= o_i[None, :]
        for j in range(0, min(BC, T - i_t * BT - i_j * BC)):
            b_v = tl.load(p_v, mask=m_v, other=0).to(tl.float32)
            b_gv = tl.load(p_gv, mask=m_v, other=0).to(tl.float32)
            b_dAj = tl.sum(b_do * b_v[None, :] * tl.exp(b_g - b_gv[None, :]), 1
                )
            b_dA = tl.where((o_i == j)[None, :], b_dAj[:, None], b_dA)
            p_v += (1 if HEAD_FIRST else H) * V
            p_gv += (1 if HEAD_FIRST else H) * V
        b_dA = tl.where(m_dA, b_dA, 0.0)
    tl.store(p_dA, b_dA.to(dA.dtype.element_ty), boundary_check=(0, 1))
