Skip to content

armgit / uzu

Public Imported from GitHub
0

A high-performance inference engine for AI models

Revert "Use Metal 4 compiler for compute pipelines (#896)" (#931)

uuuvn401d4d9e30ed
diff --git a/crates/uzu-engine/src/backends/metal/context.rs b/crates/uzu-engine/src/backends/metal/context.rs
index 01318dfab..dd77f6ed3 100644
--- a/crates/uzu-engine/src/backends/metal/context.rs
+++ b/crates/uzu-engine/src/backends/metal/context.rs
@@ -8,9 +8,9 @@ use std::{
};
use metal::{
- MTL4CommandAllocator, MTL4CommandQueue, MTL4CommandQueueDescriptor, MTL4Compiler, MTL4CompilerDescriptor,
- MTLCaptureTarget, MTLComputePipelineState, MTLDevice, MTLDeviceExt, MTLEvent, MTLEventExt,
- MTLFunctionConstantValues, MTLGPUFamily, MTLLibrary, MTLResidencySet, MTLResidencySetDescriptor, MTLSparsePageSize,
+ MTL4CommandAllocator, MTL4CommandQueue, MTL4CommandQueueDescriptor, MTLCaptureTarget, MTLComputePipelineState,
+ MTLDevice, MTLDeviceExt, MTLEvent, MTLEventExt, MTLFunctionConstantValues, MTLGPUFamily, MTLLibrary,
+ MTLResidencySet, MTLResidencySetDescriptor, MTLSparsePageSize,
};
use objc2::{rc::Retained, runtime::ProtocolObject};
use parking_lot::Mutex;
@@ -29,7 +29,7 @@ use crate::backends::{
decompression,
error::MetalError,
heaps::MetalHeaps,
- metal_extensions::{CompilerPipelineExtensions, DeviceExt},
+ metal_extensions::{DeviceExt, LibraryPipelineExtensions},
},
};
@@ -51,7 +51,6 @@ pub struct MetalContext {
pub(super) sparse_queue: Retained<ProtocolObject<dyn MTL4CommandQueue>>,
pub(super) sparse_event: Retained<ProtocolObject<dyn MTLEvent>>,
pub(super) sparse_state: Mutex<SparseState>,
- compiler: Retained<ProtocolObject<dyn MTL4Compiler>>,
pub(super) block_allocator: Arc<BlockAllocator<MetalDenseBuffer>>,
pub(super) heaps: Arc<MetalHeaps>,
pub(super) peak_memory_usage: Arc<AtomicUsize>,
@@ -103,7 +102,7 @@ impl MetalContext {
}
let library = self.library(library_data, library_compressed)?;
- let pipeline = self.compiler.compute_pipeline_state(&library, function_name, constants, Some(cache_key))?;
+ let pipeline = library.compute_pipeline_state(function_name, constants, Some(cache_key))?;
self.pipeline_cache.lock().insert(cache_key.to_string(), pipeline.clone());
Ok(pipeline)
@@ -156,10 +155,6 @@ impl Context for MetalContext {
event_value: 0,
};
- let compiler = device
- .new_compiler_with_descriptor(&MTL4CompilerDescriptor::new())
- .map_err(|error| MetalError::CannotCreateCompiler(error.to_string()))?;
-
let block_allocator = BlockAllocator::new(16 * 1024);
let heaps = MetalHeaps::new(device.clone(), peak_memory_usage.clone(), MTLSparsePageSize::KB256, 256);
@@ -175,7 +170,6 @@ impl Context for MetalContext {
sparse_queue,
sparse_event,
sparse_state: Mutex::new(sparse_state),
- compiler,
block_allocator,
heaps,
peak_memory_usage,
diff --git a/crates/uzu-engine/src/backends/metal/error.rs b/crates/uzu-engine/src/backends/metal/error.rs
index ce3c505a8..e7800cd75 100644
--- a/crates/uzu-engine/src/backends/metal/error.rs
+++ b/crates/uzu-engine/src/backends/metal/error.rs
@@ -17,8 +17,6 @@ pub enum MetalError {
CannotStartGpuCapture(String),
#[error("Cannot create library: {0}")]
CannotCreateLibrary(String),
- #[error("Cannot create compiler: {0}")]
- CannotCreateCompiler(String),
#[error("Cannot create command queue")]
CannotCreateCommandQueue,
#[error("Cannot create event")]
@@ -35,6 +33,8 @@ pub enum MetalError {
CommandBufferWait(RecvError),
#[error("Command buffer execution failed: {0}")]
CommandBufferExecution(String),
+ #[error("Cannot create function: {0}")]
+ CannotCreateFunction(String),
#[error("Cannot create pipeline state for {function_name}: {error}")]
CannotCreatePipelineState {
function_name: String,
diff --git a/crates/uzu-engine/src/backends/metal/metal_extensions/compiler_extensions_pipeline.rs b/crates/uzu-engine/src/backends/metal/metal_extensions/compiler_extensions_pipeline.rs
deleted file mode 100644
index 15de231b5..000000000
--- a/crates/uzu-engine/src/backends/metal/metal_extensions/compiler_extensions_pipeline.rs
+++ /dev/null
@@ -1,53 +0,0 @@
-use metal::{
- MTL4Compiler, MTL4CompilerExt, MTL4ComputePipelineDescriptor, MTL4LibraryFunctionDescriptor,
- MTL4SpecializedFunctionDescriptor, MTLComputePipelineState, MTLFunctionConstantValues, MTLLibrary,
-};
-use objc2::{rc::Retained, runtime::ProtocolObject};
-
-use crate::backends::metal::error::MetalError;
-
-/// Extensions for a Metal 4 compiler to create compute pipeline states.
-pub trait CompilerPipelineExtensions {
- /// Creates a compute pipeline state for a named function in the library.
- /// Optionally specializes the function with constant values.
- fn compute_pipeline_state(
- &self,
- library: &ProtocolObject<dyn MTLLibrary>,
- function_name: &str,
- constants: Option<&MTLFunctionConstantValues>,
- label: Option<&str>,
- ) -> Result<Retained<ProtocolObject<dyn MTLComputePipelineState>>, MetalError>;
-}
-
-impl CompilerPipelineExtensions for ProtocolObject<dyn MTL4Compiler> {
- fn compute_pipeline_state(
- &self,
- library: &ProtocolObject<dyn MTLLibrary>,
- function_name: &str,
- constants: Option<&MTLFunctionConstantValues>,
- label: Option<&str>,
- ) -> Result<Retained<ProtocolObject<dyn MTLComputePipelineState>>, MetalError> {
- let library_function = MTL4LibraryFunctionDescriptor::new();
- library_function.set_library(Some(library));
- library_function.set_name(Some(function_name));
-
- let pipeline_descriptor = MTL4ComputePipelineDescriptor::new();
- pipeline_descriptor.set_label(label);
- match constants {
- Some(constants) => {
- let specialized_function = MTL4SpecializedFunctionDescriptor::new();
- specialized_function.set_function_descriptor(Some(&library_function));
- specialized_function.set_constant_values(Some(constants));
- pipeline_descriptor.set_compute_function_descriptor(Some(&specialized_function));
- },
- None => pipeline_descriptor.set_compute_function_descriptor(Some(&library_function)),
- }
-
- self.new_compute_pipeline_state_with_descriptor_compiler_task_options_error(&pipeline_descriptor, None).map_err(
- |error| MetalError::CannotCreatePipelineState {
- function_name: function_name.to_owned(),
- error: error.to_string(),
- },
- )
- }
-}
diff --git a/crates/uzu-engine/src/backends/metal/metal_extensions/library_extensions_pipeline.rs b/crates/uzu-engine/src/backends/metal/metal_extensions/library_extensions_pipeline.rs
new file mode 100644
index 000000000..61225ccde
--- /dev/null
+++ b/crates/uzu-engine/src/backends/metal/metal_extensions/library_extensions_pipeline.rs
@@ -0,0 +1,49 @@
+use metal::{
+ MTLComputePipelineDescriptor, MTLComputePipelineState, MTLDeviceExt, MTLFunctionConstantValues, MTLLibrary,
+ MTLLibraryExt, MTLPipelineOption,
+};
+use objc2::{rc::Retained, runtime::ProtocolObject};
+
+use crate::backends::metal::error::MetalError;
+
+/// Extensions for Library to create compute pipeline states
+pub trait LibraryPipelineExtensions {
+ /// Creates a compute pipeline state for a named function in the library.
+ /// Optionally accepts function constants.
+ fn compute_pipeline_state(
+ &self,
+ function_name: &str,
+ constants: Option<&MTLFunctionConstantValues>,
+ label: Option<&str>,
+ ) -> Result<Retained<ProtocolObject<dyn MTLComputePipelineState>>, MetalError>;
+}
+
+impl LibraryPipelineExtensions for ProtocolObject<dyn MTLLibrary> {
+ fn compute_pipeline_state(
+ &self,
+ function_name: &str,
+ constants: Option<&MTLFunctionConstantValues>,
+ label: Option<&str>,
+ ) -> Result<Retained<ProtocolObject<dyn MTLComputePipelineState>>, MetalError> {
+ let function = match constants {
+ Some(const_values) => self
+ .new_function_with_name_constant_values(function_name, const_values)
+ .map_err(|error| MetalError::CannotCreateFunction(format!("{function_name}: {error}")))?,
+ None => self
+ .new_function_with_name(function_name)
+ .ok_or_else(|| MetalError::CannotCreateFunction(function_name.to_owned()))?,
+ };
+
+ let descriptor = MTLComputePipelineDescriptor::new();
+ descriptor.set_compute_function(Some(&function));
+ descriptor.set_label(label);
+
+ self.device()
+ .new_compute_pipeline_state_with_descriptor(&descriptor, MTLPipelineOption::NONE)
+ .map(|(pipeline, _)| pipeline)
+ .map_err(|nserror| MetalError::CannotCreatePipelineState {
+ function_name: function_name.to_owned(),
+ error: nserror.to_string(),
+ })
+ }
+}
diff --git a/crates/uzu-engine/src/backends/metal/metal_extensions/mod.rs b/crates/uzu-engine/src/backends/metal/metal_extensions/mod.rs
index 84d04caf7..88e550683 100644
--- a/crates/uzu-engine/src/backends/metal/metal_extensions/mod.rs
+++ b/crates/uzu-engine/src/backends/metal/metal_extensions/mod.rs
@@ -1,13 +1,13 @@
-mod compiler_extensions_pipeline;
mod data_type;
mod device_extensions;
mod function_constant_values_extensions_set_value;
mod gpu_family_extensions;
+mod library_extensions_pipeline;
mod sparse_page_size_extensions;
-pub use compiler_extensions_pipeline::CompilerPipelineExtensions;
pub use data_type::MetalDataTypeExt;
pub use device_extensions::DeviceExt;
pub use function_constant_values_extensions_set_value::FunctionConstantValuesSetValue;
pub use gpu_family_extensions::GpuFamilyExt;
+pub use library_extensions_pipeline::LibraryPipelineExtensions;
pub use sparse_page_size_extensions::SparsePageSizeExt;