Skip to main content

tenferro_ops/
std_tensor_op.rs

1use std::hash::{Hash, Hasher};
2use std::sync::Arc;
3
4use computegraph::GraphOperation;
5use num_complex::{Complex32, Complex64};
6
7use crate::dim_expr::DimExpr;
8use crate::ext_op::{ext_op_eq, hash_extension, ExtensionOp};
9use crate::input_key::TensorInputKey;
10use tenferro_tensor::{
11    CompareDir, DType, DotGeneralConfig, GatherConfig, PadConfig, ScatterConfig, SliceConfig,
12    TensorScalar,
13};
14
15/// Scalar values that can be encoded as tensor constant operations.
16///
17/// # Examples
18///
19/// ```rust
20/// use tenferro_ops::std_tensor_op::ConstantScalar;
21///
22/// assert_eq!(1.0_f64.constant_bytes(), 1.0_f64.to_le_bytes().to_vec());
23/// ```
24pub trait ConstantScalar: TensorScalar + private::Sealed {
25    /// Encode the scalar value as little-endian constant bytes.
26    ///
27    /// # Examples
28    ///
29    /// ```rust
30    /// use tenferro_ops::std_tensor_op::ConstantScalar;
31    ///
32    /// assert_eq!(true.constant_bytes(), vec![1]);
33    /// ```
34    fn constant_bytes(self) -> Vec<u8>;
35}
36
37mod private {
38    pub trait Sealed {}
39
40    impl Sealed for f64 {}
41    impl Sealed for f32 {}
42    impl Sealed for i64 {}
43    impl Sealed for i32 {}
44    impl Sealed for bool {}
45    impl Sealed for num_complex::Complex64 {}
46    impl Sealed for num_complex::Complex32 {}
47}
48
49impl ConstantScalar for f64 {
50    fn constant_bytes(self) -> Vec<u8> {
51        self.to_le_bytes().to_vec()
52    }
53}
54
55impl ConstantScalar for f32 {
56    fn constant_bytes(self) -> Vec<u8> {
57        self.to_le_bytes().to_vec()
58    }
59}
60
61impl ConstantScalar for i64 {
62    fn constant_bytes(self) -> Vec<u8> {
63        self.to_le_bytes().to_vec()
64    }
65}
66
67impl ConstantScalar for i32 {
68    fn constant_bytes(self) -> Vec<u8> {
69        self.to_le_bytes().to_vec()
70    }
71}
72
73impl ConstantScalar for bool {
74    fn constant_bytes(self) -> Vec<u8> {
75        vec![u8::from(self)]
76    }
77}
78
79impl ConstantScalar for Complex64 {
80    fn constant_bytes(self) -> Vec<u8> {
81        let mut bytes = Vec::with_capacity(16);
82        bytes.extend_from_slice(&self.re.to_le_bytes());
83        bytes.extend_from_slice(&self.im.to_le_bytes());
84        bytes
85    }
86}
87
88impl ConstantScalar for Complex32 {
89    fn constant_bytes(self) -> Vec<u8> {
90        let mut bytes = Vec::with_capacity(8);
91        bytes.extend_from_slice(&self.re.to_le_bytes());
92        bytes.extend_from_slice(&self.im.to_le_bytes());
93        bytes
94    }
95}
96
97tenferro_core_ops::define_std_tensor_op!();
98
99impl StdTensorOp {
100    /// Create a scalar constant op from any supported tensor scalar.
101    ///
102    /// # Examples
103    ///
104    /// ```rust
105    /// use num_complex::Complex64;
106    /// use tenferro_ops::std_tensor_op::StdTensorOp;
107    /// use tenferro_tensor::DType;
108    ///
109    /// let real = StdTensorOp::constant(1.5_f64);
110    /// let complex = StdTensorOp::constant(Complex64::new(1.0, -2.0));
111    ///
112    /// assert!(matches!(real, StdTensorOp::Constant { dtype: DType::F64, .. }));
113    /// assert!(matches!(complex, StdTensorOp::Constant { dtype: DType::C64, .. }));
114    /// ```
115    pub fn constant<T: ConstantScalar>(value: T) -> Self {
116        Self::Constant {
117            dtype: T::dtype(),
118            bytes: value.constant_bytes(),
119        }
120    }
121}
122
123impl PartialEq for StdTensorOp {
124    fn eq(&self, other: &Self) -> bool {
125        if std::mem::discriminant(self) != std::mem::discriminant(other) {
126            return false;
127        }
128        match (self, other) {
129            (Self::Add, Self::Add)
130            | (Self::Sub, Self::Sub)
131            | (Self::Mul, Self::Mul)
132            | (Self::Neg, Self::Neg)
133            | (Self::Conj, Self::Conj)
134            | (Self::Div, Self::Div)
135            | (Self::Rem, Self::Rem)
136            | (Self::Abs, Self::Abs)
137            | (Self::Sign, Self::Sign)
138            | (Self::Maximum, Self::Maximum)
139            | (Self::Minimum, Self::Minimum)
140            | (Self::Select, Self::Select)
141            | (Self::Clamp, Self::Clamp)
142            | (Self::Exp, Self::Exp)
143            | (Self::Log, Self::Log)
144            | (Self::Sin, Self::Sin)
145            | (Self::Cos, Self::Cos)
146            | (Self::Tanh, Self::Tanh)
147            | (Self::Sqrt, Self::Sqrt)
148            | (Self::Rsqrt, Self::Rsqrt)
149            | (Self::Pow, Self::Pow)
150            | (Self::Expm1, Self::Expm1)
151            | (Self::Log1p, Self::Log1p)
152            | (Self::Erf, Self::Erf)
153            | (Self::DynamicUpdateSlice, Self::DynamicUpdateSlice) => true,
154            (Self::DotGeneral { config: a }, Self::DotGeneral { config: b }) => a == b,
155            (Self::Transpose { perm: a }, Self::Transpose { perm: b }) => a == b,
156            (Self::Reshape { to_shape: a }, Self::Reshape { to_shape: b }) => a == b,
157            (
158                Self::BroadcastInDim {
159                    shape: sa,
160                    dims: da,
161                },
162                Self::BroadcastInDim {
163                    shape: sb,
164                    dims: db,
165                },
166            ) => sa == sb && da == db,
167            (Self::Convert { from: fa, to: ta }, Self::Convert { from: fb, to: tb }) => {
168                fa == fb && ta == tb
169            }
170            (
171                Self::Constant {
172                    dtype: da,
173                    bytes: ba,
174                },
175                Self::Constant {
176                    dtype: db,
177                    bytes: bb,
178                },
179            ) => da == db && ba == bb,
180            (Self::ReduceSum { axes: a }, Self::ReduceSum { axes: b })
181            | (Self::ReduceSumSquares { axes: a }, Self::ReduceSumSquares { axes: b })
182            | (Self::ReduceProd { axes: a }, Self::ReduceProd { axes: b })
183            | (Self::ReduceMax { axes: a }, Self::ReduceMax { axes: b })
184            | (Self::ReduceMin { axes: a }, Self::ReduceMin { axes: b })
185            | (Self::Reverse { axes: a }, Self::Reverse { axes: b }) => a == b,
186            (Self::Compare(a), Self::Compare(b)) => a == b,
187            (
188                Self::ExtractDiag {
189                    axis_a: aa,
190                    axis_b: ba,
191                },
192                Self::ExtractDiag {
193                    axis_a: ab,
194                    axis_b: bb,
195                },
196            )
197            | (
198                Self::EmbedDiag {
199                    axis_a: aa,
200                    axis_b: ba,
201                },
202                Self::EmbedDiag {
203                    axis_a: ab,
204                    axis_b: bb,
205                },
206            ) => aa == ab && ba == bb,
207            (Self::Tril { k: a }, Self::Tril { k: b })
208            | (Self::Triu { k: a }, Self::Triu { k: b }) => a == b,
209            (Self::Gather(a), Self::Gather(b)) => a == b,
210            (
211                Self::GatherDynamicSliceSizes {
212                    offset_dims: oa,
213                    collapsed_slice_dims: ca,
214                    start_index_map: sa,
215                    index_vector_dim: ia,
216                    slice_sizes: za,
217                },
218                Self::GatherDynamicSliceSizes {
219                    offset_dims: ob,
220                    collapsed_slice_dims: cb,
221                    start_index_map: sb,
222                    index_vector_dim: ib,
223                    slice_sizes: zb,
224                },
225            ) => oa == ob && ca == cb && sa == sb && ia == ib && za == zb,
226            (Self::Scatter(a), Self::Scatter(b)) => a == b,
227            (Self::Slice(a), Self::Slice(b)) => a == b,
228            (Self::DynamicSlice { slice_sizes: a }, Self::DynamicSlice { slice_sizes: b }) => {
229                a == b
230            }
231            (Self::Pad(a), Self::Pad(b)) => a == b,
232            (
233                Self::Concatenate {
234                    axis: a,
235                    input_count: na,
236                },
237                Self::Concatenate {
238                    axis: b,
239                    input_count: nb,
240                },
241            ) => a == b && na == nb,
242            (Self::ShapeOf { axis: a }, Self::ShapeOf { axis: b })
243            | (Self::DynamicTruncate { axis: a }, Self::DynamicTruncate { axis: b })
244            | (Self::PadToMatch { axis: a }, Self::PadToMatch { axis: b }) => a == b,
245            (Self::Extension(a), Self::Extension(b)) => ext_op_eq(a.as_ref(), b.as_ref()),
246            _ => false,
247        }
248    }
249}
250
251impl Eq for StdTensorOp {}
252
253impl Hash for StdTensorOp {
254    fn hash<H: Hasher>(&self, state: &mut H) {
255        std::mem::discriminant(self).hash(state);
256        match self {
257            Self::Add
258            | Self::Sub
259            | Self::Mul
260            | Self::Neg
261            | Self::Conj
262            | Self::Div
263            | Self::Rem
264            | Self::Abs
265            | Self::Sign
266            | Self::Maximum
267            | Self::Minimum
268            | Self::Select
269            | Self::Clamp
270            | Self::Exp
271            | Self::Log
272            | Self::Sin
273            | Self::Cos
274            | Self::Tanh
275            | Self::Sqrt
276            | Self::Rsqrt
277            | Self::Pow
278            | Self::Expm1
279            | Self::Log1p
280            | Self::Erf => {}
281            Self::DotGeneral { config } => {
282                config.hash(state);
283            }
284            Self::Transpose { perm } => perm.hash(state),
285            Self::Reshape { to_shape } => {
286                to_shape.hash(state);
287            }
288            Self::BroadcastInDim { shape, dims } => {
289                shape.hash(state);
290                dims.hash(state);
291            }
292            Self::Convert { from, to } => {
293                from.hash(state);
294                to.hash(state);
295            }
296            Self::Constant { dtype, bytes } => {
297                dtype.hash(state);
298                bytes.hash(state);
299            }
300            Self::ReduceSum { axes } | Self::ReduceSumSquares { axes } => {
301                axes.hash(state);
302            }
303            Self::Compare(dir) => dir.hash(state),
304            Self::ExtractDiag { axis_a, axis_b } | Self::EmbedDiag { axis_a, axis_b } => {
305                axis_a.hash(state);
306                axis_b.hash(state);
307            }
308            Self::Tril { k } | Self::Triu { k } => k.hash(state),
309            Self::Gather(config) => config.hash(state),
310            Self::GatherDynamicSliceSizes {
311                offset_dims,
312                collapsed_slice_dims,
313                start_index_map,
314                index_vector_dim,
315                slice_sizes,
316            } => {
317                offset_dims.hash(state);
318                collapsed_slice_dims.hash(state);
319                start_index_map.hash(state);
320                index_vector_dim.hash(state);
321                slice_sizes.hash(state);
322            }
323            Self::Scatter(config) => config.hash(state),
324            Self::Slice(config) => config.hash(state),
325            Self::DynamicSlice { slice_sizes } => slice_sizes.hash(state),
326            Self::DynamicUpdateSlice => {}
327            Self::Pad(config) => config.hash(state),
328            Self::Concatenate { axis, input_count } => {
329                axis.hash(state);
330                input_count.hash(state);
331            }
332            Self::Reverse { axes } => axes.hash(state),
333            Self::ShapeOf { axis } | Self::DynamicTruncate { axis } | Self::PadToMatch { axis } => {
334                axis.hash(state)
335            }
336            Self::ReduceProd { axes } | Self::ReduceMax { axes } | Self::ReduceMin { axes } => {
337                axes.hash(state);
338            }
339            Self::Extension(op) => hash_extension(op.as_ref(), state),
340        }
341    }
342}
343
344fn n_inputs_from_dim_exprs(min_inputs: usize, exprs: &[&[DimExpr]]) -> usize {
345    let max_idx = exprs
346        .iter()
347        .flat_map(|exprs| exprs.iter())
348        .filter_map(DimExpr::max_input_idx)
349        .max()
350        .map_or(0, |max_idx| max_idx + 1);
351    max_idx.max(min_inputs)
352}
353
354impl GraphOperation for StdTensorOp {
355    // This graph is metadata-only; execution owns tensors in the runtime
356    // submission layer rather than as graph operands.
357    type Operand = ();
358    type Context = ();
359    type InputKey = TensorInputKey;
360
361    fn input_count(&self) -> usize {
362        match self {
363            Self::Add | Self::Sub | Self::Mul | Self::DotGeneral { .. } | Self::Gather(_) => 2,
364            Self::GatherDynamicSliceSizes { slice_sizes, .. } => {
365                n_inputs_from_dim_exprs(2, &[slice_sizes])
366            }
367            Self::Neg
368            | Self::Conj
369            | Self::Transpose { .. }
370            | Self::Convert { .. }
371            | Self::ExtractDiag { .. }
372            | Self::EmbedDiag { .. }
373            | Self::Tril { .. }
374            | Self::Triu { .. }
375            | Self::Slice(_)
376            | Self::Pad(_)
377            | Self::Reverse { .. }
378            | Self::ShapeOf { .. } => 1,
379            Self::DynamicTruncate { .. } | Self::PadToMatch { .. } => 2,
380            Self::Reshape { to_shape } => n_inputs_from_dim_exprs(1, &[to_shape]),
381            Self::BroadcastInDim { shape, .. } => n_inputs_from_dim_exprs(1, &[shape]),
382            Self::ReduceSum { .. }
383            | Self::ReduceSumSquares { .. }
384            | Self::ReduceProd { .. }
385            | Self::ReduceMax { .. }
386            | Self::ReduceMin { .. } => 1,
387            Self::Div
388            | Self::Rem
389            | Self::Maximum
390            | Self::Minimum
391            | Self::Pow
392            | Self::DynamicSlice { .. } => 2,
393            Self::Constant { .. } => 0,
394            Self::Scatter(_) | Self::DynamicUpdateSlice => 3,
395            Self::Concatenate { input_count, .. } => *input_count,
396            Self::Abs
397            | Self::Sign
398            | Self::Exp
399            | Self::Log
400            | Self::Sin
401            | Self::Cos
402            | Self::Tanh
403            | Self::Sqrt
404            | Self::Rsqrt
405            | Self::Expm1
406            | Self::Log1p
407            | Self::Erf => 1,
408            Self::Select | Self::Clamp => 3,
409            Self::Compare(_) => 2,
410            Self::Extension(op) => ExtensionOp::input_count(op.as_ref()),
411        }
412    }
413
414    fn output_count(&self) -> usize {
415        match self {
416            Self::Add
417            | Self::Sub
418            | Self::Mul
419            | Self::Neg
420            | Self::Conj
421            | Self::DotGeneral { .. }
422            | Self::Transpose { .. }
423            | Self::Reshape { .. }
424            | Self::BroadcastInDim { .. }
425            | Self::Convert { .. }
426            | Self::ReduceSum { .. }
427            | Self::ReduceSumSquares { .. }
428            | Self::Div
429            | Self::Rem
430            | Self::Abs
431            | Self::Sign
432            | Self::Maximum
433            | Self::Minimum
434            | Self::Compare(_)
435            | Self::Select
436            | Self::Clamp
437            | Self::Constant { .. }
438            | Self::Exp
439            | Self::Log
440            | Self::Sin
441            | Self::Cos
442            | Self::Tanh
443            | Self::Sqrt
444            | Self::Rsqrt
445            | Self::Pow
446            | Self::Expm1
447            | Self::Log1p
448            | Self::Erf
449            | Self::ExtractDiag { .. }
450            | Self::EmbedDiag { .. }
451            | Self::Tril { .. }
452            | Self::Triu { .. }
453            | Self::Gather(_)
454            | Self::GatherDynamicSliceSizes { .. }
455            | Self::Scatter(_)
456            | Self::Slice(_)
457            | Self::DynamicSlice { .. }
458            | Self::DynamicUpdateSlice
459            | Self::Pad(_)
460            | Self::Reverse { .. }
461            | Self::ShapeOf { .. }
462            | Self::DynamicTruncate { .. }
463            | Self::PadToMatch { .. }
464            | Self::ReduceProd { .. }
465            | Self::ReduceMax { .. }
466            | Self::ReduceMin { .. } => 1,
467            Self::Concatenate { .. } => 1,
468            Self::Extension(op) => ExtensionOp::output_count(op.as_ref()),
469        }
470    }
471}