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
//! Arithmetic operations helpers
//!
//! This module contains helper types and functions for arithmetic operations.
//! The actual operations are defined in the `TensorOps` trait.
/// Compute the output shape for binary operations with broadcasting
///
/// Returns None if the shapes are incompatible for broadcasting.
pub fn broadcast_shape(a: &[usize], b: &[usize]) -> Option<Vec<usize>> {
if a == b {
return Some(a.to_vec());
}
let max_ndim = a.len().max(b.len());
let mut result = vec![1usize; max_ndim];
// Iterate from right to left, writing each output dim in place
for i in 0..max_ndim {
let a_dim = if i < a.len() { a[a.len() - 1 - i] } else { 1 };
let b_dim = if i < b.len() { b[b.len() - 1 - i] } else { 1 };
result[max_ndim - 1 - i] = if a_dim == b_dim || b_dim == 1 {
a_dim
} else if a_dim == 1 {
b_dim
} else {
return None; // Incompatible shapes
};
}
Some(result)
}
/// Binary operation kind
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum BinaryOp {
/// Addition: a + b
Add,
/// Subtraction: a - b
Sub,
/// Multiplication: a * b
Mul,
/// Division: a / b
Div,
/// Power: a^b
Pow,
/// Maximum: max(a, b)
Max,
/// Minimum: min(a, b)
Min,
/// Two-argument arctangent: atan2(y, x) - angle in radians
Atan2,
}
/// Unary operation kind
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum UnaryOp {
// === Sign and Absolute ===
/// Negation: -a
Neg,
/// Absolute value: |a|
Abs,
/// Sign: returns -1 for negative, 0 for zero, 1 for positive
Sign,
// === Power and Root ===
/// Square root: sqrt(a)
Sqrt,
/// Reciprocal square root: 1/sqrt(a) - critical for normalization layers
Rsqrt,
/// Square: a^2
Square,
/// Cube root: cbrt(a)
Cbrt,
/// Reciprocal: 1/a
Recip,
// === Exponential and Logarithmic ===
/// Exponential: e^a
Exp,
/// Base-2 exponential: 2^a
Exp2,
/// Exponential minus 1: e^a - 1 (numerically stable for small a)
Expm1,
/// Natural log: ln(a)
Log,
/// Base-2 logarithm: log2(a)
Log2,
/// Base-10 logarithm: log10(a)
Log10,
/// Natural log of 1+a: ln(1+a) (numerically stable for small a)
Log1p,
// === Trigonometric ===
/// Sine: sin(a)
Sin,
/// Cosine: cos(a)
Cos,
/// Tangent: tan(a)
Tan,
/// Arc sine (inverse sine): asin(a), domain [-1,1], range [-π/2, π/2]
Asin,
/// Arc cosine (inverse cosine): acos(a), domain [-1,1], range [0, π]
Acos,
/// Arc tangent (inverse tangent): atan(a)
Atan,
// === Hyperbolic ===
/// Hyperbolic sine: sinh(a)
Sinh,
/// Hyperbolic cosine: cosh(a)
Cosh,
/// Hyperbolic tangent: tanh(a)
Tanh,
/// Inverse hyperbolic sine: asinh(a)
Asinh,
/// Inverse hyperbolic cosine: acosh(a), domain [1, ∞)
Acosh,
/// Inverse hyperbolic tangent: atanh(a), domain (-1, 1)
Atanh,
// === Rounding ===
/// Floor: floor(a)
Floor,
/// Ceiling: ceil(a)
Ceil,
/// Round to nearest, ties away from zero: round(a).
///
/// Matches Rust's `f32::round`/`f64::round`. A tie such as 2.5 goes to 3.0
/// and -2.5 goes to -3.0. For the IEEE roundTiesToEven behaviour that
/// `np.round` and `torch.round` use, see [`UnaryOp::RoundTiesEven`].
Round,
/// Round to nearest, ties to even: IEEE 754 roundTiesToEven.
///
/// Matches Rust's `f32::round_ties_even`/`f64::round_ties_even`, and the
/// semantics of `np.round` and `torch.round`. A tie such as 2.5 goes to 2.0
/// and 3.5 goes to 4.0.
RoundTiesEven,
/// Truncate toward zero: trunc(a)
Trunc,
}
/// Comparison operation kind
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum CompareOp {
/// Equal: a == b
Eq,
/// Not equal: a != b
Ne,
/// Less than: a < b
Lt,
/// Less than or equal: a <= b
Le,
/// Greater than: a > b
Gt,
/// Greater than or equal: a >= b
Ge,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_broadcast_shape() {
// Same shapes
assert_eq!(broadcast_shape(&[2, 3], &[2, 3]), Some(vec![2, 3]));
// Broadcasting with 1
assert_eq!(broadcast_shape(&[2, 3], &[1, 3]), Some(vec![2, 3]));
assert_eq!(broadcast_shape(&[2, 1], &[2, 3]), Some(vec![2, 3]));
// Different ranks
assert_eq!(broadcast_shape(&[3], &[2, 3]), Some(vec![2, 3]));
assert_eq!(broadcast_shape(&[2, 3], &[3]), Some(vec![2, 3]));
// Incompatible shapes
assert_eq!(broadcast_shape(&[2, 3], &[2, 4]), None);
assert_eq!(broadcast_shape(&[3], &[4]), None);
// Scalars and zero extents
assert_eq!(broadcast_shape(&[], &[]), Some(vec![]));
assert_eq!(broadcast_shape(&[], &[2, 3]), Some(vec![2, 3]));
assert_eq!(broadcast_shape(&[0, 1], &[1, 4]), Some(vec![0, 4]));
assert_eq!(broadcast_shape(&[1, 1], &[0]), Some(vec![1, 0]));
assert_eq!(broadcast_shape(&[2], &[0]), None);
}
}