@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', 'BK', 'BV'])
@triton.jit
def chunk_simple_gla_bwd_kernel_dv(q, k, g, do, dv, dh, offsets, indices,
    scale, 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:
        b_g = tl.load(g + i_bh * T + i_t * BT + tl.arange(0, BT))
        b_g_last = tl.load(g + i_bh * T + min(i_t * BT + BT, T) - 1)
    else:
        b_g = tl.load(g + (bos + i_t * BT + tl.arange(0, BT)) * H + i_h)
        b_g_last = tl.load(g + (bos + min(i_t * BT + BT, T) - 1) * H + i_h)
    b_dv = tl.zeros([BT, BV], dtype=tl.float32)
    for i_k in range(tl.cdiv(K, BK)):
        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_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_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_dh = tl.load(p_dh, boundary_check=(0, 1))
        b_dv += tl.dot(b_k, b_dh.to(b_k.dtype)) * tl.exp(-b_g + b_g_last)[:,
            None]
    b_A = tl.zeros([BT, BT], dtype=tl.float32)
    for i_k in range(tl.cdiv(K, BK)):
        if HEAD_FIRST:
            p_q = tl.make_block_ptr(q + i_bh * T * K, (K, T), (1, K), (i_k *
                BK, i_t * BT), (BK, BT), (0, 1))
            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))
        else:
            p_q = tl.make_block_ptr(q + (bos * H + i_h) * K, (K, T), (1, H *
                K), (i_k * BK, i_t * BT), (BK, BT), (0, 1))
            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))
        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_k, b_q, allow_tf32=False)
    mask = (tl.arange(0, BT)[:, None] <= tl.arange(0, BT)[None, :]) & (i_t *
        BT + tl.arange(0, BT) < T)
    b_A = b_A * tl.exp(b_g[None, :] - b_g[:, None]) * scale
    b_A = tl.where(mask, b_A, 0).to(do.dtype.element_ty)
    if HEAD_FIRST:
        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_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_do = tl.load(p_do, boundary_check=(0, 1))
    b_dv += tl.dot(b_A, b_do)
    tl.store(p_dv, b_dv.to(p_dv.dtype.element_ty), boundary_check=(0, 1))
