bridge.models.gpt_oss.gpt_oss_bridge#

Module Contents#

Classes#

GPTOSSBridge

Megatron Hub Bridge for GPT-OSS models.

GPTOSSMLPDownProjMapping

MLPDownProj for expert weights in GPT-OSS models.

GPTOSSMLPGateUpProjMapping

MLPGateUpProj for expert weights in GPT-OSS models.

API#

class bridge.models.gpt_oss.gpt_oss_bridge.GPTOSSBridge#

Bases: megatron.bridge.models.conversion.model_bridge.MegatronModelBridge

Megatron Hub Bridge for GPT-OSS models.

As a user you would not use this bridge directly, but through AutoBridge.

.. rubric:: Example

from megatron.bridge import AutoBridge bridge = AutoBridge.from_hf_pretrained(“openai/gpt-oss-model”) model_config = bridge.get_model_config()

MODEL_CONFIG_CLASS#

None

USE_MODEL_CONFIG_FOR_CONVERSION#

True

_get_fused_adapter_linear_out_slices(
megatron_model: list[megatron.bridge.models.conversion.peft_bridge.MegatronModel],
base_hf_weight_names: list[str],
linear_out_tensor: torch.Tensor,
is_expert: bool = False,
) → dict[str, torch.Tensor] | None#

Put expert LoRA-B output rows in HF’s alternating gate/up order.

hf_config_to_model_config_kwargs(
hf_config: transformers.PretrainedConfig,
) → dict[str, Any]#

Convert a Hugging Face GPT-OSS config to builder config kwargs.

hf_config_to_provider_kwargs(
hf_config: transformers.PretrainedConfig,
) → dict[str, Any]#

Adapt the canonical builder mapping to the deprecated provider path.

classmethod megatron_to_hf_config(provider) → dict#

Convert Megatron provider config to a GPT-OSS Hugging Face config.

maybe_modify_loaded_hf_weight(
hf_param: str | dict[str, str],
hf_state_dict: Mapping[str, torch.Tensor],
) → torch.Tensor#

Load weights from HuggingFace state dict with MXFP4 dequantization support.

Megatron’s grouped expert linears expect per-expert linear_fc1 weights as (2*intermediate, hidden) and linear_fc2 weights as (hidden, intermediate). MXFP4-dequantized weights come out in that layout. BF16 checkpoints (e.g. unsloth/gpt-oss-20b-BF16, and what transformers.GptOssForCausalLM produces at init) store the transposes: gate_up_proj as [E, hidden, 2*intermediate] and down_proj as [E, intermediate, hidden]. Shape alone cannot tell the two layouts apart when a projection is square, as down_proj is for GPT-OSS-20B/120B (hidden == intermediate) and gate_up_proj is when hidden == 2*intermediate. So a BF16 expert weight whose last two dimensions match Hugging Face’s layout, square ones included, is transposed here. Without this, BF16 imports silently store the projection in the wrong orientation and inference is broken.

mapping_registry() → megatron.bridge.models.conversion.mapping_registry.MegatronMappingRegistry#

Return MegatronMappingRegistry containing parameter mappings from HF to Megatron format. Based on the GPT-OSS importer code provided.

class bridge.models.gpt_oss.gpt_oss_bridge.GPTOSSMLPDownProjMapping(
megatron_param: str,
hf_param: str,
permute_dims: Optional[Tuple[int, ...]] = None,
)#

Bases: megatron.bridge.models.conversion.param_mapping.AutoMapping

MLPDownProj for expert weights in GPT-OSS models.

Initialization

is_grouped_export#

True

oai_mxfp4_export#

True

property group_key: str#
hf_to_megatron(
hf_weights: torch.Tensor,
megatron_module: torch.nn.Module,
) → torch.Tensor#
megatron_to_hf(
megatron_weights: torch.Tensor,
megatron_module: torch.nn.Module,
) → Dict[str, torch.Tensor]#
class bridge.models.gpt_oss.gpt_oss_bridge.GPTOSSMLPGateUpProjMapping(
megatron_param: str,
hf_param: str,
permute_dims: Optional[Tuple[int, ...]] = None,
)#

Bases: megatron.bridge.models.conversion.param_mapping.AutoMapping

MLPGateUpProj for expert weights in GPT-OSS models.

GPT-OSS uses alternating row interleaving for gate/up projections.

Initialization

is_grouped_export#

True

oai_mxfp4_export#

True

property group_key: str#
static _interleave(gate_up_proj)#
static _uninterleave(elem: torch.Tensor) → torch.Tensor#
hf_to_megatron(
hf_weights: Union[torch.Tensor, Dict],
megatron_module: torch.nn.Module,
) → torch.Tensor#
megatron_to_hf(
megatron_weights: torch.Tensor,
megatron_module: torch.nn.Module,
) → Dict[str, torch.Tensor]#