@triton.heuristics({'USE_OFFSETS': lambda args: args['offsets'] is not None})
@triton.jit
def chunk_gsa_fwd_k_kernel_intra(v, g, o, A, offsets, indices, 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_c // NC, i_c % 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)
        T = eos - bos
    else:
        bos, eos = i_b * T, i_b * T + 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_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_gn = tl.max_contiguous(tl.multiple_of(g + i_bg * T * V + min(i_t *
            BT + i_i * BC, T) * 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_gn = tl.max_contiguous(tl.multiple_of(g + (bos + min(i_t * BT + 
            i_i * BC, T)) * H * V + i_h * V + o_v, BV), BV)
    b_gn = tl.load(p_gn, mask=m_v, other=0)
    b_o = tl.zeros([BC, BV], dtype=tl.float32)
    for i_j in range(0, i_i):
        if HEAD_FIRST:
            p_A = tl.make_block_ptr(A + i_bh * T * BT, (T, BT), (BT, 1), (
                i_t * BT + i_i * BC, i_j * BC), (BC, BC), (1, 0))
            p_v = tl.make_block_ptr(v + i_bg * T * V, (T, V), (V, 1), (i_t *
                BT + i_j * BC, i_v * BV), (BC, BV), (1, 0))
            p_gv = tl.make_block_ptr(g + i_bg * T * V, (T, V), (V, 1), (i_t *
                BT + i_j * BC, i_v * BV), (BC, BV), (1, 0))
        else:
            p_A = tl.make_block_ptr(A + (bos * HQ + i_hq) * BT, (T, BT), (
                HQ * BT, 1), (i_t * BT + i_i * BC, i_j * BC), (BC, BC), (1, 0))
            p_v = tl.make_block_ptr(v + (bos * H + i_h) * V, (T, V), (H * V,
                1), (i_t * BT + i_j * BC, i_v * BV), (BC, BV), (1, 0))
            p_gv = tl.make_block_ptr(g + (bos * H + i_h) * V, (T, V), (H *
                V, 1), (i_t * BT + i_j * BC, i_v * BV), (BC, BV), (1, 0))
        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_A = tl.load(p_A, boundary_check=(0, 1))
        b_o += tl.dot(b_A, b_vg)
    b_g = tl.load(p_g, boundary_check=(0, 1))
    b_o *= tl.exp(b_g - b_gn[None, :])
    o_i = tl.arange(0, BC)
    if HEAD_FIRST:
        o_A = i_bh * T * BT + (i_t * BT + i_i * BC + tl.arange(0, BC)
            ) * BT + i_i * BC
    else:
        o_A = (bos + i_t * BT + i_i * BC + tl.arange(0, BC)
            ) * HQ * BT + i_hq * BT + i_i * BC
    m_A = i_t * BT + i_i * BC + tl.arange(0, BC) < T
    for j in range(0, min(BC, T - i_t * BT - i_i * BC)):
        if HEAD_FIRST:
            p_v = tl.max_contiguous(tl.multiple_of(v + i_bg * T * V + (i_t *
                BT + i_i * BC + j) * V + o_v, BV), BV)
            p_gv = tl.max_contiguous(tl.multiple_of(g + i_bg * T * V + (i_t *
                BT + i_i * BC + j) * V + o_v, BV), BV)
        else:
            p_v = tl.max_contiguous(tl.multiple_of(v + (bos + i_t * BT + 
                i_i * BC + j) * H * V + i_h * V + o_v, BV), BV)
            p_gv = tl.max_contiguous(tl.multiple_of(g + (bos + i_t * BT + 
                i_i * BC + j) * H * V + i_h * V + o_v, BV), BV)
        b_A = tl.load(A + o_A + j, mask=m_A, other=0)
        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_vg = b_v[None, :] * tl.exp(b_g - b_gv[None, :])
        b_o += tl.where(o_i[:, None] >= j, b_A[:, None] * b_vg, 0.0)
    if HEAD_FIRST:
        p_o = tl.make_block_ptr(o + i_bh * T * V, (T, V), (V, 1), (i_t * BT +
            i_i * BC, i_v * BV), (BC, BV), (1, 0))
    else:
        p_o = tl.make_block_ptr(o + (bos * HQ + i_hq) * V, (T, V), (HQ * V,
            1), (i_t * BT + i_i * BC, i_v * BV), (BC, BV), (1, 0))
    b_o += tl.load(p_o, boundary_check=(0, 1))
    tl.store(p_o, b_o.to(p_o.dtype.element_ty), boundary_check=(0, 1))
