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. |
|
Return the unpadded sequence length after vision-token replacement. |
|
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 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.
The first pipeline stage can derive these counts from vision encoder outputs, but the last stage needs the same row-major metadata to expand input positions. Dynamic images contribute their post-pixel-shuffle token count per compact placeholder. Temporal tubelets contribute one tile each;
LLaVAModel.img_seq_lensupplies their fixed embedding width.- 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_merged_sequence_length(
- input_ids: torch.Tensor,
- *,
- image_token_index: int,
- num_image_tiles: torch.Tensor | None,
- image_seq_len: int,
Return the unpadded sequence length after vision-token replacement.
- 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.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.
- 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).