Update mhc_pre hip kernel support hc_head#3044
Merged
Merged
Conversation
Contributor
🏷️ CI GuideRuns automatically on every PR:
Extended tests (opt-in via labels):
|
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds an hc_head / “pre-only” execution mode for the MHC pre-kernel path (skipping post/comb mix + Sinkhorn), and updates the corresponding Python test harness to exercise it via a new CLI flag.
Changes:
- Extend
mhc_preto supportsinkhorn_repeat == 0as anhc_head-only mode (allowingfn.size(0) == hc_mult). - Update the HIP kernel to skip post/comb writes when
sinkhorn_repeat == 0, and tweak GEMM-SQRSUM dispatch heuristics for smallhc_mult3. - Add
--hc_headtoop_tests/test_mhc.pyto run the pre-only configuration.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
op_tests/test_mhc.py |
Adds --hc_head flag and adjusts reference/test logic to support pre-only validation. |
csrc/kernels/mhc_kernels.cu |
Updates kernel dispatch and gates post/comb computation on sinkhorn_repeat > 0. |
aiter/ops/mhc.py |
Adds defaults and allows hc_head mode via sinkhorn_repeat == 0 + relaxed hc_mult3 assertion. |
Comments suppressed due to low confidence (2)
csrc/kernels/mhc_kernels.cu:253
- In
MHC_PRE_GEMM_SQRSUM_KERNEL_DISPATCH, the conditionelse if (tile_k == 128 || hc_mult3 <= 16)can bypass thetile_kvalidation: if a caller passes an unsupportedtile_kwhilehc_mult3 <= 16, it will still dispatch the 128-kernel instead of throwing. Please keep thetile_kcheck strict (64 vs 128) and handle thehc_mult3 <= 16special-casing inside the valid branches so invalidtile_kstill errors out deterministically.
#define MHC_PRE_GEMM_SQRSUM_KERNEL_DISPATCH(tile_k) \
if (tile_k == 64) { \
if (cu_num * 2 > m_blocks * split_k || hc_mult3 <= 16) { \
MHC_PRE_GEMM_SQRSUM_KERNEL_IMPL(256, 16, 64); \
} else { \
MHC_PRE_GEMM_SQRSUM_KERNEL_IMPL(256, 32, 64); \
} \
} else if (tile_k == 128 || hc_mult3 <= 16) { \
if (cu_num > m_blocks * split_k) { \
MHC_PRE_GEMM_SQRSUM_KERNEL_IMPL(256, 16, 128); \
} else { \
MHC_PRE_GEMM_SQRSUM_KERNEL_IMPL(256, 32, 128); \
} \
} else { \
TORCH_CHECK(false, "tile_k must be 64 or 128"); \
}
op_tests/test_mhc.py:398
mhc_pre_ref()is annotated to returntuple[torch.Tensor, torch.Tensor, torch.Tensor], but in thetest_hc_headpath it returnsNoneforpost_mixandres_mix. Please update the type annotation to reflect the actual behavior (e.g., useOptional[torch.Tensor]for the first two elements) to avoid misleading type hints and downstream tooling issues.
# copy from tilelang/examples/deepseek_mhc/example_mhc_pre.py
def mhc_pre_ref(
residual: 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,
test_hc_head: bool = False,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
hc_mult = residual.shape[-2]
residual_flat = residual.flatten(-2, -1).float()
sqrsum = residual_flat.square().sum(-1)
out = residual_flat @ fn.T
mixes = out * (sqrsum.unsqueeze(-1) / fn.shape[-1] + rms_eps).rsqrt()
if not test_hc_head:
hc_scale = torch.cat(
[
hc_scale[0].expand(hc_mult),
hc_scale[1].expand(hc_mult),
hc_scale[2].expand(hc_mult * hc_mult),
],
)
mixes = mixes * hc_scale + hc_base
pre_mix = mixes[:, :hc_mult].sigmoid().unsqueeze(-1) + hc_pre_eps
post_mix = (
mixes[:, hc_mult : 2 * hc_mult].sigmoid() * hc_post_mult_value
).unsqueeze(-1)
res_mix = mixes[:, 2 * hc_mult :].view(-1, hc_mult, hc_mult)
def sinkhorn_normalize_ref(
x: torch.Tensor, repeat: int, eps: float
) -> torch.Tensor:
x = x.softmax(-1) + eps
x = x / (x.sum(-2, keepdim=True) + eps)
for _ in range(repeat - 1):
x = x / (x.sum(-1, keepdim=True) + eps)
x = x / (x.sum(-2, keepdim=True) + eps)
return x
res_mix = sinkhorn_normalize_ref(
res_mix, repeat=sinkhorn_repeat, eps=hc_sinkhorn_eps
)
else:
hc_scale = hc_scale[0].expand(hc_mult)
mixes = mixes * hc_scale + hc_base
pre_mix = mixes[:, :hc_mult].sigmoid().unsqueeze(-1) + hc_pre_eps
post_mix = None
res_mix = None
layer_input = (residual * pre_mix).sum(-2).bfloat16()
return post_mix, res_mix, layer_input
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+47
to
60
| rms_eps: float = 1e-6, | ||
| hc_pre_eps: float = 1e-6, | ||
| hc_sinkhorn_eps: float = 1e-6, | ||
| hc_post_mult_value: float = 1.0, | ||
| sinkhorn_repeat: int = 20, # if 0, only do pre for hc_head | ||
| ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: | ||
| m = residual.size(0) | ||
| hc_mult = residual.size(1) | ||
| hidden_size = residual.size(2) | ||
| hc_mult3 = fn.size(0) | ||
| assert hc_mult3 == hc_mult * 2 + hc_mult * hc_mult | ||
| assert hc_mult3 == hc_mult * 2 + hc_mult * hc_mult or ( | ||
| hc_mult3 == hc_mult and sinkhorn_repeat == 0 | ||
| ) | ||
| hc_hidden_size = hc_mult * hidden_size |
Comment on lines
+51
to
+59
| sinkhorn_repeat: int = 20, # if 0, only do pre for hc_head | ||
| ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: | ||
| m = residual.size(0) | ||
| hc_mult = residual.size(1) | ||
| hidden_size = residual.size(2) | ||
| hc_mult3 = fn.size(0) | ||
| assert hc_mult3 == hc_mult * 2 + hc_mult * hc_mult | ||
| assert hc_mult3 == hc_mult * 2 + hc_mult * hc_mult or ( | ||
| hc_mult3 == hc_mult and sinkhorn_repeat == 0 | ||
| ) |
valarLip
approved these changes
May 6, 2026
Liang-jianhao97
pushed a commit
that referenced
this pull request
May 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
test hc_head cmd: python3 op_tests/test_mhc.py --hc_head