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 const_set(&mut self, _: crate::scalar::Scalar, _: &Layout) -> Result<()> {
45 Err(Error::NotCompiledWithMetalSupport)
46 }
47
48 fn to_cpu_storage(&self) -> Result<CpuStorage> {
49 Err(Error::NotCompiledWithMetalSupport)
50 }
51
52 fn affine(&self, _: &Layout, _: f64, _: f64) -> Result<Self> {
53 Err(Error::NotCompiledWithMetalSupport)
54 }
55
56 fn powf(&self, _: &Layout, _: f64) -> Result<Self> {
57 Err(Error::NotCompiledWithMetalSupport)
58 }
59
60 fn elu(&self, _: &Layout, _: f64) -> Result<Self> {
61 Err(Error::NotCompiledWithMetalSupport)
62 }
63
64 fn reduce_op(&self, _: ReduceOp, _: &Layout, _: &[usize]) -> Result<Self> {
65 Err(Error::NotCompiledWithMetalSupport)
66 }
67
68 fn cmp(&self, _: CmpOp, _: &Self, _: &Layout, _: &Layout) -> Result<Self> {
69 Err(Error::NotCompiledWithMetalSupport)
70 }
71
72 fn to_dtype(&self, _: &Layout, _: DType) -> Result<Self> {
73 Err(Error::NotCompiledWithMetalSupport)
74 }
75
76 fn unary_impl<B: UnaryOpT>(&self, _: &Layout) -> Result<Self> {
77 Err(Error::NotCompiledWithMetalSupport)
78 }
79
80 fn binary_impl<B: BinaryOpT>(&self, _: &Self, _: &Layout, _: &Layout) -> Result<Self> {
81 Err(Error::NotCompiledWithMetalSupport)
82 }
83
84 fn where_cond(&self, _: &Layout, _: &Self, _: &Layout, _: &Self, _: &Layout) -> Result<Self> {
85 Err(Error::NotCompiledWithMetalSupport)
86 }
87
88 fn conv1d(
89 &self,
90 _: &Layout,
91 _: &Self,
92 _: &Layout,
93 _: &crate::conv::ParamsConv1D,
94 ) -> Result<Self> {
95 Err(Error::NotCompiledWithMetalSupport)
96 }
97
98 fn conv_transpose1d(
99 &self,
100 _l: &Layout,
101 _kernel: &Self,
102 _kernel_l: &Layout,
103 _params: &crate::conv::ParamsConvTranspose1D,
104 ) -> Result<Self> {
105 Err(Error::NotCompiledWithMetalSupport)
106 }
107
108 fn conv2d(
109 &self,
110 _: &Layout,
111 _: &Self,
112 _: &Layout,
113 _: &crate::conv::ParamsConv2D,
114 ) -> Result<Self> {
115 Err(Error::NotCompiledWithMetalSupport)
116 }
117
118 fn conv_transpose2d(
119 &self,
120 _l: &Layout,
121 _kernel: &Self,
122 _kernel_l: &Layout,
123 _params: &crate::conv::ParamsConvTranspose2D,
124 ) -> Result<Self> {
125 Err(Error::NotCompiledWithMetalSupport)
126 }
127
128 fn index_select(&self, _: &Self, _: &Layout, _: &Layout, _: usize) -> Result<Self> {
129 Err(Error::NotCompiledWithMetalSupport)
130 }
131 fn gather(&self, _: &Layout, _: &Self, _: &Layout, _: usize) -> Result<Self> {
132 Err(Error::NotCompiledWithMetalSupport)
133 }
134
135 fn scatter_set(
136 &mut self,
137 _: &Layout,
138 _: &Self,
139 _: &Layout,
140 _: &Self,
141 _: &Layout,
142 _: usize,
143 ) -> Result<()> {
144 Err(Error::NotCompiledWithMetalSupport)
145 }
146
147 fn scatter_add_set(
148 &mut self,
149 _: &Layout,
150 _: &Self,
151 _: &Layout,
152 _: &Self,
153 _: &Layout,
154 _: usize,
155 ) -> Result<()> {
156 Err(Error::NotCompiledWithMetalSupport)
157 }
158
159 fn index_add(
160 &self,
161 _: &Layout,
162 _: &Self,
163 _: &Layout,
164 _: &Self,
165 _: &Layout,
166 _: usize,
167 ) -> Result<Self> {
168 Err(Error::NotCompiledWithMetalSupport)
169 }
170
171 fn matmul(
172 &self,
173 _: &Self,
174 _: (usize, usize, usize, usize),
175 _: &Layout,
176 _: &Layout,
177 ) -> Result<Self> {
178 Err(Error::NotCompiledWithMetalSupport)
179 }
180
181 fn copy_strided_src(&self, _: &mut Self, _: usize, _: &Layout) -> Result<()> {
182 Err(Error::NotCompiledWithMetalSupport)
183 }
184
185 fn copy2d(
186 &self,
187 _: &mut Self,
188 _: usize,
189 _: usize,
190 _: usize,
191 _: usize,
192 _: usize,
193 _: usize,
194 ) -> Result<()> {
195 Err(Error::NotCompiledWithMetalSupport)
196 }
197
198 fn avg_pool2d(&self, _: &Layout, _: (usize, usize), _: (usize, usize)) -> Result<Self> {
199 Err(Error::NotCompiledWithMetalSupport)
200 }
201
202 fn max_pool2d(&self, _: &Layout, _: (usize, usize), _: (usize, usize)) -> Result<Self> {
203 Err(Error::NotCompiledWithMetalSupport)
204 }
205
206 fn upsample_nearest1d(&self, _: &Layout, _: usize) -> Result<Self> {
207 Err(Error::NotCompiledWithMetalSupport)
208 }
209
210 fn upsample_nearest2d(&self, _: &Layout, _: usize, _: usize) -> Result<Self> {
211 Err(Error::NotCompiledWithMetalSupport)
212 }
213}
214
215impl crate::backend::BackendDevice for MetalDevice {
216 type Storage = MetalStorage;
217 fn new(_: usize) -> Result<Self> {
218 Err(Error::NotCompiledWithMetalSupport)
219 }
220
221 fn set_seed(&self, _: u64) -> Result<()> {
222 Err(Error::NotCompiledWithMetalSupport)
223 }
224
225 fn location(&self) -> crate::DeviceLocation {
226 fail!()
227 }
228
229 fn same_device(&self, _: &Self) -> bool {
230 fail!()
231 }
232
233 fn zeros_impl(&self, _shape: &Shape, _dtype: DType) -> Result<Self::Storage> {
234 Err(Error::NotCompiledWithMetalSupport)
235 }
236
237 unsafe fn alloc_uninit(&self, _shape: &Shape, _dtype: DType) -> Result<Self::Storage> {
238 Err(Error::NotCompiledWithMetalSupport)
239 }
240
241 fn storage_from_slice<T: crate::WithDType>(&self, _: &[T]) -> Result<Self::Storage> {
242 Err(Error::NotCompiledWithMetalSupport)
243 }
244
245 fn storage_from_cpu_storage(&self, _: &CpuStorage) -> Result<Self::Storage> {
246 Err(Error::NotCompiledWithMetalSupport)
247 }
248
249 fn storage_from_cpu_storage_owned(&self, _: CpuStorage) -> Result<Self::Storage> {
250 Err(Error::NotCompiledWithMetalSupport)
251 }
252
253 fn rand_uniform(&self, _: &Shape, _: DType, _: f64, _: f64) -> Result<Self::Storage> {
254 Err(Error::NotCompiledWithMetalSupport)
255 }
256
257 fn rand_normal(&self, _: &Shape, _: DType, _: f64, _: f64) -> Result<Self::Storage> {
258 Err(Error::NotCompiledWithMetalSupport)
259 }
260
261 fn synchronize(&self) -> Result<()> {
262 Ok(())
263 }
264}