@triton.heuristics({'STORE_INITIAL_STATE_GRADIENT': lambda args: args['dh0'
    ] is not None, 'USE_OFFSETS': lambda args: args['offsets'] is not None})
@triton.autotune(configs=[triton.Config({'BK': BK, 'BV': BV}, num_warps=
    num_warps, num_stages=num_stages) for BK in [32, 64] for BV in [32, 64] for
    num_warps in [2, 4, 8] for num_stages in [2, 3, 4]], key=['BT', 'USE_G',
    'USE_GK', 'USE_GV'])
@triton.jit
def chunk_bwd_kernel_dh_reduction(g, gk, gv, dhs, dhr, dh0, offsets,
    split_offsets, T: tl.constexpr, S: tl.constexpr, H: tl.constexpr, HQ:
    tl.constexpr, K: tl.constexpr, V: tl.constexpr, BT: tl.constexpr, BK:
    tl.constexpr, BV: tl.constexpr, NG: tl.constexpr, USE_G: tl.constexpr,
    USE_GK: tl.constexpr, USE_GV: tl.constexpr,
    STORE_INITIAL_STATE_GRADIENT: tl.constexpr, USE_OFFSETS: tl.constexpr,
    HEAD_FIRST: tl.constexpr):
    i_k, i_v, i_nh = tl.program_id(0), tl.program_id(1), tl.program_id(2)
    i_n, i_hq = i_nh // HQ, i_nh % HQ
    i_ng, i_h = i_nh // NG, i_hq // NG
    if USE_OFFSETS:
        bos, eos = tl.load(offsets + i_n).to(tl.int32), tl.load(offsets +
            i_n + 1).to(tl.int32)
        T = eos - bos
        NS = tl.cdiv(T, S)
        boh = tl.load(split_offsets + i_n).to(tl.int32)
    else:
        bos, eos = i_n * T, i_n * T + T
        NS = tl.cdiv(T, S)
        boh = i_n * NS
    b_dh = tl.zeros([BK, BV], dtype=tl.float32)
    for i_s in range(NS - 2, -1, -1):
        p_dhs = tl.make_block_ptr(dhs + ((boh + i_s + 1) * H + i_h) * K * V,
            (K, V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
        p_dhr = tl.make_block_ptr(dhr + ((boh + i_s) * H + i_h) * K * V, (K,
            V), (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
        b_dh += tl.load(p_dhs, boundary_check=(0, 1)).to(tl.float32)
        tl.store(p_dhr, b_dh.to(p_dhr.dtype.element_ty), boundary_check=(0, 1))
        for i_t in range(tl.cdiv(min(i_s * S + S, T), BT) - 1, tl.cdiv(i_s *
            S, BT) - 1, -1):
            last_idx = min(i_t * BT + BT, T) - 1
            if USE_G:
                if HEAD_FIRST:
                    b_g_last = tl.load(g + i_ng * T + last_idx)
                else:
                    b_g_last = tl.load(g + (bos + last_idx) * H + i_h)
                b_dh *= tl.exp(b_g_last)
            if USE_GK:
                if HEAD_FIRST:
                    p_gk_last = gk + (i_ng * T + last_idx
                        ) * K + i_k * BK + tl.arange(0, BK)
                else:
                    p_gk_last = gk + (bos + last_idx
                        ) * H * K + i_h * K + i_k * BK + tl.arange(0, BK)
                p_gk_last = tl.max_contiguous(tl.multiple_of(p_gk_last, BK), BK
                    )
                b_gk_last = tl.load(p_gk_last, mask=i_k * BK + tl.arange(0,
                    BK) < K, other=0.0)
                b_dh *= tl.exp(b_gk_last)[:, None]
            if USE_GV:
                if HEAD_FIRST:
                    p_gv_last = gv + (i_ng * T + last_idx
                        ) * V + i_v * BV + tl.arange(0, BV)
                else:
                    p_gv_last = gv + (bos + last_idx
                        ) * H * V + i_h * V + i_v * BV + tl.arange(0, BV)
                p_gv_last = tl.max_contiguous(tl.multiple_of(p_gv_last, BV), BV
                    )
                b_gv_last = tl.load(p_gv_last, mask=i_v * BV + tl.arange(0,
                    BV) < V, other=0.0)
                b_dh *= tl.exp(b_gv_last)[None, :]
    if NS > 1:
        if STORE_INITIAL_STATE_GRADIENT:
            p_dhs = tl.make_block_ptr(dhs + (boh * H + i_h) * K * V, (K, V),
                (V, 1), (i_k * BK, i_v * BV), (BK, BV), (1, 0))
            p_dh0 = tl.make_block_ptr(dh0 + i_nh * K * V, (K, V), (V, 1), (
                i_k * BK, i_v * BV), (BK, BV), (1, 0))
            b_dh += tl.load(p_dhs, boundary_check=(0, 1)).to(tl.float32)
            tl.store(p_dh0, b_dh.to(p_dh0.dtype.element_ty), boundary_check
                =(0, 1))
