@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=['BK'])
@triton.jit
def fwd_prepare_wy_repr_kernel_chunk32(k, g, beta, Aw, Au, offsets, indices,
    T: tl.constexpr, K: tl.constexpr, H: tl.constexpr, BT: tl.constexpr, BK:
    tl.constexpr, BC: tl.constexpr, USE_OFFSETS: tl.constexpr, HEAD_FIRST:
    tl.constexpr):
    i_t, i_bh = tl.program_id(0), tl.program_id(1)
    i_b, i_h = i_bh // H, i_bh % H
    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
    b_Aw = tl.zeros([BC, BC], dtype=tl.float32)
    if HEAD_FIRST:
        p_beta = tl.make_block_ptr(beta + i_bh * T, (T,), (1,), (i_t * BT,),
            (BT,), (0,))
    else:
        p_beta = tl.make_block_ptr(beta + bos * H + i_h, (T,), (H,), (i_t *
            BT,), (BT,), (0,))
    b_beta = tl.load(p_beta, boundary_check=(0,))
    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))
        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))
        b_k = tl.load(p_k, boundary_check=(0, 1))
        b_kb = (b_k * b_beta[:, None]).to(b_k.dtype)
        b_Aw += tl.dot(b_kb, tl.trans(b_k))
    b_Aw = -tl.where(tl.arange(0, BC)[:, None] > tl.arange(0, BC)[None, :],
        b_Aw, 0)
    if HEAD_FIRST:
        p_g = tl.make_block_ptr(g + i_bh * T, (T,), (1,), (i_t * BT,), (BT,
            ), (0,))
    else:
        p_g = tl.make_block_ptr(g + bos * H + i_h, (T,), (H,), (i_t * BT,),
            (BT,), (0,))
    b_g = tl.load(p_g, boundary_check=(0,))
    b_Au = b_Aw * tl.exp(b_g[:, None] - b_g[None, :])
    for i in range(1, BC):
        mask = tl.arange(0, BC) == i
        b_aw = tl.sum(tl.where(mask[:, None], b_Aw, 0), 0)
        b_au = tl.sum(tl.where(mask[:, None], b_Au, 0), 0)
        b_aw = b_aw + tl.sum(b_aw[:, None] * b_Aw, 0) * (tl.arange(0, BC) < i)
        b_au = b_au + tl.sum(b_au[:, None] * b_Au, 0) * (tl.arange(0, BC) < i)
        b_Aw = tl.where(mask[:, None], b_aw, b_Aw)
        b_Au = tl.where(mask[:, None], b_au, b_Au)
    b_Aw += tl.arange(0, BC)[:, None] == tl.arange(0, BC)[None, :]
    b_Au += tl.arange(0, BC)[:, None] == tl.arange(0, BC)[None, :]
    if HEAD_FIRST:
        p_Aw = tl.make_block_ptr(Aw + i_bh * T * BT, (T, BT), (BT, 1), (i_t *
            BT, 0), (BC, BC), (1, 0))
        p_Au = tl.make_block_ptr(Au + i_bh * T * BT, (T, BT), (BT, 1), (i_t *
            BT, 0), (BC, BC), (1, 0))
    else:
        p_Aw = tl.make_block_ptr(Aw + (bos * H + i_h) * BT, (T, BT), (H *
            BT, 1), (i_t * BT, 0), (BC, BC), (1, 0))
        p_Au = tl.make_block_ptr(Au + (bos * H + i_h) * BT, (T, BT), (H *
            BT, 1), (i_t * BT, 0), (BC, BC), (1, 0))
    tl.store(p_Aw, b_Aw.to(p_Aw.dtype.element_ty), boundary_check=(0, 1))
    tl.store(p_Au, b_Au.to(p_Au.dtype.element_ty), boundary_check=(0, 1))
