Fused mHC: support `mhc_norm_eps_inside_sqrt` and `mhc_keep_mappings_in_fp32`
Is your feature request related to a problem? Please describe.
use_fused_mhc=True cannot be combined with mhc_norm_eps_inside_sqrt or mhc_keep_mappings_in_fp32 (both added in #7521). TransformerConfig.__post_init__ and HyperConnectionModule.__init__ raise a ValueError for either combination. Models whose reference mHC needs these flags can therefore only use the unfused path:
- DeepSeek-V4 normalizes the mHC mapping input as
x * rsqrt(mean(x**2) + eps). Its reference also keepspre,post,comband the stream mixing in FP32 (Block.hc_preandBlock.hc_postin DeepSeek'sinference/model.py). - GLM-5.3-Flash needs both flags. Megatron Bridge sets
mhc_norm_eps_inside_sqrt=Trueandmhc_keep_mappings_in_fp32=Truefor it.
The fused path hard-codes the epsilon outside the square root, 1 / (sqrt(sum(x**2) / K) + eps):
- cuTile forward,
_ct_reduce_compute_h_kernel:inv_r_eps = 1.0 / (r_val + eps)(fused_mhc_kernels.py#L1516) - PyTorch fallback,
_torch_proj_rms_compute_h:proj * alpha / (r + eps)(fused_mhc_kernels.py#L2745) - cuTile backward,
_cutile_fused_compute_h_proj_rms_bwd(fused_mhc_kernels.py#L2317)
compute_mappings also casts h_pre, h_post and h_res to the activation dtype before fused_h_aggregate and fused_h_post_bda (hyper_connection.py#L416).
Because of this, Megatron Bridge now maps DeepSeek-V4 to the unfused path by default so it matches the reference (NVIDIA-NeMo/Megatron-Bridge#6797). Only its Blackwell training recipes keep fused mHC, with the epsilon outside the square root.
Describe the solution you'd like
- Support
mhc_norm_eps_inside_sqrtin the fused path: computersqrt(sum(x**2) / K + eps)in the cuTile forward, update the cuTile backward and the PyTorch fallback to match, and remove the guard for this flag. - Support
mhc_keep_mappings_in_fp32in the fused path: passh_pre,h_postandh_restofused_h_aggregateandfused_h_post_bdain FP32 (the Triton kernels already do their math in FP32), and remove the remaining guard.
Describe alternatives you've considered
- Run the unfused path. This is what Megatron Bridge does for DeepSeek-V4 outside its Blackwell recipes, and it is slower.
- Keep fused mHC with the epsilon outside the square root. With
eps = 1e-6and stream RMSr, the normalization factor changes by abouteps / (2 r^2) - eps / r: about 4e-5 at r = 0.1, 5e-4 at r = 0.03 and 0.5% at r = 0.01. This has not been measured on real DeepSeek-V4 activations.
Additional context
- The rest of the fused path matches DeepSeek's reference: the
sigmoid + epsand2 * sigmoidgates, Sinkhorn (softmax + eps, a column pass, then row/column passes), theH_res^T @ residualmixing direction, andlearned_output_contract. - The cuTile kernels run the mapping-projection GEMMs on TF32 tensor cores (fused_mhc_kernels.py#L1428), while the reference and the unfused path use FP32. This is a smaller, precision-only difference.
- Guards at a0ef646: transformer_config.py#L3172 and hyper_connection.py#L270.
- DeepSeek-V4-Flash reference at 60d8d70: inference/model.py#L673 (
hc_pre,hc_post) and inference/kernel.py#L372 (hc_split_sinkhorn).
@kamran-nvidia I opened #9802 to support mhc_norm_eps_inside_sqrt with use_fused_mhc — both the cuTile fused kernels and the native fallback, forward and backward, including padded rows. mhc_keep_mappings_in_fp32 still requires the unfused path; that half of the request remains open.