# 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 # This file was automatically generated from src/transformers/models/nemotron_h_omni/modular_nemotron_h_omni.py. # Do NOT edit this file manually as any edits will be overwritten by the generation of # the file from the modular. If any change should be done, please apply the change to the # modular_nemotron_h_omni.py file directly. One of our CI enforces this. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨 # Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. import warnings import torch from torch import nn from torch.nn import CrossEntropyLoss from transformers.activations import ACT2FN from transformers.generation import GenerationMixin from transformers.integrations import use_kernel_forward_from_hub from transformers.modeling_outputs import CausalLMOutputWithPast from transformers.modeling_utils import PreTrainedModel from transformers.conversion_mapping import register_checkpoint_conversion_mapping from transformers.core_model_loading import WeightRenaming from transformers.models.auto import CONFIG_MAPPING, FEATURE_EXTRACTOR_MAPPING, AutoModel, AutoModelForCausalLM from .configuration_nemotron_h_omni import NemotronH_Omni_Reasoning_V3_Config # Transformers 5.8 only copies direct sibling imports into its dynamic-module cache. from .configuration_radio import RadioConfig as _RadioConfig # noqa: F401 from .modeling_radio import RadioModel def register_nemotron_h_omni_conversion_mapping(): """ Register the conversion mapping for the Omni composition level (projector and sound prefixes). See `register_radio_conversion_mapping` in `modeling_radio.py` for why this lives in the remote code rather than in the global `conversion_mapping.py`. The vision mapping is registered there and the `nemotron_h` mapping ships with Transformers itself (upstream since v5.3.0); this entry covers the keys owned by the top-level model. """ register_checkpoint_conversion_mapping( "nemotron_h_omni", [ WeightRenaming(r"^mlp1\.0\.", r"vision_projector\.mlp1\.norm\."), WeightRenaming(r"^mlp1\.1\.", r"vision_projector\.mlp1\.linear1\."), WeightRenaming(r"^mlp1\.3\.", r"vision_projector\.mlp1\.linear2\."), WeightRenaming(r"^sound_encoder\.", r"sound_projector\.sound_encoder\."), WeightRenaming(r"^sound_projection\.", r"sound_projector\.sound_projection\."), ], overwrite=True, ) @use_kernel_forward_from_hub("RMSNorm") class NemotronH_Omni_RMSNorm(nn.Module): def __init__(self, hidden_size, eps: float = 1e-6) -> None: """ NemotronHOmniRMSNorm is equivalent to T5LayerNorm """ super().__init__() self.weight = nn.Parameter(torch.ones(hidden_size)) self.variance_epsilon = eps def forward(self, hidden_states) -> torch.Tensor: input_dtype = hidden_states.dtype hidden_states = hidden_states.to(torch.float32) variance = hidden_states.pow(2).mean(-1, keepdim=True) hidden_states = hidden_states * torch.rsqrt(variance + self.variance_epsilon) return (self.weight.to(torch.float32) * hidden_states).to(input_dtype) def extra_repr(self): return f"{tuple(self.weight.shape)}, eps={self.variance_epsilon}" class NemotronH_Omni_Reasoning_V3SoundEncoder(nn.Module): """Thin wrapper exposing the Parakeet encoder's `last_hidden_state` for audio embedding.""" def __init__(self, config): super().__init__() self.config = config self.encoder = AutoModel.from_config(config) def forward(self, input_features: torch.Tensor, attention_mask: torch.Tensor | None = None) -> torch.Tensor: return self.encoder(input_features=input_features, attention_mask=attention_mask).last_hidden_state @property def hidden_size(self) -> int: return self.config.hidden_size class NemotronH_Omni_Reasoning_V3SoundProjector(nn.Module): """Single-forward audio pipeline: feature-extract raw clip(s), encode with Parakeet and project, returning flattened LLM-space embeddings ready to scatter onto `