@triton.heuristics({'USE_OFFSETS': lambda args: args['offsets'] is not None})
@triton.autotune(configs=[triton.Config({}, num_warps=num_warps, num_stages
    =num_stages) for num_warps in [2, 4] for num_stages in [2, 3, 4]], key=
    ['BT'])
@triton.jit
def chunk_gsa_bwd_k_kernel_dqkvg(q, k, v, h, g, A, do, dh, dq, dk, dv, dg,
    dgv, dA, offsets, indices, scale, B: tl.constexpr, T: tl.constexpr, HQ:
    tl.constexpr, H: tl.constexpr, K: tl.constexpr, V: tl.constexpr, BT: tl
    .constexpr, BK: tl.constexpr, BV: tl.constexpr, NG: tl.constexpr,
    USE_OFFSETS: tl.constexpr, HEAD_FIRST: tl.constexpr):
    i_k, i_t, 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
    if USE_OFFSETS:
        i_tg = i_t
        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
        NT = tl.cdiv(T, BT)
    else:
        NT = tl.cdiv(T, BT)
        i_tg = i_b * NT + i_t
        bos, eos = i_b * T, i_b * T + T
        all = B * T
    o_i = tl.arange(0, BT)
    o_t = min(i_t * BT + BT, T)
    m_s = o_i[:, None] >= o_i[None, :]
    if HEAD_FIRST:
        p_q = tl.make_block_ptr(q + i_bh * T * K, (T, K), (K, 1), (i_t * BT,
            i_k * BK), (BT, BK), (1, 0))
        p_k = tl.make_block_ptr(k + i_bg * T * K, (T, K), (K, 1), (i_t * BT,
            i_k * BK), (BT, BK), (1, 0))
        p_A = tl.make_block_ptr(A + (i_k * B * H + i_bh) * T * BT, (T, BT),
            (BT, 1), (i_t * BT, 0), (BT, BT), (1, 0))
    else:
        p_q = tl.make_block_ptr(q + (bos * HQ + i_hq) * K, (T, K), (HQ * K,
            1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
        p_k = tl.make_block_ptr(k + (bos * H + i_h) * K, (T, K), (H * K, 1),
            (i_t * BT, i_k * BK), (BT, BK), (1, 0))
        p_A = tl.make_block_ptr(A + ((i_k * all + bos) * HQ + i_hq) * BT, (
            T, BT), (HQ * BT, 1), (i_t * BT, 0), (BT, BT), (1, 0))
    b_q = tl.load(p_q, boundary_check=(0, 1))
    b_k = tl.load(p_k, boundary_check=(0, 1))
    b_A = tl.dot((b_q * scale).to(b_q.dtype), tl.trans(b_k))
    b_A = tl.where(m_s, b_A, 0.0)
    tl.store(p_A, b_A.to(p_A.dtype.element_ty), boundary_check=(0, 1))
    b_dq = tl.zeros([BT, BK], dtype=tl.float32)
    b_dk = tl.zeros([BT, BK], dtype=tl.float32)
    for i_v in range(tl.cdiv(V, BV)):
        o_v = i_v * BV + tl.arange(0, BV)
        if HEAD_FIRST:
            p_v = tl.make_block_ptr(v + i_bg * T * V, (T, V), (V, 1), (i_t *
                BT, i_v * BV), (BT, BV), (1, 0))
            p_g = tl.make_block_ptr(g + i_bg * T * V, (T, V), (V, 1), (i_t *
                BT, i_v * BV), (BT, BV), (1, 0))
            p_gn = tl.max_contiguous(tl.multiple_of(g + i_bg * T * V + (o_t -
                1) * V + o_v, BV), BV)
            p_do = tl.make_block_ptr(do + i_bh * T * V, (T, V), (V, 1), (
                i_t * BT, i_v * BV), (BT, BV), (1, 0))
            p_dv = tl.make_block_ptr(dv + (i_k * B * H + i_bh) * T * V, (T,
                V), (V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
            p_dg = tl.make_block_ptr(dg + i_bh * T * V, (T, V), (V, 1), (
                i_t * BT, i_v * BV), (BT, BV), (1, 0))
            p_dgv = tl.make_block_ptr(dgv + (i_k * B * H + i_bh) * T * V, (
                T, V), (V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
            p_h = tl.make_block_ptr(h + i_bg * NT * K * V + i_t * K * V, (V,
                K), (1, V), (i_v * BV, i_k * BK), (BV, BK), (0, 1))
            p_dh = tl.make_block_ptr(dh + i_bh * NT * K * V + i_t * K * V,
                (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
        else:
            p_v = tl.make_block_ptr(v + (bos * H + i_h) * V, (T, V), (H * V,
                1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
            p_g = tl.make_block_ptr(g + (bos * H + i_h) * V, (T, V), (H * V,
                1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
            p_gn = tl.max_contiguous(tl.multiple_of(g + (bos + o_t - 1) * H *
                V + i_h * V + o_v, BV), BV)
            p_do = tl.make_block_ptr(do + (bos * HQ + i_hq) * V, (T, V), (
                HQ * V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
            p_dv = tl.make_block_ptr(dv + ((i_k * all + bos) * HQ + i_hq) *
                V, (T, V), (HQ * V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
            p_dg = tl.make_block_ptr(dg + (bos * HQ + i_hq) * V, (T, V), (
                HQ * V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
            p_dgv = tl.make_block_ptr(dgv + ((i_k * all + bos) * HQ + i_hq) *
                V, (T, V), (HQ * V, 1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
            p_h = tl.make_block_ptr(h + (i_tg * H + i_h) * K * V, (V, K), (
                1, V), (i_v * BV, i_k * BK), (BV, BK), (0, 1))
            p_dh = tl.make_block_ptr(dh + (i_tg * HQ + i_hq) * K * V, (K, V
                ), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
        m_v = o_v < V
        b_gn = tl.load(p_gn, mask=m_v, other=0)
        b_v = tl.load(p_v, boundary_check=(0, 1))
        b_g = tl.load(p_g, boundary_check=(0, 1))
        b_gv = tl.exp(b_gn[None, :] - b_g)
        b_h = tl.load(p_h, boundary_check=(0, 1))
        b_do = tl.load(p_do, boundary_check=(0, 1))
        b_do = (b_do * tl.exp(b_g) * scale).to(b_do.dtype)
        b_dh = tl.load(p_dh, boundary_check=(0, 1))
        b_dg = tl.sum(tl.trans(b_h) * b_dh, 0) * tl.exp(b_gn)
        b_dh = b_dh.to(b_k.dtype)
        b_dq += tl.dot(b_do, b_h.to(b_k.dtype))
        b_dk += tl.dot((b_v * b_gv).to(b_v.dtype), tl.trans(b_dh))
        b_dv = tl.dot(b_k, b_dh) * b_gv
        b_dg += tl.sum(b_dv * b_v, 0)
        if i_k == 0:
            b_dgv = tl.load(p_dg, boundary_check=(0, 1)) + b_dg[None, :]
        else:
            b_dgv = tl.zeros([BT, BV], dtype=tl.float32) + b_dg[None, :]
        tl.store(p_dgv, b_dgv.to(p_dgv.dtype.element_ty), boundary_check=(0, 1)
            )
        tl.store(p_dv, b_dv.to(p_dv.dtype.element_ty), boundary_check=(0, 1))
    if HEAD_FIRST:
        p_dA = tl.make_block_ptr(dA + i_bh * T * BT, (T, BT), (BT, 1), (i_t *
            BT, 0), (BT, BT), (1, 0))
        p_dq = tl.make_block_ptr(dq + i_bh * T * K, (T, K), (K, 1), (i_t *
            BT, i_k * BK), (BT, BK), (1, 0))
        p_dk = tl.make_block_ptr(dk + i_bh * T * K, (T, K), (K, 1), (i_t *
            BT, i_k * BK), (BT, BK), (1, 0))
    else:
        p_dA = tl.make_block_ptr(dA + (bos * HQ + i_hq) * BT, (T, BT), (HQ *
            BT, 1), (i_t * BT, 0), (BT, BT), (1, 0))
        p_dq = tl.make_block_ptr(dq + (bos * HQ + i_hq) * K, (T, K), (HQ *
            K, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
        p_dk = tl.make_block_ptr(dk + (bos * HQ + i_hq) * K, (T, K), (HQ *
            K, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
    b_dA = tl.load(p_dA, boundary_check=(0, 1))
    b_dq += tl.dot(b_dA, b_k)
    b_dk += tl.dot(tl.trans(b_dA).to(b_k.dtype), b_q)
    tl.store(p_dq, b_dq.to(p_dq.dtype.element_ty), boundary_check=(0, 1))
    tl.store(p_dk, b_dk.to(p_dk.dtype.element_ty), boundary_check=(0, 1))
