bridge.models.gemma.gemma4_bridge#
Megatron Bridge for Gemma 4 text-only (CausalLM).
Supports all Gemma 4 text variants:
MoE (
enable_moe_block=True):Gemma4ForCausalLM(26B-A4B and similar)Dense (
enable_moe_block=False): same HF class, dispatched viaGemma4DenseProvider
Usage::
AutoBridge.from_hf_pretrained(“google/gemma-4-26B-A4B”) └─ Gemma4Bridge (registered for Gemma4ForCausalLM) ├─ provider_bridge() MoE → Gemma4ModelProvider │ Dense → Gemma4DenseProvider └─ mapping_registry() MoE path → _moe_mapping_registry() Dense path → _dense_mapping_registry()
Module Contents#
Classes#
QKV mapping tolerating missing v_proj on global attention layers (K=V). |
|
QKV mapping tolerating missing k_proj AND v_proj on shared-KV layers. |
|
Megatron Bridge for Gemma 4 text-only (CausalLM). |
Functions#
Use a compact cycle when possible, otherwise preserve the per-layer pattern. |
|
Read an attention field from a concrete HF layer config when available. |
|
Return whether a provider or builder config describes a dense Gemma 4 model. |
|
Reconstruct the Hugging Face per-layer attention pattern. |
|
Reconstruct Gemma 4’s dual local/global RoPE configuration. |
Data#
API#
- class bridge.models.gemma.gemma4_bridge._Gemma4QKVMapping(*args, **kwargs)#
Bases:
megatron.bridge.models.conversion.param_mapping.QKVMappingQKV mapping tolerating missing v_proj on global attention layers (K=V).
Initialization
- class bridge.models.gemma.gemma4_bridge._Gemma4DenseQKVMapping(*args, **kwargs)#
Bases:
megatron.bridge.models.conversion.param_mapping.QKVMappingQKV mapping tolerating missing k_proj AND v_proj on shared-KV layers.
Initialization
- bridge.models.gemma.gemma4_bridge._infer_attn_pattern(
- layer_types: list[str],
Use a compact cycle when possible, otherwise preserve the per-layer pattern.
- bridge.models.gemma.gemma4_bridge._attention_config_value(
- hf_config: Any,
- layer_type: str,
- field_name: str,
- default: Any,
- *,
- legacy_field_name: str | None = None,
Read an attention field from a concrete HF layer config when available.
- bridge.models.gemma.gemma4_bridge._Gemma4Config#
None
- bridge.models.gemma.gemma4_bridge._is_dense( ) bool#
Return whether a provider or builder config describes a dense Gemma 4 model.
- bridge.models.gemma.gemma4_bridge._layer_types_from_provider( ) list[str]#
Reconstruct the Hugging Face per-layer attention pattern.
- bridge.models.gemma.gemma4_bridge._rope_parameters_from_provider( ) dict[str, dict]#
Reconstruct Gemma 4’s dual local/global RoPE configuration.
- bridge.models.gemma.gemma4_bridge._GEMMA4_DENSE_DEFAULTS: dict[str, Any]#
None
- bridge.models.gemma.gemma4_bridge._GEMMA4_MOE_DEFAULTS: dict[str, Any]#
None
- bridge.models.gemma.gemma4_bridge._GPT_PROVIDER_PARITY_DEFAULTS: dict[str, Any]#
None
- class bridge.models.gemma.gemma4_bridge.Gemma4Bridge#
Bases:
megatron.bridge.models.conversion.model_bridge.MegatronModelBridgeMegatron Bridge for Gemma 4 text-only (CausalLM).
Dispatches to Dense or MoE path based on
enable_moe_blockin HF config:get_model_config()returns aGemma4DenseModelConfigorGemma4ModelConfig, and the legacyto_megatron_provider()the matching provider, both built from the same settings.- MODEL_CONFIG_CLASS#
None
- USE_MODEL_CONFIG_FOR_CONVERSION#
True
- _CONDITIONAL_MOE_FIELDS#
‘frozenset(…)’
- CONFIG_MAPPING#
None
- _should_map_hf_config_field(
- hf_config: Any,
- hf_name: str,
- megatron_name: str,
- value: Any,
- provider_bridge(
- hf_pretrained: megatron.bridge.models.hf_pretrained.causal_lm.PreTrainedCausalLM,
- _text_config() Any | None#
Return the text config used to dispatch dense vs MoE behavior.
- _is_dense_config() bool#
Return whether the current HF config describes a dense Gemma 4 model.
- hf_config_to_model_config(
- hf_config: Any,
Build the dense or MoE Gemma 4 builder config from a Gemma 4 text config.
- _text_model_config_kwargs(
- hf_config: Any,
Flat builder config kwargs for a Gemma 4 text config, dense or MoE.
The vision-language bridge reuses them for its language model. They omit the MoE layer spec, which
_build_model_config()adds for builder configs only.
- _build_model_config(
- config_class: type[megatron.bridge.models.gemma.gemma4_config.Gemma4ModelConfig] | type[megatron.bridge.models.gemma.gemma4_config.Gemma4DenseModelConfig],
- config_kwargs: dict[str, Any],
Construct a Gemma 4 builder config, or a subclass of one, from flat kwargs.
- _build_dense_provider(
- hf_config,
Build a Gemma4DenseProvider from HF config.
- _dense_config_kwargs(hf_config) dict[str, Any]#
Flat dense settings shared by
Gemma4DenseModelConfigandGemma4DenseProvider.
- _build_moe_provider(
- hf_config,
Build a Gemma4ModelProvider from HF config (MoE path).
- _moe_config_kwargs(
- base_kwargs: dict[str, Any],
- hf_config,
Add Gemma 4 MoE settings to kwargs mapped from
CONFIG_MAPPING.Shared by
Gemma4ModelConfigandGemma4ModelProvider.
- classmethod megatron_to_hf_config( ) dict#
Preserve Gemma 4 architecture fields affected by provider conversion.
Accepts a legacy provider or a builder config, which exposes its nested transformer fields as attributes.
- _is_synthesized_kv_projection(hf_name: str) bool#
Return whether a KV projection exists only in the Megatron model.
- maybe_modify_converted_hf_weight(
- task: megatron.bridge.models.conversion.model_bridge.WeightConversionTask | None,
- converted_weights_dict: dict[str, torch.Tensor],
- hf_state_dict: Mapping[str, torch.Tensor] | None,
Drop runtime-only synthesized KV projections on export.
- maybe_modify_loaded_hf_weight(
- hf_param: str | dict[str, str],
- hf_state_dict: Mapping[str, torch.Tensor],
Handle special weight loading for Gemma 4.
- mapping_registry() megatron.bridge.models.conversion.mapping_registry.MegatronMappingRegistry#
- _dense_mapping_registry(
- megatron_prefix: str = '',
Parameter mappings for the Dense variant.
- _hf_layer_prefix() str#
Text-only CausalLM: weights at
model.*; override in VL subclass.
- _moe_mapping_registry(
- megatron_prefix: str = '',
Parameter mappings for the MoE variant.
- _split_qkv_linear_out_weight(megatron_model, linear_out_weight)#
Detect global vs sliding layers by tensor size for LoRA export.