|
| 1 | +use crate::source::{ |
| 2 | + AFFINE, BINARY, CAST, CONV, FILL, INDEXING, MLX_GEMM, MLX_SORT, QUANTIZED, RANDOM, REDUCE, |
| 3 | + SDPA, SORT, TERNARY, UNARY, |
| 4 | +}; |
| 5 | +use crate::{ |
| 6 | + ComputePipeline, ConstantValues, Device, Function, Library, MTLCompileOptions, MTLMathMode, |
| 7 | + MetalKernelError, Source, |
| 8 | +}; |
| 9 | +use std::collections::HashMap; |
| 10 | +use std::sync::RwLock; |
| 11 | + |
| 12 | +#[derive(Debug, Clone)] |
| 13 | +pub enum KernelName { |
| 14 | + Ref(&'static str), |
| 15 | + Value(String), |
| 16 | +} |
| 17 | + |
| 18 | +impl AsRef<str> for KernelName { |
| 19 | + fn as_ref(&self) -> &str { |
| 20 | + match self { |
| 21 | + Self::Ref(r) => r, |
| 22 | + Self::Value(v) => v.as_str(), |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +impl std::hash::Hash for KernelName { |
| 28 | + fn hash<H: std::hash::Hasher>(&self, state: &mut H) { |
| 29 | + match self { |
| 30 | + Self::Ref(r) => r.hash(state), |
| 31 | + Self::Value(v) => v.hash(state), |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl PartialEq for KernelName { |
| 37 | + fn eq(&self, other: &Self) -> bool { |
| 38 | + let v1: &str = self.as_ref(); |
| 39 | + let v2: &str = other.as_ref(); |
| 40 | + v1 == v2 |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +impl Eq for KernelName {} |
| 45 | + |
| 46 | +impl From<&'static str> for KernelName { |
| 47 | + fn from(value: &'static str) -> Self { |
| 48 | + Self::Ref(value) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +impl From<String> for KernelName { |
| 53 | + fn from(value: String) -> Self { |
| 54 | + Self::Value(value) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +type Libraries = HashMap<Source, Library>; |
| 59 | +type Pipelines = HashMap<(KernelName, Option<ConstantValues>), ComputePipeline>; |
| 60 | + |
| 61 | +#[derive(Debug)] |
| 62 | +pub struct Kernels { |
| 63 | + libraries: RwLock<Libraries>, |
| 64 | + pipelines: RwLock<Pipelines>, |
| 65 | +} |
| 66 | + |
| 67 | +impl Default for Kernels { |
| 68 | + fn default() -> Self { |
| 69 | + Self::new() |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl Kernels { |
| 74 | + pub fn new() -> Self { |
| 75 | + let libraries = RwLock::new(Libraries::new()); |
| 76 | + let pipelines = RwLock::new(Pipelines::new()); |
| 77 | + Self { |
| 78 | + libraries, |
| 79 | + pipelines, |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + fn get_library_source(&self, source: Source) -> &'static str { |
| 84 | + match source { |
| 85 | + Source::Affine => AFFINE, |
| 86 | + Source::Binary => BINARY, |
| 87 | + Source::Cast => CAST, |
| 88 | + Source::Conv => CONV, |
| 89 | + Source::Fill => FILL, |
| 90 | + Source::Gemm => MLX_GEMM, |
| 91 | + Source::Indexing => INDEXING, |
| 92 | + Source::MlxSort => MLX_SORT, |
| 93 | + Source::Quantized => QUANTIZED, |
| 94 | + Source::Random => RANDOM, |
| 95 | + Source::Reduce => REDUCE, |
| 96 | + Source::Sort => SORT, |
| 97 | + Source::Ternary => TERNARY, |
| 98 | + Source::Unary => UNARY, |
| 99 | + Source::Sdpa => SDPA, |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /// Load the give library from its [`source`]. |
| 104 | + /// If this has been previously loaded it will just fetch it from cache. |
| 105 | + pub fn load_library( |
| 106 | + &self, |
| 107 | + device: &Device, |
| 108 | + source: Source, |
| 109 | + ) -> Result<Library, MetalKernelError> { |
| 110 | + let mut libraries = self.libraries.write()?; |
| 111 | + if let Some(lib) = libraries.get(&source) { |
| 112 | + Ok(lib.clone()) |
| 113 | + } else { |
| 114 | + let lib = { |
| 115 | + let source_content = self.get_library_source(source); |
| 116 | + let compile_options = MTLCompileOptions::new(); |
| 117 | + //unsafe { compile_options.setEnableLogging(true) }; |
| 118 | + unsafe { compile_options.setMathMode(MTLMathMode::Fast) }; |
| 119 | + device |
| 120 | + .new_library_with_source(source_content, Some(&compile_options)) |
| 121 | + .map_err(|e| MetalKernelError::LoadLibraryError(e.to_string()))? |
| 122 | + }; |
| 123 | + libraries.insert(source, lib.clone()); |
| 124 | + Ok(lib) |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + fn load_function( |
| 129 | + &self, |
| 130 | + device: &Device, |
| 131 | + source: Source, |
| 132 | + name: &str, |
| 133 | + constants: Option<&ConstantValues>, |
| 134 | + ) -> Result<Function, MetalKernelError> { |
| 135 | + let func = self |
| 136 | + .load_library(device, source)? |
| 137 | + .get_function(name, constants) |
| 138 | + .map_err(|e| MetalKernelError::LoadFunctionError(e.to_string()))?; |
| 139 | + Ok(func) |
| 140 | + } |
| 141 | + |
| 142 | + /// Load the give pipeline |
| 143 | + /// loads the library from source, then gets the function [`name`] from |
| 144 | + /// that source |
| 145 | + pub fn load_pipeline_with_constants( |
| 146 | + &self, |
| 147 | + device: &Device, |
| 148 | + source: Source, |
| 149 | + name: impl Into<KernelName>, |
| 150 | + constants: Option<ConstantValues>, |
| 151 | + ) -> Result<ComputePipeline, MetalKernelError> { |
| 152 | + let mut pipelines = self.pipelines.write()?; |
| 153 | + let key = (name.into(), constants); |
| 154 | + if let Some(pipeline) = pipelines.get(&key) { |
| 155 | + Ok(pipeline.clone()) |
| 156 | + } else { |
| 157 | + let (name, constants) = key; |
| 158 | + let func = self.load_function(device, source, name.as_ref(), constants.as_ref())?; |
| 159 | + let pipeline = device |
| 160 | + .new_compute_pipeline_state_with_function(&func) |
| 161 | + .map_err(|e| MetalKernelError::FailedToCreatePipeline(e.to_string()))?; |
| 162 | + pipelines.insert((name, constants), pipeline.clone()); |
| 163 | + |
| 164 | + Ok(pipeline) |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + /// Load the give pipeline |
| 169 | + /// loads the library from source, then gets the function [`name`] from |
| 170 | + /// that source (without constants) |
| 171 | + pub fn load_pipeline( |
| 172 | + &self, |
| 173 | + device: &Device, |
| 174 | + source: Source, |
| 175 | + name: impl Into<KernelName>, |
| 176 | + ) -> Result<ComputePipeline, MetalKernelError> { |
| 177 | + self.load_pipeline_with_constants(device, source, name, None) |
| 178 | + } |
| 179 | +} |
0 commit comments