zyx 0.14.0

Zyx machine learning library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! Graph node, each node is one operation. Nodes
//! represent the opset that is available on tensors.

use crate::{dtype::Constant, shape::Axis, tensor::TensorId, DType, Scalar};

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, bitcode::Encode, bitcode::Decode)]
pub(super) enum BOp {
    Add,
    Sub,
    Mul,
    Div,
    Pow,
    Cmplt,
    Cmpgt,
    Max,
    Or,
    And,
    BitXor,
    BitOr,
    BitAnd,
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, bitcode::Encode, bitcode::Decode)]
pub(super) enum UOp {
    Cast(DType),
    ReLU,
    Neg,
    Exp2,
    Log2,
    Inv,
    Sqrt,
    Sin,
    Cos,
    Not,
    Nonzero,
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy, bitcode::Encode, bitcode::Decode)]
pub(super) enum ROp {
    Sum,
    Max,
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub(super) enum Node {
    // Constant tensor baked into kernels
    Const {
        value: Constant,
    },
    // Tensor stored on device
    Leaf,
    Expand {
        x: TensorId,
    },
    Permute {
        x: TensorId,
        axes: Vec<Axis>,
    },
    // Reshape can be sometimes axis split or axis join
    Reshape {
        x: TensorId,
    },
    Pad {
        x: TensorId,
        padding: Vec<(isize, isize)>,
    },
    Reduce {
        x: TensorId,
        axes: Vec<Axis>,
        rop: ROp,
    },
    Unary {
        x: TensorId,
        uop: UOp,
    },
    Binary {
        x: TensorId,
        y: TensorId,
        bop: BOp,
    },
}

impl Default for Node {
    fn default() -> Self {
        Self::Const {
            value: Constant::Bool(false),
        }
    }
}

pub(super) struct NodeParametersIterator {
    parameters: [TensorId; 2],
    len: u8,
    idx: u8,
}

impl Iterator for NodeParametersIterator {
    type Item = TensorId;
    fn next(&mut self) -> Option<Self::Item> {
        if self.idx == self.len {
            return None;
        }
        let idx = self.idx;
        self.idx += 1;
        return Some(self.parameters[idx as usize]);
    }
}

impl Node {
    /// Get all parameters of self. This method does not allocate.
    pub const fn parameters(&self) -> impl Iterator<Item = TensorId> {
        return match self {
            Node::Const { .. } | Node::Leaf { .. } => NodeParametersIterator {
                parameters: [0, 0],
                idx: 0,
                len: 0,
            },
            Node::Unary { x, .. }
            | Node::Reshape { x, .. }
            | Node::Expand { x, .. }
            | Node::Permute { x, .. }
            | Node::Pad { x, .. }
            | Node::Reduce { x, .. } => NodeParametersIterator {
                parameters: [*x, 0],
                idx: 0,
                len: 1,
            },
            Node::Binary { x, y, .. } => NodeParametersIterator {
                parameters: [*x, *y],
                idx: 0,
                len: 2,
            },
        };
    }

    pub(super) fn is_movement(&self) -> bool {
        matches!(
            self,
            Node::Pad { .. } | Node::Reshape { .. } | Node::Expand { .. } | Node::Permute { .. }
        )
    }

    pub(super) fn is_unary(&self) -> bool {
        matches!(self, Node::Unary { .. })
    }
}

trait CastDType: Scalar {
    fn cast_dtype(self, dtype: DType) -> Constant {
        match dtype {
            #[cfg(feature = "half")]
            DType::F16 => Constant::F16(unsafe { std::mem::transmute(self.cast::<half::f16>()) }),
            #[cfg(feature = "half")]
            DType::BF16 => {
                Constant::BF16(unsafe { std::mem::transmute(self.cast::<half::bf16>()) })
            }
            DType::F32 => Constant::F32(unsafe { std::mem::transmute(self.cast::<f32>()) }),
            DType::F64 => Constant::F64(unsafe { std::mem::transmute(self.cast::<f64>()) }),
            #[cfg(feature = "complex")]
            DType::CF32 => todo!("Complex numbers"),
            #[cfg(feature = "complex")]
            DType::CF64 => todo!("Complex numbers"),
            DType::U8 => Constant::U8(self.cast()),
            DType::I8 => Constant::I8(self.cast()),
            DType::I16 => Constant::I16(self.cast()),
            DType::I32 => Constant::I32(self.cast()),
            DType::I64 => Constant::I64(self.cast()),
            DType::Bool => Constant::Bool(self.cast()),
        }
    }
}

impl<T: Scalar> CastDType for T {}

impl Constant {
    pub(super) fn unary(self, uop: UOp) -> Constant {
        use std::mem::transmute as t;
        match uop {
            UOp::Cast(dtype) => match self {
                #[cfg(feature = "half")]
                Constant::F16(x) => unsafe { t::<_, half::f16>(x) }.cast_dtype(dtype),
                #[cfg(feature = "half")]
                Constant::BF16(x) => unsafe { t::<_, half::bf16>(x) }.cast_dtype(dtype),
                Constant::F32(x) => unsafe { t::<_, f32>(x) }.cast_dtype(dtype),
                Constant::F64(x) => unsafe { t::<_, f64>(x) }.cast_dtype(dtype),
                #[cfg(feature = "complex")]
                Constant::CF32(..) => todo!("Complex numbers"),
                #[cfg(feature = "complex")]
                Constant::CF64(..) => todo!("Complex numbers"),
                Constant::U8(x) => x.cast_dtype(dtype),
                Constant::I8(x) => x.cast_dtype(dtype),
                Constant::I16(x) => x.cast_dtype(dtype),
                Constant::U32(_) => panic!(),
                Constant::I32(x) => x.cast_dtype(dtype),
                Constant::I64(x) => x.cast_dtype(dtype),
                Constant::Bool(x) => x.cast_dtype(dtype),
            },
            UOp::ReLU => match self {
                #[cfg(feature = "half")]
                Constant::F16(x) => Constant::F16(unsafe { t(t::<_, half::f16>(x).relu()) }),
                #[cfg(feature = "half")]
                Constant::BF16(x) => Constant::F16(unsafe { t(t::<_, half::bf16>(x).relu()) }),
                Constant::F32(x) => Constant::F32(unsafe { t(t::<_, f32>(x).relu()) }),
                Constant::F64(x) => Constant::F64(unsafe { t(t::<_, f64>(x).relu()) }),
                #[cfg(feature = "complex")]
                Constant::CF32(..) => todo!("Complex numbers"),
                #[cfg(feature = "complex")]
                Constant::CF64(..) => todo!("Complex numbers"),
                Constant::U8(x) => Constant::U8(x.relu()),
                Constant::I8(x) => Constant::I8(x.relu()),
                Constant::I16(x) => Constant::I16(x.relu()),
                Constant::U32(_) => panic!(),
                Constant::I32(x) => Constant::I32(x.relu()),
                Constant::I64(x) => Constant::I64(x.relu()),
                Constant::Bool(x) => Constant::Bool(x.relu()),
            },
            UOp::Neg => match self {
                #[cfg(feature = "half")]
                Constant::F16(x) => Constant::F16(unsafe { t(t::<_, half::f16>(x).neg()) }),
                #[cfg(feature = "half")]
                Constant::BF16(x) => Constant::F16(unsafe { t(t::<_, half::bf16>(x).neg()) }),
                Constant::F32(x) => Constant::F32(unsafe { t(t::<_, f32>(x).neg()) }),
                Constant::F64(x) => Constant::F64(unsafe { t(t::<_, f64>(x).neg()) }),
                #[cfg(feature = "complex")]
                Constant::CF32(..) => todo!("Complex numbers"),
                #[cfg(feature = "complex")]
                Constant::CF64(..) => todo!("Complex numbers"),
                Constant::U8(x) => Constant::U8(x.neg()),
                Constant::I8(x) => Constant::I8(x.neg()),
                Constant::I16(x) => Constant::I16(x.neg()),
                Constant::U32(_) => panic!(),
                Constant::I32(x) => Constant::I32(x.neg()),
                Constant::I64(x) => Constant::I64(x.neg()),
                Constant::Bool(x) => Constant::Bool(x.neg()),
            },
            UOp::Exp2 => match self {
                #[cfg(feature = "half")]
                Constant::F16(x) => Constant::F16(unsafe { t(t::<_, half::f16>(x).exp2()) }),
                #[cfg(feature = "half")]
                Constant::BF16(x) => Constant::F16(unsafe { t(t::<_, half::bf16>(x).exp2()) }),
                Constant::F32(x) => Constant::F32(unsafe { t(t::<_, f32>(x).exp2()) }),
                Constant::F64(x) => Constant::F64(unsafe { t(t::<_, f64>(x).exp2()) }),
                #[cfg(feature = "complex")]
                Constant::CF32(..) => todo!("Complex numbers"),
                #[cfg(feature = "complex")]
                Constant::CF64(..) => todo!("Complex numbers"),
                Constant::U8(x) => Constant::U8(2.pow(x)),
                Constant::I8(x) => Constant::I8(2.pow(x)),
                Constant::I16(x) => Constant::I16(2.pow(x)),
                Constant::U32(_) => panic!(),
                Constant::I32(x) => Constant::I32(2.pow(x)),
                Constant::I64(x) => Constant::I64(2.pow(x)),
                Constant::Bool(_) => todo!(),
            },
            UOp::Log2 => match self {
                #[cfg(feature = "half")]
                Constant::F16(x) => Constant::F16(unsafe { t(t::<_, half::f16>(x).log2()) }),
                #[cfg(feature = "half")]
                Constant::BF16(x) => Constant::F16(unsafe { t(t::<_, half::bf16>(x).log2()) }),
                Constant::F32(x) => Constant::F32(unsafe { t(t::<_, f32>(x).log2()) }),
                Constant::F64(x) => Constant::F64(unsafe { t(t::<_, f64>(x).log2()) }),
                #[cfg(feature = "complex")]
                Constant::CF32(..) => todo!("Complex numbers"),
                #[cfg(feature = "complex")]
                Constant::CF64(..) => todo!("Complex numbers"),
                Constant::U8(x) => Constant::U8(x.ilog2() as u8),
                Constant::I8(x) => Constant::I8(x.ilog2() as i8),
                Constant::I16(x) => Constant::I16(x.ilog2() as i16),
                Constant::U32(_) => panic!(),
                Constant::I32(x) => Constant::I32(x.ilog2() as i32),
                Constant::I64(x) => Constant::I64(x.ilog2() as i64),
                Constant::Bool(_) => todo!(),
            },
            UOp::Inv => match self {
                #[cfg(feature = "half")]
                Constant::F16(x) => {
                    Constant::F16(unsafe { t(half::f16::ONE / t::<_, half::f16>(x)) })
                }
                #[cfg(feature = "half")]
                Constant::BF16(x) => {
                    Constant::F16(unsafe { t(half::bf16::ONE / t::<_, half::bf16>(x)) })
                }
                Constant::F32(x) => Constant::F32(unsafe { t(1f32 / t::<_, f32>(x)) }),
                Constant::F64(x) => Constant::F64(unsafe { t(1f64 / t::<_, f64>(x)) }),
                #[cfg(feature = "complex")]
                Constant::CF32(..) => todo!("Complex numbers"),
                #[cfg(feature = "complex")]
                Constant::CF64(..) => todo!("Complex numbers"),
                Constant::U8(x) => Constant::U8(1 / x),
                Constant::I8(x) => Constant::I8(1 / x),
                Constant::I16(x) => Constant::I16(1 / x),
                Constant::U32(_) => panic!(),
                Constant::I32(x) => Constant::I32(1 / x),
                Constant::I64(x) => Constant::I64(1 / x),
                Constant::Bool(_) => todo!(),
            },
            UOp::Sqrt => match self {
                #[cfg(feature = "half")]
                Constant::F16(x) => Constant::F16(unsafe { t(t::<_, half::f16>(x).relu()) }),
                #[cfg(feature = "half")]
                Constant::BF16(x) => Constant::F16(unsafe { t(t::<_, half::bf16>(x).relu()) }),
                Constant::F32(x) => Constant::F32(unsafe { t(t::<_, f32>(x).sqrt()) }),
                Constant::F64(x) => Constant::F64(unsafe { t(t::<_, f64>(x).sqrt()) }),
                #[cfg(feature = "complex")]
                Constant::CF32(..) => todo!("Complex numbers"),
                #[cfg(feature = "complex")]
                Constant::CF64(..) => todo!("Complex numbers"),
                Constant::U8(x) => Constant::U8(x.sqrt()),
                Constant::I8(x) => Constant::I8(x.sqrt()),
                Constant::I16(x) => Constant::I16(x.sqrt()),
                Constant::U32(_) => panic!(),
                Constant::I32(x) => Constant::I32(x.sqrt()),
                Constant::I64(x) => Constant::I64(x.sqrt()),
                Constant::Bool(_) => todo!(),
            },
            UOp::Sin => match self {
                #[cfg(feature = "half")]
                Constant::F16(x) => Constant::F16(unsafe { t(t::<_, half::f16>(x).relu()) }),
                #[cfg(feature = "half")]
                Constant::BF16(x) => Constant::F16(unsafe { t(t::<_, half::bf16>(x).relu()) }),
                Constant::F32(x) => Constant::F32(unsafe { t(t::<_, f32>(x).sin()) }),
                Constant::F64(x) => Constant::F64(unsafe { t(t::<_, f64>(x).sin()) }),
                #[cfg(feature = "complex")]
                Constant::CF32(..) => todo!("Complex numbers"),
                #[cfg(feature = "complex")]
                Constant::CF64(..) => todo!("Complex numbers"),
                Constant::U8(x) => Constant::U8(x.sin()),
                Constant::I8(x) => Constant::I8(x.sin()),
                Constant::I16(x) => Constant::I16(x.sin()),
                Constant::U32(_) => panic!(),
                Constant::I32(x) => Constant::I32(x.sin()),
                Constant::I64(x) => Constant::I64(x.sin()),
                Constant::Bool(_) => todo!(),
            },
            UOp::Cos => match self {
                #[cfg(feature = "half")]
                Constant::F16(x) => Constant::F16(unsafe { t(t::<_, half::f16>(x).relu()) }),
                #[cfg(feature = "half")]
                Constant::BF16(x) => Constant::F16(unsafe { t(t::<_, half::bf16>(x).relu()) }),
                Constant::F32(x) => Constant::F32(unsafe { t(t::<_, f32>(x).cos()) }),
                Constant::F64(x) => Constant::F64(unsafe { t(t::<_, f64>(x).cos()) }),
                #[cfg(feature = "complex")]
                Constant::CF32(..) => todo!("Complex numbers"),
                #[cfg(feature = "complex")]
                Constant::CF64(..) => todo!("Complex numbers"),
                Constant::U8(x) => Constant::U8(x.cos()),
                Constant::I8(x) => Constant::I8(x.cos()),
                Constant::I16(x) => Constant::I16(x.cos()),
                Constant::U32(_) => panic!(),
                Constant::I32(x) => Constant::I32(x.cos()),
                Constant::I64(x) => Constant::I64(x.cos()),
                Constant::Bool(_) => todo!(),
            },
            UOp::Not => match self {
                #[cfg(feature = "half")]
                Constant::F16(x) => Constant::F16(unsafe { t(t::<_, half::f16>(x).relu()) }),
                #[cfg(feature = "half")]
                Constant::BF16(x) => Constant::F16(unsafe { t(t::<_, half::bf16>(x).relu()) }),
                Constant::F32(x) => {
                    Constant::F32(unsafe { t(if t::<_, f32>(x) == 0f32 { 1f32 } else { 0f32 }) })
                }
                Constant::F64(x) => {
                    Constant::F64(unsafe { t(if t::<_, f64>(x) == 0f64 { 1f64 } else { 0f64 }) })
                }
                #[cfg(feature = "complex")]
                Constant::CF32(..) => todo!("Complex numbers"),
                #[cfg(feature = "complex")]
                Constant::CF64(..) => todo!("Complex numbers"),
                Constant::U8(x) => Constant::U8(!x),
                Constant::I8(x) => Constant::I8(!x),
                Constant::I16(x) => Constant::I16(!x),
                Constant::U32(_) => panic!(),
                Constant::I32(x) => Constant::I32(!x),
                Constant::I64(x) => Constant::I64(!x),
                Constant::Bool(_) => todo!(),
            },
            UOp::Nonzero => match self {
                #[cfg(feature = "half")]
                Constant::F16(x) => Constant::F16(unsafe { t(t::<_, half::f16>(x).relu()) }),
                #[cfg(feature = "half")]
                Constant::BF16(x) => Constant::F16(unsafe { t(t::<_, half::bf16>(x).relu()) }),
                Constant::F32(x) => {
                    Constant::F32(unsafe { t((t::<_, f32>(x) != 0.) as i32 as f32) })
                }
                Constant::F64(x) => {
                    Constant::F64(unsafe { t((t::<_, f64>(x) != 0.) as i64 as f64) })
                }
                #[cfg(feature = "complex")]
                Constant::CF32(..) => todo!("Complex numbers"),
                #[cfg(feature = "complex")]
                Constant::CF64(..) => todo!("Complex numbers"),
                Constant::U8(x) => Constant::U8((x != 0) as u8),
                Constant::I8(x) => Constant::I8((x != 0) as i8),
                Constant::I16(x) => Constant::I16((x != 0) as i16),
                Constant::U32(_) => panic!(),
                Constant::I32(x) => Constant::I32((x != 0) as i32),
                Constant::I64(x) => Constant::I64((x != 0) as i64),
                Constant::Bool(_) => todo!(),
            },
        }
    }

    // TODO binary constant evaluation
    // Assumes both constants are the same dtype
    //pub(super) fn binary(x: Constant, y: Constant, bop: BOp) -> Constant { todo!() }
}