[Refactor] Remove kernel dead code (#51051)
Signed-off-by: yewentao256 <zhyanwentao@126.com>
This commit is contained in:
@@ -501,21 +501,6 @@ class AttentionProgram:
|
||||
cdna4.buffer_store(part_o, self.mid_o_ptr, mid_o_offsets, mask=valid[:, None])
|
||||
cdna4.buffer_store(part_lse, self.mid_lse_ptr, mid_lse_offsets, mask=valid)
|
||||
|
||||
@gluon.jit
|
||||
def store_output(self, acc, l_i):
|
||||
cfg = self.cfg
|
||||
offs_m = gl.arange(0, cfg.BLOCK_M, layout=gl.SliceLayout(1, cfg.store_layout))
|
||||
offs_d = gl.arange(0, cfg.HEAD_DIM, layout=gl.SliceLayout(0, cfg.store_layout))
|
||||
q_heads = self.kv_head * cfg.GROUP_SIZE + self.group_start + offs_m
|
||||
valid = (self.group_start + offs_m) < cfg.GROUP_SIZE
|
||||
acc = gl.convert_layout(acc, cfg.store_layout)
|
||||
l_i = gl.convert_layout(l_i, gl.SliceLayout(1, cfg.store_layout))
|
||||
output = acc * (1.0 / l_i)[:, None]
|
||||
output = output.to(self.mid_o_ptr.dtype.element_ty)
|
||||
offsets = (self.q_index * cfg.NUM_Q_HEADS + q_heads[:, None]) * cfg.HEAD_DIM
|
||||
offsets += offs_d[None, :]
|
||||
cdna4.buffer_store(output, self.mid_o_ptr, offsets, mask=valid[:, None])
|
||||
|
||||
|
||||
# ===-----------------------------------------------------------------------===#
|
||||
# Entry Point
|
||||
@@ -625,104 +610,6 @@ def _rel_mha_decode_fp16(
|
||||
program.store_split(acc, l_i, m_i)
|
||||
|
||||
|
||||
@gluon.jit
|
||||
def _rel_mha_decode_sliding_fp16(
|
||||
q_ptr,
|
||||
rel_logits_ptr,
|
||||
k_cache_ptr,
|
||||
v_cache_ptr,
|
||||
page_table_ptr,
|
||||
cache_seqlens_ptr,
|
||||
out_ptr,
|
||||
Q_STRIDE_B: gl.constexpr,
|
||||
Q_STRIDE_H: gl.constexpr,
|
||||
Q_STRIDE_D: gl.constexpr,
|
||||
K_STRIDE_B: gl.constexpr,
|
||||
K_STRIDE_P: gl.constexpr,
|
||||
K_STRIDE_H: gl.constexpr,
|
||||
K_STRIDE_D: gl.constexpr,
|
||||
V_STRIDE_B: gl.constexpr,
|
||||
V_STRIDE_P: gl.constexpr,
|
||||
V_STRIDE_H: gl.constexpr,
|
||||
V_STRIDE_D: gl.constexpr,
|
||||
SM_SCALE: gl.constexpr,
|
||||
PAGE_TABLE_STRIDE: gl.constexpr,
|
||||
PAGE_SIZE: gl.constexpr,
|
||||
MAX_SEQLEN_Q: gl.constexpr,
|
||||
NUM_Q_HEADS: gl.constexpr,
|
||||
NUM_KV_HEADS: gl.constexpr,
|
||||
HEAD_DIM: gl.constexpr,
|
||||
BLOCK_M: gl.constexpr,
|
||||
BLOCK_N: gl.constexpr,
|
||||
IS_SLIDING: gl.constexpr,
|
||||
WINDOW_LEFT: gl.constexpr,
|
||||
REL_STRIDE_T: gl.constexpr,
|
||||
REL_STRIDE_H: gl.constexpr,
|
||||
REL_STRIDE_E: gl.constexpr,
|
||||
REL_EXTENT: gl.constexpr,
|
||||
REL_BIAS_QK_SCALE: gl.constexpr,
|
||||
IS_FP8: gl.constexpr,
|
||||
):
|
||||
cfg = AttentionConfig(
|
||||
SM_SCALE,
|
||||
PAGE_TABLE_STRIDE,
|
||||
PAGE_SIZE,
|
||||
1,
|
||||
MAX_SEQLEN_Q,
|
||||
NUM_Q_HEADS,
|
||||
NUM_KV_HEADS,
|
||||
HEAD_DIM,
|
||||
BLOCK_M,
|
||||
BLOCK_N,
|
||||
IS_SLIDING,
|
||||
WINDOW_LEFT,
|
||||
REL_EXTENT,
|
||||
REL_BIAS_QK_SCALE,
|
||||
IS_FP8,
|
||||
InputStrides(Q_STRIDE_B, Q_STRIDE_H, Q_STRIDE_D),
|
||||
InputStrides(REL_STRIDE_T, REL_STRIDE_H, REL_STRIDE_E),
|
||||
PagedKVStrides(K_STRIDE_B, K_STRIDE_P, K_STRIDE_H, K_STRIDE_D),
|
||||
PagedKVStrides(V_STRIDE_B, V_STRIDE_P, V_STRIDE_H, V_STRIDE_D),
|
||||
)
|
||||
program = AttentionProgram.create(
|
||||
cfg,
|
||||
q_ptr,
|
||||
rel_logits_ptr,
|
||||
k_cache_ptr,
|
||||
v_cache_ptr,
|
||||
page_table_ptr,
|
||||
cache_seqlens_ptr,
|
||||
out_ptr,
|
||||
out_ptr,
|
||||
)
|
||||
k_smem = gl.allocate_shared_memory(
|
||||
k_cache_ptr.dtype.element_ty, [cfg.BLOCK_N, cfg.HEAD_DIM], cfg.k_smem_layout
|
||||
)
|
||||
v_smem = gl.allocate_shared_memory(
|
||||
v_cache_ptr.dtype.element_ty, [cfg.BLOCK_N, cfg.HEAD_DIM], cfg.v_smem_layout
|
||||
)
|
||||
|
||||
q = program.load_q()
|
||||
m_i, l_i, acc = program.init_state()
|
||||
|
||||
for start_n in range(program.split_start, program.split_end, cfg.BLOCK_N):
|
||||
physical_page = program.load_page(start_n)
|
||||
program.issue_load_k(physical_page, k_smem)
|
||||
program.issue_load_v(physical_page, v_smem)
|
||||
async_copy.wait_group(1)
|
||||
k = program.shared_load_k(k_smem)
|
||||
qk = program.compute_qk(q, k)
|
||||
qk = program.apply_rel_bias(qk, start_n)
|
||||
qk = program.apply_kv_mask(qk, start_n)
|
||||
p, m_i, l_i, acc = program.softmax(qk, m_i, l_i, acc)
|
||||
|
||||
async_copy.wait_group(0)
|
||||
v = program.shared_load_v(v_smem)
|
||||
acc = program.compute_pv(p, v, acc)
|
||||
|
||||
program.store_output(acc, l_i)
|
||||
|
||||
|
||||
@gluon.jit
|
||||
def _rel_mha_decode_reduce_fp16(
|
||||
mid_o_ptr,
|
||||
|
||||
@@ -40,75 +40,6 @@ def max(input, axis=None, keep_dims=False):
|
||||
return gl.reduce(input, axis, maximum, keep_dims=keep_dims)
|
||||
|
||||
|
||||
@gluon.constexpr_function
|
||||
def attention_layouts(head_dim, block_n, is_fp8, dtype, num_warps, instr_shape):
|
||||
mfma = gl.amd.AMDMFMALayout(
|
||||
version=4,
|
||||
instr_shape=instr_shape,
|
||||
transposed=True,
|
||||
warps_per_cta=[num_warps, 1],
|
||||
)
|
||||
qk_layout = mfma
|
||||
pv_layout = mfma
|
||||
# qk_kw is derived from a 128-bit load / dtype bitwidth; pv_kw is tuned.
|
||||
qk_kw = 16 if is_fp8 else 8
|
||||
pv_kw = 8 if is_fp8 else 4
|
||||
q_layout = gl.DotOperandLayout(0, qk_layout, k_width=qk_kw)
|
||||
k_layout = gl.DotOperandLayout(1, qk_layout, k_width=qk_kw)
|
||||
p_layout = gl.DotOperandLayout(0, pv_layout, k_width=pv_kw)
|
||||
v_layout = gl.DotOperandLayout(1, pv_layout, k_width=pv_kw)
|
||||
# load_vec = elems/lane (dtype-dependent, == qk_kw); load_threads span HEAD_DIM.
|
||||
load_vec = 16 if is_fp8 else 8
|
||||
load_threads = head_dim // load_vec
|
||||
load_layout = gl.BlockedLayout(
|
||||
[1, load_vec], [64 // load_threads, load_threads], [num_warps, 1], [1, 0]
|
||||
)
|
||||
# store_vec is always 16-bit (128 / 16 = 8) regardless of input dtype.
|
||||
store_vec = 8
|
||||
store_threads = head_dim // store_vec
|
||||
store_layout = gl.BlockedLayout(
|
||||
[1, store_vec], [64 // store_threads, store_threads], [num_warps, 1], [1, 0]
|
||||
)
|
||||
# Take only the built-in's padding, not its swizzle: the swizzle scatters
|
||||
# head_dim across banks, making the LDS write stride non-constant, so it can't
|
||||
# lower through async_copy's affine [1, 0] load. Padding-only is affine and
|
||||
# DMA-legal.
|
||||
# TODO(perf): to also use the swizzle, co-design a matched load layout so the
|
||||
# DMA stays legal.
|
||||
k_api = gl.amd.cdna4.compute_efficient_padded_shared_layout(
|
||||
k_layout, [block_n, head_dim], dtype, is_k_contig=True
|
||||
)
|
||||
v_api = gl.amd.cdna4.compute_efficient_padded_shared_layout(
|
||||
v_layout, [block_n, head_dim], dtype, is_k_contig=False
|
||||
)
|
||||
assert k_api is not None and v_api is not None, (
|
||||
"no CDNA4 padded shared layout for this operand/dtype"
|
||||
)
|
||||
k_pairs = list(k_api.interval_padding_pairs)
|
||||
v_pairs = list(v_api.interval_padding_pairs)
|
||||
assert len(k_pairs) == 1 and len(v_pairs) == 1, (
|
||||
"expected a single interval padding pair from the built-in"
|
||||
)
|
||||
k_smem_layout = gl.PaddedSharedLayout.with_identity_for(
|
||||
[[int(k_pairs[0][0]), int(k_pairs[0][1])]], [block_n, head_dim], [1, 0]
|
||||
)
|
||||
v_smem_layout = gl.PaddedSharedLayout.with_identity_for(
|
||||
[[int(v_pairs[0][0]), int(v_pairs[0][1])]], [block_n, head_dim], [1, 0]
|
||||
)
|
||||
return (
|
||||
qk_layout,
|
||||
pv_layout,
|
||||
q_layout,
|
||||
k_layout,
|
||||
p_layout,
|
||||
v_layout,
|
||||
load_layout,
|
||||
store_layout,
|
||||
k_smem_layout,
|
||||
v_smem_layout,
|
||||
)
|
||||
|
||||
|
||||
@aggregate
|
||||
class InputStrides:
|
||||
stride_t: gl.constexpr
|
||||
|
||||
Reference in New Issue
Block a user