@triton.heuristics({'USE_OFFSETS': lambda args: args['offsets'] is not None})
@triton.autotune(configs=[triton.Config({'BK': BK, 'BV': BV}, num_warps=
    num_warps) for BK in [32, 64] for BV in [64, 128] for num_warps in [2, 
    4, 8]], key=['BT'])
@triton.jit
def chunk_gla_bwd_kernel_dv(k, g, A, do, dh, dv, offsets, indices, T: tl.
    constexpr, H: tl.constexpr, K: tl.constexpr, V: tl.constexpr, BT: tl.
    constexpr, BK: tl.constexpr, BV: tl.constexpr, USE_OFFSETS: tl.
    constexpr, HEAD_FIRST: tl.constexpr):
    i_v, i_t, i_bh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
    i_b, i_h = i_bh // H, i_bh % H
    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)
        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
    if HEAD_FIRST:
        p_A = tl.make_block_ptr(A + i_bh * T * BT, (BT, T), (1, BT), (0, 
            i_t * BT), (BT, BT), (0, 1))
        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_bh * T * V, (T, V), (V, 1), (i_t *
            BT, i_v * BV), (BT, BV), (1, 0))
    else:
        p_A = tl.make_block_ptr(A + (bos * H + i_h) * BT, (BT, T), (1, H *
            BT), (0, i_t * BT), (BT, BT), (0, 1))
        p_do = tl.make_block_ptr(do + (bos * H + i_h) * V, (T, V), (H * V, 
            1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
        p_dv = tl.make_block_ptr(dv + (bos * H + i_h) * V, (T, V), (H * V, 
            1), (i_t * BT, i_v * BV), (BT, BV), (1, 0))
    b_A = tl.load(p_A, boundary_check=(0, 1))
    b_A = tl.where(tl.arange(0, BT)[:, None] <= tl.arange(0, BT)[None, :],
        b_A, 0.0)
    b_do = tl.load(p_do, boundary_check=(0, 1))
    b_dv = tl.dot(b_A, b_do.to(b_A.dtype), allow_tf32=False)
    for i_k in range(tl.cdiv(K, BK)):
        o_k = i_k * BK + tl.arange(0, BK)
        m_k = o_k < K
        if HEAD_FIRST:
            p_k = tl.make_block_ptr(k + i_bh * T * K, (T, K), (K, 1), (i_t *
                BT, i_k * BK), (BT, BK), (1, 0))
            p_gk = tl.make_block_ptr(g + i_bh * T * K, (T, K), (K, 1), (i_t *
                BT, i_k * BK), (BT, BK), (1, 0))
            p_gn = tl.max_contiguous(tl.multiple_of(g + i_bh * T * K + min(
                i_t * BT + BT, T) * K - K + o_k, BK), BK)
            p_dh = tl.make_block_ptr(dh + (i_bh * NT + i_t) * K * V, (K, V),
                (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
        else:
            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_gk = tl.make_block_ptr(g + (bos * H + i_h) * K, (T, K), (H *
                K, 1), (i_t * BT, i_k * BK), (BT, BK), (1, 0))
            p_gn = tl.max_contiguous(tl.multiple_of(g + (bos + min(i_t * BT +
                BT, T) - 1) * H * K + i_h * K + o_k, BK), BK)
            p_dh = tl.make_block_ptr(dh + (i_tg * H + i_h) * K * V, (K, V),
                (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
        b_k = tl.load(p_k, boundary_check=(0, 1))
        b_gk = tl.load(p_gk, boundary_check=(0, 1))
        b_gn = tl.exp(tl.load(p_gn, mask=m_k, other=0)[None, :] - b_gk)
        b_k = (b_k * b_gn).to(b_k.dtype)
        b_dh = tl.load(p_dh, boundary_check=(0, 1))
        b_dv += tl.dot(b_k, b_dh.to(b_k.dtype))
    tl.store(p_dv, b_dv.to(p_dv.dtype.element_ty), boundary_check=(0, 1))
