1#![allow(dead_code)]
2use crate::op::{BinaryOpT, CmpOp, ReduceOp, UnaryOpT};
3use crate::{CpuStorage, DType, Error, Layout, Result, Shape};
4
5#[derive(Debug, Clone)]
6pub struct MetalDevice;
7
8#[derive(Debug)]
9pub struct MetalStorage;
10
11#[derive(thiserror::Error, Debug)]
12pub enum MetalError {
13 #[error("{0}")]
14 Message(String),
15}
16
17impl From<String> for MetalError {
18 fn from(e: String) -> Self {
19 MetalError::Message(e)
20 }
21}
22
23macro_rules! fail {
24 () => {
25 unimplemented!("metal support has not been enabled, add `metal` feature to enable.")
26 };
27}
28
29impl crate::backend::BackendStorage for MetalStorage {
30 type Device = MetalDevice;
31
32 fn try_clone(&self, _: &Layout) -> Result<Self> {
33 Err(Error::NotCompiledWithMetalSupport)
34 }
35
36 fn dtype(&self) -> DType {
37 fail!()
38 }
39
40 fn device(&self) -> &Self::Device {
41 fail!()
42 }
43
44 fn to_cpu_storage(&self) -> Result<CpuStorage> {
45 Err(Error::NotCompiledWithMetalSupport)
46 }
47
48 fn affine(&self, _: &Layout, _: f64, _: f64) -> Result<Self> {
49 Err(Error::NotCompiledWithMetalSupport)
50 }
51
52 fn powf(&self, _: &Layout, _: f64) -> Result<Self> {
53 Err(Error::NotCompiledWithMetalSupport)
54 }
55
56 fn elu(&self, _: &Layout, _: f64) -> Result<Self> {
57 Err(Error::NotCompiledWithMetalSupport)
58 }
59
60 fn reduce_op(&self, _: ReduceOp, _: &Layout, _: &[usize]) -> Result<Self> {
61 Err(Error::NotCompiledWithMetalSupport)
62 }
63
64 fn cmp(&self, _: CmpOp, _: &Self, _: &Layout, _: &Layout) -> Result<Self> {
65 Err(Error::NotCompiledWithMetalSupport)
66 }
67
68 fn to_dtype(&self, _: &Layout, _: DType) -> Result<Self> {
69 Err(Error::NotCompiledWithMetalSupport)
70 }
71
72 fn unary_impl<B: UnaryOpT>(&self, _: &Layout) -> Result<Self> {
73 Err(Error::NotCompiledWithMetalSupport)
74 }
75
76 fn binary_impl<B: BinaryOpT>(&self, _: &Self, _: &Layout, _: &Layout) -> Result<Self> {
77 Err(Error::NotCompiledWithMetalSupport)
78 }
79
80 fn where_cond(&self, _: &Layout, _: &Self, _: &Layout, _: &Self, _: &Layout) -> Result<Self> {
81 Err(Error::NotCompiledWithMetalSupport)
82 }
83
84 fn conv1d(
85 &self,
86 _: &Layout,
87 _: &Self,
88 _: &Layout,
89 _: &crate::conv::ParamsConv1D,
90 ) -> Result<Self> {
91 Err(Error::NotCompiledWithMetalSupport)
92 }
93
94 fn conv_transpose1d(
95 &self,
96 _l: &Layout,
97 _kernel: &Self,
98 _kernel_l: &Layout,
99 _params: &crate::conv::ParamsConvTranspose1D,
100 ) -> Result<Self> {
101 Err(Error::NotCompiledWithMetalSupport)
102 }
103
104 fn conv2d(
105 &self,
106 _: &Layout,
107 _: &Self,
108 _: &Layout,
109 _: &crate::conv::ParamsConv2D,
110 ) -> Result<Self> {
111 Err(Error::NotCompiledWithMetalSupport)
112 }
113
114 fn conv_transpose2d(
115 &self,
116 _l: &Layout,
117 _kernel: &Self,
118 _kernel_l: &Layout,
119 _params: &crate::conv::ParamsConvTranspose2D,
120 ) -> Result<Self> {
121 Err(Error::NotCompiledWithMetalSupport)
122 }
123
124 fn index_select(&self, _: &Self, _: &Layout, _: &Layout, _: usize) -> Result<Self> {
125 Err(Error::NotCompiledWithMetalSupport)
126 }
127 fn gather(&self, _: &Layout, _: &Self, _: &Layout, _: usize) -> Result<Self> {
128 Err(Error::NotCompiledWithMetalSupport)
129 }
130
131 fn scatter_add(
132 &self,
133 _: &Layout,
134 _: &Self,
135 _: &Layout,
136 _: &Self,
137 _: &Layout,
138 _: usize,
139 ) -> Result<Self> {
140 Err(Error::NotCompiledWithMetalSupport)
141 }
142
143 fn index_add(
144 &self,
145 _: &Layout,
146 _: &Self,
147 _: &Layout,
148 _: &Self,
149 _: &Layout,
150 _: usize,
151 ) -> Result<Self> {
152 Err(Error::NotCompiledWithMetalSupport)
153 }
154
155 fn matmul(
156 &self,
157 _: &Self,
158 _: (usize, usize, usize, usize),
159 _: &Layout,
160 _: &Layout,
161 ) -> Result<Self> {
162 Err(Error::NotCompiledWithMetalSupport)
163 }
164
165 fn copy_strided_src(&self, _: &mut Self, _: usize, _: &Layout) -> Result<()> {
166 Err(Error::NotCompiledWithMetalSupport)
167 }
168
169 fn copy2d(
170 &self,
171 _: &mut Self,
172 _: usize,
173 _: usize,
174 _: usize,
175 _: usize,
176 _: usize,
177 _: usize,
178 ) -> Result<()> {
179 Err(Error::NotCompiledWithMetalSupport)
180 }
181
182 fn avg_pool2d(&self, _: &Layout, _: (usize, usize), _: (usize, usize)) -> Result<Self> {
183 Err(Error::NotCompiledWithMetalSupport)
184 }
185
186 fn max_pool2d(&self, _: &Layout, _: (usize, usize), _: (usize, usize)) -> Result<Self> {
187 Err(Error::NotCompiledWithMetalSupport)
188 }
189
190 fn upsample_nearest1d(&self, _: &Layout, _: usize) -> Result<Self> {
191 Err(Error::NotCompiledWithMetalSupport)
192 }
193
194 fn upsample_nearest2d(&self, _: &Layout, _: usize, _: usize) -> Result<Self> {
195 Err(Error::NotCompiledWithMetalSupport)
196 }
197}
198
199impl crate::backend::BackendDevice for MetalDevice {
200 type Storage = MetalStorage;
201 fn new(_: usize) -> Result<Self> {
202 Err(Error::NotCompiledWithMetalSupport)
203 }
204
205 fn set_seed(&self, _: u64) -> Result<()> {
206 Err(Error::NotCompiledWithMetalSupport)
207 }
208
209 fn location(&self) -> crate::DeviceLocation {
210 fail!()
211 }
212
213 fn same_device(&self, _: &Self) -> bool {
214 fail!()
215 }
216
217 fn zeros_impl(&self, _shape: &Shape, _dtype: DType) -> Result<Self::Storage> {
218 Err(Error::NotCompiledWithMetalSupport)
219 }
220
221 fn ones_impl(&self, _shape: &Shape, _dtype: DType) -> Result<Self::Storage> {
222 Err(Error::NotCompiledWithMetalSupport)
223 }
224
225 unsafe fn alloc_uninit(&self, _shape: &Shape, _dtype: DType) -> Result<Self::Storage> {
226 Err(Error::NotCompiledWithMetalSupport)
227 }
228
229 fn storage_from_slice<T: crate::WithDType>(&self, _: &[T]) -> Result<Self::Storage> {
230 Err(Error::NotCompiledWithMetalSupport)
231 }
232
233 fn storage_from_cpu_storage(&self, _: &CpuStorage) -> Result<Self::Storage> {
234 Err(Error::NotCompiledWithMetalSupport)
235 }
236
237 fn storage_from_cpu_storage_owned(&self, _: CpuStorage) -> Result<Self::Storage> {
238 Err(Error::NotCompiledWithMetalSupport)
239 }
240
241 fn rand_uniform(&self, _: &Shape, _: DType, _: f64, _: f64) -> Result<Self::Storage> {
242 Err(Error::NotCompiledWithMetalSupport)
243 }
244
245 fn rand_normal(&self, _: &Shape, _: DType, _: f64, _: f64) -> Result<Self::Storage> {
246 Err(Error::NotCompiledWithMetalSupport)
247 }
248
249 fn synchronize(&self) -> Result<()> {
250 Ok(())
251 }
252}