From 370bd27f3a29668dfc8f14b0f3f167c0baf29491 Mon Sep 17 00:00:00 2001 From: Praneth Paruchuri Date: Wed, 24 Dec 2025 22:45:44 +0530 Subject: [PATCH] [model-gateway] Implement Zero-Copy Vision Tensor Access (#15750) --- .../src/multimodal/vision/image_processor.rs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/sgl-model-gateway/src/multimodal/vision/image_processor.rs b/sgl-model-gateway/src/multimodal/vision/image_processor.rs index 59a19f046..cb15ee1ee 100644 --- a/sgl-model-gateway/src/multimodal/vision/image_processor.rs +++ b/sgl-model-gateway/src/multimodal/vision/image_processor.rs @@ -3,7 +3,7 @@ //! This module defines the interface for model-specific image processors //! and the common output format for preprocessed images. -use std::collections::HashMap; +use std::{borrow::Cow, collections::HashMap}; use image::DynamicImage; use ndarray::{Array4, ArrayD}; @@ -206,9 +206,12 @@ impl PreprocessedImages { self.num_img_tokens.iter().sum() } - /// Get pixel values as a flat f32 slice (row-major order). - pub fn pixel_values_flat(&self) -> Vec { - self.pixel_values.iter().copied().collect() + /// Get pixel values as a flat f32 slice without copying if possible. + pub fn pixel_values_flat(&self) -> Cow<'_, [f32]> { + match self.pixel_values.as_slice() { + Some(slice) => Cow::Borrowed(slice), + None => Cow::Owned(self.pixel_values.iter().copied().collect()), + } } /// Get the shape of pixel values as a vector.