#!/usr/bin/env python3 # SPDX-FileCopyrightText: Copyright (c) 2023-2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. # SPDX-License-Identifier: Apache-2.0 # # 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 asyncio import concurrent.futures import multiprocessing import os import queue import subprocess import sys import time from argparse import ArgumentParser from dataclasses import dataclass, field from enum import Enum from threading import Event, Lock, Thread from typing import Callable, Optional import nvtx import torch import yaml from grpc._channel import _MultiThreadedRendezvous from api_models.captions import VlmQuery from api_models.embeddings import TextEmbeddingsQuery from common.chunk_info import ChunkInfo from common.health_status import HealthStatus from common.logger import LOG_STATUS_LEVEL, logger from common.service_exception import ServiceException from models.base_vlm_model import VlmGenerationConfig, VlmModelOutput from models.dynamic_model_loader import DynamicModelLoader, load_model from utils.asset_manager import Asset from .errors import CUDA_OOM_STATUS_CODE, format_cuda_oom_error, is_cuda_oom_error from .ipc_frame_source import DEFAULT_IPC_SOCKET_DIR, select_ipc_stream_identity from .model_path_policy import validate_model_config, validate_model_path_source from .ngc_model_downloader import download_model, download_model_git from .process_base import ( ProcessBase, _contains_cuda_tensor, _move_cuda_frames_to_cpu, _parse_bool_env, _safe_cuda_empty_cache, _spill_cuda_frames_to_cpu, ) # Built-in model class paths BUILTIN_MODEL_CLASSES = { "cosmos-reason1": "models.vllm_compatible.vllm_compatible_model.VllmCompatible", "cosmos-reason2": "models.vllm_compatible.vllm_compatible_model.VllmCompatible", "cosmos-reason3": "models.vllm_compatible.vllm_compatible_model.VllmCompatible", "vllm-compatible": "models.vllm_compatible.vllm_compatible_model.VllmCompatible", "openai-compat": "models.openai_compat.openai_compat_model.CompOpenAIModel", } # Location to download and cache NGC models NGC_MODEL_CACHE = os.environ.get("NGC_MODEL_CACHE") or "/opt/nvidia/rtvi/.rtvi/ngc_model_cache/" DEFAULT_DECODE_MAX_ATTEMPTS = 2 MODEL_HEALTH_CHECK_INTERVAL_SEC = 0.1 MODEL_BACKEND_UNAVAILABLE_MESSAGE = "VLM model backend is unavailable" FAST_IMAGE_ASSET_CHUNK_DECODE_ENV = "RTVI_FAST_IMAGE_ASSET_CHUNK_DECODE" STRICT_FIXED_FRAME_CHUNK_DECODE_ENV = "RTVI_STRICT_FIXED_FRAME_CHUNK_DECODE" VLM_QUEUE_MAXSIZE_ENV = "RTVI_VLM_QUEUE_MAXSIZE" IMAGE_ASSET_EXTENSIONS = frozenset((".jpg", ".jpeg", ".png", ".bmp", ".webp")) def _decode_max_attempts() -> int: raw_value = os.environ.get("RTVI_DECODE_MAX_ATTEMPTS", str(DEFAULT_DECODE_MAX_ATTEMPTS)) try: return max(1, int(raw_value)) except ValueError: logger.warning( "Invalid RTVI_DECODE_MAX_ATTEMPTS=%r; using %d", raw_value, DEFAULT_DECODE_MAX_ATTEMPTS, ) return DEFAULT_DECODE_MAX_ATTEMPTS def _reuse_file_decoder_pipeline() -> bool: return os.environ.get("RTVI_REUSE_FILE_DECODER_PIPELINE", "true").strip().lower() not in ( "0", "false", "no", "off", ) def _strict_fixed_frame_chunk_decode_enabled() -> bool: return os.environ.get(STRICT_FIXED_FRAME_CHUNK_DECODE_ENV, "false").strip().lower() in ( "1", "true", "yes", "on", ) def _vlm_queue_maxsize(num_gpus: int) -> int: default = 128 * num_gpus raw_value = os.environ.get(VLM_QUEUE_MAXSIZE_ENV) if raw_value is None: return default try: return max(1, int(raw_value)) except ValueError: logger.warning( "Invalid %s=%r; using default %d", VLM_QUEUE_MAXSIZE_ENV, raw_value, default, ) return default def _fast_image_asset_chunk_decode_enabled() -> bool: return os.environ.get(FAST_IMAGE_ASSET_CHUNK_DECODE_ENV, "false").strip().lower() in ( "1", "true", "yes", "on", ) def _split_local_image_asset_paths(chunk_file: str) -> Optional[list[str]]: """Return local image paths represented by a chunk file, or None when ineligible.""" if not chunk_file or "://" in chunk_file: return None paths = [path for path in chunk_file.split(";") if path] if not paths: return None for path in paths: if os.path.splitext(path)[1].lower() not in IMAGE_ASSET_EXTENSIONS: return None if not os.path.isfile(path): return None return paths def _required_file_chunk_frame_count( chunk: ChunkInfo, requested_frame_count: float | int | None, use_fps_for_chunking: bool, ) -> int: if ( not _strict_fixed_frame_chunk_decode_enabled() or use_fps_for_chunking or not requested_frame_count or chunk.chunk_type != "video" ): return 1 try: requested = int(requested_frame_count) except (TypeError, ValueError): return 1 if requested <= 1: return 1 try: from .video_file_frame_getter import DefaultFrameSelector if requested == DefaultFrameSelector.ALL_FRAMES: return 1 except Exception: pass return requested def _try_decode_image_asset_chunk( chunk: ChunkInfo, frame_width: int = 0, frame_height: int = 0, enable_audio: bool = False, enable_jpeg_tensors: bool = False, ) -> Optional[tuple[torch.Tensor, list[float], list, None]]: """Fast-path uploaded image chunks without constructing a video decoder pipeline.""" if ( not _fast_image_asset_chunk_decode_enabled() or enable_audio or enable_jpeg_tensors or chunk.chunk_type != "video" ): return None image_paths = _split_local_image_asset_paths(chunk.file) if image_paths is None: return None try: import numpy as np from PIL import Image resampling = getattr(getattr(Image, "Resampling", Image), "BILINEAR") arrays = [] for image_path in image_paths: with Image.open(image_path) as image: image = image.convert("RGB") if frame_width and frame_height: image = image.resize((frame_width, frame_height), resampling) arrays.append(np.asarray(image, dtype=np.uint8).copy()) if not arrays: return None frames = torch.from_numpy(np.stack(arrays, axis=0)) # Static image assets arrive as one-frame files with PTS 0 in the generic # decoder. Preserve that contract so timestamp prompt behavior stays stable. frame_times = [0.0] * len(arrays) return frames, frame_times, [], None except Exception: logger.warning("Fast image asset chunk decode failed for %s", chunk, exc_info=True) return None class VlmModelType(Enum): OPENAI_COMPATIBLE = "openai-compat" # Any OpenAI API compatible on NIM/OpenAI/Azure-OpenAI VLLM_COMPATIBLE = "vllm-compatible" COSMOS_REASON1 = "cosmos-reason1" COSMOS_REASON2 = "cosmos-reason2" COSMOS_REASON3 = "cosmos-reason3" CUSTOM = "custom" def __str__(self): return self.value def get_model_class_path(vlm_model_type, model_path, model_implementation_path=None): """ Determine the class path to use for model loading. Args: vlm_model_type: VlmModelType enum (use VlmModelType.CUSTOM for custom models) model_path: Path to the model weights/files model_implementation_path: Path to model implementation (for custom models) Returns: str: The class path to use for dynamic model loading (never None) """ # Normalize to string identifier to support both Enum and plain string inputs model_type_value = getattr(vlm_model_type, "value", vlm_model_type) # Check if custom model by comparing the value if model_type_value == "custom": # Custom model return model_implementation_path or model_path else: # Built-in model model_type_key = model_type_value return BUILTIN_MODEL_CLASSES[model_type_key] @dataclass class VlmModelInfo: """Model inforamtion""" id: str = "" created: int = 0 owned_by: str = "" api_type: str = "" @dataclass class VlmRequestParams: vlm_generation_config: Optional[VlmGenerationConfig] = None vlm_prompt: Optional[str] = None chat_messages: Optional[list[dict[str, str]]] = None # Structured OpenAI-format messages stream: bool = False # Enable token-level streaming for text-only requests # True for POST /generate_text_embeddings — embedding models use generate() with text chunks is_text_embeddings_query: bool = False @staticmethod def from_vlm_query(vlm_query: VlmQuery) -> "VlmRequestParams": """Build VlmRequestParams from VlmQuery object. Args: vlm_query: VlmQuery object containing query parameters Returns: VlmRequestParams object """ params = VlmRequestParams() params.vlm_prompt = vlm_query.prompt # Build generation config from query fields config_kwargs = {} if vlm_query.max_tokens is not None: config_kwargs["max_new_tokens"] = vlm_query.max_tokens if vlm_query.min_tokens is not None: config_kwargs["min_tokens"] = vlm_query.min_tokens if vlm_query.top_p is not None: config_kwargs["top_p"] = vlm_query.top_p if vlm_query.top_k is not None: config_kwargs["top_k"] = vlm_query.top_k if vlm_query.temperature is not None: config_kwargs["temperature"] = vlm_query.temperature if vlm_query.seed is not None: config_kwargs["seed"] = vlm_query.seed if vlm_query.enable_reasoning: config_kwargs["enable_reasoning"] = vlm_query.enable_reasoning if vlm_query._prompt_driven_reasoning: config_kwargs["prompt_driven_reasoning"] = True if getattr(vlm_query, "preserve_reasoning_tags", False): config_kwargs["preserve_reasoning_tags"] = True if vlm_query.system_prompt: config_kwargs["system_prompt"] = vlm_query.system_prompt if vlm_query.ignore_eos is not None: config_kwargs["ignore_eos"] = vlm_query.ignore_eos if hasattr(vlm_query, "mm_processor_kwargs") and vlm_query.mm_processor_kwargs: config_kwargs["mm_processor_kwargs"] = vlm_query.mm_processor_kwargs if hasattr(vlm_query, "media_io_kwargs") and vlm_query.media_io_kwargs: config_kwargs["media_io_kwargs"] = vlm_query.media_io_kwargs if vlm_query.response_format: config_kwargs["response_format"] = vlm_query.response_format.model_dump( by_alias=True, exclude_none=True ) # Create VlmGenerationConfig instance with provided values params.vlm_generation_config = VlmGenerationConfig(**config_kwargs) params.stream = vlm_query.stream if hasattr(vlm_query, "stream") else False return params @staticmethod def from_text_embeddings_query( text_embeddings_query: TextEmbeddingsQuery, ) -> "VlmRequestParams": """Build VlmRequestParams from TextEmbeddingsQuery object. Args: text_embeddings_query: TextEmbeddingsQuery object containing query parameters Returns: VlmRequestParams object """ return VlmRequestParams( vlm_prompt="dummy prompt", is_text_embeddings_query=True, ) # Caption emitted for a chunk that the frame selector deliberately left empty # (nvdsframeselector classified it STATIC and static-frame-count=0). The chunk # never reaches the VLM — this text is substituted directly. NO_ACTIVITY_CAPTION = "no activity found" def _fselect_emits_empty_static_chunks() -> bool: """True when nvdsframeselector is configured to emit NOTHING for a STATIC chunk. Requires CHOOSE_FSELECT=true AND NVDS_FSELECT_STATIC_FRAME_COUNT=0. Under that config a 0-frame chunk is the *intended* outcome, not a decode failure, so the chunk is captioned NO_ACTIVITY_CAPTION without invoking the VLM. Any other config keeps the existing hard error, so genuine decode failures (corrupt file, bad seek, missing codec) still surface loudly. """ if os.environ.get("CHOOSE_FSELECT", "false").strip().lower() != "true": return False raw = os.environ.get("NVDS_FSELECT_STATIC_FRAME_COUNT", "").strip() if raw == "": return False try: return int(raw) == 0 except ValueError: return False class DecoderProcess(ProcessBase): """Chunk decoder process""" def __init__( self, args, gpu_id=0, disabled=False, input_queue=None, input_queue_lock=None ) -> None: super().__init__( gpu_id=gpu_id, disabled=disabled, input_queue=input_queue, input_queue_lock=input_queue_lock, description="DecoderProcess", ) self._vlm_model_type = args.vlm_model_type self._num_decoders_per_gpu = args.num_decoders_per_gpu self._num_frames_per_second_or_fixed_frames_chunk = ( args.num_frames_per_second_or_fixed_frames_chunk ) self._model_path = args.model_path self._model_implementation_path = args.model_implementation_path or args.model_path self._module_loader = None self._max_live_streams = max(1, -(-args.max_live_streams // args.num_gpus)) self._enable_audio = args.enable_audio self._use_fps_for_chunking = args.use_fps_for_chunking or False self._ipc_frame_copy = getattr(args, "ipc_frame_copy", False) self._ipc_socket_dir = getattr(args, "ipc_socket_dir", DEFAULT_IPC_SOCKET_DIR) self._ipc_socket_template = getattr( args, "ipc_socket_template", "nvds_ipc_{camera_id}.sock" ) def _initialize(self): from .video_file_frame_getter import DefaultFrameSelector, VideoFileFrameGetter self._live_stream_handle_info: dict[str, dict] = {} self._live_stream_handle_info_lock = Lock() self._nfrms = self._num_frames_per_second_or_fixed_frames_chunk self._image_mean = None self._rescale_factor = None self._image_std = None self._crop_height = None self._crop_width = None self._shortest_edge = None self._do_preprocess = False self._image_aspect_ratio = "" self._enable_jpeg_tensors = False self._width = 0 self._height = 0 self._data_type_int8 = False # Populate model-specific frame pre-processing parameters # Use model-specific decoder configuration for all other models from models.dynamic_model_loader import DynamicModelLoader class_path = get_model_class_path( self._vlm_model_type, self._model_path, self._model_implementation_path ) if self._vlm_model_type == VlmModelType.CUSTOM: # Custom model logger.info(f"Model implementation path: {self._model_implementation_path}") loader = DynamicModelLoader(class_path) # Get input configuration from model class input_config = loader.get_input_config(self._model_path, self._vlm_model_type.value) logger.info(f"Model input config: {input_config}") # Apply input configuration if not self._nfrms: self._nfrms = input_config.num_frames self._minframes = 1 self._data_type_int8 = True # Assume true for dynamic model loader self._enable_jpeg_tensors = input_config.use_jpeg_encoding self._width = input_config.width self._height = input_config.height if ( "VLM_INPUT_WIDTH" in os.environ and os.environ["VLM_INPUT_WIDTH"] and "VLM_INPUT_HEIGHT" in os.environ and os.environ["VLM_INPUT_HEIGHT"] ): self._width = int(os.environ["VLM_INPUT_WIDTH"]) self._height = int(os.environ["VLM_INPUT_HEIGHT"]) logger.info(f"Forcing input to VLM {self._width}X{self._height}") # Initialize multiple frame getters (decoders) self._fgetters = [ VideoFileFrameGetter( frame_selector=DefaultFrameSelector(self._nfrms), frame_width=self._width, frame_height=self._height, gpu_id=0, do_preprocess=self._do_preprocess, image_mean=self._image_mean, rescale_factor=self._rescale_factor, image_std=self._image_std, crop_height=self._crop_height, crop_width=self._crop_width, shortest_edge=self._shortest_edge, image_aspect_ratio=self._image_aspect_ratio, enable_jpeg_output=self._enable_jpeg_tensors, data_type_int8=self._data_type_int8, audio_support=self._enable_audio, ) for _ in range(self._num_decoders_per_gpu) ] self._thread_pool = concurrent.futures.ThreadPoolExecutor( max_workers=int(self._max_live_streams + 1) ) self._file_thread_pool = concurrent.futures.ThreadPoolExecutor( max_workers=int(self._num_decoders_per_gpu + 1) ) self._fgetter_handoff_lock = Lock() # A live GStreamer callback must not block while it still owns the # completed chunk's per-frame CUDA cache. Once the bounded raw CUDA # transport is full, stage a host copy here and let one dispatcher wait # for the multiprocessing queue. The unsaturated CUDA-IPC path remains # direct. self._live_output_handoff_queue = queue.Queue(maxsize=self._num_decoders_per_gpu) self._live_output_handoff_pending = 0 self._live_output_handoff_lock = Lock() self._dropped_live_handoff_stream_ids = set() self._live_output_handoff_thread = Thread( target=self._live_output_handoff_loop, name=f"live-output-handoff-{self._gpu_id}", daemon=True, ) self._live_output_handoff_thread.start() return True def _live_output_handoff_loop(self): while not self._stop.is_set(): try: item = self._live_output_handoff_queue.get(timeout=0.1) except queue.Empty: continue delivered = False chunk = item.get("chunk") stream_id = getattr(chunk, "streamId", None) if stream_id not in self._dropped_live_handoff_stream_ids: while not self._stop.is_set(): try: self._output_queue.put(item, timeout=0.1) delivered = True break except queue.Full: if stream_id in self._dropped_live_handoff_stream_ids: break continue with self._live_output_handoff_lock: self._live_output_handoff_pending -= 1 self._live_output_handoff_queue.task_done() if not delivered: # Preserve the completion count used by drain/EOS without # retaining or forwarding frames from a deleted stream. self._final_output_queue.put( {key: value for key, value in item.items() if key != "frames"} ) del item def _enqueue_live_output(self, item, raw_transport_slots): """Enqueue without blocking a live callback that owns CUDA frames.""" with self._live_output_handoff_lock: must_stage = self._live_output_handoff_pending > 0 if not must_stage: try: self._output_queue.put_nowait(item) return except queue.Full: pass if self._live_output_handoff_pending >= raw_transport_slots: item.pop("frames", None) item["error"] = "Live decoder backlog exceeded the bounded decoder-to-VLM transport" item["error_status_code"] = 503 self._final_output_queue.put(item) logger.warning( "Dropped a live decoded chunk because the %d-slot host " "handoff queue was full", raw_transport_slots, ) return self._live_output_handoff_pending += 1 try: if _contains_cuda_tensor(item.get("frames")): item["frames"] = _spill_cuda_frames_to_cpu(item["frames"]) logger.debug( "Spilled live decoded CUDA frames to CPU while the " "%d-slot CUDA transport window was full", raw_transport_slots, ) self._live_output_handoff_queue.put(item) except Exception: with self._live_output_handoff_lock: self._live_output_handoff_pending -= 1 raise def _warmup(self): if _parse_bool_env("SKIP_DECODER_WARMUP"): logger.info("Skipping decoder warmup") return chunk = ChunkInfo() chunk.file = "/opt/nvidia/rtvi/warmup_streams/its_264.mp4" chunk.end_pts = 5000000000 if os.path.exists(chunk.file): for fgetter in self._fgetters: fgetter.get_frames(chunk) chunk.file = "/opt/nvidia/rtvi/warmup_streams/its_265.mp4" if os.path.exists(chunk.file): for fgetter in self._fgetters: fgetter.get_frames(chunk) # Decoder warmup is local to this process. Do not forward the decoded # warmup frames to VLM: they have no prompt/request_params and may still # be CUDA tensors, which are unsafe to send through the process queue. def _decode_chunk( self, fgetter, chunk: ChunkInfo, vlm_query: VlmQuery, video_codec, **kwargs, ): if chunk.chunk_type == "text": return { "chunk": chunk, **kwargs, } from .video_file_frame_getter import DefaultFrameSelector decode_start_time = time.time() logger.log(LOG_STATUS_LEVEL, "Chunk (%s) decode starting", chunk) nvtx_decode_start = nvtx.start_range(message="Decode Process-" + str(chunk), color="blue") # Extract parameters from VlmQuery num_frames_per_second_or_fixed_frames_chunk = ( vlm_query.num_frames_per_second_or_fixed_frames_chunk ) use_fps_for_chunking = vlm_query.use_fps_for_chunking or False if num_frames_per_second_or_fixed_frames_chunk: frame_selector = DefaultFrameSelector( num_frames_per_second_or_fixed_frames_chunk, use_fps_for_chunking=use_fps_for_chunking, ) else: frame_selector = DefaultFrameSelector( self._nfrms, use_fps_for_chunking=self._use_fps_for_chunking, ) min_required_frames = _required_file_chunk_frame_count( chunk, num_frames_per_second_or_fixed_frames_chunk or self._nfrms, ( use_fps_for_chunking if num_frames_per_second_or_fixed_frames_chunk else self._use_fps_for_chunking ), ) enable_audio = vlm_query.enable_audio vlm_input_width = vlm_query.vlm_input_width vlm_input_height = vlm_query.vlm_input_height fast_decoded = _try_decode_image_asset_chunk( chunk, frame_width=vlm_input_width or self._width, frame_height=vlm_input_height or self._height, enable_audio=enable_audio, enable_jpeg_tensors=self._enable_jpeg_tensors, ) if fast_decoded is not None: frames, frame_times, audio_frames, error = fast_decoded if len(frames) < min_required_frames: nvtx.end_range(nvtx_decode_start) self._fgetters.append(fgetter) return { "chunk": chunk, "error": ( f"Decode error: decoded {len(frames)} frame(s), " f"required at least {min_required_frames}" ), "error_status_code": 500, "decode_retry_count": 0, **kwargs, } nvtx.end_range(nvtx_decode_start) self._fgetters.append(fgetter) decode_end_time = time.time() logger.log( LOG_STATUS_LEVEL, "Chunk (%s) decoded via fast image asset path, frames=%d", chunk, len(frames), ) return { "chunk": chunk, "frames": frames, "error": error, "error_status_code": 500, "frame_times": frame_times, "audio_frames": audio_frames, "audio_transcript": [], "decode_start_time": decode_start_time, "decode_end_time": decode_end_time, "decode_retry_count": 0, "is_live_stream": False, **kwargs, } frames = [] frame_times = [] audio_frames = [] error = None error_status_code = 500 decode_retry_count = 0 decode_max_attempts = _decode_max_attempts() static_skip_enabled = _fselect_emits_empty_static_chunks() for attempt in range(decode_max_attempts): try: frames, frame_times, audio_frames, error = fgetter.get_frames( chunk, frame_selector, enable_audio, request_id=kwargs["request_id"], frame_width=vlm_input_width, frame_height=vlm_input_height, video_codec=video_codec, ) except torch.OutOfMemoryError as exc: error = format_cuda_oom_error(exc, "extracting decoded chunk frames") error_status_code = CUDA_OOM_STATUS_CODE logger.error(error, exc_info=True) frames = [] frame_times = [] audio_frames = [] break decoded_frame_count = len(frames) if not error and decoded_frame_count >= min_required_frames: break # Intentional empty STATIC chunk: the frame selector was asked to emit # nothing, so 0 frames is success, not failure. Break immediately — # retrying would tear down and rebuild the GStreamer pipeline + CUDA # decoder on every static chunk, making them the most expensive chunks # in the run instead of the cheapest. if not error and static_skip_enabled: break if is_cuda_oom_error(error): error_status_code = CUDA_OOM_STATUS_CODE break if not error and not static_skip_enabled: error = ( f"decoded {decoded_frame_count} frame(s), " f"required at least {min_required_frames}" ) if attempt + 1 >= decode_max_attempts: break decode_retry_count += 1 logger.warning( "Retrying decode for chunk %s after frame extraction error " "(attempt %d/%d): %s", chunk, attempt + 1, decode_max_attempts, error, ) # Tear down the broken pipeline AND its cached decoders. # The next get_frames() call will rebuild a fresh pipeline + # fresh CUDA decoder. If the retry succeeds, the freshly-built # decoder becomes the new cached one and is preserved by # flush_pipeline() in the delayed append below — we only pay # CUDA decoder creation once per failure, not per chunk. try: fgetter.destroy_pipeline() except Exception as ex: logger.warning( "Failed to reset decoder pipeline before retry for chunk %s: %s", chunk, ex, ) frame_times = [float("%.2f" % frame_ele) for frame_ele in frame_times] nvtx.end_range(nvtx_decode_start) def append_fgetter_delayed(): # If the retry happened and succeeded, fgetter now holds a freshly # built pipeline + cached decoder; we want flush_pipeline() so the # new decoder is reused on the next chunk. CUDA decoder context # creation is expensive — that's the whole point of reuse. # Destroy only on terminal failure or when reuse is globally off. try: if ( error or not _reuse_file_decoder_pipeline() or not getattr(fgetter, "_file_pipeline_reusable", True) ): with self._fgetter_handoff_lock: fgetter.destroy_pipeline(force_gc=bool(error)) except Exception as ex: logger.warning("Failed to reset decoder pipeline for chunk %s: %s", chunk, ex) try: fgetter.destroy_pipeline() except Exception as destroy_ex: logger.warning( "Failed to destroy decoder pipeline after reset failure " "for chunk %s: %s", chunk, destroy_ex, ) finally: self._fgetters.append(fgetter) self._file_thread_pool.submit(append_fgetter_delayed) logger.log(LOG_STATUS_LEVEL, "Chunk (%s) decoded, frames=%d", chunk, len(frames)) decode_end_time = time.time() error_msg = f"Decode error: {error}" if error else None # An intentionally-empty STATIC chunk carries 0 frames and no error; let it # through so VlmProcess can substitute NO_ACTIVITY_CAPTION. if not error_msg and (len(frames) >= min_required_frames or static_skip_enabled): return { "chunk": chunk, "frames": frames, "error": error_msg, "error_status_code": error_status_code, "frame_times": frame_times, "audio_frames": audio_frames, "audio_transcript": [], "decode_start_time": decode_start_time, "decode_end_time": decode_end_time, "decode_retry_count": decode_retry_count, "is_live_stream": False, **kwargs, } elif error_msg: return { "chunk": chunk, "error": error_msg, "error_status_code": error_status_code, "decode_retry_count": decode_retry_count, **kwargs, } else: return {} def _handle_command(self, command, **kwargs): logger.debug(f"command is {command}") if command == "drop-live-handoff": self._dropped_live_handoff_stream_ids.add(kwargs["stream_id"]) return None if command == "start-live-stream": asset = kwargs["asset"] live_stream_id = asset.asset_id request_id = kwargs.get("request_id") or live_stream_id logger.debug("start-live-stream") with self._live_stream_handle_info_lock: if live_stream_id in self._live_stream_handle_info: self._live_stream_handle_info[live_stream_id].setdefault("subscribers", {})[ request_id ] = kwargs.get("request_params") return "already-running" self._live_stream_handle_info[live_stream_id] = { "frame_getter": None, "num_chunks": 0, "subscribers": {request_id: kwargs.get("request_params")}, "stop_requested": False, } self._thread_pool.submit(self._live_stream, **kwargs) logger.debug("start-live-stream") return "started" if command == "add-live-stream-subscriber": live_stream_id = kwargs["live_stream_id"] request_id = kwargs.get("request_id") or live_stream_id with self._live_stream_handle_info_lock: if live_stream_id not in self._live_stream_handle_info: logger.error( "Live stream %s not found for subscriber %s", live_stream_id, request_id, ) return False handle_info = self._live_stream_handle_info[live_stream_id] handle_info.setdefault("subscribers", {})[request_id] = kwargs.get("request_params") num_chunks = handle_info.get("num_chunks", 0) logger.debug("Added live stream subscriber %s for %s", request_id, live_stream_id) return {"added": True, "num_chunks": num_chunks} if command == "remove-live-stream-subscriber": live_stream_id = kwargs["live_stream_id"] request_id = kwargs.get("request_id") or live_stream_id fgetter = None with self._live_stream_handle_info_lock: handle_info = self._live_stream_handle_info.get(live_stream_id) if not handle_info: logger.debug( "Live stream %s not found while removing subscriber %s", live_stream_id, request_id, ) return False subscribers = handle_info.setdefault("subscribers", {}) if subscribers.pop(request_id, None) is None: logger.debug( "Live stream subscriber %s for %s was already absent", request_id, live_stream_id, ) return False remaining = len(subscribers) if remaining == 0: fgetter = handle_info.get("frame_getter") if fgetter is None: handle_info["stop_requested"] = True if fgetter is not None: self._thread_pool.submit(fgetter.stop_stream) logger.debug( "Removed live stream subscriber %s for %s; remaining=%d", request_id, live_stream_id, remaining, ) return {"removed": True, "remaining": remaining} if command == "stop-live-stream": live_stream_id = kwargs["live_stream_id"] logger.debug(f"Stop live stream - {live_stream_id} checking") with self._live_stream_handle_info_lock: handle_info = self._live_stream_handle_info.get(live_stream_id) if handle_info: fgetter = handle_info.get("frame_getter") if fgetter is None: handle_info["stop_requested"] = True else: fgetter = None if fgetter is not None: logger.debug(f"Stop live stream - {live_stream_id} found") self._thread_pool.submit(fgetter.stop_stream) else: logger.error(f"Stop live stream - {live_stream_id} not found") return None def _live_stream( self, asset: Asset, vlm_query: VlmQuery, **kwargs, ): """Process a live stream. Args: asset: Asset object containing stream URL and credentials vlm_query: VlmQuery object containing all query parameters **kwargs: Additional parameters passed through """ from .video_file_frame_getter import DefaultFrameSelector, VideoFileFrameGetter logger.info(f"Starting live stream {asset.asset_id}") use_fps_for_chunking = vlm_query.use_fps_for_chunking or False if vlm_query.num_frames_per_second_or_fixed_frames_chunk: frame_selector = DefaultFrameSelector( vlm_query.num_frames_per_second_or_fixed_frames_chunk, use_fps_for_chunking=use_fps_for_chunking, ) else: frame_selector = DefaultFrameSelector( self._nfrms, use_fps_for_chunking=self._use_fps_for_chunking, ) fgetter = VideoFileFrameGetter( frame_selector=frame_selector, frame_width=vlm_query.vlm_input_width or self._width, frame_height=vlm_query.vlm_input_height or self._height, gpu_id=0, do_preprocess=self._do_preprocess, image_mean=self._image_mean, rescale_factor=self._rescale_factor, image_std=self._image_std, crop_height=self._crop_height, crop_width=self._crop_width, shortest_edge=self._shortest_edge, image_aspect_ratio=self._image_aspect_ratio, enable_jpeg_output=self._enable_jpeg_tensors, data_type_int8=self._data_type_int8, audio_support=self._enable_audio, ) with self._live_stream_handle_info_lock: handle_info = self._live_stream_handle_info.setdefault( asset.asset_id, { "frame_getter": None, "num_chunks": 0, "subscribers": {}, "stop_requested": False, }, ) handle_info["frame_getter"] = fgetter request_id = kwargs.get("request_id") or asset.asset_id handle_info.setdefault("subscribers", {}).setdefault( request_id, kwargs.get("request_params"), ) stop_requested = handle_info.get("stop_requested", False) if stop_requested: self._thread_pool.submit(fgetter.stop_stream) passthrough_kwargs = { key: value for key, value in kwargs.items() if key not in ("chunk_id", "request_id", "request_params") } def _copy_live_chunk(chunk_: ChunkInfo, live_stream_id_: str) -> ChunkInfo: if hasattr(chunk_, "model_copy"): chunk_copy = chunk_.model_copy(deep=True) else: chunk_copy = chunk_.copy(deep=True) chunk_copy.streamId = live_stream_id_ return chunk_copy def on_chunk_decoded( chunk: ChunkInfo, frames, frame_times, transcripts, error, live_stream_id, decode_start_time, decode_end_time, audio_frames=None, **kwargs, ): frame_times = [float("%.2f" % frame_ele) for frame_ele in frame_times] asr_output = "" for asr_transcript in transcripts: asr_output += asr_transcript["transcript"] + " " transcript = asr_output if len(asr_output) != 0 else None if error is not None: error_msg = "Decode error: " + error logger.error(f"Error decoding chunk {chunk}: {error}") else: error_msg = None logger.log( LOG_STATUS_LEVEL, "Decoded new chunk (%s), frames=%d", chunk, len(frames) ) will_emit_result = bool(error_msg) or len(frames) >= self._minframes with self._live_stream_handle_info_lock: handle_info = self._live_stream_handle_info.get(live_stream_id) subscribers = dict(handle_info.get("subscribers", {})) if handle_info else {} if subscribers and will_emit_result and handle_info is not None: handle_info["num_chunks"] = handle_info.get("num_chunks", 0) + 1 if not subscribers: logger.debug("No subscribers registered for live stream %s", live_stream_id) return if len(frames) >= self._minframes and not error_msg: try: frames_for_vlm = _move_cuda_frames_to_cpu(frames) except Exception as ex: error_msg = f"Failed to transfer decoded frames from GPU to CPU: {ex}" logger.error( "Failed to prepare decoded live frames for stream %s: %s", live_stream_id, ex, exc_info=True, ) _safe_cuda_empty_cache(force=True) else: raw_transport_slots = max(1, self._num_decoders_per_gpu) for request_id, request_params in subscribers.items(): self._enqueue_live_output( { "chunk": _copy_live_chunk(chunk, live_stream_id), "frames": frames_for_vlm, "frame_times": frame_times, "request_params": request_params, "request_id": request_id, "chunk_id": f"{chunk.chunkIdx}:{request_id}", "enqueue_time": decode_start_time, "audio_frames": audio_frames or [], "audio_transcript": transcript, "error": None, "is_live_stream": True, "decode_start_time": decode_start_time, "decode_end_time": decode_end_time, **passthrough_kwargs, }, raw_transport_slots, ) return if error_msg: error_status_code = CUDA_OOM_STATUS_CODE if is_cuda_oom_error(error_msg) else 500 for request_id in subscribers: self._final_output_queue.put( { "chunk": _copy_live_chunk(chunk, live_stream_id), "request_id": request_id, "chunk_id": f"{chunk.chunkIdx}:{request_id}", "error": error_msg, "error_status_code": error_status_code, "is_live_stream": True, "decode_start_time": decode_start_time, "decode_end_time": decode_end_time, **passthrough_kwargs, } ) def on_stream_error(error_message: str, stream_id: str, attempt_count: int): """Callback to send stream errors to output queue for Kafka logging.""" self._final_output_queue.put( { "stream_error": True, "stream_id": stream_id, "error_message": error_message, "attempt_count": attempt_count, } ) vlm_supports_audio = os.environ.get("VLM_MODEL_SUPPORTS_AUDIO", "false").lower() == "true" use_vlm_audio = vlm_query.enable_audio and vlm_supports_audio logger.debug(f"Pipeline for live stream starting up: {asset.asset_id}") live_stream_identity = select_ipc_stream_identity( asset.camera_id, asset.sensor_name, asset.asset_id, ) fgetter.stream( live_stream_url=asset.path, chunk_duration=vlm_query.chunk_duration, chunk_overlap_duration=0, username=asset.username, password=asset.password, live_stream_id=asset.asset_id, live_stream_identity=live_stream_identity, ipc_frame_copy_enabled=self._ipc_frame_copy, ipc_socket_dir=self._ipc_socket_dir, ipc_socket_template=self._ipc_socket_template, on_chunk_decoded=( lambda chunk, frames, frame_times, transcripts, error, decode_start_time, decode_end_time, audio_frames, live_stream_id=asset.asset_id, kwargs=kwargs: on_chunk_decoded( # noqa: E501 chunk, frames, frame_times, transcripts, error, live_stream_id, decode_start_time, decode_end_time, audio_frames=audio_frames, **kwargs, ) ), enable_audio=vlm_query.enable_audio, use_vlm_audio=use_vlm_audio, on_stream_error_callback=on_stream_error, ) logger.debug(f"Pipeline for live stream tearing down: {asset.asset_id}") fgetter.destroy_pipeline() logger.debug(f"Pipeline for live stream torn down: {asset.asset_id}") with self._live_stream_handle_info_lock: handle_info = self._live_stream_handle_info.get(asset.asset_id, {}) total_chunks = handle_info.get("num_chunks", 0) self._final_output_queue.put( { "live_stream_ended": True, "live_stream_id": asset.asset_id, "total_chunks": total_chunks, } ) with self._live_stream_handle_info_lock: self._live_stream_handle_info.pop(asset.asset_id, None) def _deinitialize(self): for fgetter in self._fgetters: fgetter.destroy_pipeline() if hasattr(self, "_live_output_handoff_thread"): self._live_output_handoff_thread.join(timeout=2) # Shutdown thread pools if hasattr(self, "_thread_pool") and self._thread_pool is not None: try: self._thread_pool.shutdown(wait=True, cancel_futures=True) except TypeError: # For older Python versions without cancel_futures self._thread_pool.shutdown(wait=True) if hasattr(self, "_file_thread_pool") and self._file_thread_pool is not None: try: self._file_thread_pool.shutdown(wait=True, cancel_futures=True) except TypeError: self._file_thread_pool.shutdown(wait=True) def _is_busy(self): return len(self._fgetters) == 0 def _process(self, **kwargs): """Decode a chunk and return selected frames as raw frames / JPEG images""" return self._file_thread_pool.submit(self._decode_chunk, self._fgetters.pop(), **kwargs) class VlmProcess(ProcessBase): """VLM Process""" def __init__( self, args, asset_dir, gpu_id: int | str = 0, disabled=False, input_queue=None, input_queue_lock=None, model_unhealthy_event=None, ) -> None: super().__init__( batch_size=args.vlm_batch_size, gpu_id=gpu_id, disabled=disabled, input_queue=input_queue, input_queue_lock=input_queue_lock, description="VlmProcess", ) self._vlm_model_type = args.vlm_model_type self._model_path = args.model_path self._model_implementation_path = args.model_implementation_path or args.model_path self._args = args self._asset_dir = asset_dir self._num_gpus = args.num_gpus # Handle None vlm_batch_size - default to 1 if None vlm_batch_size = args.vlm_batch_size if args.vlm_batch_size is not None else 1 self._num_futures_threads = max(1, vlm_batch_size) self._model_unhealthy_event = ( model_unhealthy_event if model_unhealthy_event is not None else multiprocessing.get_context("spawn").Event() ) self._next_model_health_check_at = 0.0 def _initialize(self): # Determine the class path to use class_path = get_model_class_path( self._vlm_model_type, self._model_path, self._model_implementation_path ) # Load model using dynamic loader # Prepare model loading parameters model_kwargs = { "max_batch_size": self._batch_size, "async_output": True, "vlm_model_type": self._vlm_model_type.value, } self._model = load_model(class_path, self._model_path, **model_kwargs) return True def _handle_command(self, command, **kwargs): if command == "close-evs-session": if hasattr(self._model, "_close_evs_session"): self._model._close_evs_session(kwargs["stream_id"]) return None if command == "abort-live-stream-requests": if not hasattr(self._model, "abort_live_stream_requests"): return 0 try: return self._model.abort_live_stream_requests(kwargs["stream_id"]) except Exception: logger.exception( "Failed to abort in-flight VLM requests for live stream %s", kwargs["stream_id"], ) return 0 if command == "release-idle-resources": if not hasattr(self._model, "release_idle_resources"): return False try: return bool( self._model.release_idle_resources( wait_timeout_sec=kwargs.get("wait_timeout_sec", 0.0) ) ) except Exception as ex: logger.error( "Failed to release idle VLM resources: %s", ex, exc_info=True, ) return False def _deinitialize(self): self._model = None def _supports_batching(self): return True def _can_batch(self, item1, item2): if item1.get("is_live_stream", False) or item2.get("is_live_stream", False): return False if hasattr(self._model, "can_batch"): return self._model.can_batch(item1, item2) else: return False def _is_busy(self): if not self._refresh_model_health(): return False if hasattr(self._model, "can_enqueue_requests"): return not self._model.can_enqueue_requests() else: return False def _mark_model_unhealthy(self, message: str) -> None: if not self._model_unhealthy_event.is_set(): logger.error(message) self._model_unhealthy_event.set() def _refresh_model_health(self, force: bool = False) -> bool: if self._model_unhealthy_event.is_set(): return False now = time.monotonic() if not force and now < getattr(self, "_next_model_health_check_at", 0.0): return True self._next_model_health_check_at = now + MODEL_HEALTH_CHECK_INTERVAL_SEC try: is_healthy = self._model.is_healthy() except Exception: logger.exception("VLM model health check failed") is_healthy = False if not is_healthy: self._mark_model_unhealthy("VLM model backend is unhealthy; rejecting further requests") return is_healthy def is_model_healthy(self) -> bool: return not self._model_unhealthy_event.is_set() @staticmethod def _engine_dead_error_signals(error: BaseException | None) -> tuple[bool, bool]: seen = set() has_engine_dead_message = False while error is not None and id(error) not in seen: seen.add(id(error)) error_type = type(error) if error_type.__name__ == "EngineDeadError" and error_type.__module__.startswith( "vllm." ): return True, has_engine_dead_message has_engine_dead_message = has_engine_dead_message or ( "EngineCore encountered an issue" in str(error) ) error = error.__cause__ or error.__context__ return False, has_engine_dead_message def _handle_result(self, result, **kwargs): if isinstance(result, concurrent.futures.Future): try: error = result.exception() except concurrent.futures.CancelledError as cancelled_error: error = cancelled_error result = cancelled_error else: error = result if isinstance(error, BaseException): is_engine_dead_type, has_engine_dead_message = self._engine_dead_error_signals(error) if is_engine_dead_type: self._mark_model_unhealthy("vLLM EngineCore is dead; marking VLM process unhealthy") elif has_engine_dead_message: self._refresh_model_health(force=True) return super()._handle_result(result, **kwargs) def _warmup(self): if hasattr(self._model, "warmup"): self._model.warmup() def _process( self, chunk: list[ChunkInfo], request_params: list[VlmRequestParams | None], **kwargs ): """Generate VLM output for a batch of chunks""" if not self._refresh_model_health(force=True): raise ServiceException(MODEL_BACKEND_UNAVAILABLE_MESSAGE, "ServiceUnavailable", 503) if not request_params[0] or not request_params[0].vlm_prompt: for chunk_ in chunk: logger.log(LOG_STATUS_LEVEL, "Skipping VLM output generation for (%s)", chunk_) return vlm_start_time = time.time() nvtx_vlm_process_start = nvtx.start_range(message="VLM Process-" + str(chunk), color="blue") for chunk_ in chunk: logger.log(LOG_STATUS_LEVEL, "Generating VLM output for (%s)", chunk_) # Use model classes directly instead of context classes ctx = self._model frames = kwargs.pop("frames", None) frame_times = kwargs.pop("frame_times", [[]] * len(chunk)) # Extract audio_frames before calling VLM to pass to generate() audio_frame_dummy = [{"audio": None, "start": None, "end": None}] audio_frames = kwargs.pop("audio_frames", [audio_frame_dummy] * len(chunk)) audio_transcript = kwargs.pop("audio_transcript", [[]] * len(chunk)) error_msg = kwargs.pop("error", [None] * len(chunk)) decode_only = kwargs.pop("decode_only", [False])[0] # frames is [[]] when batched from empty frames=[] — detect text-only is_text_only = not frames or all( (f is None or (isinstance(f, list) and len(f) == 0)) for f in frames ) if decode_only: vlm_output_batch = [ VlmModelOutput( output="Skipping since dense captioning is enabled", input_tokens=0, output_tokens=0, ) for _ in chunk ] elif ( is_text_only and chunk and chunk[0].chunk_type != "text" and _fselect_emits_empty_static_chunks() ): # nvdsframeselector emitted nothing for this chunk (classified STATIC # with static-frame-count=0). Substitute the caption directly — the VLM # is never invoked, so this chunk costs no inference at all. for _chunk_ in chunk: logger.info( "[fselect] NO_ACTIVITY chunk_id=%s stream=%s — frame selector " "emitted 0 frames (STATIC); captioning %r without the VLM.", getattr(_chunk_, "chunkIdx", "?"), getattr(_chunk_, "streamId", "?"), NO_ACTIVITY_CAPTION, ) vlm_output_batch = [ VlmModelOutput( output=NO_ACTIVITY_CAPTION, input_tokens=0, output_tokens=0, ) for _ in chunk ] elif is_text_only and chunk and chunk[0].chunk_type == "text": # Text-only VLM request — bypass frame processing # Use structured chat_messages if available (multi-turn), else build from prompt if request_params[0].chat_messages: messages = request_params[0].chat_messages else: messages = [] if ( request_params[0].vlm_generation_config and request_params[0].vlm_generation_config.system_prompt ): messages.append( { "role": "system", "content": request_params[0].vlm_generation_config.system_prompt, } ) messages.append({"role": "user", "content": request_params[0].vlm_prompt}) if ( request_params[0].stream and hasattr(self, "_token_stream_queue") and hasattr(ctx, "_event_loop") ): # Token-level streaming: consume async generator, push deltas to queue chunk_id = kwargs.get("chunk_id", [0]) chunk_id_val = chunk_id[0] if isinstance(chunk_id, list) else chunk_id async def _consume_stream(): token_count = 0 try: async for delta in ctx.generate_text_only_stream( messages=messages, generation_config=request_params[0].vlm_generation_config, ): token_count += 1 self._token_stream_queue.put( {"chunk_id": chunk_id_val, "type": "token", "delta": delta} ) except (RuntimeError, ConnectionError, asyncio.CancelledError) as e: logger.error("Token streaming error: %s", e, exc_info=True) self._token_stream_queue.put( {"chunk_id": chunk_id_val, "type": "error", "message": str(e)} ) except Exception as e: # Catch-all for unexpected errors during streaming logger.error("Unexpected token streaming error: %s", e, exc_info=True) self._token_stream_queue.put( {"chunk_id": chunk_id_val, "type": "error", "message": str(e)} ) finally: logger.debug("Token streaming complete: %d deltas sent", token_count) self._token_stream_queue.put({"chunk_id": chunk_id_val, "type": "done"}) # Run the stream consumer on the model's event loop future = asyncio.run_coroutine_threadsafe(_consume_stream(), ctx._event_loop) future.result() # Wait for streaming to complete vlm_output_batch = [ VlmModelOutput(output="[streamed]", input_tokens=0, output_tokens=0) ] elif request_params[0].is_text_embeddings_query: vlm_output_batch = ctx.generate( query=request_params[0].vlm_prompt, chunks=chunk, video_frames=frames, video_frames_times=frame_times, generation_config=request_params[0].vlm_generation_config, ) else: try: vlm_output_batch = ctx.generate_text_only( messages=messages, generation_config=request_params[0].vlm_generation_config, ) except NotImplementedError: vlm_output_batch = ctx.generate( query=request_params[0].vlm_prompt, chunks=chunk, video_frames=frames, video_frames_times=frame_times, generation_config=request_params[0].vlm_generation_config, ) else: vlm_output_batch = ctx.generate( query=request_params[0].vlm_prompt, chunks=chunk, video_frames=frames, video_frames_times=frame_times, generation_config=request_params[0].vlm_generation_config, audio_frames=audio_frames, is_live_stream=bool(kwargs.get("is_live_stream", [False])[0]), ) if "is_live_stream" in kwargs and self._num_gpus > 1: time.sleep(0.1) def process_vlm_output( chunk, request_params, vlm_output_list, frame_times, audio_frames, audio_transcript, error_msg, vlm_start_time, nvtx_vlm_process_start, ): if not isinstance(vlm_output_list, list): raise TypeError(f"Expected List[VlmModelOutput], got {type(vlm_output_list)}") if len(vlm_output_list) > 0 and not isinstance(vlm_output_list[0], VlmModelOutput): raise TypeError(f"Expected VlmModelOutput objects, got {type(vlm_output_list[0])}") if len(vlm_output_list) != len(chunk): logger.warning( "Mismatch between VLM outputs (%d) and chunks (%d); normalizing lengths", len(vlm_output_list), len(chunk), ) if len(vlm_output_list) > len(chunk): vlm_output_list = vlm_output_list[: len(chunk)] else: padding = len(chunk) - len(vlm_output_list) vlm_output_list.extend( VlmModelOutput( output="No response generated for this chunk", input_tokens=0, output_tokens=0, ) for _ in range(padding) ) nvtx.end_range(nvtx_vlm_process_start) for idx, chunk_ in enumerate(chunk): logger.log( LOG_STATUS_LEVEL, "VLM output generated for (%s), %s", chunk_, vlm_output_list[idx].output, ) _vlm_end = time.time() return { "chunk": chunk, "request_params": request_params, "vlm_output": vlm_output_list, # Pass VlmModelOutput objects directly "frame_times": frame_times, "audio_frames": audio_frames, "audio_transcript": audio_transcript, "error": error_msg, "vlm_start_time": [vlm_start_time] * len(chunk), "vlm_end_time": [_vlm_end] * len(chunk), **kwargs, } process_output_args = ( chunk, request_params, vlm_output_batch, frame_times, audio_frames, audio_transcript, error_msg, vlm_start_time, nvtx_vlm_process_start, ) if isinstance(vlm_output_batch, concurrent.futures.Future): return self._handle_future_result( process_vlm_output, *process_output_args, ) else: return process_vlm_output(*process_output_args) class AsrProcess(ProcessBase): """ASR Process""" def __init__( self, args, asset_dir, gpu_id=0, disabled=False, input_queue=None, input_queue_lock=None, ) -> None: super().__init__( batch_size=1, gpu_id=gpu_id, disabled=disabled, input_queue=input_queue, input_queue_lock=input_queue_lock, ) self._args = args self._model_name = None self._server_uri = None self._riva_nim_server = True self._asr_config_file = "/tmp/rtvi/riva_asr_grpc_conf.yaml" self._server_config = None self._asr_config = None try: with open(self._asr_config_file, mode="r", encoding="utf8") as c: config_docs = yaml.safe_load_all(c) for doc in config_docs: if doc["name"] == "riva_server": self._server_config = doc["detail"] self._server_uri = self._server_config["server_uri"] if doc["name"] == "riva_model": self._model_name = doc["detail"]["model_name"] if doc["name"] == "riva_asr_stream": self._asr_config = doc["detail"] except Exception as e: raise ValueError(f"{self._asr_config_file} is not a valid YAML file") from e if self._asr_config is None or self._server_uri is None: raise Exception("RIVA ASR configuration is not valid.") self._auth = None self._asr_service = None def _initialize(self): import riva.client # noqa: PLC0415 # Create GRPC channel ssl_cert = self._server_config.get("ssl_cert", None) use_ssl = self._server_config.get("use_ssl", False) metadata_args = [] if use_ssl: metadata = self._server_config.get("metadata", None) if metadata is not None: for k, v in metadata.items(): metadata_args.append([k, v]) self._auth = riva.client.Auth( use_ssl=use_ssl, ssl_cert=ssl_cert, uri=self._server_uri, metadata_args=metadata_args ) self._asr_service = riva.client.ASRService(self._auth) language_code = self._asr_config.get("language_code", "en-US") enable_automatic_punctuation = self._asr_config.get("enable_automatic_punctuation", True) profanity_filter = self._asr_config.get("profanity_filter", True) if self._riva_nim_server: # Do not pass model name for NIM self._riva_asr_config = riva.client.RecognitionConfig( encoding=riva.client.AudioEncoding.LINEAR_PCM, sample_rate_hertz=16000, language_code=language_code, max_alternatives=1, enable_automatic_punctuation=enable_automatic_punctuation, profanity_filter=profanity_filter, verbatim_transcripts=False, ) else: self._riva_asr_config = riva.client.RecognitionConfig( encoding=riva.client.AudioEncoding.LINEAR_PCM, sample_rate_hertz=16000, language_code=language_code, max_alternatives=1, enable_automatic_punctuation=enable_automatic_punctuation, model=self._model_name, profanity_filter=profanity_filter, verbatim_transcripts=False, ) return True def _deinitialize(self): self._model_name = None def _supports_batching(self): return False def _can_batch(self, item1, item2): return False def _is_busy(self): return False def _warmup(self): pass def _process(self, chunk: ChunkInfo, request_params: VlmRequestParams | None, **kwargs): """Generate ASR response for a chunk (non batching)""" if not request_params: logger.log(LOG_STATUS_LEVEL, "Skipping ASR response generation for (%s)", chunk) return asr_start_time = time.time() nvtx_asr_process_start = nvtx.start_range(message="ASR Process-" + str(chunk), color="blue") logger.log(LOG_STATUS_LEVEL, "Generating ASR response for (%s)", chunk) audio_frames = kwargs.pop("audio_frames", None) audio_transcript = kwargs.pop("audio_transcript", None) error_msg = kwargs.pop("error", [None] * len(chunk)) if audio_transcript is not None and len(audio_transcript) > 0: return { "chunk": chunk, "request_params": request_params, "audio_transcript": audio_transcript, "error": error_msg, "asr_start_time": asr_start_time, "asr_end_time": time.time(), **kwargs, } audio_data = bytes() bytes_list = [] if audio_frames is not None: for frame in audio_frames: if frame["audio"] is not None: bytes_list.append(frame["audio"].tobytes()) audio_data = b"".join(bytes_list) if len(audio_data) == 0: asr_response = None else: asr_response = self._asr_service.offline_recognize( audio_data, self._riva_asr_config, future=True ) def process_asr_response( chunk, request_params, asr_response, error_msg, asr_start_time, nvtx_asr_process_start, ): nvtx.end_range(nvtx_asr_process_start) if asr_response is not None and len(asr_response.results) >= 1: transcript = "" for result in asr_response.results: transcript += result.alternatives[0].transcript else: transcript = None logger.log(LOG_STATUS_LEVEL, "ASR response generated for (%s), %s", chunk, transcript) return { "chunk": chunk, "request_params": request_params, "audio_transcript": transcript, "error": error_msg, "asr_start_time": asr_start_time, "asr_end_time": time.time(), **kwargs, } process_output_args = ( chunk, request_params, asr_response, error_msg, asr_start_time, nvtx_asr_process_start, ) if isinstance(asr_response, _MultiThreadedRendezvous): return self._handle_future_result( process_asr_response, *process_output_args, ) else: return process_asr_response(*process_output_args) @dataclass class PipelineChunkResult: chunk: ChunkInfo = None vlm_model_output: VlmModelOutput | None = None # VlmModelOutput object directly audio_transcript: str = "" error: str | None = None error_status_code: int = 500 queue_time: float = 0 processing_latency: float = 0 is_live_stream_ended: bool = False is_stream_error: bool = False # Stream reconnection error stream_error_message: str | None = None # Error message for Kafka logging stream_error_attempt_count: int = 0 # Reconnection attempt count decode_start_time: float = 0 decode_end_time: float = 0 decode_retry_count: int = 0 vlm_start_time: float = 0 vlm_end_time: float = 0 asr_start_time: float = 0 asr_end_time: float = 0 frame_times: list[float] = field(default_factory=list) def __str__(self) -> str: """String representation of the chunk result for debugging""" timings = { "decode": ( f"{self.decode_start_time:.3f}-{self.decode_end_time:.3f}" if self.decode_start_time and self.decode_end_time else "N/A" ), "vlm": ( f"{self.vlm_start_time:.3f}-{self.vlm_end_time:.3f}" if self.vlm_start_time and self.vlm_end_time else "N/A" ), "asr": ( f"{self.asr_start_time:.3f}-{self.asr_end_time:.3f}" if self.asr_start_time and self.asr_end_time else "N/A" ), } chunk_info = f"chunk[{self.chunk.chunkIdx}]" if self.chunk else "No chunk" tokens = ( f"in:{self.vlm_model_output.input_tokens}/out:{self.vlm_model_output.output_tokens}" if self.vlm_model_output else "N/A" ) return ( f"PipelineChunkResult({chunk_info}, error={bool(self.error)}, " f"timings={timings}, tokens={tokens}, " f"transcript={bool(self.audio_transcript)}, frames={len(self.frame_times)})" ) def check_peer_access(): """ Checks if all GPU pairs have peer access enabled. Returns False if any pair cannot access each other, True if all pairs have access. """ try: # Get number of available GPUs num_gpus = torch.cuda.device_count() # If less than 2 GPUs, peer access is not relevant if num_gpus < 2: return True # Check all pairs of GPUs for i in range(num_gpus): for j in range(i + 1, num_gpus): # Check peer access in both directions if not torch.cuda.can_device_access_peer( i, j ) or not torch.cuda.can_device_access_peer(j, i): return False return True except Exception as e: print(f"Error checking peer access: {e}") return False class VlmPipeline: """VLM Pipeline""" @dataclass class _LiveStreamSubscriber: on_chunk_result: Callable[[PipelineChunkResult], None] | None = None start_chunk_count: int = 0 num_chunks_processed: int = 0 all_chunks_processed: bool = False @dataclass class _LiveStreamInfo: subscribers: dict[str, "VlmPipeline._LiveStreamSubscriber"] = field(default_factory=dict) end_of_stream: bool = False total_chunks_at_eos: int = 0 all_chunks_processed: bool = False evs_sessions_closed: bool = False abort_requested: bool = False gpu_id: int = -1 decode_signature: tuple = field(default_factory=tuple) # Kept as instance fields because tests and live-context features attach # rolling-caption state here. context_window_size: int = 0 caption_history: object | None = None context_summarize: bool = False _summarize_in_progress: bool = False current_summary: str | None = None summarize_frequency: int = 0 @staticmethod def _subscriber_expected_chunks_at_eos( subscriber: "VlmPipeline._LiveStreamSubscriber", total_chunks_at_eos: int, ) -> int: return max(total_chunks_at_eos - subscriber.start_chunk_count, 0) def __init__(self, asset_dir, args) -> None: """Initialize the VLM pipeline""" logger.info("Initializing VLM pipeline") self._start_time = time.time() self._args = args mp_ctx = multiprocessing.get_context("spawn") self._dec_q = mp_ctx.Queue() self._dec_q_lock = mp_ctx.Lock() have_peer_access = check_peer_access() logger.info(f"Have peer access: {have_peer_access}") if have_peer_access: vlm_queue_maxsize = _vlm_queue_maxsize(self._args.num_gpus) logger.info("VLM queue maxsize set to %d", vlm_queue_maxsize) self._vlm_q = mp_ctx.Queue(maxsize=vlm_queue_maxsize) self._vlm_q_lock = mp_ctx.Lock() else: self._vlm_q = None self._vlm_q_lock = None self._asr_q = mp_ctx.Queue() self._asr_q_lock = mp_ctx.Lock() self._asr_procs = [] self._chunk_counter = 0 self._chunk_callback_map: dict[int, Callable[[PipelineChunkResult], None]] = {} self._live_stream_id_map: dict[str, VlmPipeline._LiveStreamInfo] = {} self._live_stream_lock = Lock() self._enqueue_lock = Lock() # Model path is required for locally executed models like CR1 if ( args.vlm_model_type in [ VlmModelType.COSMOS_REASON1, VlmModelType.VLLM_COMPATIBLE, ] and not args.model_path ): raise Exception("model-path not provided") if args.vlm_model_type == VlmModelType.CUSTOM and not args.model_implementation_path: raise Exception("model-implementation-path not provided") model_source = args.model_path if model_source: validate_model_path_source(model_source) if args.model_path and args.model_path.startswith("ngc:"): # NGC model path provided, download the model if not found in cache # Workaround for some asyncio issue def download_thread_func(ngc_model_path, download_prefix, model_path_): try: model_path = download_model( ngc_model_path, download_prefix, args.vlm_model_type.value ) except Exception as ex: model_path_[1] = ex return model_path_[0] = model_path model_path_ = ["", ""] download_thread = Thread( target=download_thread_func, args=(args.model_path[4:], NGC_MODEL_CACHE, model_path_), ) download_thread.start() download_thread.join() if model_path_[1]: raise model_path_[1] from None args.model_path = model_path_[0] if args.model_path and args.model_path.startswith("git:"): args.model_path = download_model_git(args.model_path[4:], NGC_MODEL_CACHE) if args.model_path: validate_model_config(args.model_path, model_source=model_source) if args.model_path and args.model_repository_script_path: logger.info(f"Running model repository script: {args.model_repository_script_path}") subprocess.run( [ "python3", args.model_repository_script_path, "--model_path", args.model_path, "--triton_repo_path", "/tmp/triton_model_repo", "--max_batch_size", str(args.vlm_batch_size), ], check=True, ) logger.info("Model repository script completed") self._processed_chunk_queue = mp_ctx.Queue() self._token_stream_queue = mp_ctx.Queue() # For text-only token-level streaming self._processed_chunk_queue_watcher_stop_event = Event() self._processed_chunk_queue_watcher_thread = None # Create the ASR processes # Only create ASR process if audio enabled AND VLM doesn't handle audio natively self._num_asr_procs = 1 vlm_supports_audio = os.environ.get("VLM_MODEL_SUPPORTS_AUDIO", "false").lower() == "true" if self._args.enable_audio and not vlm_supports_audio: logger.info( "Creating ASR process for audio transcription (VLM_MODEL_SUPPORTS_AUDIO=false)" ) self._asr_procs = [ AsrProcess( args, asset_dir, i, not self._args.enable_audio, self._asr_q, self._asr_q_lock, ) for i in range(self._num_asr_procs) ] for idx, asr_proc in enumerate(self._asr_procs): asr_proc.set_output_queue(self._processed_chunk_queue) asr_proc.set_final_output_queue(self._processed_chunk_queue) asr_proc.start() else: if self._args.enable_audio: logger.info( "Skipping ASR process - VLM handles audio natively " "(VLM_MODEL_SUPPORTS_AUDIO=true)" ) self._asr_procs = [] # No ASR processes needed self._num_gpus_per_vlm_proc = max(int(os.environ.get("NUM_GPUS_PER_VLM_PROC", "") or 1), 1) self._num_vlm_procs = args.num_gpus if args.vlm_model_type == VlmModelType.OPENAI_COMPATIBLE: self._num_vlm_procs = 1 elif not args.disable_vlm: self._num_vlm_procs = args.num_gpus // self._num_gpus_per_vlm_proc if self._num_vlm_procs == 0: raise Exception( f"Not enough GPUs to run VLM pipeline. Available GPUs: {args.num_gpus}." f" GPUs per VLM instance: {self._num_gpus_per_vlm_proc}" ) logger.info(f"GPUs per VLM instance: {self._num_gpus_per_vlm_proc}") logger.info(f"num_vlm_procs set to {self._num_vlm_procs}") self._model_unhealthy_event = mp_ctx.Event() # Create the VLM processes, one on each GPU self._vlm_procs = [ VlmProcess( args, asset_dir, ",".join( map( str, range( i * self._num_gpus_per_vlm_proc, (i + 1) * self._num_gpus_per_vlm_proc ), ) ), args.disable_vlm, self._vlm_q, self._vlm_q_lock, self._model_unhealthy_event, ) for i in range(self._num_vlm_procs) ] self._decoder_procs = [] for idx, vlm_proc in enumerate(self._vlm_procs): # Only route to ASR if audio is enabled AND VLM doesn't handle audio natively if self._args.enable_audio and not vlm_supports_audio: # Audio enabled but VLM doesn't support it → use ASR for transcription vlm_proc.set_output_queue(self._asr_procs[idx % self._num_asr_procs].input_queue) else: # Either audio disabled OR VLM handles audio natively → skip ASR vlm_proc.set_output_queue(self._processed_chunk_queue) vlm_proc.set_final_output_queue(self._processed_chunk_queue) vlm_proc.set_token_stream_queue(self._token_stream_queue) vlm_proc.start() # vLLM profiles available GPU memory during startup. Initializing the # decoder at the same time can transiently release GPU memory and trip # vLLM's memory-profiling guard, so let VLM settle first. for idx, proc in enumerate(self._vlm_procs): if not proc.wait_for_initialization(): self.stop() raise Exception(f"Failed to load VLM on GPU {idx}") # Create the chunk decoding processes, one on each GPU self._decoder_procs = [ DecoderProcess(args, i, args.disable_decoding, self._dec_q, self._dec_q_lock) for i in range(args.num_gpus) ] for idx, dec_proc in enumerate(self._decoder_procs): dec_proc.set_output_queue( self._vlm_procs[idx % self._num_vlm_procs].input_queue ) dec_proc.set_final_output_queue(self._processed_chunk_queue) dec_proc.start() # Wait for all processes to complete initialization for idx, proc in enumerate(self._decoder_procs): if not proc.wait_for_initialization(): self.stop() raise Exception(f"Failed to load Decoder on GPU {idx}") for idx, proc in enumerate(self._asr_procs): if not proc.wait_for_initialization(): self.stop() raise Exception(f"Failed to load ASR process {idx}") # Create a thread to gather chunks processed by the VLM pipeline self._processed_chunk_queue_watcher_stop_event = Event() self._processed_chunk_queue_watcher_thread = Thread( target=self._watch_processed_chunk_queue ) self._processed_chunk_queue_watcher_thread.start() logger.info("Initialized VLM pipeline") def _watch_processed_chunk_queue(self): """Gather chunks processed by the pipeline and return via callback""" while not self._processed_chunk_queue_watcher_stop_event.is_set(): try: item: dict = self._processed_chunk_queue.get(timeout=1) except queue.Empty: continue if item.get("stream_error", False): # Handle stream reconnection errors - send to callback for Kafka logging stream_id = item.get("stream_id", "") with self._live_stream_lock: lsinfo = self._live_stream_id_map.get(stream_id) subscribers = list(lsinfo.subscribers.values()) if lsinfo else [] if subscribers: chunk_result = PipelineChunkResult() chunk_result.is_stream_error = True chunk_result.stream_error_message = item.get("error_message", "") chunk_result.stream_error_attempt_count = item.get("attempt_count", 0) for subscriber in subscribers: if subscriber.on_chunk_result: subscriber.on_chunk_result(chunk_result) continue if item.get("live_stream_ended", False): live_stream_id = item["live_stream_id"] # Membership guard: remove_live_stream may have popped the entry # after a drain timeout, in which case a late EOS from decoder # must be ignored rather than crash the watcher with KeyError. # Mirrors the guards used on stream-error (above) and chunk # results (below). eos_callbacks = [] close_sessions = False with self._live_stream_lock: lsinfo = self._live_stream_id_map.get(live_stream_id) if not lsinfo: continue lsinfo.end_of_stream = True lsinfo.total_chunks_at_eos = item["total_chunks"] # EOS can overtake queued tail chunks. Even forced deletion # must account for their completions before closing EVS. for subscriber in lsinfo.subscribers.values(): if not subscriber.all_chunks_processed and ( subscriber.num_chunks_processed >= self._subscriber_expected_chunks_at_eos( subscriber, lsinfo.total_chunks_at_eos, ) ): subscriber.all_chunks_processed = True if subscriber.on_chunk_result: eos_callbacks.append(subscriber.on_chunk_result) if lsinfo.subscribers and all( subscriber.all_chunks_processed for subscriber in lsinfo.subscribers.values() ): lsinfo.all_chunks_processed = True close_sessions = True if eos_callbacks: chunk_result = PipelineChunkResult() chunk_result.is_live_stream_ended = True for callback in eos_callbacks: callback(chunk_result) if close_sessions: Thread( target=self._close_evs_sessions_after_eos, args=(lsinfo, live_stream_id), daemon=True, ).start() continue chunk_result = PipelineChunkResult() chunk_result.error = item.get("error", None) chunk_result.error_status_code = item.get("error_status_code", 500) chunk_result.chunk = item["chunk"] chunk_result.decode_retry_count = item.get("decode_retry_count", 0) if not chunk_result.error: # vlm_output is a single VlmModelOutput object (already unbatched) chunk_result.vlm_model_output = item.get("vlm_output", None) # Server-log: VLM caption per chunk, flattened to one line. if ( chunk_result.vlm_model_output and getattr(chunk_result.vlm_model_output, "output", None) ): big_vlm_text = chunk_result.vlm_model_output.output logger.info( "[BigVLMCaption] chunk_id=%s stream=%s caption='%s'", item.get("chunk_id"), chunk_result.chunk.streamId if chunk_result.chunk else "", big_vlm_text.replace("\n", " "), ) audio_transcript_raw = item.get("audio_transcript", None) if isinstance(audio_transcript_raw, list): chunk_result.audio_transcript = ( " ".join(audio_transcript_raw) if audio_transcript_raw else "" ) else: chunk_result.audio_transcript = audio_transcript_raw chunk_result.queue_time = max( item.get("decode_start_time", item.get("vlm_start_time", 0)) - item.get("enqueue_time", 0), 0, ) chunk_result.processing_latency = max( item.get("vlm_end_time", 0) - item.get("decode_start_time", item.get("vlm_start_time", 0)), 0, ) chunk_result.decode_start_time = item.get("decode_start_time", 0) chunk_result.decode_end_time = item.get("decode_end_time", 0) chunk_result.vlm_start_time = item.get("vlm_start_time", 0) chunk_result.vlm_end_time = item.get("vlm_end_time", 0) chunk_result.asr_start_time = item.get("asr_start_time", 0) chunk_result.asr_end_time = item.get("asr_end_time", 0) chunk_result.frame_times = item.get("frame_times", []) if item.get("is_live_stream", False): if chunk_result.vlm_end_time and chunk_result.vlm_end_time - ( chunk_result.vlm_start_time ) > (chunk_result.chunk.end_pts - chunk_result.chunk.start_pts): logger.warning( "Detected high load on the system. This may result in higher response" " times. Try reducing number of streams or increasing the chunk size" ) stream_id = chunk_result.chunk.streamId request_id = item.get("request_id") or "" result_callbacks = [] eos_callbacks = [] close_sessions = False with self._live_stream_lock: lsinfo = self._live_stream_id_map.get(stream_id) if not lsinfo: continue if request_id: subscriber = lsinfo.subscribers.get(request_id) if not subscriber: logger.debug( "Live stream result for %s had unknown request_id %s", stream_id, request_id, ) continue target_subscribers = [subscriber] else: target_subscribers = list(lsinfo.subscribers.values()) if len(target_subscribers) > 1: logger.warning( "Live stream result for %s had no request_id; " "broadcasting to %d subscribers", stream_id, len(target_subscribers), ) for subscriber in target_subscribers: subscriber.num_chunks_processed += 1 if subscriber.on_chunk_result: result_callbacks.append(subscriber.on_chunk_result) if ( lsinfo.end_of_stream and not subscriber.all_chunks_processed and subscriber.num_chunks_processed >= self._subscriber_expected_chunks_at_eos( subscriber, lsinfo.total_chunks_at_eos, ) ): subscriber.all_chunks_processed = True if subscriber.on_chunk_result: eos_callbacks.append(subscriber.on_chunk_result) if lsinfo.subscribers and all( subscriber.all_chunks_processed for subscriber in lsinfo.subscribers.values() ): lsinfo.all_chunks_processed = True close_sessions = True for callback in result_callbacks: callback(chunk_result) if eos_callbacks: live_stream_ended = PipelineChunkResult() live_stream_ended.is_live_stream_ended = True for callback in eos_callbacks: callback(live_stream_ended) if close_sessions: Thread( target=self._close_evs_sessions_after_eos, args=(lsinfo, stream_id), daemon=True, ).start() continue callback = self._chunk_callback_map.pop(item["chunk_id"], None) if callback: callback(chunk_result) def _close_evs_sessions_after_eos(self, lsinfo: "VlmPipeline._LiveStreamInfo", stream_id: str): try: self.close_evs_sessions(stream_id) finally: # A restart must not start before this close finishes: both runs # share the stream id, so a late close would delete the new # run's EVS sessions. lsinfo.evs_sessions_closed = True def close_evs_sessions(self, stream_id: str): for proc in self._vlm_procs: try: proc.send_command("close-evs-session", stream_id=stream_id) except Exception: logger.debug("Failed to close EVS session for stream %s", stream_id, exc_info=True) def release_idle_vlm_resources(self, wait_timeout_sec: float = 0.0) -> bool: """Ask every VLM process to release caches while the pipeline is idle.""" released = bool(self._vlm_procs) for proc in self._vlm_procs: try: proc_released = bool( proc.send_command( "release-idle-resources", wait_timeout_sec=wait_timeout_sec, ) ) except Exception: logger.warning( "Idle VLM resource release command failed for one process", exc_info=True, ) proc_released = False released = proc_released and released return released def abort_chunks(self, stream_id: str): for proc in self._decoder_procs + self._vlm_procs + self._asr_procs: proc.send_command("drop-chunks", stream_id=stream_id) def abort_chunks_done(self, stream_id: str): for proc in self._decoder_procs + self._vlm_procs + self._asr_procs: proc.send_command("stop-drop-chunks", stream_id=stream_id) def stop(self, force=False): """Stop the VLM Pipeline""" logger.info("Stopping VLM pipeline") if force: # Force terminate the processes started by the pipeline for proc in self._decoder_procs: proc.terminate() for proc in self._vlm_procs: proc.terminate() for proc in self._asr_procs: proc.terminate() else: # Wait for the processes started by VLM pipeline to stop gracefully for proc in self._decoder_procs: proc.stop() for proc in self._vlm_procs: proc.stop() for proc in self._asr_procs: proc.stop() # Stop the processed chunk result watcher thread if self._processed_chunk_queue_watcher_thread: self._processed_chunk_queue_watcher_stop_event.set() self._processed_chunk_queue_watcher_thread.join() logger.info("Stopped VLM pipeline") def get_token_stream_queue(self): """Get the token stream queue for text-only SSE streaming.""" return self._token_stream_queue def get_models_info(self): """Get loaded model information using dynamic model loader""" # Determine the class path to use class_path = get_model_class_path( self._args.vlm_model_type, self._args.model_path, self._args.model_implementation_path ) # Load model class and get info try: loader = DynamicModelLoader(class_path) model_class = loader.get_model_class() id, api_type, owned_by = model_class.get_model_info( self._args.model_path, self._args.vlm_model_type ) except Exception as e: logger.warning(f"Failed to get model info via dynamic loader: {e}") # Fallback for models that don't implement get_model_info properly id = ( os.path.basename(os.path.abspath(self._args.model_path)) if self._args.model_path else "unknown" ) api_type = "internal" owned_by = "custom" info = VlmModelInfo() info.api_type = api_type info.id = id info.created = self._start_time info.owned_by = owned_by return info def enqueue_chunk( self, chunk: ChunkInfo, on_chunk_result: Callable[[PipelineChunkResult], None], vlm_query: VlmQuery, request_id="", video_codec=None, decode_only=False, ): # Build request params from vlm_query request_params = VlmRequestParams.from_vlm_query(vlm_query) curr_chunk_counter = self._reserve_chunk_callback( chunk, on_chunk_result, raise_on_unhealthy=True ) self._decoder_procs[curr_chunk_counter % self._args.num_gpus].enqueue_chunk( chunk, vlm_query=vlm_query, request_params=request_params, chunk_id=curr_chunk_counter, enqueue_time=time.time(), request_id=request_id, video_codec=video_codec, decode_only=decode_only, ) def enqueue_vlm_text_chunk( self, chunk: ChunkInfo, on_chunk_result: Callable[[PipelineChunkResult], None], vlm_query: VlmQuery, request_id="", chat_messages: list | None = None, ): """Enqueue a text-only chunk for VLM processing (no frames). Unlike enqueue_text_chunk (for embeddings), this uses VlmQuery params and routes through the VLM model's generate_text_only path. Args: chat_messages: Optional structured messages (system/user/assistant dicts) for multi-turn conversation. If provided, used instead of flattened vlm_query.prompt. """ request_params = VlmRequestParams.from_vlm_query(vlm_query) if chat_messages: request_params.chat_messages = chat_messages curr_chunk_counter = self._reserve_chunk_callback(chunk, on_chunk_result) if curr_chunk_counter is None: return with self._enqueue_lock: decode_start_time = decode_end_time = time.time() self._vlm_procs[curr_chunk_counter % self._args.num_vlm_procs].enqueue_chunk( chunk, request_params=request_params, chunk_id=curr_chunk_counter, enqueue_time=time.time(), request_id=request_id, decode_start_time=decode_start_time, decode_end_time=decode_end_time, frames=[], frame_times=[], audio_frames=[], audio_transcript=[], error=None, is_live_stream=False, ) def enqueue_text_chunk( self, chunk: ChunkInfo, on_chunk_result: Callable[[PipelineChunkResult], None], text_embeddings_query: TextEmbeddingsQuery, request_id="", ): request_params = VlmRequestParams.from_text_embeddings_query(text_embeddings_query) curr_chunk_counter = self._reserve_chunk_callback(chunk, on_chunk_result) if curr_chunk_counter is None: return with self._enqueue_lock: decode_start_time = decode_end_time = time.time() self._vlm_procs[curr_chunk_counter % self._args.num_vlm_procs].enqueue_chunk( chunk, request_params=request_params, chunk_id=curr_chunk_counter, enqueue_time=time.time(), request_id=request_id, decode_start_time=decode_start_time, decode_end_time=decode_end_time, frames=[], frame_times=[], audio_frames=[], audio_transcript=[], error=None, is_live_stream=False, ) def _reserve_chunk_callback( self, chunk: ChunkInfo, on_chunk_result: Callable[[PipelineChunkResult], None], raise_on_unhealthy: bool = False, ) -> int | None: with self._enqueue_lock: curr_chunk_counter = self._chunk_counter self._chunk_counter += 1 if self._all_vlm_processes_healthy(): self._chunk_callback_map[curr_chunk_counter] = on_chunk_result return curr_chunk_counter if raise_on_unhealthy: raise ServiceException(MODEL_BACKEND_UNAVAILABLE_MESSAGE, "ServiceUnavailable", 503) on_chunk_result( PipelineChunkResult( chunk=chunk, error=MODEL_BACKEND_UNAVAILABLE_MESSAGE, error_status_code=503, ) ) return None def _all_vlm_processes_healthy(self) -> bool: return all( getattr(process, "_disabled", False) or (process.is_alive() and process.is_model_healthy()) for process in self._vlm_procs ) def ensure_model_available(self) -> None: if not self._all_vlm_processes_healthy(): raise ServiceException(MODEL_BACKEND_UNAVAILABLE_MESSAGE, "ServiceUnavailable", 503) def _live_stream_decode_signature(self, vlm_query: VlmQuery) -> tuple: return ( int(vlm_query.chunk_duration or 0), float(vlm_query.num_frames_per_second_or_fixed_frames_chunk or 0), bool(vlm_query.use_fps_for_chunking or False), int(vlm_query.vlm_input_width or 0), int(vlm_query.vlm_input_height or 0), bool(vlm_query.enable_audio or False), ) def add_live_stream( self, asset: Asset, vlm_query: VlmQuery, on_chunk_result: Callable[[PipelineChunkResult], None], request_id: str = "", ): """Add a live stream for processing. Args: asset: Asset object containing stream URL and credentials vlm_query: VlmQuery object containing all query parameters (including chunk_duration settings) on_chunk_result: Callback function for chunk results """ self.ensure_model_available() request_id = request_id or asset.asset_id request_params = VlmRequestParams.from_vlm_query(vlm_query) decode_signature = self._live_stream_decode_signature(vlm_query) should_start_decoder = False with self._live_stream_lock: lsinfo = self._live_stream_id_map.get(asset.asset_id) if lsinfo and lsinfo.end_of_stream and lsinfo.all_chunks_processed: if not lsinfo.evs_sessions_closed: # The EOS cleanup is still closing this stream's EVS sessions # under the same stream id; starting now would let it delete # the restarted stream's sessions. raise ServiceException( f"Live stream {asset.asset_id} is still shutting down; retry", "StreamNotActive", 409, ) # The decoder dropped this stream at EOS and every subscriber has # seen its final chunk, so the entry is stale: subscribing to it # would fail with StreamNotActive forever. Start a fresh decoder. self._live_stream_id_map.pop(asset.asset_id, None) lsinfo = None if lsinfo: if lsinfo.decode_signature != decode_signature: raise ServiceException( "Live stream already has caption request(s) with different " "decode settings. Stop the active request(s) before changing " "chunk duration, frame sampling, input size, or audio settings.", "BadParameters", 400, ) subscriber = self._LiveStreamSubscriber(on_chunk_result) lsinfo.subscribers[request_id] = subscriber gpu_id = lsinfo.gpu_id else: gpu_dec_use_cnt = {i: 0 for i in range(self._args.num_gpus)} for info in self._live_stream_id_map.values(): gpu_dec_use_cnt[info.gpu_id] += 1 gpu_id = min(gpu_dec_use_cnt, key=gpu_dec_use_cnt.get) lsinfo = self._LiveStreamInfo( subscribers={ request_id: self._LiveStreamSubscriber(on_chunk_result), }, gpu_id=gpu_id, decode_signature=decode_signature, ) self._live_stream_id_map[asset.asset_id] = lsinfo should_start_decoder = True try: if should_start_decoder: started = self._decoder_procs[gpu_id].send_command( "start-live-stream", asset=asset, vlm_query=vlm_query, request_params=request_params, request_id=request_id, ) if started == "already-running": # The previous decoder handle is still tearing down after # EOS, so no decoder will serve this request. Fail # retryably instead of leaving it without a terminal callback. raise ServiceException( f"Live stream {asset.asset_id} is still shutting down; retry", "StreamNotActive", 409, ) else: added = self._decoder_procs[gpu_id].send_command( "add-live-stream-subscriber", live_stream_id=asset.asset_id, request_id=request_id, request_params=request_params, ) if added is False: raise ServiceException( f"Live stream {asset.asset_id} is not active in the decoder", "StreamNotActive", 409, ) if isinstance(added, dict): subscriber.start_chunk_count = int(added.get("num_chunks", 0)) except Exception: current = self._live_stream_id_map.get(asset.asset_id) if current: current.subscribers.pop(request_id, None) if should_start_decoder or not current.subscribers: self._live_stream_id_map.pop(asset.asset_id, None) raise def remove_live_stream_subscriber(self, live_stream_id: str, request_id: str) -> bool: """Remove one subscriber from a shared live stream. Returns True when the stream still has remaining subscribers after the removal. Returns False when the stream/request was already absent or no subscribers remain. """ live_stream_lock = getattr(self, "_live_stream_lock", None) request_id = str(request_id) if live_stream_lock: with live_stream_lock: lsinfo = self._live_stream_id_map.get(live_stream_id) if not lsinfo: return False if lsinfo.subscribers.pop(request_id, None) is None: return False gpu_id = lsinfo.gpu_id remaining = bool(lsinfo.subscribers) if not remaining: lsinfo.all_chunks_processed = True else: lsinfo = self._live_stream_id_map.get(live_stream_id) if not lsinfo: return False if lsinfo.subscribers.pop(request_id, None) is None: return False gpu_id = lsinfo.gpu_id remaining = bool(lsinfo.subscribers) if not remaining: lsinfo.all_chunks_processed = True self._decoder_procs[gpu_id].send_command( "remove-live-stream-subscriber", live_stream_id=live_stream_id, request_id=request_id, ) if not remaining: if live_stream_lock: with live_stream_lock: current = self._live_stream_id_map.get(live_stream_id) if current is lsinfo and not current.subscribers: self._live_stream_id_map.pop(live_stream_id, None) else: current = self._live_stream_id_map.get(live_stream_id) if current is lsinfo and not current.subscribers: self._live_stream_id_map.pop(live_stream_id, None) return remaining def remove_live_stream( self, live_stream_id: str, timeout_sec: Optional[float] = None, abort_inflight: bool = False, ) -> Optional[float]: """Drain and remove a live stream. Returns the drain wall-clock in seconds, or ``None`` if the stream wasn't registered (nothing to drain). The return value lets callers record per-stream drain latency without racing on shared state — critical under parallel batch delete. """ live_stream_lock = getattr(self, "_live_stream_lock", None) if live_stream_lock: with live_stream_lock: lsinfo = self._live_stream_id_map.get(live_stream_id) if lsinfo and abort_inflight: lsinfo.abort_requested = True else: lsinfo = self._live_stream_id_map.get(live_stream_id) if lsinfo and abort_inflight: lsinfo.abort_requested = True if not lsinfo: return None if timeout_sec is None: timeout_sec = float(os.environ.get("RTVI_STREAM_DELETE_DRAIN_TIMEOUT_SEC", "30")) decoder_proc = self._decoder_procs[lsinfo.gpu_id] decoder_proc.send_command("drop-live-handoff", stream_id=live_stream_id) decoder_proc.send_command("stop-live-stream", live_stream_id=live_stream_id) for proc in self._vlm_procs: proc.send_command("drop-chunks", stream_id=live_stream_id, idempotent=True) for proc in self._asr_procs: proc.send_command("drop-chunks", stream_id=live_stream_id, idempotent=True) if abort_inflight: aborted_requests = self._abort_live_stream_vlm_requests(live_stream_id) logger.info( "Aborted %d in-flight VLM request(s) for blocking live-stream delete %s", aborted_requests, live_stream_id, ) drain_start = time.monotonic() deadline = drain_start + timeout_sec timed_out = False while not lsinfo.all_chunks_processed: if time.monotonic() >= deadline: timed_out = True break time.sleep(0.1) drain_elapsed = time.monotonic() - drain_start if timed_out: aborted_requests = self._abort_live_stream_vlm_requests(live_stream_id) logger.warning( "Drain timed out after %.1fs for live-stream %s; " "aborted %d in-flight VLM request(s); retaining stream state for retry.", drain_elapsed, live_stream_id, aborted_requests, ) # Queued tail chunks still need completion accounting. Clearing the # drop mask here would let them recreate EVS sessions after deletion. raise ServiceException( f"Timed out draining live stream {live_stream_id}; retry deletion", "ServiceUnavailable", 503, ) # Release the stream's EVS sessions before the map pop. The EOS-driven # close (see the end-of-stream branch in the output loop) only fires on a # natural end of stream and bails once the stream is out of # _live_stream_id_map, so an explicitly deleted stream would otherwise # leak its sessions for the life of the process -- eventually failing # session creation with "max sessions reached". Sent after the drain and # while drop-chunks is still active, so no in-flight chunk can recreate a # session behind us. Idempotent: a stream already closed via EOS has no # cache entries left. self.close_evs_sessions(live_stream_id) if live_stream_lock: with live_stream_lock: removed = self._live_stream_id_map.pop(live_stream_id, None) else: removed = self._live_stream_id_map.pop(live_stream_id, None) if removed is None: # can happen if multiple stream delete requests are happening in parallel logger.info("%s: live stream already removed from map;", live_stream_id) for proc in self._vlm_procs: proc.send_command("stop-drop-chunks", stream_id=live_stream_id) for proc in self._asr_procs: proc.send_command("stop-drop-chunks", stream_id=live_stream_id) return drain_elapsed def _abort_live_stream_vlm_requests(self, live_stream_id: str) -> int: aborted_requests = 0 for proc in self._vlm_procs: aborted_requests += int( proc.send_command( "abort-live-stream-requests", stream_id=live_stream_id, ) or 0 ) return aborted_requests def get_health_status(self): checks = [] for i, decoder in enumerate(self._decoder_procs): if decoder and not decoder._disabled: is_alive = decoder.is_alive() checks.append( HealthStatus( healthy=is_alive, message=f"Decoder process {i} GPU {decoder._gpu_id} is {'running' if is_alive else 'not running'}", # noqa: E501 component=f"decoder_process_{i}", timestamp=time.time(), ) ) for i, vlm in enumerate(self._vlm_procs): if vlm and not vlm._disabled: is_alive = vlm.is_alive() is_model_healthy = is_alive and vlm.is_model_healthy() checks.append( HealthStatus( healthy=is_model_healthy, message=( f"VLM process {i} is not running" if not is_alive else ( f"VLM process {i} model backend is unhealthy" if not is_model_healthy else f"VLM process {i} is running" ) ), component=f"vlm_process_{i}", timestamp=time.time(), ) ) for i, asr in enumerate(self._asr_procs): if asr and not asr._disabled: is_alive = asr.is_alive() checks.append( HealthStatus( healthy=is_alive, message=f"ASR process {i} is {'running' if is_alive else 'not running'}", component=f"asr_process_{i}", timestamp=time.time(), ) ) return checks @staticmethod def populate_argument_parser(parser: ArgumentParser): """Add VLM Pipeline arguments to the argument parser""" parser.add_argument( "--num-gpus", default=1, type=int, help="Number of GPUs to run the pipeline on", ) parser.add_argument( "--num-vlm-procs", default=1, type=int, help="Number of VLM processes to use in parallel;" " applicable only for openai-compat; others == num-gpus", ) parser.add_argument( "--num-decoders-per-gpu", default=5, type=int, help="Number of Decoder pipelines to run on each GPU in parallel", ) parser.add_argument( "--vlm-model-type", type=VlmModelType, choices=list(VlmModelType), required=True, help="Vision Language Model to use. Use 'custom' for custom model implementations.", ) parser.add_argument( "--vlm-batch-size", type=int, default=1, help="Batch size to use for the VLM model", ) parser.add_argument( "--disable-vlm", action="store_true", default=False, help="Disable the VLM", ) parser.add_argument( "--disable-decoding", action="store_true", default=False, help="Disable Decoding", ) parser.add_argument( "--model-path", type=str, required=False, help="Location of the model weights/files", ) parser.add_argument( "--model-implementation-path", type=str, required=False, help="Location of the model implementation (inference.py). " "If not specified, uses --model-path for both implementation and weights.", ) parser.add_argument( "--model-repository-script-path", type=str, required=False, help="Location of the model repository script. " "If provided, the script is used to export the model to ONNX and " "create the TRITON model repository. " "The script should be a python script that takes the following arguments: " "--model_path, --triton_repo_path, --max_batch_size", ) parser.add_argument( "--num-frames-per-second-or-fixed-frames-chunk", type=int, help="Number of frames to pick from each chunk", ) parser.add_argument( "--max-live-streams", type=int, default=256, help="Number of maximum live streams to support at a time", ) parser.add_argument( "--enable-audio", action="store_true", default=False, help="Enable audio transcription using ASR", ) parser.add_argument( "--use-fps-for-chunking", action="store_true", default=False, help="Use FPS for chunking", ) parser.add_argument( "--ipc-frame-copy", action="store_true", default=False, help="Consume live RTSP frames from CV-published nvunixfd IPC sockets", ) parser.add_argument( "--ipc-socket-dir", type=str, default=DEFAULT_IPC_SOCKET_DIR, help="Directory containing nvunixfd IPC sockets for live streams", ) parser.add_argument( "--ipc-socket-template", type=str, default="nvds_ipc_{camera_id}.sock", help="Socket filename template; must include {camera_id}, {sensor_id}, or {stream_id}.", ) if __name__ == "__main__": import argparse parser = argparse.ArgumentParser( description="VLM Pipeline", formatter_class=argparse.ArgumentDefaultsHelpFormatter ) VlmPipeline.populate_argument_parser(parser) args = parser.parse_args() try: pipeline = VlmPipeline("/tmp/rtvi/assets", args) except Exception as ex: logger.error("Could not load VLM Pipeline - " + str(ex)) sys.exit(-1) pipeline.stop()