[model-gateway] Implement Zero-Copy Vision Tensor Access (#15750)

This commit is contained in:
Praneth Paruchuri
2025-12-24 09:15:44 -08:00
committed by GitHub
parent b27b5a834e
commit 370bd27f3a
@@ -3,7 +3,7 @@
//! This module defines the interface for model-specific image processors //! This module defines the interface for model-specific image processors
//! and the common output format for preprocessed images. //! and the common output format for preprocessed images.
use std::collections::HashMap; use std::{borrow::Cow, collections::HashMap};
use image::DynamicImage; use image::DynamicImage;
use ndarray::{Array4, ArrayD}; use ndarray::{Array4, ArrayD};
@@ -206,9 +206,12 @@ impl PreprocessedImages {
self.num_img_tokens.iter().sum() self.num_img_tokens.iter().sum()
} }
/// Get pixel values as a flat f32 slice (row-major order). /// Get pixel values as a flat f32 slice without copying if possible.
pub fn pixel_values_flat(&self) -> Vec<f32> { pub fn pixel_values_flat(&self) -> Cow<'_, [f32]> {
self.pixel_values.iter().copied().collect() 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. /// Get the shape of pixel values as a vector.