Megatron-LM icon indicating copy to clipboard operation
Megatron-LM copied to clipboard

Fused mHC: support `mhc_norm_eps_inside_sqrt` and `mhc_keep_mappings_in_fp32`

Open kamran-nvidia opened this issue 1 month ago • 1 comments

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 keeps pre, post, comb and the stream mixing in FP32 (Block.hc_pre and Block.hc_post in DeepSeek's inference/model.py).
  • GLM-5.3-Flash needs both flags. Megatron Bridge sets mhc_norm_eps_inside_sqrt=True and mhc_keep_mappings_in_fp32=True for it.

The fused path hard-codes the epsilon outside the square root, 1 / (sqrt(sum(x**2) / K) + eps):

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

  1. Support mhc_norm_eps_inside_sqrt in the fused path: compute rsqrt(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.
  2. Support mhc_keep_mappings_in_fp32 in the fused path: pass h_pre, h_post and h_res to fused_h_aggregate and fused_h_post_bda in 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-6 and stream RMS r, the normalization factor changes by about eps / (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 + eps and 2 * sigmoid gates, Sinkhorn (softmax + eps, a column pass, then row/column passes), the H_res^T @ residual mixing direction, and learned_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 avatar Nov 05 '26 19:11 kamran-nvidia

@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.

YYuJLiu avatar Nov 26 '26 01:11 YYuJLiu