bridge.models.gpt_oss.gpt_oss_bridge#
Module Contents#
Classes#
Megatron Hub Bridge for GPT-OSS models. |
|
MLPDownProj for expert weights in GPT-OSS models. |
|
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.MegatronModelBridgeMegatron 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,
Put expert LoRA-B output rows in HF’s alternating gate/up order.
- hf_config_to_model_config_kwargs(
- hf_config: transformers.PretrainedConfig,
Convert a Hugging Face GPT-OSS config to builder config kwargs.
- hf_config_to_provider_kwargs(
- hf_config: transformers.PretrainedConfig,
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],
Load weights from HuggingFace state dict with MXFP4 dequantization support.
Megatron’s grouped expert linears expect per-expert
linear_fc1weights as(2*intermediate, hidden)andlinear_fc2weights as(hidden, intermediate). MXFP4-dequantized weights come out in that layout. BF16 checkpoints (e.g.unsloth/gpt-oss-20b-BF16, and whattransformers.GptOssForCausalLMproduces at init) store the transposes:gate_up_projas[E, hidden, 2*intermediate]anddown_projas[E, intermediate, hidden]. Shape alone cannot tell the two layouts apart when a projection is square, asdown_projis for GPT-OSS-20B/120B (hidden == intermediate) andgate_up_projis 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.AutoMappingMLPDownProj 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,
- megatron_to_hf(
- megatron_weights: torch.Tensor,
- megatron_module: torch.nn.Module,
- 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.AutoMappingMLPGateUpProj 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,
- megatron_to_hf(
- megatron_weights: torch.Tensor,
- megatron_module: torch.nn.Module,