nemo_rl.environments.nemotron_utils#
Module Contents#
Functions#
Return the SFT/vLLM-compatible |
|
Resize one frame with the same numeric order as stock vLLM. |
|
Replace locally decoded frame items with ordered |
|
Match vLLM’s timestamped one-wrapper-per-tubelet video replacement. |
|
Port the source branch’s dynamic video-frame preprocessing contract. |
|
(image_token_id, image_start_id, image_end_id), or None if unavailable. |
|
(image_token_id, image_start_id, image_end_id), or None if unavailable. |
|
Whether rollout tokens can be parsed into per-image placeholder runs. |
|
Per-image placeholder run lengths from rollout token ids, in order. |
|
Per-media placeholder run lengths from token ids, in order. |
|
Whether a source user turn carries a statically-preprocessed video. |
|
Verify rollout tokens match the static video tensors before attach. |
|
Predict per-image token counts of the processor’s static image path. |
|
Run the processor on one image, pinned to an exact media token count. |
|
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,
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,
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]],
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],
- 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,
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,
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>',
(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,
(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,
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],
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(one1per 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,
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 exactlysum(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]],
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_patchesfor 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:
ValueErrorThe 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,
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],
Re-run the processor per image at the rollout’s exact per-image budget.