core.transformer.wide_residual#
Wide-residual parameters, connection policy, and model-boundary helpers.
Module Contents#
Classes#
Bounded learned retention with one controller per full-width stream. |
|
Positive scalar controller for each contiguous full-width stream. |
|
Learn an independent ordinary-width read from a wide residual stream. |
|
Streamwise residual maps restricted to a read or write at construction. |
|
Learned streamwise readout from D’ to D before final normalization. |
Functions#
Create a parameter vector padded for small distributed-optimizer shards. |
|
Return the active prefix of a potentially padded vector parameter. |
|
Attach optimizer and TP-gradient metadata to a replicated residual map. |
|
Mark a replicated controller for the correct TP gradient reduction. |
|
Attach optimizer and TP-gradient metadata to a replicated retention parameter. |
|
Accept existing residual model keys with strict loading. |
|
Build the streamwise model-boundary readout when wide residuals are enabled. |
|
Replicate an ordinary-width activation into contiguous residual streams. |
|
Return whether selective residual-stream replay is active for this forward. |
Data#
API#
- core.transformer.wide_residual._MIN_CONTROLLER_NUMEL#
128
- core.transformer.wide_residual._make_padded_vector(values: torch.Tensor) torch.nn.Parameter#
Create a parameter vector padded for small distributed-optimizer shards.
- core.transformer.wide_residual._active_vector(param: torch.Tensor, length: int) torch.Tensor#
Return the active prefix of a potentially padded vector parameter.
- core.transformer.wide_residual._mark_residual_map_parameter(
- param: torch.nn.Parameter,
- config: megatron.core.transformer.transformer_config.TransformerConfig,
Attach optimizer and TP-gradient metadata to a replicated residual map.
- core.transformer.wide_residual._mark_replicated_tp_parameter(
- param: torch.nn.Parameter,
- config: megatron.core.transformer.transformer_config.TransformerConfig,
Mark a replicated controller for the correct TP gradient reduction.
- core.transformer.wide_residual._mark_retention_parameter(
- param: torch.nn.Parameter,
- config: megatron.core.transformer.transformer_config.TransformerConfig,
Attach optimizer and TP-gradient metadata to a replicated retention parameter.
- class core.transformer.wide_residual.LearnedWideResidualRetention(
- config: megatron.core.transformer.transformer_config.TransformerConfig,
- layer_number: int,
- branch_name: str,
- *,
- num_streams: int,
Bases:
torch.nn.ModuleBounded learned retention with one controller per full-width stream.
Initialization
- factors() torch.Tensor#
Return one FP32 retention factor for every full-width stream.
- forward(*, return_logits: bool = False) torch.Tensor#
Return padded logits or materialized retention factors.
- class core.transformer.wide_residual.StreamwiseSigmoidMap(
- config: megatron.core.transformer.transformer_config.TransformerConfig,
- *,
- map_kind: Literal[read, write],
Bases:
torch.nn.ModulePositive scalar controller for each contiguous full-width stream.
Initialization
- factors() torch.Tensor#
Return one positive factor for every full-width residual stream.
- forward(*, return_logits: bool = False) torch.Tensor#
Return padded logits or one positive factor per full-width stream.
- class core.transformer.wide_residual.StreamwiseSigmoidWideResidualRead(
- config: megatron.core.transformer.transformer_config.TransformerConfig,
- layer_number: int,
- branch_name: str,
Bases:
torch.nn.ModuleLearn an independent ordinary-width read from a wide residual stream.
Initialization
- forward(hidden_states: torch.Tensor) torch.Tensor#
Read in branch precision; the operator fixes the output shape and dtype.
- class core.transformer.wide_residual.StreamwiseSigmoidWideResidualConnection(
- config: megatron.core.transformer.transformer_config.TransformerConfig,
- layer_number: int,
- branch_name: str,
- pg_collection: megatron.core.process_groups_config.ProcessGroupCollection | None = None,
- name: str | None = None,
- *,
- operation: megatron.core.transformer.residual_connection.ResidualConnectionOperation,
Bases:
megatron.core.transformer.residual_connection.ResidualConnectionStreamwise residual maps restricted to a read or write at construction.
Initialization
- _read(
- hidden_states: torch.Tensor,
- *,
- fp32_residual_connection: bool,
- branch_input_dtype: torch.dtype | None,
Read in branch precision before promoting the carried residual.
- _write(
- branch_output: megatron.core.transformer.residual_connection.ResidualBranchOutput,
- state: megatron.core.transformer.residual_connection.ResidualConnectionState,
- *,
- dropout_probability: float,
- training: bool,
- _checkpoint_read(
- hidden_states: torch.Tensor,
- context: megatron.core.transformer.residual_recompute.ResidualStreamRecomputeContext,
- *,
- fp32_residual_connection: bool,
- branch_input_dtype: torch.dtype | None = None,
Replay WR’s read while retaining its incoming residual stream.
An optional branch-input dtype request is captured by the replay closure so eager execution and backward reconstruction produce the same branch dtype.
- _checkpoint_write(
- branch_output: megatron.core.transformer.residual_connection.ResidualBranchOutput,
- state: megatron.core.transformer.residual_connection.ResidualConnectionState,
- context: megatron.core.transformer.residual_recompute.ResidualStreamRecomputeContext,
- *,
- dropout_probability: float,
- training: bool,
Replay WR’s whole write, whose output has independent storage.
- core.transformer.wide_residual._RESIDUAL_STATE_PREFIXES#
‘tuple(…)’
- core.transformer.wide_residual.load_residual_state(
- module,
- state_dict,
- prefix,
- local_metadata,
- strict,
- missing,
- unexpected,
- errors,
Accept existing residual model keys with strict loading.
- class core.transformer.wide_residual.StreamwiseSigmoidResidualReadout(
- config: megatron.core.transformer.transformer_config.TransformerConfig,
Bases:
torch.nn.ModuleLearned streamwise readout from D’ to D before final normalization.
Initialization
- forward(hidden_states: torch.Tensor) torch.Tensor#
Mix the full-width streams into one backbone-width activation.
- core.transformer.wide_residual.build_wide_residual_readout(
- config: megatron.core.transformer.transformer_config.TransformerConfig,
Build the streamwise model-boundary readout when wide residuals are enabled.
- core.transformer.wide_residual.expand_wide_residual_stream(
- hidden_states: torch.Tensor,
- num_streams: int,
Replicate an ordinary-width activation into contiguous residual streams.
- core.transformer.wide_residual.wide_residual_recompute_enabled(
- config: megatron.core.transformer.transformer_config.TransformerConfig,
- training: bool,
Return whether selective residual-stream replay is active for this forward.