A high-performance inference engine for AI models
remove dead code (#179)
uuuvnb34be7e5f3b1
diff --git a/crates/uzu/src/backends/metal/image.rs b/crates/uzu/src/backends/metal/image.rsdeleted file mode 100644index e11ce662d..000000000--- a/crates/uzu/src/backends/metal/image.rs+++ /dev/null@@ -1,196 +0,0 @@-use std::fmt;--use crate::backends::metal::{- MTLDevice, MTLDeviceExt, MTLPixelFormat, MTLResourceExt, MTLStorageMode, MTLTexture, MTLTextureDescriptor,- MTLTextureUsage, ProtocolObject, Retained,-};--#[derive(Debug, Clone, Copy, PartialEq, Eq)]-pub enum PixelFormat {- RGBA8Unorm,- RGBA32Float,- BGRA8Unorm,- R8Unorm,- R32Float,-}--impl From<PixelFormat> for MTLPixelFormat {- fn from(format: PixelFormat) -> Self {- match format {- PixelFormat::RGBA8Unorm => MTLPixelFormat::RGBA8Unorm,- PixelFormat::RGBA32Float => MTLPixelFormat::RGBA32Float,- PixelFormat::BGRA8Unorm => MTLPixelFormat::BGRA8Unorm,- PixelFormat::R8Unorm => MTLPixelFormat::R8Unorm,- PixelFormat::R32Float => MTLPixelFormat::R32Float,- }- }-}--impl From<MTLPixelFormat> for PixelFormat {- fn from(format: MTLPixelFormat) -> Self {- match format {- MTLPixelFormat::RGBA8Unorm => PixelFormat::RGBA8Unorm,- MTLPixelFormat::RGBA32Float => PixelFormat::RGBA32Float,- MTLPixelFormat::BGRA8Unorm => PixelFormat::BGRA8Unorm,- MTLPixelFormat::R8Unorm => PixelFormat::R8Unorm,- MTLPixelFormat::R32Float => PixelFormat::R32Float,- _ => panic!("Unsupported pixel format conversion"),- }- }-}--#[derive(Debug, Clone, Copy, PartialEq, Eq)]-pub struct TextureUsage {- pub shader_read: bool,- pub shader_write: bool,- pub render_target: bool,- pub pixel_format_view: bool,-}--impl Default for TextureUsage {- fn default() -> Self {- Self {- shader_read: true,- shader_write: true,- render_target: false,- pixel_format_view: false,- }- }-}--impl From<TextureUsage> for MTLTextureUsage {- fn from(usage: TextureUsage) -> Self {- let mut result = MTLTextureUsage::empty();- if usage.shader_read {- result = result | MTLTextureUsage::SHADER_READ;- }- if usage.shader_write {- result = result | MTLTextureUsage::SHADER_WRITE;- }- if usage.render_target {- result = result | MTLTextureUsage::RENDER_TARGET;- }- if usage.pixel_format_view {- result = result | MTLTextureUsage::PIXEL_FORMAT_VIEW;- }- result- }-}--#[derive(Debug)]-pub struct Image {- texture: Retained<ProtocolObject<dyn MTLTexture>>,- width: u32,- height: u32,- pixel_format: PixelFormat,-}--impl Image {- pub fn new(- device: &Retained<ProtocolObject<dyn MTLDevice>>,- width: u32,- height: u32,- pixel_format: PixelFormat,- ) -> Self {- Self::new_with_usage(device, width, height, pixel_format, TextureUsage::default())- }-- pub fn new_with_usage(- device: &Retained<ProtocolObject<dyn MTLDevice>>,- width: u32,- height: u32,- pixel_format: PixelFormat,- usage: TextureUsage,- ) -> Self {- let descriptor = MTLTextureDescriptor::new();- descriptor.set_pixel_format(pixel_format.into());- descriptor.set_width(width as usize);- descriptor.set_height(height as usize);- descriptor.set_storage_mode(MTLStorageMode::Private);- descriptor.set_usage(usage.into());-- let texture = device.new_texture_with_descriptor(&descriptor).expect("Failed to create texture");- texture.set_label(Some("Image"));-- Self {- texture,- width,- height,- pixel_format,- }- }-- pub fn new_with_descriptor(- device: &Retained<ProtocolObject<dyn MTLDevice>>,- descriptor: &MTLTextureDescriptor,- ) -> Self {- let texture = device.new_texture_with_descriptor(descriptor).expect("Failed to create texture");- texture.set_label(Some("Image"));-- let width = descriptor.width() as u32;- let height = descriptor.height() as u32;- let pixel_format = descriptor.pixel_format().into();-- Self {- texture,- width,- height,- pixel_format,- }- }-- pub fn from_texture(texture: Retained<ProtocolObject<dyn MTLTexture>>) -> Self {- let width = texture.width() as u32;- let height = texture.height() as u32;- let pixel_format = texture.pixel_format().into();-- Self {- texture,- width,- height,- pixel_format,- }- }-- pub fn width(&self) -> u32 {- self.width- }-- pub fn height(&self) -> u32 {- self.height- }-- pub fn pixel_format(&self) -> PixelFormat {- self.pixel_format- }-- pub fn texture(&self) -> &Retained<ProtocolObject<dyn MTLTexture>> {- &self.texture- }-- pub fn texture_ref(&self) -> &ProtocolObject<dyn MTLTexture> {- &self.texture- }-- pub fn set_label(- &self,- label: &str,- ) {- self.texture.set_label(Some(label));- }-}--impl AsRef<ProtocolObject<dyn MTLTexture>> for Image {- fn as_ref(&self) -> &ProtocolObject<dyn MTLTexture> {- &self.texture- }-}--impl fmt::Display for Image {- fn fmt(- &self,- f: &mut fmt::Formatter<'_>,- ) -> fmt::Result {- write!(f, "Image({}x{}, {:?})", self.width, self.height, self.pixel_format)- }-}diff --git a/crates/uzu/src/backends/metal/kernel/media_kernels/image_ops.metal b/crates/uzu/src/backends/metal/kernel/media_kernels/image_ops.metaldeleted file mode 100644index 687acf318..000000000--- a/crates/uzu/src/backends/metal/kernel/media_kernels/image_ops.metal+++ /dev/null@@ -1,131 +0,0 @@-#include <metal_stdlib>-using namespace metal;--constexpr sampler bicubicSampler(filter::linear, address::clamp_to_edge);--struct ImageParameters {- uint2 inputDimensions;- float3 imageMean;- float3 imageStd;- float paddingValueRgb;-};--struct PatchParameters {- uint2 paddedDimensions;- uint patchSize;- uint numChannels;- uint temporalSlices;-};--[[max_total_threads_per_threadgroup(1024)]]-kernel void scalePadNormalizeImage(- texture2d<float, access::sample> inputTexture [[texture(0)]],- texture2d<float, access::write> outputTexture [[texture(1)]],- constant ImageParameters& params [[buffer(0)]],- uint2 gid [[thread_position_in_grid]]-) {-- uint2 outputDims =- uint2(outputTexture.get_width(), outputTexture.get_height());-- if (gid.x >= outputDims.x || gid.y >= outputDims.y) {- return;- }-- float inputAspectRatio =- float(params.inputDimensions.x) / float(params.inputDimensions.y);- float outputAspectRatio = float(outputDims.x) / float(outputDims.y);-- float scaleFactor;- if (inputAspectRatio > outputAspectRatio) {- scaleFactor = float(outputDims.x) / float(params.inputDimensions.x);- } else {- scaleFactor = float(outputDims.y) / float(params.inputDimensions.y);- }-- float scaledInputWidth = float(params.inputDimensions.x) * scaleFactor;- float scaledInputHeight = float(params.inputDimensions.y) * scaleFactor;-- float offsetX = (float(outputDims.x) - scaledInputWidth) / 2.0f;- float offsetY = (float(outputDims.y) - scaledInputHeight) / 2.0f;-- float targetXInScaledArea = float(gid.x) - offsetX;- float targetYInScaledArea = float(gid.y) - offsetY;-- float4 pixelColor;-- if (targetXInScaledArea >= 0.0f && targetXInScaledArea < scaledInputWidth &&- targetYInScaledArea >= 0.0f && targetYInScaledArea < scaledInputHeight) {-- float uNorm = targetXInScaledArea / scaledInputWidth;- float vNorm = targetYInScaledArea / scaledInputHeight;-- pixelColor = inputTexture.sample(bicubicSampler, float2(uNorm, vNorm));- } else {- pixelColor = float4(- params.paddingValueRgb,- params.paddingValueRgb,- params.paddingValueRgb,- 1.0f- );- }-- float3 normalizedRgb;- float epsilon = 1e-6f;- normalizedRgb.r =- (pixelColor.r - params.imageMean.r) / (params.imageStd.r + epsilon);- normalizedRgb.g =- (pixelColor.g - params.imageMean.g) / (params.imageStd.g + epsilon);- normalizedRgb.b =- (pixelColor.b - params.imageMean.b) / (params.imageStd.b + epsilon);-- outputTexture.write(float4(normalizedRgb, 1.0f), gid);-}--[[max_total_threads_per_threadgroup(1024)]]-kernel void extractImagePatches(- texture2d<float, access::read> paddedNormalizedTexture [[texture(0)]],- device float* outputBuffer [[buffer(0)]],- constant PatchParameters& params [[buffer(1)]],- uint2 gid [[thread_position_in_grid]]-) {-- if (gid.x >= params.paddedDimensions.x ||- gid.y >= params.paddedDimensions.y) {- return;- }-- uint numPatchesX = params.paddedDimensions.x / params.patchSize;-- uint patchCol = gid.x / params.patchSize;- uint patchRow = gid.y / params.patchSize;-- uint spatialPatchIdxFlat = patchRow * numPatchesX + patchCol;-- uint pxInPatch = gid.x % params.patchSize;- uint pyInPatch = gid.y % params.patchSize;-- float4 normalizedPixelRgba = paddedNormalizedTexture.read(gid);-- uint patchPixelFlatIdx = pyInPatch * params.patchSize + pxInPatch;- uint singlePatchChannelTemporalSliceSize =- params.patchSize * params.patchSize;- uint singlePatchChannelSize =- params.temporalSlices * singlePatchChannelTemporalSliceSize;- uint singlePatchSize = params.numChannels * singlePatchChannelSize;-- uint outputPatchBaseIdx = spatialPatchIdxFlat * singlePatchSize;-- for (uint c = 0; c < params.numChannels; ++c) {- float valueToStore = normalizedPixelRgba[c];- for (uint t = 0; t < params.temporalSlices; ++t) {- uint channelBlockOffset = c * singlePatchChannelSize;- uint temporalBlockOffset = t * singlePatchChannelTemporalSliceSize;-- uint outputBufferIdx = outputPatchBaseIdx + channelBlockOffset +- temporalBlockOffset + patchPixelFlatIdx;-- outputBuffer[outputBufferIdx] = valueToStore;- }- }-}\ No newline at end of filediff --git a/crates/uzu/src/backends/metal/kernel/media_kernels/mod.rs b/crates/uzu/src/backends/metal/kernel/media_kernels/mod.rsdeleted file mode 100644index 02149fc08..000000000--- a/crates/uzu/src/backends/metal/kernel/media_kernels/mod.rs+++ /dev/null@@ -1,149 +0,0 @@-use std::{mem, ptr::NonNull};--use objc2::rc::Retained;--use crate::{- backends::metal::{- MTLBuffer, MTLCommandBuffer, MTLCommandEncoder, MTLCommandEncoderExt, MTLComputeCommandEncoder,- MTLComputePipelineState, MTLContext, MTLSize, Metal, ProtocolObject, error::MTLError, image::Image,- metal_extensions::ComputeEncoderDispatch,- },- encodable_block::{EncodableBlock, EncodingParameters},- forward_pass::state::ForwardPassState,-};--#[repr(C)]-#[derive(Debug, Copy, Clone)]-pub struct ImageParameters {- pub input_dimensions: [u32; 2],- pub image_mean: [f32; 3],- pub image_std: [f32; 3],- pub padding_value_rgb: f32,-}--#[repr(C)]-#[derive(Debug, Copy, Clone)]-pub struct PatchParameters {- pub padded_dimensions: [u32; 2],- pub patch_size: u32,- pub num_channels: u32,- pub temporal_slices: u32,-}--#[derive(Debug)]-pub struct ScalePadNormalizeImage {- pipeline_state: Retained<ProtocolObject<dyn MTLComputePipelineState>>,-}--impl ScalePadNormalizeImage {- pub fn new(context: &MTLContext) -> Result<Self, MTLError> {- let function_name = "scalePadNormalizeImage";- let pipeline_state = context.compute_pipeline_state(&function_name, None)?;- Ok(Self {- pipeline_state,- })- }-- pub fn encode_internal(- &self,- input_image: &Image,- output_image: &Image,- image_params_ptr: *const std::ffi::c_void,- command_buffer: &Retained<ProtocolObject<dyn MTLCommandBuffer>>,- ) {- let compute_encoder =- command_buffer.new_compute_command_encoder().expect("Failed to create compute command encoder");- compute_encoder.set_label(Some("ScalePadNormalizeImageEncoder"));-- compute_encoder.set_compute_pipeline_state(&self.pipeline_state);- compute_encoder.set_texture(Some(input_image.texture_ref()), 0);- compute_encoder.set_texture(Some(output_image.texture_ref()), 1);- unsafe {- compute_encoder.set_bytes(- NonNull::new_unchecked(image_params_ptr as *mut _),- mem::size_of::<ImageParameters>(),- 0,- );- }-- let grid_size = MTLSize {- width: output_image.width() as usize,- height: output_image.height() as usize,- depth: 1,- };-- compute_encoder.dispatch_2d(&self.pipeline_state, grid_size, None);- compute_encoder.end_encoding();- }-}--impl EncodableBlock<Metal> for ScalePadNormalizeImage {- fn encode(- &self,- _state: &mut ForwardPassState<Metal>,- _parameters: &EncodingParameters<Metal>,- command_buffer: &Retained<ProtocolObject<dyn MTLCommandBuffer>>,- ) {- let _ = command_buffer;- }-- fn supports_shared_encoder(&self) -> bool {- false // Image processing uses its own encoding path- }-- fn encode_with_shared_encoder(- &self,- _state: &mut ForwardPassState<Metal>,- _parameters: &EncodingParameters<Metal>,- _encoder: &ProtocolObject<dyn MTLComputeCommandEncoder>,- ) {- unreachable!("ScalePadNormalizeImage does not support shared compute encoder");- }-}--#[derive(Debug)]-pub struct ExtractImagePatches {- pipeline_state: Retained<ProtocolObject<dyn MTLComputePipelineState>>,-}--impl ExtractImagePatches {- pub fn new(context: &MTLContext) -> Result<Self, MTLError> {- let function_name = "extractImagePatches";- let pipeline_state = context.compute_pipeline_state(&function_name, None)?;- Ok(Self {- pipeline_state,- })- }-- pub fn encode_internal(- &self,- padded_normalized_image: &Image,- output_buffer_mtl: &Retained<ProtocolObject<dyn MTLBuffer>>,- patch_params_ptr: *const std::ffi::c_void,- command_buffer: &Retained<ProtocolObject<dyn MTLCommandBuffer>>,- ) {- let compute_encoder =- command_buffer.new_compute_command_encoder().expect("Failed to create compute command encoder");- compute_encoder.set_label(Some("ExtractImagePatchesEncoder"));-- compute_encoder.set_compute_pipeline_state(&self.pipeline_state);- compute_encoder.set_texture(Some(padded_normalized_image.texture_ref()), 0);- compute_encoder.set_buffer(Some(output_buffer_mtl), 0, 0);- unsafe {- compute_encoder.set_bytes(- NonNull::new_unchecked(patch_params_ptr as *mut _),- mem::size_of::<PatchParameters>(),- 1,- );- }-- let grid_size = MTLSize {- width: padded_normalized_image.width() as usize,- height: padded_normalized_image.height() as usize,- depth: 1,- };-- compute_encoder.dispatch_2d(&self.pipeline_state, grid_size, None);- compute_encoder.end_encoding();- }-}diff --git a/crates/uzu/src/backends/metal/kernel/mod.rs b/crates/uzu/src/backends/metal/kernel/mod.rsindex 060187e34..17f263a99 100644--- a/crates/uzu/src/backends/metal/kernel/mod.rs+++ b/crates/uzu/src/backends/metal/kernel/mod.rs@@ -6,7 +6,6 @@ pub mod dsl {pub(super) const MTLB: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/default.metallib"));pub mod matmul;-pub mod media_kernels;pub mod mlp;pub use mlp::{MLP_ACTIVATION_FC_INDEX, MLP_FUSED_FC_INDEX, MLP_HIDDEN_DIM_FC_INDEX, MlpActivationType, MlpFusedConfig,@@ -34,9 +33,3 @@ pub use short_conv::{ShortConvDecodeArguments, ShortConvKernel, ShortConvKernelEpub use ssm::{Conv1dPackArguments, Conv1dScanArguments, Conv1dScanKernel, SSDPrefillArguments, SSDPrefillMode, SSMKernelError,};--pub mod media {- pub use super::media_kernels::{ExtractImagePatches, ScalePadNormalizeImage};-}--pub use media_kernels::{ExtractImagePatches, ImageParameters, PatchParameters, ScalePadNormalizeImage};diff --git a/crates/uzu/src/backends/metal/media_utils.rs b/crates/uzu/src/backends/metal/media_utils.rsdeleted file mode 100644index a29171d61..000000000--- a/crates/uzu/src/backends/metal/media_utils.rs+++ /dev/null@@ -1,227 +0,0 @@-use std::{cell::RefCell, mem, rc::Rc};--use crate::{- DataType,- backends::metal::{- MTLCommandBuffer, MTLContext, MTLDevice, MTLDeviceExt, MTLResourceOptions, MetalArray, ProtocolObject,- Retained,- error::MTLError,- image::{Image, PixelFormat, TextureUsage},- kernel::media_kernels::{ExtractImagePatches, ImageParameters, PatchParameters, ScalePadNormalizeImage},- },-};--/// Calculate scaled and padded dimensions for preprocessing input images.-/// This function handles resizing constraints and ensures dimensions are-/// divisible by patch_size.-fn calculate_preprocessing_dimensions(- input_width: u32,- input_height: u32,- min_pixels: u32,- max_pixels: u32,- patch_size: u32,-) -> (u32, u32, f32) {- if input_width == 0 || input_height == 0 {- return (patch_size, patch_size, 1.0);- }-- let original_pixels = (input_width * input_height) as f32;- let min_pixels_f = min_pixels as f32;- let max_pixels_f = max_pixels as f32;-- let mut scale_factor = 1.0;-- if original_pixels < min_pixels_f {- scale_factor = (min_pixels_f / original_pixels).sqrt();- } else if original_pixels > max_pixels_f {- scale_factor = (max_pixels_f / original_pixels).sqrt();- }-- let scaled_width = (input_width as f32 * scale_factor).ceil() as u32;- let scaled_height = (input_height as f32 * scale_factor).ceil() as u32;-- let padded_width = ((scaled_width + patch_size - 1) / patch_size) * patch_size;- let padded_height = ((scaled_height + patch_size - 1) / patch_size) * patch_size;-- (padded_width, padded_height, scale_factor)-}--#[derive(Debug, Clone)]-pub struct ImagePreprocessingParams {- pub min_pixels: u32,- pub max_pixels: u32,- pub patch_size: u32,- pub num_channels: u32,- pub temporal_slices: u32,- pub image_mean: [f32; 3],- pub image_std: [f32; 3],- pub padding_value_rgb: f32,-}--impl Default for ImagePreprocessingParams {- fn default() -> Self {- Self {- min_pixels: 224 * 224,- max_pixels: 1024 * 1024,- patch_size: 14,- num_channels: 3,- temporal_slices: 2,- image_mean: [0.48145466, 0.4578275, 0.40821073],- image_std: [0.26862954, 0.26130258, 0.27577711],- padding_value_rgb: 0.0,- }- }-}--#[derive(Debug, Clone)]-pub struct ImagePreprocessingRequirements {- pub padded_width: u32,- pub padded_height: u32,- pub output_buffer_element_count: u32,- pub intermediate_texture_pixel_format: PixelFormat,- pub intermediate_texture_usage: TextureUsage,- pub output_buffer_data_type: DataType,-}--pub fn calculate_image_preprocessing_requirements(- input_width: u32,- input_height: u32,- params: &ImagePreprocessingParams,-) -> ImagePreprocessingRequirements {- let (padded_width, padded_height, _scale_factor) = calculate_preprocessing_dimensions(- input_width,- input_height,- params.min_pixels,- params.max_pixels,- params.patch_size,- );-- let num_patches_x = padded_width / params.patch_size;- let num_patches_y = padded_height / params.patch_size;- let total_spatial_patches = num_patches_x * num_patches_y;- let output_buffer_element_count =- total_spatial_patches * params.temporal_slices * params.num_channels * params.patch_size * params.patch_size;-- let intermediate_texture_usage = TextureUsage {- shader_read: true,- shader_write: true,- render_target: false,- pixel_format_view: false,- };-- ImagePreprocessingRequirements {- padded_width,- padded_height,- output_buffer_element_count,- intermediate_texture_pixel_format: PixelFormat::RGBA32Float,- intermediate_texture_usage,- output_buffer_data_type: DataType::F32,- }-}--pub struct MetalImagePreprocessor {- device: Retained<ProtocolObject<dyn MTLDevice>>,- scale_pad_normalize_kernel: ScalePadNormalizeImage,- extract_patches_kernel: ExtractImagePatches,-}--impl MetalImagePreprocessor {- pub fn new(context: &MTLContext) -> Result<Self, MTLError> {- let scale_pad_normalize_kernel = ScalePadNormalizeImage::new(context)?;- let extract_patches_kernel = ExtractImagePatches::new(context)?;- Ok(Self {- device: context.device.clone(),- scale_pad_normalize_kernel,- extract_patches_kernel,- })- }-- pub unsafe fn encode_preprocessing(- &self,- command_buffer: &Retained<ProtocolObject<dyn MTLCommandBuffer>>,- input_image: &Image,- params: &ImagePreprocessingParams,- requirements: &ImagePreprocessingRequirements,- intermediate_texture: &Image,- output_patch_buffer_cell: Rc<RefCell<MetalArray>>,- ) -> Result<(), MTLError> {- let padded_width = requirements.padded_width;- let padded_height = requirements.padded_height;-- let input_dimensions_data = [input_image.width(), input_image.height()];- let mut input_dimensions_tensor =- data_to_mtl_tensor(&self.device, &input_dimensions_data, vec![2], DataType::F32)?;- let mut image_mean_tensor = data_to_mtl_tensor(&self.device, ¶ms.image_mean, vec![3], DataType::F32)?;- let mut image_std_tensor = data_to_mtl_tensor(&self.device, ¶ms.image_std, vec![3], DataType::F32)?;- let mut padding_value_rgb_tensor =- data_to_mtl_tensor(&self.device, ¶ms.padding_value_rgb, vec![1], DataType::F32)?;-- let _spn_tensors: Vec<&mut MetalArray> = vec![- &mut input_dimensions_tensor,- &mut image_mean_tensor,- &mut image_std_tensor,- &mut padding_value_rgb_tensor,- ];- let _spn_images = [input_image, intermediate_texture];-- let image_params = ImageParameters {- input_dimensions: [input_image.width(), input_image.height()],- image_mean: params.image_mean,- image_std: params.image_std,- padding_value_rgb: params.padding_value_rgb,- };- self.scale_pad_normalize_kernel.encode_internal(- input_image,- intermediate_texture,- &image_params as *const _ as *const std::ffi::c_void,- command_buffer,- );-- let mut final_output_buffer_array_ref = output_patch_buffer_cell.borrow_mut();-- let padded_dimensions_data = [padded_width, padded_height];- let mut padded_dimensions_tensor =- data_to_mtl_tensor(&self.device, &padded_dimensions_data, vec![2], DataType::F32)?;- let mut patch_size_tensor = data_to_mtl_tensor(&self.device, ¶ms.patch_size, vec![1], DataType::F32)?;- let mut num_channels_tensor = data_to_mtl_tensor(&self.device, ¶ms.num_channels, vec![1], DataType::F32)?;- let mut temporal_slices_tensor =- data_to_mtl_tensor(&self.device, ¶ms.temporal_slices, vec![1], DataType::F32)?;-- let _eip_tensors: Vec<&mut MetalArray> = vec![- &mut *final_output_buffer_array_ref,- &mut padded_dimensions_tensor,- &mut patch_size_tensor,- &mut num_channels_tensor,- &mut temporal_slices_tensor,- ];- let _eip_images = [intermediate_texture];-- let patch_params = PatchParameters {- padded_dimensions: [requirements.padded_width, requirements.padded_height],- patch_size: params.patch_size,- num_channels: params.num_channels,- temporal_slices: params.temporal_slices,- };- self.extract_patches_kernel.encode_internal(- intermediate_texture,- final_output_buffer_array_ref.buffer(),- &patch_params as *const _ as *const std::ffi::c_void,- command_buffer,- );-- Ok(())- }-}--fn data_to_mtl_tensor<T: Copy>(- device: &Retained<ProtocolObject<dyn MTLDevice>>,- data: &T,- shape: Vec<usize>,- data_type: DataType,-) -> Result<MetalArray, MTLError> {- let size = mem::size_of::<T>();- let bytes = unsafe { std::slice::from_raw_parts(data as *const T as *const u8, size) };- let buffer =- device.new_buffer_with_data(bytes, MTLResourceOptions::STORAGE_MODE_SHARED).expect("Failed to create buffer");- unsafe { Ok(MetalArray::from_parts(buffer, 0, &shape, data_type)) }-}diff --git a/crates/uzu/src/backends/metal/mod.rs b/crates/uzu/src/backends/metal/mod.rsindex 8eb7dda33..93c1c5306 100644--- a/crates/uzu/src/backends/metal/mod.rs+++ b/crates/uzu/src/backends/metal/mod.rs@@ -8,13 +8,9 @@ mod context;mod copy_encoder;pub mod encodable_block;pub mod error;-pub mod image;pub mod kernel;mod language_model_generator_context;-pub mod media_utils;pub mod metal_extensions;-pub mod placement_analysis;-pub mod utils;pub use array::MetalArray;pub use backend::Metal;pub use classifier_context::ClassifierContext;@@ -23,9 +19,7 @@ pub use encodable_block::Decoder;pub use error::MTLError;pub use kernel::{KernelDataType, dsl::MetalKernels};pub use language_model_generator_context::LanguageModelGeneratorContext;-pub use media_utils::{ImagePreprocessingParams, ImagePreprocessingRequirements, MetalImagePreprocessor};pub use metal::prelude::*;pub use metal_extensions::{ComputeEncoderSetValue, FunctionConstantValuesSetValue};-pub use placement_analysis::PlacementAnalysis;pub use crate::backends::common::kernel::kv_cache_update::KVCacheUpdate;diff --git a/crates/uzu/src/backends/metal/placement_analysis.rs b/crates/uzu/src/backends/metal/placement_analysis.rsdeleted file mode 100644index f0c4f24f9..000000000--- a/crates/uzu/src/backends/metal/placement_analysis.rs+++ /dev/null@@ -1,229 +0,0 @@-use std::collections::HashSet;--use regex::Regex;-use serde::{Deserialize, Serialize};--/// Represents ANE/GPU placement analysis results-#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]-pub struct PlacementAnalysis {- /// Total layers in the graph- pub total_layers: usize,- /// Layers placed on ANE- pub ane_layers: usize,- /// Layers placed on GPU- pub gpu_layers: usize,- /// ANE utilization percentage- pub ane_percentage: f64,- /// GPU utilization percentage- pub gpu_percentage: f64,- /// Operations placed on ANE- pub ane_operations: Vec<String>,- /// Operations placed on GPU- pub gpu_operations: Vec<String>,-}--impl Default for PlacementAnalysis {- fn default() -> Self {- Self {- total_layers: 0,- ane_layers: 0,- gpu_layers: 0,- ane_percentage: 0.0,- gpu_percentage: 0.0,- ane_operations: Vec::new(),- gpu_operations: Vec::new(),- }- }-}--impl PlacementAnalysis {- /// Initialize with parsed values- #[allow(clippy::too_many_arguments)]- pub fn new(- total_layers: usize,- ane_layers: usize,- gpu_layers: usize,- ane_percentage: f64,- gpu_percentage: f64,- ane_operations: Vec<String>,- gpu_operations: Vec<String>,- ) -> Self {- Self {- total_layers,- ane_layers,- gpu_layers,- ane_percentage,- gpu_percentage,- ane_operations,- gpu_operations,- }- }-- /// Parse the MPS placement analysis output to extract the placement data- pub fn from_log_output(log_output: &str) -> Self {- // println!("Parsing placement log output:");- // println!("Log contains {} characters", log_output.len());- // println!(- // "First 100 chars: {}",- // &log_output.chars().take(100).collect::<String>()- // );-- let mut result = Self::default();-- // Extract layer counts- let re_layers =- Regex::new(r"Layers Count: (\d+): ANE: (\d+) \(([\d\.e\-]+)%\), GPU: (\d+) \(([\d\.e\-]+)%\)").unwrap();- if let Some(captures) = re_layers.captures(log_output) {- // println!("Found layer count information");- if let (Some(total), Some(ane), Some(ane_pct), Some(gpu), Some(gpu_pct)) =- (captures.get(1), captures.get(2), captures.get(3), captures.get(4), captures.get(5))- {- result.total_layers = total.as_str().parse().unwrap_or(0);- result.ane_layers = ane.as_str().parse().unwrap_or(0);- result.gpu_layers = gpu.as_str().parse().unwrap_or(0);- result.ane_percentage = ane_pct.as_str().parse().unwrap_or(0.0);- result.gpu_percentage = gpu_pct.as_str().parse().unwrap_or(0.0);-- // println!(- // "Parsed counts - Total: {}, ANE: {}, GPU: {}",- // result.total_layers, result.ane_layers, result.gpu_layers- // );- }- } else {- // println!("Did not find layer count information in log output");- }-- // Extract operations that couldn't be placed on ANE (i.e., run on GPU)- let re_op_start = Regex::new(r"Unplaced operations:").unwrap();- if re_op_start.is_match(log_output) {- // println!("Found unplaced operations section");-- // The operations section begins after "Unplaced operations:"- if let Some(ops_section) = log_output.split("Unplaced operations:").nth(1) {- // The operations section ends at "Couldn't find any ANERegionCall operation."- if let Some(ops_text) = ops_section.split("Couldn't find any ANERegionCall operation").next() {- // Process operations section- let mut current_op_type = String::new();- let mut _current_op_count = 0;-- // Deduplicate operations since we just want to know types- let mut unique_ops = HashSet::new();-- // Process line by line- for line in ops_text.lines() {- // Line like "mps.multiply (6):" starts a new operation type section- if let Some(caps) = Regex::new(r"(\w+\.\w+) \((\d+)\):").unwrap().captures(line) {- if let (Some(op_type), Some(count)) = (caps.get(1), caps.get(2)) {- current_op_type = op_type.as_str().to_string();- _current_op_count = count.as_str().parse().unwrap_or(0);- // Insert immediately to handle logs that omit per-op lines- let trimmed = current_op_type.trim_start_matches("mps.").to_string();- unique_ops.insert(trimmed);- // println!(- // "Found operation type: {} ({})",- // current_op_type, _current_op_count- // );- }- // Lines starting with "%" contain actual operation instances- } else if line.trim().starts_with('%') {- // Store operation type **without** "mps." prefix so tests expect plain names- let trimmed = current_op_type.trim_start_matches("mps.").to_string();- unique_ops.insert(trimmed);- }- }-- // Convert to vec- result.gpu_operations = unique_ops.into_iter().collect();- // println!(- // "Parsed {} unique GPU operation types",- // result.gpu_operations.len()- // );- }- }- } else {- // println!("Did not find unplaced operations section in log output");- }-- // For ANE operations, we would need to extract them if present- // Extract ANERegionCall occurrences so tests can validate presence- let ane_region_re = Regex::new(r"ANERegionCall operations|ANERegionCall operation").unwrap();- if ane_region_re.is_match(log_output) {- // We don't have detailed operation names – the tests only check for a placeholder string- // so insert a canonical name once if any ANERegionCall string appears.- result.ane_operations.push("ANE region call".to_string());- }-- result- }-}--#[cfg(test)]-mod tests {- use super::*;-- #[test]- fn test_parse_placement_analysis_basic() {- let log_output = r#"- Some preamble- Layers Count: 10: ANE: 8 (80.0%), GPU: 2 (20.0%)- Some other lines- "#;- let analysis = PlacementAnalysis::from_log_output(log_output);- assert_eq!(analysis.total_layers, 10);- assert_eq!(analysis.ane_layers, 8);- assert_eq!(analysis.gpu_layers, 2);- assert_eq!(analysis.ane_percentage, 80.0);- assert_eq!(analysis.gpu_percentage, 20.0);- }-- #[test]- fn test_parse_gpu_ops() {- let log_output = r#"- Layers Count: 5: ANE: 0 (0.0%), GPU: 5 (100.0%)- Unplaced operations:- mps.matmul (2):- mps.add (3):- "#;- let analysis = PlacementAnalysis::from_log_output(log_output);- assert_eq!(analysis.gpu_layers, 5);- assert!(analysis.gpu_operations.contains(&"matmul".to_string()));- assert!(analysis.gpu_operations.contains(&"add".to_string()));- assert_eq!(analysis.gpu_operations.len(), 2);- }-- #[test]- fn test_parse_ane_ops() {- let log_output = r#"- Layers Count: 3: ANE: 3 (100.0%), GPU: 0 (0.0%)- Found exactly one ANERegionCall operation.- "#;- let analysis = PlacementAnalysis::from_log_output(log_output);- assert_eq!(analysis.ane_layers, 3);- assert!(analysis.ane_operations.contains(&"ANE region call".to_string()));- }-- #[test]- fn test_parse_mixed_ops_favor_gpu_if_unplaced_listed() {- // This case is a bit ambiguous in the original Swift, but if "Unplaced operations"- // are listed, they are typically GPU ops.- let log_output = r#"- Layers Count: 10: ANE: 5 (50.0%), GPU: 5 (50.0%)- Unplaced operations:- mps.convolution (5):- ANERegionCall operations:- "#;- let analysis = PlacementAnalysis::from_log_output(log_output);- assert_eq!(analysis.ane_layers, 5);- assert_eq!(analysis.gpu_layers, 5);- assert!(analysis.gpu_operations.contains(&"convolution".to_string()));- assert_eq!(analysis.gpu_operations.len(), 1);- assert!(analysis.ane_operations.contains(&"ANE region call".to_string()));- }-- #[test]- fn test_no_placement_info() {- let log_output = "Some random log output without placement details.";- let analysis = PlacementAnalysis::from_log_output(log_output);- assert_eq!(analysis, PlacementAnalysis::default());- }-}diff --git a/crates/uzu/src/backends/metal/utils.rs b/crates/uzu/src/backends/metal/utils.rsdeleted file mode 100644index 59ba7a8b8..000000000--- a/crates/uzu/src/backends/metal/utils.rs+++ /dev/null@@ -1,137 +0,0 @@-#![allow(dead_code)]-use std::{- ffi,- sync::{Arc, Mutex},-};--bitflags::bitflags! {- pub struct CaptureOptions: u8 {- const STDOUT = 0b00000001;- const STDERR = 0b00000010;- const BOTH = Self::STDOUT.bits() | Self::STDERR.bits();- }-}--/// Utility for capturing stdout/stderr output (for placement analysis, etc.)-pub struct StdoutCapture {- capture_stdout: bool,- capture_stderr: bool,- _stdout_old: Option<*mut ffi::c_void>,- _stderr_old: Option<*mut ffi::c_void>,- stdout_pipe: Option<[i32; 2]>,- stderr_pipe: Option<[i32; 2]>,- stdout_thread: Option<std::thread::JoinHandle<()>>,- stderr_thread: Option<std::thread::JoinHandle<()>>,- stdout_buffer: Option<Arc<Mutex<String>>>,- stderr_buffer: Option<Arc<Mutex<String>>>,-}--impl StdoutCapture {- pub fn new(options: CaptureOptions) -> Self {- Self {- capture_stdout: options.contains(CaptureOptions::STDOUT),- capture_stderr: options.contains(CaptureOptions::STDERR),- _stdout_old: None,- _stderr_old: None,- stdout_pipe: None,- stderr_pipe: None,- stdout_thread: None,- stderr_thread: None,- stdout_buffer: None,- stderr_buffer: None,- }- }-- pub fn start(&mut self) {- unsafe {- if self.capture_stdout {- let mut pipes = [0; 2];- libc::pipe(pipes.as_mut_ptr());- self.stdout_pipe = Some(pipes);- let stdout_old = libc::dup(libc::STDOUT_FILENO);- self._stdout_old = Some(stdout_old as *mut ffi::c_void);- libc::dup2(self.stdout_pipe.unwrap()[1], libc::STDOUT_FILENO);- libc::close(self.stdout_pipe.unwrap()[1]);- let read_fd = self.stdout_pipe.unwrap()[0];- let buffer = Arc::new(Mutex::new(String::new()));- let buffer_clone = Arc::clone(&buffer);- self.stdout_buffer = Some(buffer);- self.stdout_thread = Some(std::thread::spawn(move || {- let mut buf = [0u8; 4096];- loop {- let bytes_read = libc::read(read_fd, buf.as_mut_ptr() as *mut libc::c_void, buf.len());- if bytes_read <= 0 {- break;- }- if let Ok(s) = std::str::from_utf8(&buf[0..bytes_read as usize]) {- let mut b = buffer_clone.lock().unwrap();- b.push_str(s);- }- }- libc::close(read_fd);- }));- }- if self.capture_stderr {- let mut pipes = [0; 2];- libc::pipe(pipes.as_mut_ptr());- self.stderr_pipe = Some(pipes);- let stderr_old = libc::dup(libc::STDERR_FILENO);- self._stderr_old = Some(stderr_old as *mut ffi::c_void);- libc::dup2(self.stderr_pipe.unwrap()[1], libc::STDERR_FILENO);- libc::close(self.stderr_pipe.unwrap()[1]);- let read_fd = self.stderr_pipe.unwrap()[0];- let buffer = Arc::new(Mutex::new(String::new()));- let buffer_clone = Arc::clone(&buffer);- self.stderr_buffer = Some(buffer);- self.stderr_thread = Some(std::thread::spawn(move || {- let mut buf = [0u8; 4096];- loop {- let bytes_read = libc::read(read_fd, buf.as_mut_ptr() as *mut libc::c_void, buf.len());- if bytes_read <= 0 {- break;- }- if let Ok(s) = std::str::from_utf8(&buf[0..bytes_read as usize]) {- let mut b = buffer_clone.lock().unwrap();- b.push_str(s);- }- }- libc::close(read_fd);- }));- }- }- }-- pub fn stop(&mut self) -> String {- unsafe {- libc::fflush(std::ptr::null_mut());- if let Some(stdout_old) = self._stdout_old {- libc::dup2(stdout_old as i32, libc::STDOUT_FILENO);- libc::close(stdout_old as i32);- self._stdout_old = None;- }- if let Some(stderr_old) = self._stderr_old {- libc::dup2(stderr_old as i32, libc::STDERR_FILENO);- libc::close(stderr_old as i32);- self._stderr_old = None;- }- }- if let Some(thread) = self.stdout_thread.take() {- let _ = thread.join();- }- if let Some(thread) = self.stderr_thread.take() {- let _ = thread.join();- }- let mut captured = String::new();- if let Some(buffer) = &self.stdout_buffer {- let b = buffer.lock().unwrap();- captured.push_str(&b);- }- if let Some(buffer) = &self.stderr_buffer {- let b = buffer.lock().unwrap();- captured.push_str(&b);- }- self.stdout_buffer = None;- self.stderr_buffer = None;- captured- }-}