bridge.models.nemotron_omni.nemotron_omni_utils#
Module Contents#
Functions#
Resize and normalize one frame for MCoreβs square temporal RADIO path. |
|
Return frames passed to MCore for one temporal-video sample. |
|
Build image-placeholder replacement counts for pipeline inference. |
|
Aggregate projected feature counts for canonical inference prompts. |
|
Return the legacy unpadded length after model-owned vision expansion. |
|
Select the next token from the last real position, excluding PP padding. |
|
Load an audio file and resample to |
|
Construct one reusable feature extractor per audio configuration. |
|
Convert Parakeet feature masks to contiguous-prefix frame lengths. |
|
Convert one waveform to physical mel features and its valid frame length. |
|
Convert a raw waveform to a mel spectrogram tensor. |
|
Compute the expected number of audio tokens for a waveform. |
Data#
API#
- bridge.models.nemotron_omni.nemotron_omni_utils._FrameT#
βTypeVar(β¦)β
- bridge.models.nemotron_omni.nemotron_omni_utils.COMPACT_IMAGE_PLACEHOLDER#
β
β
- bridge.models.nemotron_omni.nemotron_omni_utils.patchify_temporal_frame(
- frame: Any,
- *,
- height: int,
- width: int,
- patch_dim: int,
Resize and normalize one frame for MCoreβs square temporal RADIO path.
The public HF processor preserves aspect ratio, but pinned MCore requires temporal tubelets to share one square spatial grid. This helper is shared by training collation and inference so both paths use the same antialiased bicubic interpolation, RADIO normalization, and patch layout.
- Parameters:
frame β PIL-compatible image with
convert("RGB")support.height β Compatibility-canvas height.
width β Compatibility-canvas width.
patch_dim β Vision patch edge length.
- Returns:
A tensor with shape
[num_patches, 3 * patch_dim * patch_dim].
- bridge.models.nemotron_omni.nemotron_omni_utils.temporal_model_frames(
- frames: collections.abc.Sequence[bridge.models.nemotron_omni.nemotron_omni_utils._FrameT],
- temporal_patch_size: int,
Return frames passed to MCore for one temporal-video sample.
MCore pads incomplete groups with the final frame, so odd multi-frame samples retain their original metadata. A single frame needs explicit repetition to select the temporal embedder instead of the image embedder.
- Parameters:
frames β Sampled frames in prompt order.
temporal_patch_size β Number of frames fused into one temporal tubelet.
- Returns:
Frames for model patchification and
num_framesmetadata.
- bridge.models.nemotron_omni.nemotron_omni_utils.inference_num_image_tiles(
- imgs_sizes: torch.Tensor,
- *,
- patch_dim: int,
- pixel_shuffle_factor: int = 2,
- num_frames: torch.Tensor | None = None,
- temporal_patch_size: int = 1,
Build image-placeholder replacement counts for pipeline inference.
Dynamic images contribute their post-pixel-shuffle feature count per tile. Temporal tubelets contribute one logical count each; canonical inference applies the fixed tubelet width through
- Func:
inference_expanded_image_token_counts. The deprecated LLaVA path instead applies that width inside the model.- Parameters:
imgs_sizes β Per-image or per-frame
(height, width)metadata.patch_dim β Vision patch edge length.
pixel_shuffle_factor β Spatial downsampling factor per dimension.
num_frames β Frame counts per temporal video, or
Nonefor images.temporal_patch_size β Frames fused into one temporal tubelet.
- Returns:
One integer replacement count per compact image placeholder.
- bridge.models.nemotron_omni.nemotron_omni_utils.inference_expanded_image_token_counts(
- tile_feature_counts: torch.Tensor,
- tiles_per_media: int | collections.abc.Sequence[int] | torch.Tensor,
- *,
- feature_multiplier: int = 1,
Aggregate projected feature counts for canonical inference prompts.
Dynamic image processors can split one source image into multiple RADIO tiles, while each
<img>...</img>region belongs to the source image. The canonical model needs one<image>placeholder per projected feature, so per-tile counts must be summed back to one count per region. Temporal inference uses one logical tile per tubelet and a fixed feature multiplier for the post-pixel-shuffle tubelet width.- Parameters:
tile_feature_counts β Number of projected feature rows produced by each RADIO tile or temporal tubelet.
tiles_per_media β Number of entries in
tile_feature_countsowned by each<img>...</img>region.feature_multiplier β Additional projected width per count. Use one for dynamic images and the tubelet feature width for temporal video.
- Returns:
One expanded placeholder count per
<img>...</img>region.- Raises:
ValueError β If counts are non-positive or do not account for every tile/tubelet.
- bridge.models.nemotron_omni.nemotron_omni_utils.inference_merged_sequence_length(
- input_ids: torch.Tensor,
- *,
- image_token_index: int,
- num_image_tiles: torch.Tensor | None,
- image_seq_len: int,
Return the legacy unpadded length after model-owned vision expansion.
This helper is deprecated because the canonical model consumes an already expanded sequence; its merged length is simply
input_ids.shape[1].- Parameters:
input_ids β One inference prompt row, including generated tokens so far.
image_token_index β Token ID replaced by vision embeddings.
num_image_tiles β Row-major replacement metadata per image placeholder.
image_seq_len β Embeddings contributed by each tile.
- Returns:
The real merged sequence length before pipeline padding.
- bridge.models.nemotron_omni.nemotron_omni_utils.select_inference_next_token(
- logits: torch.Tensor,
- merged_sequence_length: int,
Select the next token from the last real position, excluding PP padding.
- bridge.models.nemotron_omni.nemotron_omni_utils.load_audio(path: str, target_sr: int = 16000) numpy.ndarray#
Load an audio file and resample to
target_srHz.Supports WAV, MP3, FLAC, and other formats handled by soundfile (with librosa as a fallback for MP3 and other FFmpeg-decoded formats).
- Parameters:
path β Path to the audio file.
target_sr β Target sampling rate in Hz.
- Returns:
1-D float32 numpy array of the mono waveform at
target_sr.
- bridge.models.nemotron_omni.nemotron_omni_utils._parakeet_feature_extractor(
- num_mel_bins: int,
- sampling_rate: int,
Construct one reusable feature extractor per audio configuration.
- bridge.models.nemotron_omni.nemotron_omni_utils.valid_audio_feature_lengths(
- attention_mask: torch.Tensor,
- *,
- num_frames: int,
Convert Parakeet feature masks to contiguous-prefix frame lengths.
Parakeet retains a padded boundary frame in
input_featureswhile its feature-level attention mask records the semantic frame count. Bridgeβs sound encoder accepts that mask in compressed form assound_length.- Parameters:
attention_mask β Binary mask with shape
(batch, frames).num_frames β Physical frame width of the corresponding feature tensor.
- Returns:
Long tensor containing one valid frame length per batch item.
- Raises:
ValueError β If the mask is empty, non-binary, has the wrong shape, or is not a non-empty contiguous prefix for every batch item.
- bridge.models.nemotron_omni.nemotron_omni_utils.compute_mel_features_with_length(
- waveform: Union[numpy.ndarray, list],
- sampling_rate: int = 16000,
- num_mel_bins: int = 128,
Convert one waveform to physical mel features and its valid frame length.
- Parameters:
waveform β 1-D float32 numpy array (or list) of the mono waveform.
sampling_rate β Sampling rate of waveform (must match the extractor).
num_mel_bins β Number of mel frequency bins.
- Returns:
A
(mel, valid_length)pair.melhas physical shape(frames, num_mel_bins)and may include padded boundary rows;valid_lengthis derived from Parakeetβs feature attention mask.
- bridge.models.nemotron_omni.nemotron_omni_utils.compute_mel_features(
- waveform: Union[numpy.ndarray, list],
- sampling_rate: int = 16000,
- num_mel_bins: int = 128,
Convert a raw waveform to a mel spectrogram tensor.
Uses HF
ParakeetFeatureExtractor(fromtransformers) to produce mel features compatible withBridgeSoundEncoder/ParakeetEncoder.- Parameters:
waveform β 1-D float32 numpy array (or list) of the mono waveform.
sampling_rate β Sampling rate of waveform (must match the extractor).
num_mel_bins β Number of mel frequency bins.
- Returns:
Float tensor of shape
(frames, num_mel_bins)β a single clip ready to be batched and passed assound_clipsto the model.Call :func:
compute_mel_features_with_lengthwhen the corresponding semantic frame length is also required.
- bridge.models.nemotron_omni.nemotron_omni_utils.compute_audio_token_count(
- waveform: Union[numpy.ndarray, list],
- hop_length: int = 160,
- subsampling_factor: int = 8,
Compute the expected number of audio tokens for a waveform.
Uses the same Conv2D subsampling math as
ParakeetEncoder/ParakeetEncoderSubsamplingConv2D: kernel_size=3, stride=2, padding=1, applied log2(subsampling_factor) times to the mel frame count.- Parameters:
waveform β 1-D waveform array (only its length is used).
hop_length β Hop length in samples for mel feature extraction.
subsampling_factor β Subsampling factor of the conformer encoder.
- Returns:
Number of audio tokens (at least 1).