nemo_rl.environments.nemotron_utils#

Module Contents#

Functions#

_required_config_value

load_nemotron_video_model_config

_nemotron_video_target_resolution

Return the SFT/vLLM-compatible (width, height) for a video frame.

_resize_and_normalize_nemotron_video_frame

Resize one frame with the same numeric order as stock vLLM.

_flatten_nemotron_video_frame_messages

Replace locally decoded frame items with ordered <image> markers.

_render_nemotron_video_prompt

_expand_nemotron_video_placeholders

Match vLLM’s timestamped one-wrapper-per-tubelet video replacement.

process_nemotron_video_frames

Port the source branch’s dynamic video-frame preprocessing contract.

image_placeholder_token_ids_from_tokenizer

(image_token_id, image_start_id, image_end_id), or None if unavailable.

get_image_placeholder_token_ids

(image_token_id, image_start_id, image_end_id), or None if unavailable.

supports_image_placeholder_run_parity

Whether rollout tokens can be parsed into per-image placeholder runs.

count_image_placeholder_runs

Per-image placeholder run lengths from rollout token ids, in order.

placeholder_runs_from_token_ids

Per-media placeholder run lengths from token ids, in order.

_is_static_video_turn

Whether a source user turn carries a statically-preprocessed video.

verify_static_video_media_alignment

Verify rollout tokens match the static video tensors before attach.

predicted_static_image_num_tokens

Predict per-image token counts of the processor’s static image path.

_process_single_image_at_num_tokens

Run the processor on one image, pinned to an exact media token count.

reprocess_images_at_rollout_budgets

Re-run the processor per image at the rollout’s exact per-image budget.

Data#

API#

nemo_rl.environments.nemotron_utils.NEMOTRON_VIDEO_PROCESSOR_NAMES#

‘frozenset(…)’

nemo_rl.environments.nemotron_utils._required_config_value(config: Any, name: str) → Any#
nemo_rl.environments.nemotron_utils.load_nemotron_video_model_config(model_name: str) → Any#
nemo_rl.environments.nemotron_utils._nemotron_video_target_resolution(
*,
original_width: int,
original_height: int,
target_num_patches: int,
patch_size: int,
downsample_ratio: float,
maintain_aspect_ratio: bool,
) → tuple[int, int]#

Return the SFT/vLLM-compatible (width, height) for a video frame.

nemo_rl.environments.nemotron_utils._resize_and_normalize_nemotron_video_frame(
frame: PIL.Image.Image,
*,
target_height: int,
target_width: int,
norm_mean: torch.Tensor,
norm_std: torch.Tensor,
) → torch.Tensor#

Resize one frame with the same numeric order as stock vLLM.

nemo_rl.environments.nemotron_utils._flatten_nemotron_video_frame_messages(
messages: list[dict[str, Any]],
) → tuple[list[dict[str, Any]], list[PIL.Image.Image], list[int], float]#

Replace locally decoded frame items with ordered <image> markers.

nemo_rl.environments.nemotron_utils._render_nemotron_video_prompt(
processor: Any,
messages: list[dict[str, Any]],
template_kwargs: dict[str, Any],
) → str#
nemo_rl.environments.nemotron_utils._expand_nemotron_video_placeholders(
rendered_text: str,
*,
embeddings_per_tubelet: list[int],
frame_indices: list[int],
fps: float,
temporal_patch_size: int,
) → str#

Match vLLM’s timestamped one-wrapper-per-tubelet video replacement.

nemo_rl.environments.nemotron_utils.process_nemotron_video_frames(
processor: Any,
messages: list[dict[str, Any]],
*,
template_kwargs: dict[str, Any],
temporal_patch_size: int,
target_num_patches: int,
maintain_aspect_ratio: bool,
) → dict[str, torch.Tensor]#

Port the source branch’s dynamic video-frame preprocessing contract.

nemo_rl.environments.nemotron_utils.image_placeholder_token_ids_from_tokenizer(
tokenizer: Any,
*,
image_token: str = '<image>',
image_start_token: str = '<img>',
image_end_token: str = '</img>',
) → tuple[int, int, int] | None#

(image_token_id, image_start_id, image_end_id), or None if unavailable.

nemo_rl.environments.nemotron_utils.get_image_placeholder_token_ids(
processor: Any,
) → tuple[int, int, int] | None#

(image_token_id, image_start_id, image_end_id), or None if unavailable.

nemo_rl.environments.nemotron_utils.supports_image_placeholder_run_parity(processor: Any) → bool#

Whether rollout tokens can be parsed into per-image placeholder runs.

nemo_rl.environments.nemotron_utils.count_image_placeholder_runs(
token_ids: Sequence[int] | torch.Tensor,
processor: Any,
) → list[int]#

Per-image placeholder run lengths from rollout token ids, in order.

The rollout engine (and this processor) expand each image to <img><image>*N</img>; each run’s N is the exact number of projected media features the model expects for that image.

nemo_rl.environments.nemotron_utils.placeholder_runs_from_token_ids(
token_ids: Sequence[int] | torch.Tensor,
placeholder_ids: tuple[int, int, int],
) → list[int]#

Per-media placeholder run lengths from token ids, in order.

nemo_rl.environments.nemotron_utils._is_static_video_turn(message: dict[str, Any]) → bool#

Whether a source user turn carries a statically-preprocessed video.

Still-image turns also carry num_frames (one 1 per image, added by the image attach path); a video turn is the one whose frame count exceeds 1.

nemo_rl.environments.nemotron_utils.verify_static_video_media_alignment(
source_message: dict[str, Any],
target_message: dict[str, Any],
tokenizer: Any,
) → None#

Verify rollout tokens match the static video tensors before attach.

The static video datum expands its video to one <img><image>*k</img> run per tubelet, and its tensors project exactly sum(k) features. The rollout engine performs its own expansion; if the two disagree, training would crash deep in Megatron with an unattributable “media alignment failed”. Compare the run structures here and raise a diagnostic that names the mismatch (tubelet count vs per-tubelet token count) instead.

A no-op for non-video turns or when placeholder ids cannot be resolved.

nemo_rl.environments.nemotron_utils.predicted_static_image_num_tokens(
processor: Any,
image_sizes: list[tuple[int, int]],
) → list[int] | None#

Predict per-image token counts of the processor’s static image path.

Mirrors the budget selection in the checkpoint’s image processor _preprocess (image path) and reuses its own _compute_target_patches for the grid math, so it costs no pixel work. Returns None when the processor does not expose the needed hooks (callers must then assume the static output is unverified and attach rollout-matched media themselves).

exception nemo_rl.environments.nemotron_utils.RolloutGeometryUnderdetermined#

Bases: ValueError

The rollout’s image token count cannot be reproduced without guessing.

A placeholder-run length does not uniquely identify the rollout engine’s preprocessing grid (several grids share one product), so when re-running the processor under the rollout’s own budget fails to reproduce the count, inferring a grid from the count risks training against different pixels and feature ordering than generated the rollout. Callers must treat the affected sample as unusable media rather than guess.

Initialization

Initialize self. See help(type(self)) for accurate signature.

nemo_rl.environments.nemotron_utils._process_single_image_at_num_tokens(
processor: Any,
image: PIL.Image.Image,
num_tokens: int,
) → dict[str, Any]#

Run the processor on one image, pinned to an exact media token count.

nemo_rl.environments.nemotron_utils.reprocess_images_at_rollout_budgets(
processor: Any,
images: list[PIL.Image.Image],
expected_num_tokens: list[int],
) → dict[str, Any]#

Re-run the processor per image at the rollout’s exact per-image budget.