bridge.models.qwen.modeling_qwen4_exp.gated_residual#
Qwen4-Exp gated residual streams and learned output contraction.
Module Contents#
Classes#
Gated Residual hyper-connection of Qwen4-Exp (Qwen3.8-Flash-Next). |
|
Block-level learned contraction of the Qwen4-Exp residual streams. |
Functions#
Zero-centered RMSNorm applied independently to each of the |
API#
- bridge.models.qwen.modeling_qwen4_exp.gated_residual.gated_residual_group_rmsnorm(
- x: torch.Tensor,
- weight: torch.Tensor,
- n: int,
- eps: float,
Zero-centered RMSNorm applied independently to each of the
nresidual streams.- Parameters:
x β [β¦, n*C] n-stream hidden states.
weight β [n*C] zero-centered gain (the effective scale is
1 + weight).n β Number of residual streams.
eps β RMSNorm epsilon.
- Returns:
[β¦, n*C] normalized streams in
x.dtype(computed in fp32).
- class bridge.models.qwen.modeling_qwen4_exp.gated_residual.GatedResidualHyperConnection(
- config: megatron.core.transformer.transformer_config.TransformerConfig,
- layer_number: int = 1,
- use_combine: bool = True,
Bases:
megatron.core.transformer.module.MegatronModuleGated Residual hyper-connection of Qwen4-Exp (Qwen3.8-Flash-Next).
Every sub-layer (attention / MLP) of a decoder layer is wrapped by one instance. With
nresidual streams of widthCtravelling between layers as[s, b, n*C]:xn = GroupRMSNorm(x) # per-stream, zero-centered gain mix = sigmoid(W_up silu(W_down xn / n)) # [.., n*C] element-wise read gate block_in = mean_j(mix_j * xn_j) # [.., C] sub-layer input inject = 2 * sigmoid(W_inject xn / n) # [.., n] per-stream write gate x_new = x + inject_j * F(block_in) for every stream j
The interface matches :class:
HyperConnectionModuleso that- Class:
~megatron.core.transformer.transformer_layer.HyperConnectionTransformerLayerdrives both formulations:forwardreturns(block_input, h_res, h_post)whereh_resis an empty tensor (the residual mixing is the identity) andh_postisinject, and- Meth:
fused_h_res_h_post_bdaapplies the write gate. Withuse_combine=Falsethe module is the block-level output mixer: it only returnsblock_in(see- Class:
GatedResidualOutputMixer).
Parameters are replicated across tensor-parallel ranks and kept in
params_dtypelike the reference checkpoints; the norm statistics run in fp32; gates retain activation-dtype operation boundaries.Initialization
- classmethod estimate_flops(
- config: megatron.core.transformer.transformer_config.TransformerConfig,
- *,
- total_real_tokens_in_batch: float,
- seqlen_squared_sum_in_batch: float,
Count the read mixer and residual-injection projections (forward only).
- _mix(
- hidden_states: torch.Tensor,
- forward(
- hidden_states: torch.Tensor,
- mhc_recompute_manager: object | None = None,
- return_residual: bool = False,
- *,
- checkpoint_fn: collections.abc.Callable[..., torch.Tensor] | None = None,
Read the streams.
- Parameters:
hidden_states β [s, b, n*C] n-stream hidden states.
mhc_recompute_manager β Must be None; residual recomputation is unsupported.
return_residual β Also return the (unchanged) n-stream residual as a 4th element, like :class:
HyperConnectionModule.checkpoint_fn β Must be None; residual recomputation is unsupported.
- Returns:
(block_input [s, b, C], h_res=empty tensor, h_post [s, b, n]).h_postisNonefor the output mixer (use_combine=False).
- apply_h_res(
- h_res: Optional[torch.Tensor],
- residual: torch.Tensor,
The gated residual keeps the streams untouched (identity residual mixing).
- static _inject(
- residual: torch.Tensor,
- inject: torch.Tensor,
- x: torch.Tensor,
- bias: Optional[torch.Tensor],
- n: int,
- fused_h_res_h_post_bda(
- h_res: Optional[torch.Tensor],
- original_residual: torch.Tensor,
- h_post: torch.Tensor,
- layer_output_with_bias: Tuple[torch.Tensor, Optional[torch.Tensor]],
- dropout_prob: float,
- training: bool,
- fused: bool,
- manager: object | None = None,
- *,
- checkpoint_fn: collections.abc.Callable[..., torch.Tensor] | None = None,
Write the sub-layer output back to every stream:
x + inject_j * (F(x) + bias).- Parameters:
h_res β Unused (identity residual mixing).
original_residual β [s, b, n*C] n-stream hidden states before the sub-layer.
h_post β [s, b, n] per-stream write gates returned by :meth:
forward.layer_output_with_bias β
(x [s, b, C], bias [C] or None).fused (dropout_prob / training /) β bias-dropout-add settings; dropout is applied to the broadcast update when non-zero.
manager β Must be None; residual recomputation is unsupported.
checkpoint_fn β Must be None; residual recomputation is unsupported.
- Returns:
[s, b, n*C] updated streams.
- class bridge.models.qwen.modeling_qwen4_exp.gated_residual.GatedResidualOutputMixer(
- config: megatron.core.transformer.transformer_config.TransformerConfig,
- hidden_size: Optional[int] = None,
- eps: Optional[float] = None,
Bases:
bridge.models.qwen.modeling_qwen4_exp.gated_residual.GatedResidualHyperConnectionBlock-level learned contraction of the Qwen4-Exp residual streams.
Replaces the final layer norm of a
- Class:
~megatron.core.transformer.transformer_block.TransformerBlockbuilt with the Bridge-local Qwen4-Exp layer spec: it consumes the[s, b, n*C]streams and returns the[s, b, C]gated mean of their normalized values (hyper_connection_mixerin the HF checkpoint). Coreβs generic mHC path remains disabled; the local HybridModel expands streams before the decoder so the learned mixer receives every stream without Coreβs unweighted mean contraction.
The
(config, hidden_size, eps)signature matches the layer-norm builders used in block specs.Initialization
- classmethod estimate_flops(
- config: megatron.core.transformer.transformer_config.TransformerConfig,
- *,
- total_real_tokens_in_batch: float,
- seqlen_squared_sum_in_batch: float,
Count the final read mixer; it has no residual-injection projection.
- forward(hidden_states: torch.Tensor) torch.Tensor#
[s, b, n*C] streams -> [s, b, C] mixed output.