bridge.models.conversion.glu#
GLU row-layout conversions shared by checkpointing and model conversion.
Module Contents#
Functions#
Classify a Megatron FC1 parameter that may use GLU interleaving. |
|
Return the physical GLU interleave size for a Megatron FC1 parameter. |
|
Convert a logical contiguous GLU tensor to Megatron’s physical layout. |
|
Convert a physical block-interleaved GLU tensor to logical order. |
|
Return the FC1 projection-row dimension for ordinary or grouped storage. |
|
Apply the configured logical-to-physical GLU conversion when needed. |
|
Apply the configured physical-to-logical GLU conversion when needed. |
Data#
API#
- bridge.models.conversion.glu.GLUFC1Kind#
None
- bridge.models.conversion.glu._FC1_PARAM_RE#
‘compile(…)’
- bridge.models.conversion.glu._BIAS_PARAM_RE#
‘compile(…)’
- bridge.models.conversion.glu.classify_glu_fc1(
- param_name: str,
Classify a Megatron FC1 parameter that may use GLU interleaving.
Dense FC1 parameters use the routed-expert interleave configuration only when dense activation fusion is explicitly enabled, matching Megatron checkpoint loading and saving. Scale metadata, extra state and adapter parameters are not FC1 weights or biases and are excluded. Sequential experts keep contiguous gate/up rows even when the grouped interleave configuration remains set.
- Parameters:
param_name – Fully resolved Megatron parameter name.
- Returns:
The FC1 kind, or
Nonewhen the parameter is not interleaved.
- bridge.models.conversion.glu.get_glu_interleave_size(
- model_config: object,
- param_name: str,
Return the physical GLU interleave size for a Megatron FC1 parameter.
- Parameters:
model_config – Finalized Megatron model configuration.
param_name – Fully resolved Megatron parameter name.
- Returns:
A positive interleave size when the parameter uses the physical block-interleaved layout, otherwise
None.- Raises:
ValueError – If the selected configuration value is not a positive integer or
None.
- bridge.models.conversion.glu._validate_glu_layout(
- tensor: torch.Tensor,
- interleave_size: int,
- row_dim: int,
- bridge.models.conversion.glu.interleave_glu_tensor(
- tensor: torch.Tensor,
- interleave_size: int,
- *,
- row_dim: int = 0,
Convert a logical contiguous GLU tensor to Megatron’s physical layout.
The logical projection-row dimension is
[gate; up]. The physical projection-row dimension alternates fixed-size gate and up blocks.- Parameters:
tensor – GLU FC1 weight or bias in logical
[gate; up]order.interleave_size – Number of gate or up rows in each physical block.
row_dim – Projection-row dimension. Defaults to the leading dimension.
- Returns:
A contiguous tensor in the block-interleaved physical layout.
- bridge.models.conversion.glu.deinterleave_glu_tensor(
- tensor: torch.Tensor,
- interleave_size: int,
- *,
- row_dim: int = 0,
Convert a physical block-interleaved GLU tensor to logical order.
- Parameters:
tensor – GLU FC1 weight or bias in Megatron’s physical layout.
interleave_size – Number of gate or up rows in each physical block.
row_dim – Projection-row dimension. Defaults to the leading dimension.
- Returns:
A contiguous tensor in logical
[gate; up]order.
- bridge.models.conversion.glu._get_glu_projection_row_dim(
- tensor: torch.Tensor,
- param_name: str,
Return the FC1 projection-row dimension for ordinary or grouped storage.
- bridge.models.conversion.glu.logical_to_physical_glu_tensor(
- tensor: torch.Tensor,
- *,
- model_config: object,
- param_name: str,
Apply the configured logical-to-physical GLU conversion when needed.
- Parameters:
tensor – Logical GLU FC1 weight or bias.
model_config – Finalized Megatron model configuration.
param_name – Fully resolved Megatron parameter name.
- Returns:
The original tensor when interleaving is disabled, otherwise a contiguous tensor in the configured physical layout.
- bridge.models.conversion.glu.physical_to_logical_glu_tensor(
- tensor: torch.Tensor,
- *,
- model_config: object,
- param_name: str,
Apply the configured physical-to-logical GLU conversion when needed.
Quantized tensors must be dequantized before calling this function so that physical FP8 data remains paired with its scale metadata. Callers with a tensor containing multiple TP-local layouts must convert each shard before concatenating across TP. Leading expert/batch axes are preserved for grouped weights and biases. This is a one-time layout conversion, not an idempotent normalization.
- Parameters:
tensor – GLU FC1 weight or bias in Megatron’s physical layout.
model_config – Finalized Megatron model configuration.
param_name – Fully resolved Megatron parameter name.
- Returns:
The original tensor when interleaving is disabled, otherwise a contiguous tensor in logical
[gate; up]order.