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
//! Unary operations trait.
use crate::error::Result;
use crate::runtime::Runtime;
use crate::tensor::Tensor;
/// Unary operations
///
/// This trait defines element-wise unary operations on tensors.
/// Each operation is applied independently to each element.
///
/// The exponential, logarithm, trigonometric and hyperbolic methods promise no bit
/// reproducibility. Results can differ across tensor length, instruction set, runtime and
/// host libm. For reproducible F64 `exp`, `log` and `log1p`, use
/// [`CorrectlyRoundedOps`](super::CorrectlyRoundedOps).
pub trait UnaryOps<R: Runtime> {
// ===== Sign and Absolute =====
/// Negation: -a
fn neg(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Absolute value: |a|
fn abs(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Sign: returns -1 for negative, 0 for zero, 1 for positive
fn sign(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
// ===== Power and Root =====
/// Square root: sqrt(a)
///
/// Check [`Runtime::square_root_contract`] before requiring correctly rounded results.
fn sqrt(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Reciprocal square root: 1/sqrt(a) - critical for normalization layers
fn rsqrt(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Square: a²
fn square(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Cube root: cbrt(a)
fn cbrt(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Reciprocal: 1/a
fn recip(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
// ===== Exponential and Logarithmic =====
/// Exponential: e^a
///
/// No bit reproducibility across tensor length, instruction set, runtime or host libm.
/// On CPU, F64 input shorter than 32 elements uses the host libm. Longer input uses
/// numr's SIMD polynomial. CUDA uses the CUDA math library. For reproducible F64
/// results, use [`CorrectlyRoundedOps::cr_exp`](super::CorrectlyRoundedOps::cr_exp).
fn exp(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Base-2 exponential: 2^a
fn exp2(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Exponential minus 1: e^a - 1 (numerically stable for small a)
fn expm1(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Natural logarithm: ln(a)
///
/// No bit reproducibility across tensor length, instruction set, runtime or host libm.
/// On CPU, F64 input shorter than 32 elements uses the host libm. Longer input uses
/// numr's SIMD polynomial. CUDA uses the CUDA math library. For reproducible F64
/// results, use [`CorrectlyRoundedOps::cr_log`](super::CorrectlyRoundedOps::cr_log).
fn log(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Base-2 logarithm: log2(a)
fn log2(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Base-10 logarithm: log10(a)
fn log10(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Natural log of 1+a: ln(1+a) (numerically stable for small a)
///
/// No bit reproducibility across tensor length, instruction set, runtime or host libm.
/// For reproducible F64 results, use
/// [`CorrectlyRoundedOps::cr_log1p`](super::CorrectlyRoundedOps::cr_log1p).
fn log1p(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
// ===== Trigonometric =====
/// Sine: sin(a)
fn sin(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Cosine: cos(a)
fn cos(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Tangent: tan(a)
fn tan(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Arc sine (inverse sine): asin(a), domain [-1,1], range [-π/2, π/2]
fn asin(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Arc cosine (inverse cosine): acos(a), domain [-1,1], range [0, π]
///
/// No bit reproducibility across runtime or host libm. For reproducible F64 results, use
/// [`CorrectlyRoundedOps::cr_acos`](super::CorrectlyRoundedOps::cr_acos).
fn acos(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Arc tangent (inverse tangent): atan(a)
fn atan(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
// ===== Hyperbolic =====
/// Hyperbolic sine: sinh(a)
fn sinh(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Hyperbolic cosine: cosh(a)
fn cosh(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Hyperbolic tangent: tanh(a)
fn tanh(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Inverse hyperbolic sine: asinh(a)
fn asinh(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Inverse hyperbolic cosine: acosh(a), domain [1, ∞)
fn acosh(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Inverse hyperbolic tangent: atanh(a), domain (-1, 1)
fn atanh(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
// ===== Rounding =====
/// Floor: floor(a)
fn floor(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Ceiling: ceil(a)
fn ceil(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Round to nearest integer, ties away from zero.
///
/// Delegates to Rust's `f32::round`/`f64::round`: 0.5 becomes 1.0, 2.5
/// becomes 3.0, and -2.5 becomes -3.0.
///
/// This is NOT what `np.round` and `torch.round` do — they break ties to
/// even. Use [`UnaryOps::round_ties_even`] to match them.
fn round(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Round to nearest integer, ties to even (IEEE 754 roundTiesToEven).
///
/// Delegates to Rust's `f32::round_ties_even`/`f64::round_ties_even`: 0.5
/// becomes 0.0, 2.5 becomes 2.0, and 3.5 becomes 4.0. This matches
/// `np.round` and `torch.round` element for element.
///
/// Use [`UnaryOps::round`] for half-away-from-zero rounding.
fn round_ties_even(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Truncate toward zero: trunc(a)
fn trunc(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
// ===== Special Checks =====
/// Check for NaN values: returns U8 tensor (1 if NaN, 0 otherwise)
fn isnan(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
/// Check for Inf values: returns U8 tensor (1 if Inf, 0 otherwise)
fn isinf(&self, a: &Tensor<R>) -> Result<Tensor<R>>;
}