def hc_up_gate_mix(
lowrank: torch.Tensor, xn: torch.Tensor, w_up: torch.Tensor
) -> torch.Tensor:
"""Fuse the BF16 up projection and gate mix for four streams, H=2560, K=320.
SM90 rounds the gate to BF16 before sigmoid. SM100 keeps it in FP32.
"""
if current_platform.is_device_capability(90):
assert lowrank.shape == (xn.shape[0], 320)
assert xn.ndim == 2 and xn.shape[1] == 10240
assert w_up.shape == (10240, 320)
assert lowrank.dtype == xn.dtype == w_up.dtype == torch.bfloat16
assert lowrank.device == xn.device == w_up.device
out = xn.new_empty(xn.shape[0], 2560)
block_m, block_n, block_k = 64, 128, 32
grid = (triton.cdiv(xn.shape[0], block_m) * (xn.shape[1] // block_n),)
_hc_up_gate_mix_sm90_kernel[grid](
lowrank,
w_up,
xn,
out,
xn.shape[0],
*lowrank.stride(),
*w_up.stride(),
*xn.stride(),
block_m,
block_n,
block_k,
num_warps=4,
num_stages=4,
)
return out
out = xn.new_empty(xn.shape[0], xn.shape[1] // 4)
torch.ops._C.qwen4_exp_hc_up_gate_mix(out, lowrank, xn, w_up)
return out