[Perf][DSv4.1] Fold the mHC post block into the delayed pre projection (#56633)
Signed-off-by: Yongye Zhu <zyy1102000@gmail.com>
This commit is contained in:
@@ -194,6 +194,7 @@ def mhc_pre_delayed_tilelang(
|
||||
norm_weight,
|
||||
pre_mix if pre_mix is not None else post,
|
||||
next_pre_mix,
|
||||
layer_input,
|
||||
hidden_size=hidden_size,
|
||||
rms_eps=rms_eps,
|
||||
hc_pre_eps=hc_pre_eps,
|
||||
@@ -218,6 +219,7 @@ def mhc_pre_delayed_tilelang(
|
||||
layer_input,
|
||||
pre_mix if pre_mix is not None else post,
|
||||
next_pre_mix,
|
||||
layer_input,
|
||||
hidden_size,
|
||||
rms_eps,
|
||||
hc_pre_eps,
|
||||
@@ -263,6 +265,278 @@ def _mhc_pre_delayed_tilelang_fake(
|
||||
)
|
||||
|
||||
|
||||
def mhc_fused_post_pre_delayed_tilelang(
|
||||
x: torch.Tensor,
|
||||
residual: torch.Tensor,
|
||||
post_layer_mix: torch.Tensor,
|
||||
comb_res_mix: torch.Tensor,
|
||||
fn: torch.Tensor,
|
||||
hc_scale: torch.Tensor,
|
||||
hc_base: torch.Tensor,
|
||||
rms_eps: float,
|
||||
hc_pre_eps: float,
|
||||
hc_sinkhorn_eps: float,
|
||||
hc_post_mult_value: float,
|
||||
sinkhorn_repeat: int,
|
||||
pre_mix: torch.Tensor | None = None,
|
||||
norm_weight: torch.Tensor | None = None,
|
||||
norm_eps: float = 1e-6,
|
||||
capture_aux: bool = False,
|
||||
) -> tuple[
|
||||
torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor
|
||||
]:
|
||||
"""Run one mHC post block followed by the next delayed mHC pre block.
|
||||
|
||||
Within the fused kernel's token range the post mapping is folded into the
|
||||
pre-norm GEMM, so the updated residual streams feed the projection from
|
||||
registers instead of a second pass over global memory. Above it this runs
|
||||
the same post kernel and split-k GEMM as the unfused pair.
|
||||
|
||||
Args:
|
||||
x: BF16 sublayer output of shape (tokens, hidden_size).
|
||||
residual: BF16 residual streams of shape (tokens, hc_mult, hidden_size).
|
||||
post_layer_mix: FP32 post coefficients of shape (tokens, hc_mult, 1).
|
||||
comb_res_mix: FP32 residual coefficients, (tokens, hc_mult, hc_mult).
|
||||
fn: FP32 projection of shape (hc_mult * (hc_mult + 2), input_size).
|
||||
hc_scale: FP32 scales of shape (3,).
|
||||
hc_base: FP32 bias of shape (hc_mult * (hc_mult + 2),).
|
||||
rms_eps: RMS normalization epsilon.
|
||||
hc_pre_eps: Pre-mix epsilon.
|
||||
hc_sinkhorn_eps: Sinkhorn epsilon.
|
||||
hc_post_mult_value: Post-mix multiplier.
|
||||
sinkhorn_repeat: Number of Sinkhorn iterations.
|
||||
pre_mix: FP32 coefficients from the previous sublayer, or None to
|
||||
select residual stream zero.
|
||||
norm_weight: Optional BF16 RMSNorm weight for the collapsed input.
|
||||
norm_eps: RMSNorm epsilon for the collapsed input.
|
||||
capture_aux: Also return the mean over the post-mapped streams, which
|
||||
draft models consume as the target's hidden state. It is folded
|
||||
into the collapse, which already reads those streams.
|
||||
|
||||
Returns:
|
||||
The post-mapped residual streams, the post and residual coefficients,
|
||||
the optionally normalized BF16 layer input, the next FP32 pre-mix, and
|
||||
the BF16 stream mean (empty unless capture_aux), with shapes
|
||||
(tokens, hc_mult, hidden_size), (tokens, hc_mult, 1),
|
||||
(tokens, hc_mult, hc_mult), (tokens, hidden_size), (tokens, hc_mult),
|
||||
and (tokens, hidden_size).
|
||||
"""
|
||||
from vllm.model_executor.kernels.mhc.tilelang_kernels import (
|
||||
_HC_PRENORM_GEMM_TILELANG_KERNEL,
|
||||
_MHC_FUSED_TILELANG_KERNEL,
|
||||
_MHC_POST_TILELANG_KERNEL,
|
||||
mhc_fused_post_pre_split_config,
|
||||
mhc_pre_big_fuse_tilelang,
|
||||
)
|
||||
from vllm.model_executor.kernels.mhc.warmup import (
|
||||
MHC_PRE_NORM_KERNEL,
|
||||
compute_mhc_pre_num_splits,
|
||||
)
|
||||
from vllm.utils.deep_gemm import (
|
||||
is_deep_gemm_supported,
|
||||
tf32_hc_prenorm_gemm,
|
||||
)
|
||||
|
||||
assert residual.ndim == 3 and residual.dtype == torch.bfloat16
|
||||
assert residual.is_contiguous()
|
||||
num_tokens, hc_mult, hidden_size = residual.shape
|
||||
input_size = hc_mult * hidden_size
|
||||
mix_size = hc_mult * (hc_mult + 2)
|
||||
assert x.shape == (num_tokens, hidden_size) and x.dtype == torch.bfloat16
|
||||
assert x.is_contiguous()
|
||||
assert post_layer_mix.shape[:2] == (num_tokens, hc_mult)
|
||||
assert post_layer_mix.dtype == torch.float32 and post_layer_mix.is_contiguous()
|
||||
assert comb_res_mix.shape == (num_tokens, hc_mult, hc_mult)
|
||||
assert comb_res_mix.dtype == torch.float32 and comb_res_mix.is_contiguous()
|
||||
assert fn.shape == (mix_size, input_size) and fn.dtype == torch.float32
|
||||
assert hc_scale.shape == (3,) and hc_scale.dtype == torch.float32
|
||||
assert hc_base.shape == (mix_size,) and hc_base.dtype == torch.float32
|
||||
if pre_mix is not None:
|
||||
assert pre_mix.shape == (num_tokens, hc_mult)
|
||||
assert pre_mix.dtype == torch.float32 and pre_mix.is_contiguous()
|
||||
|
||||
next_pre_mix = torch.empty(
|
||||
num_tokens, hc_mult, dtype=torch.float32, device=residual.device
|
||||
)
|
||||
post = torch.empty_like(next_pre_mix)
|
||||
comb = torch.empty(
|
||||
num_tokens, hc_mult * hc_mult, dtype=torch.float32, device=residual.device
|
||||
)
|
||||
layer_input = torch.empty(
|
||||
num_tokens, hidden_size, dtype=torch.bfloat16, device=residual.device
|
||||
)
|
||||
aux = torch.empty(
|
||||
num_tokens if capture_aux else 0,
|
||||
hidden_size,
|
||||
dtype=torch.bfloat16,
|
||||
device=residual.device,
|
||||
)
|
||||
if num_tokens == 0:
|
||||
return (
|
||||
torch.empty_like(residual),
|
||||
post.unsqueeze(-1),
|
||||
comb.view(num_tokens, hc_mult, hc_mult),
|
||||
layer_input,
|
||||
next_pre_mix,
|
||||
aux,
|
||||
)
|
||||
|
||||
fused_config = mhc_fused_post_pre_split_config(num_tokens, hidden_size, hc_mult)
|
||||
if fused_config is not None:
|
||||
mixes, sqrsum, residual_cur = _MHC_FUSED_TILELANG_KERNEL(
|
||||
comb_res_mix,
|
||||
residual,
|
||||
post_layer_mix.view(num_tokens, hc_mult),
|
||||
x,
|
||||
fn.view(mix_size, hc_mult, hidden_size),
|
||||
hc_mult,
|
||||
hidden_size,
|
||||
mix_size,
|
||||
)
|
||||
else:
|
||||
residual_cur = torch.empty_like(residual)
|
||||
_MHC_POST_TILELANG_KERNEL(
|
||||
comb_res_mix,
|
||||
residual,
|
||||
post_layer_mix.view(num_tokens, hc_mult),
|
||||
x,
|
||||
residual_cur,
|
||||
hc_mult,
|
||||
hidden_size,
|
||||
)
|
||||
# The delayed epilogue is compiled per bucketed split count, so the
|
||||
# projection has to use the same bucket rather than the raw estimate.
|
||||
use_deep_gemm = is_deep_gemm_supported()
|
||||
n_splits = (
|
||||
compute_mhc_pre_num_splits(input_size, num_tokens) if use_deep_gemm else 1
|
||||
)
|
||||
mixes = torch.empty(
|
||||
n_splits, num_tokens, mix_size, dtype=torch.float32, device=residual.device
|
||||
)
|
||||
sqrsum = torch.empty(
|
||||
n_splits, num_tokens, dtype=torch.float32, device=residual.device
|
||||
)
|
||||
residual_cur_2d = residual_cur.view(num_tokens, input_size)
|
||||
if use_deep_gemm:
|
||||
tf32_hc_prenorm_gemm(residual_cur_2d, fn, mixes, sqrsum, n_splits)
|
||||
else:
|
||||
_HC_PRENORM_GEMM_TILELANG_KERNEL(
|
||||
residual_cur_2d,
|
||||
fn,
|
||||
mixes,
|
||||
sqrsum,
|
||||
input_size,
|
||||
1,
|
||||
)
|
||||
|
||||
outputs = (
|
||||
residual_cur,
|
||||
post.unsqueeze(-1),
|
||||
comb.view(num_tokens, hc_mult, hc_mult),
|
||||
layer_input,
|
||||
next_pre_mix,
|
||||
aux,
|
||||
)
|
||||
if norm_weight is not None:
|
||||
assert norm_weight.shape == (hidden_size,)
|
||||
assert norm_weight.dtype == torch.bfloat16 and norm_weight.is_contiguous()
|
||||
MHC_PRE_NORM_KERNEL(
|
||||
mixes,
|
||||
sqrsum,
|
||||
hc_scale,
|
||||
hc_base,
|
||||
residual_cur,
|
||||
post,
|
||||
comb,
|
||||
layer_input,
|
||||
norm_weight,
|
||||
pre_mix if pre_mix is not None else post,
|
||||
next_pre_mix,
|
||||
aux if capture_aux else layer_input,
|
||||
hidden_size=hidden_size,
|
||||
rms_eps=rms_eps,
|
||||
hc_pre_eps=hc_pre_eps,
|
||||
hc_sinkhorn_eps=hc_sinkhorn_eps,
|
||||
hc_post_mult_value=hc_post_mult_value,
|
||||
sinkhorn_repeat=sinkhorn_repeat,
|
||||
norm_eps=norm_eps,
|
||||
hc_mult=hc_mult,
|
||||
use_pre_mix_in=pre_mix is not None,
|
||||
save_pre_mix=True,
|
||||
rms_numel=input_size,
|
||||
write_aux=capture_aux,
|
||||
)
|
||||
return outputs
|
||||
mhc_pre_big_fuse_tilelang(
|
||||
mixes,
|
||||
sqrsum,
|
||||
hc_scale,
|
||||
hc_base,
|
||||
residual_cur,
|
||||
post,
|
||||
comb,
|
||||
layer_input,
|
||||
pre_mix if pre_mix is not None else post,
|
||||
next_pre_mix,
|
||||
aux if capture_aux else layer_input,
|
||||
hidden_size,
|
||||
rms_eps,
|
||||
hc_pre_eps,
|
||||
hc_sinkhorn_eps,
|
||||
hc_post_mult_value,
|
||||
sinkhorn_repeat,
|
||||
mixes.shape[0],
|
||||
hc_mult,
|
||||
use_pre_mix_in=pre_mix is not None,
|
||||
save_pre_mix=True,
|
||||
rms_numel=input_size,
|
||||
write_aux=capture_aux,
|
||||
)
|
||||
return outputs
|
||||
|
||||
|
||||
def _mhc_fused_post_pre_delayed_tilelang_fake(
|
||||
x: torch.Tensor,
|
||||
residual: torch.Tensor,
|
||||
post_layer_mix: torch.Tensor,
|
||||
comb_res_mix: torch.Tensor,
|
||||
fn: torch.Tensor,
|
||||
hc_scale: torch.Tensor,
|
||||
hc_base: torch.Tensor,
|
||||
rms_eps: float,
|
||||
hc_pre_eps: float,
|
||||
hc_sinkhorn_eps: float,
|
||||
hc_post_mult_value: float,
|
||||
sinkhorn_repeat: int,
|
||||
pre_mix: torch.Tensor | None = None,
|
||||
norm_weight: torch.Tensor | None = None,
|
||||
norm_eps: float = 1e-6,
|
||||
capture_aux: bool = False,
|
||||
) -> tuple[
|
||||
torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor
|
||||
]:
|
||||
num_tokens, hc_mult, hidden_size = residual.shape
|
||||
return (
|
||||
torch.empty_like(residual),
|
||||
torch.empty(
|
||||
num_tokens, hc_mult, 1, dtype=torch.float32, device=residual.device
|
||||
),
|
||||
torch.empty(
|
||||
num_tokens, hc_mult, hc_mult, dtype=torch.float32, device=residual.device
|
||||
),
|
||||
torch.empty(
|
||||
num_tokens, hidden_size, dtype=torch.bfloat16, device=residual.device
|
||||
),
|
||||
torch.empty(num_tokens, hc_mult, dtype=torch.float32, device=residual.device),
|
||||
torch.empty(
|
||||
num_tokens if capture_aux else 0,
|
||||
hidden_size,
|
||||
dtype=torch.bfloat16,
|
||||
device=residual.device,
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def mhc_pre_tilelang(
|
||||
residual: torch.Tensor,
|
||||
fn: torch.Tensor,
|
||||
@@ -574,6 +848,7 @@ def mhc_fused_post_pre_tilelang(
|
||||
_MHC_FUSED_TILELANG_KERNEL,
|
||||
_MHC_POST_TILELANG_KERNEL,
|
||||
_MHC_PRE_BIG_FUSE_TILELANG_KERNEL,
|
||||
mhc_fused_post_pre_split_config,
|
||||
)
|
||||
|
||||
assert residual.dtype == torch.bfloat16
|
||||
@@ -614,7 +889,7 @@ def mhc_fused_post_pre_tilelang(
|
||||
post_layer_mix_flat = post_layer_mix.view(num_tokens, hc_mult)
|
||||
comb_res_mix_flat = comb_res_mix.view(num_tokens, hc_mult, hc_mult)
|
||||
|
||||
use_small_fma = num_tokens <= 16
|
||||
fused_config = mhc_fused_post_pre_split_config(num_tokens, hidden_size, hc_mult)
|
||||
|
||||
post_mix_cur = torch.empty(
|
||||
num_tokens,
|
||||
@@ -635,7 +910,7 @@ def mhc_fused_post_pre_tilelang(
|
||||
device=residual.device,
|
||||
)
|
||||
|
||||
if use_small_fma:
|
||||
if fused_config is not None:
|
||||
gemm_out_mul, gemm_out_sqrsum, residual_cur = _MHC_FUSED_TILELANG_KERNEL(
|
||||
comb_res_mix_flat,
|
||||
residual_flat,
|
||||
@@ -815,6 +1090,12 @@ direct_register_custom_op(
|
||||
mutates_args=[],
|
||||
fake_impl=_mhc_pre_delayed_tilelang_fake,
|
||||
)
|
||||
direct_register_custom_op(
|
||||
op_name="mhc_fused_post_pre_delayed_tilelang",
|
||||
op_func=mhc_fused_post_pre_delayed_tilelang,
|
||||
mutates_args=[],
|
||||
fake_impl=_mhc_fused_post_pre_delayed_tilelang_fake,
|
||||
)
|
||||
direct_register_custom_op(
|
||||
op_name="mhc_pre_tilelang",
|
||||
op_func=mhc_pre_tilelang,
|
||||
|
||||
@@ -37,8 +37,67 @@ def compute_num_split(block_k: int, k: int | None, grid_size: int) -> int:
|
||||
return split_k
|
||||
|
||||
|
||||
def _mhc_fused_n_splits(num_tokens: int, hidden_size: int) -> int:
|
||||
return 8 if num_tokens < 8 and hidden_size <= 4096 else 4
|
||||
# ``mhc_fused_tilelang`` splits the hidden size across a fixed thread block, so
|
||||
# every (tile_n, n_thr) pair is one more kernel to compile. Tuned on GB300
|
||||
# (hc_mult 4, hidden_size 5120 and 7168) against the separate post + split-k
|
||||
# GEMM it replaces; the bands are coarse because the loss against a per-shape
|
||||
# optimum is ~1-2%, while one fixed config gives up to 18% at the top of the
|
||||
# range.
|
||||
_FUSED_POST_PRE_N_THR = 128
|
||||
_FUSED_POST_PRE_MAX_TOKENS = 32
|
||||
|
||||
|
||||
def mhc_fused_post_pre_split_config(
|
||||
num_tokens: int, hidden_size: int, hc_mult: int
|
||||
) -> tuple[int, int, int] | None:
|
||||
"""Pick ``(tile_n, n_splits, n_thr)`` for the fused post + pre-norm GEMM.
|
||||
|
||||
Returns None when a separate post kernel followed by a split-k GEMM is the
|
||||
better choice, or when the hidden size does not divide evenly across the
|
||||
fused kernel's block. One source of truth for the dispatch decision, the
|
||||
compile key and the launch, which must agree.
|
||||
"""
|
||||
if num_tokens > _FUSED_POST_PRE_MAX_TOKENS:
|
||||
return None
|
||||
n_thr = _FUSED_POST_PRE_N_THR
|
||||
if hidden_size % n_thr:
|
||||
return None
|
||||
# The projection tiles supply the parallelism a small token count cannot,
|
||||
# so the tile grows with the batch: 1.3x at one token and 1.03x at 32
|
||||
# against a split-k GEMM given the split its own estimator computes.
|
||||
if num_tokens < 16:
|
||||
tile_n, n_splits = 2, 8
|
||||
else:
|
||||
tile_n, n_splits = 6, 8
|
||||
# The kernel drops projection tiles and k-slices it cannot fill evenly.
|
||||
mix_size = hc_mult * (hc_mult + 2)
|
||||
while tile_n > 1 and mix_size % tile_n:
|
||||
tile_n -= 1
|
||||
while n_splits > 1 and hidden_size % (n_splits * n_thr):
|
||||
n_splits //= 2
|
||||
return tile_n, n_splits, n_thr
|
||||
|
||||
|
||||
def require_fused_post_pre_config(
|
||||
num_tokens: int, hidden_size: int, hc_mult: int
|
||||
) -> tuple[int, int, int]:
|
||||
"""The config for a shape the caller has already committed to fusing."""
|
||||
config = mhc_fused_post_pre_split_config(num_tokens, hidden_size, hc_mult)
|
||||
if config is None:
|
||||
raise ValueError(
|
||||
"the fused mHC post + pre-norm GEMM does not cover num_tokens="
|
||||
f"{num_tokens} at hidden_size={hidden_size}"
|
||||
)
|
||||
return config
|
||||
|
||||
|
||||
def mhc_fused_post_pre_splits(hidden_size: int, hc_mult: int) -> tuple[int, ...]:
|
||||
"""Every split-k factor the fused post + pre-norm GEMM path can pick."""
|
||||
configs = (
|
||||
mhc_fused_post_pre_split_config(num_tokens, hidden_size, hc_mult)
|
||||
for num_tokens in range(1, _FUSED_POST_PRE_MAX_TOKENS + 1)
|
||||
)
|
||||
return tuple(sorted({config[1] for config in configs if config is not None}))
|
||||
|
||||
|
||||
@tilelang_jit
|
||||
@@ -53,6 +112,7 @@ def mhc_pre_big_fuse_tilelang(
|
||||
layer_input,
|
||||
pre_mix_in,
|
||||
pre_mix_out,
|
||||
aux_out,
|
||||
hidden_size: int,
|
||||
rms_eps: float,
|
||||
hc_pre_eps: float,
|
||||
@@ -64,6 +124,7 @@ def mhc_pre_big_fuse_tilelang(
|
||||
use_pre_mix_in: bool = False,
|
||||
save_pre_mix: bool = False,
|
||||
rms_numel: int = 0,
|
||||
write_aux: bool = False,
|
||||
):
|
||||
"""Fuse coefficient generation and residual collapse after the projection.
|
||||
|
||||
@@ -88,6 +149,7 @@ def mhc_pre_big_fuse_tilelang(
|
||||
|
||||
pre_mix_in: T.Tensor[[num_tokens, hc_mult], T.float32] # type: ignore[no-redef, valid-type]
|
||||
pre_mix_out: T.Tensor[[num_tokens, hc_mult], T.float32] # type: ignore[no-redef, valid-type]
|
||||
aux_out: T.Tensor[[num_tokens, hidden_size], T.bfloat16] # type: ignore[no-redef, valid-type]
|
||||
|
||||
with T.Kernel(num_tokens, threads=96) as i:
|
||||
if ENABLE_PDL:
|
||||
@@ -188,11 +250,24 @@ def mhc_pre_big_fuse_tilelang(
|
||||
|
||||
ol = T.alloc_fragment(hidden_block, T.float32)
|
||||
T.clear(ol)
|
||||
if write_aux:
|
||||
# Aux consumers (draft models) take the plain stream mean
|
||||
# of the same residual this collapse reads.
|
||||
aux = T.alloc_fragment(hidden_block, T.float32)
|
||||
T.clear(aux)
|
||||
|
||||
for i_hc in T.serial(hc_mult):
|
||||
pre = pre_mix_shared[i_hc]
|
||||
for i1_h in T.Parallel(hidden_block):
|
||||
ol[i1_h] += pre * xl[i_hc, i1_h]
|
||||
if write_aux:
|
||||
aux[i1_h] += xl[i_hc, i1_h]
|
||||
|
||||
if write_aux:
|
||||
for i1_h in T.Parallel(hidden_block):
|
||||
aux_out[i, i0_h * hidden_block + i1_h] = T.bfloat16(
|
||||
aux[i1_h] / hc_mult
|
||||
)
|
||||
|
||||
T.copy(ol, layer_input[i, i0_h * hidden_block])
|
||||
|
||||
@@ -216,6 +291,7 @@ def mhc_pre_big_fuse_with_norm_tilelang(
|
||||
norm_weight,
|
||||
pre_mix_in,
|
||||
pre_mix_out,
|
||||
aux_out,
|
||||
hidden_size: int,
|
||||
rms_eps: float,
|
||||
hc_pre_eps: float,
|
||||
@@ -229,6 +305,7 @@ def mhc_pre_big_fuse_with_norm_tilelang(
|
||||
use_pre_mix_in: bool = False,
|
||||
save_pre_mix: bool = False,
|
||||
rms_numel: int = 0,
|
||||
write_aux: bool = False,
|
||||
):
|
||||
num_tokens = T.dynamic("num_tokens")
|
||||
hc_mult3 = hc_mult * (2 + hc_mult)
|
||||
@@ -250,6 +327,7 @@ def mhc_pre_big_fuse_with_norm_tilelang(
|
||||
|
||||
pre_mix_in: T.Tensor[[num_tokens, hc_mult], T.float32] # type: ignore[no-redef, valid-type]
|
||||
pre_mix_out: T.Tensor[[num_tokens, hc_mult], T.float32] # type: ignore[no-redef, valid-type]
|
||||
aux_out: T.Tensor[[num_tokens, hidden_size], T.bfloat16] # type: ignore[no-redef, valid-type]
|
||||
|
||||
with T.Kernel(num_tokens, threads=96) as i:
|
||||
rms = T.alloc_fragment(1, T.float32)
|
||||
@@ -345,11 +423,24 @@ def mhc_pre_big_fuse_with_norm_tilelang(
|
||||
|
||||
ol = T.alloc_fragment(hidden_block, T.float32)
|
||||
T.clear(ol)
|
||||
if write_aux:
|
||||
# Aux consumers (draft models) take the plain stream mean
|
||||
# of the same residual this collapse reads.
|
||||
aux = T.alloc_fragment(hidden_block, T.float32)
|
||||
T.clear(aux)
|
||||
|
||||
for i_hc in T.serial(hc_mult):
|
||||
pre = pre_mix_shared[i_hc]
|
||||
for i1_h in T.Parallel(hidden_block):
|
||||
ol[i1_h] += pre * xl[i_hc, i1_h]
|
||||
if write_aux:
|
||||
aux[i1_h] += xl[i_hc, i1_h]
|
||||
|
||||
if write_aux:
|
||||
for i1_h in T.Parallel(hidden_block):
|
||||
aux_out[i, i0_h * hidden_block + i1_h] = T.bfloat16(
|
||||
aux[i1_h] / hc_mult
|
||||
)
|
||||
|
||||
if save_pre_mix:
|
||||
# Keep the BF16 boundary before the delayed input RMSNorm.
|
||||
@@ -1211,9 +1302,17 @@ class MhcPreBigFuseTileLangKernel(
|
||||
middle_args = (residual_out, *output_args, norm_weight)
|
||||
elif compile_key.use_norm_weight:
|
||||
assert norm_weight is not None
|
||||
middle_args = (*output_args, norm_weight, post_mix, post_mix)
|
||||
# layer_input stands in for the aux buffer on both epilogues: same
|
||||
# shape and dtype, and write_aux is off here so it is never written.
|
||||
middle_args = (
|
||||
*output_args,
|
||||
norm_weight,
|
||||
post_mix,
|
||||
post_mix,
|
||||
layer_input,
|
||||
)
|
||||
else:
|
||||
middle_args = (*output_args, post_mix, post_mix)
|
||||
middle_args = (*output_args, post_mix, post_mix, layer_input)
|
||||
norm_eps_args = (compile_key.norm_eps,) if compile_key.use_norm_weight else ()
|
||||
return (
|
||||
*common_args,
|
||||
@@ -1247,8 +1346,14 @@ class MhcPreBigFuseTileLangKernel(
|
||||
if use_pre_gemm_splits
|
||||
else n_splits
|
||||
)
|
||||
fused_n_splits = _mhc_fused_n_splits(num_tokens, hidden_size)
|
||||
actual_n_splits = fused_n_splits if use_fused_tilelang else pre_gemm_n_splits
|
||||
# The epilogue reduces over whatever the fused kernel split the GEMM
|
||||
# into, so both have to read the same config.
|
||||
fused_config = mhc_fused_post_pre_split_config(num_tokens, hidden_size, hc_mult)
|
||||
actual_n_splits = (
|
||||
fused_config[1]
|
||||
if use_fused_tilelang and fused_config is not None
|
||||
else pre_gemm_n_splits
|
||||
)
|
||||
actual_norm_eps = broadcast_norm_eps if is_broadcast else norm_eps
|
||||
actual_use_norm_weight = use_norm_weight or is_broadcast
|
||||
return self.CompileKey(
|
||||
@@ -1540,6 +1645,7 @@ class MhcFusedTileLangKernel(
|
||||
hc_mult: int
|
||||
n_splits: int
|
||||
tile_n: int
|
||||
n_thr: int
|
||||
|
||||
@staticmethod
|
||||
def kernel() -> Any:
|
||||
@@ -1552,14 +1658,15 @@ class MhcFusedTileLangKernel(
|
||||
hidden_size: int,
|
||||
hc_mult: int,
|
||||
) -> CompileKey:
|
||||
# TODO(gnovack): investigate autotuning these heuristics
|
||||
tile_n = 2 if num_tokens < 8 else 3
|
||||
n_splits = _mhc_fused_n_splits(num_tokens, hidden_size)
|
||||
# Subscripts rather than an unpack: the warmup tracer parses this body
|
||||
# and allows only plain assignments before the return.
|
||||
config = require_fused_post_pre_config(num_tokens, hidden_size, hc_mult)
|
||||
return self.CompileKey(
|
||||
hidden_size=hidden_size,
|
||||
hc_mult=hc_mult,
|
||||
n_splits=n_splits,
|
||||
tile_n=tile_n,
|
||||
n_splits=config[1],
|
||||
tile_n=config[0],
|
||||
n_thr=config[2],
|
||||
)
|
||||
|
||||
def get_warmup_keys(
|
||||
@@ -1574,11 +1681,11 @@ class MhcFusedTileLangKernel(
|
||||
num_tokens=WarmupIntRange(1, max_tokens + 1),
|
||||
hidden_size=hidden_size,
|
||||
hc_mult=hc_mult,
|
||||
_when=lambda *, num_tokens: num_tokens <= 16,
|
||||
_when=lambda *, num_tokens: num_tokens <= _FUSED_POST_PRE_MAX_TOKENS,
|
||||
)
|
||||
|
||||
def warmup_inputs(self, compile_key: CompileKey) -> dict[str, Any]:
|
||||
num_tokens = 1 if compile_key.tile_n == 2 else 8
|
||||
num_tokens = 1 if compile_key.tile_n == 2 else 16
|
||||
hidden_size = compile_key.hidden_size
|
||||
hc_mult = compile_key.hc_mult
|
||||
hc_mult3 = hc_mult * (2 + hc_mult)
|
||||
@@ -1617,8 +1724,9 @@ class MhcFusedTileLangKernel(
|
||||
hc_mult3: int,
|
||||
) -> TileLangLaunchSpec:
|
||||
num_tokens = residual_in.shape[0]
|
||||
tile_n = 2 if num_tokens < 8 else 3
|
||||
n_splits = _mhc_fused_n_splits(num_tokens, hidden_size)
|
||||
tile_n, n_splits, n_thr = require_fused_post_pre_config(
|
||||
num_tokens, hidden_size, hc_mult
|
||||
)
|
||||
yp_out = residual_in.new_empty(
|
||||
(n_splits, num_tokens, hc_mult3), dtype=torch.float32
|
||||
)
|
||||
@@ -1639,7 +1747,7 @@ class MhcFusedTileLangKernel(
|
||||
hidden_size,
|
||||
hc_mult3,
|
||||
),
|
||||
dict(tile_n=tile_n, split_k=n_splits),
|
||||
dict(n_thr=n_thr, tile_n=tile_n),
|
||||
(yp_out, rp_out, residual_out),
|
||||
)
|
||||
|
||||
|
||||
@@ -36,23 +36,33 @@ class MHCPreNormKernel(VllmJitKernel["MHCPreNormKernel.CompileKey"]):
|
||||
use_pre_mix_in: bool
|
||||
rms_numel: int
|
||||
save_pre_mix: bool = True
|
||||
write_aux: bool = False
|
||||
|
||||
kernel: Any = staticmethod(mhc_pre_big_fuse_with_norm_tilelang)
|
||||
|
||||
def dispatch(self, *, n_splits, **fields) -> CompileKey: # type: ignore[override]
|
||||
return self.CompileKey(n_splits=n_splits, **fields)
|
||||
|
||||
def get_warmup_keys(self, *, max_tokens: int, **fields) -> list[CompileKey]:
|
||||
# The split heuristic changes only at 64-token boundaries.
|
||||
splits = (
|
||||
sorted(
|
||||
def get_warmup_keys(
|
||||
self,
|
||||
*,
|
||||
max_tokens: int,
|
||||
extra_splits: Iterable[int] = (),
|
||||
**fields,
|
||||
) -> list[CompileKey]:
|
||||
# The split heuristic changes only at 64-token boundaries. Callers that
|
||||
# reach this kernel through another GEMM (the fused post + pre-norm
|
||||
# GEMM) pass the splits that GEMM picks in extra_splits.
|
||||
splits = sorted(
|
||||
set(extra_splits)
|
||||
| (
|
||||
{
|
||||
compute_mhc_pre_num_splits(fields["rms_numel"], num_tokens)
|
||||
for num_tokens in range(1, max_tokens + 1, 64)
|
||||
}
|
||||
if is_deep_gemm_supported()
|
||||
else {1}
|
||||
)
|
||||
if is_deep_gemm_supported()
|
||||
else [1]
|
||||
)
|
||||
return self._trace_dispatch(self.dispatch)(n_splits=splits, **fields)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user