ronn-core 0.1.0

Core runtime engine for RONN - fundamental tensor operations, data types, and session management
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
//! Arithmetic tensor operations with broadcasting support.
//!
//! This module provides element-wise arithmetic operations (Add, Sub, Mul, Div)
//! with full broadcasting support using the Candle backend.

use crate::tensor::Tensor;
use anyhow::{Result, anyhow};

/// Trait for arithmetic operations on tensors.
pub trait ArithmeticOps {
    /// Element-wise addition with broadcasting.
    fn add(&self, other: &Tensor) -> Result<Tensor>;

    /// Element-wise subtraction with broadcasting.
    fn sub(&self, other: &Tensor) -> Result<Tensor>;

    /// Element-wise multiplication with broadcasting.
    fn mul(&self, other: &Tensor) -> Result<Tensor>;

    /// Element-wise division with broadcasting.
    fn div(&self, other: &Tensor) -> Result<Tensor>;

    /// Add a scalar to all elements.
    fn add_scalar(&self, scalar: f32) -> Result<Tensor>;

    /// Subtract a scalar from all elements.
    fn sub_scalar(&self, scalar: f32) -> Result<Tensor>;

    /// Multiply all elements by a scalar.
    fn mul_scalar(&self, scalar: f32) -> Result<Tensor>;

    /// Divide all elements by a scalar.
    fn div_scalar(&self, scalar: f32) -> Result<Tensor>;

    /// Element-wise negation.
    fn neg(&self) -> Result<Tensor>;

    /// Element-wise absolute value.
    fn abs(&self) -> Result<Tensor>;

    /// Element-wise power operation.
    fn pow(&self, exponent: f32) -> Result<Tensor>;

    /// Element-wise square root.
    fn sqrt(&self) -> Result<Tensor>;

    /// Element-wise exponential.
    fn exp(&self) -> Result<Tensor>;

    /// Element-wise natural logarithm.
    fn log(&self) -> Result<Tensor>;
}

impl ArithmeticOps for Tensor {
    fn add(&self, other: &Tensor) -> Result<Tensor> {
        // Check if tensors can be broadcast together
        if !self.is_broadcastable_with(other) {
            return Err(anyhow!(
                "Cannot broadcast tensors with shapes {:?} and {:?}",
                self.shape(),
                other.shape()
            ));
        }

        let result_candle = self.candle_tensor().broadcast_add(other.candle_tensor())?;

        Ok(Tensor::from_candle(
            result_candle,
            self.dtype(),
            self.layout(),
        ))
    }

    fn sub(&self, other: &Tensor) -> Result<Tensor> {
        if !self.is_broadcastable_with(other) {
            return Err(anyhow!(
                "Cannot broadcast tensors with shapes {:?} and {:?}",
                self.shape(),
                other.shape()
            ));
        }

        let result_candle = self.candle_tensor().broadcast_sub(other.candle_tensor())?;

        Ok(Tensor::from_candle(
            result_candle,
            self.dtype(),
            self.layout(),
        ))
    }

    fn mul(&self, other: &Tensor) -> Result<Tensor> {
        if !self.is_broadcastable_with(other) {
            return Err(anyhow!(
                "Cannot broadcast tensors with shapes {:?} and {:?}",
                self.shape(),
                other.shape()
            ));
        }

        let result_candle = self.candle_tensor().broadcast_mul(other.candle_tensor())?;

        Ok(Tensor::from_candle(
            result_candle,
            self.dtype(),
            self.layout(),
        ))
    }

    fn div(&self, other: &Tensor) -> Result<Tensor> {
        if !self.is_broadcastable_with(other) {
            return Err(anyhow!(
                "Cannot broadcast tensors with shapes {:?} and {:?}",
                self.shape(),
                other.shape()
            ));
        }

        let result_candle = self.candle_tensor().broadcast_div(other.candle_tensor())?;

        Ok(Tensor::from_candle(
            result_candle,
            self.dtype(),
            self.layout(),
        ))
    }

    fn add_scalar(&self, scalar: f32) -> Result<Tensor> {
        let result_candle = (self.candle_tensor() + scalar as f64)?;

        Ok(Tensor::from_candle(
            result_candle,
            self.dtype(),
            self.layout(),
        ))
    }

    fn sub_scalar(&self, scalar: f32) -> Result<Tensor> {
        let result_candle = (self.candle_tensor() - scalar as f64)?;

        Ok(Tensor::from_candle(
            result_candle,
            self.dtype(),
            self.layout(),
        ))
    }

    fn mul_scalar(&self, scalar: f32) -> Result<Tensor> {
        let result_candle = (self.candle_tensor() * scalar as f64)?;

        Ok(Tensor::from_candle(
            result_candle,
            self.dtype(),
            self.layout(),
        ))
    }

    fn div_scalar(&self, scalar: f32) -> Result<Tensor> {
        if scalar == 0.0 {
            return Err(anyhow!("Division by zero"));
        }

        let result_candle = (self.candle_tensor() / scalar as f64)?;

        Ok(Tensor::from_candle(
            result_candle,
            self.dtype(),
            self.layout(),
        ))
    }

    fn neg(&self) -> Result<Tensor> {
        let result_candle = self.candle_tensor().neg()?;

        Ok(Tensor::from_candle(
            result_candle,
            self.dtype(),
            self.layout(),
        ))
    }

    fn abs(&self) -> Result<Tensor> {
        let result_candle = self.candle_tensor().abs()?;

        Ok(Tensor::from_candle(
            result_candle,
            self.dtype(),
            self.layout(),
        ))
    }

    fn pow(&self, exponent: f32) -> Result<Tensor> {
        let result_candle = self.candle_tensor().powf(exponent as f64)?;

        Ok(Tensor::from_candle(
            result_candle,
            self.dtype(),
            self.layout(),
        ))
    }

    fn sqrt(&self) -> Result<Tensor> {
        let result_candle = self.candle_tensor().sqrt()?;

        Ok(Tensor::from_candle(
            result_candle,
            self.dtype(),
            self.layout(),
        ))
    }

    fn exp(&self) -> Result<Tensor> {
        let result_candle = self.candle_tensor().exp()?;

        Ok(Tensor::from_candle(
            result_candle,
            self.dtype(),
            self.layout(),
        ))
    }

    fn log(&self) -> Result<Tensor> {
        let result_candle = self.candle_tensor().log()?;

        Ok(Tensor::from_candle(
            result_candle,
            self.dtype(),
            self.layout(),
        ))
    }
}

/// Convenience functions for arithmetic operations.
impl Tensor {
    /// Clamp tensor values between min and max.
    pub fn clamp(&self, min: f32, max: f32) -> Result<Tensor> {
        if min > max {
            return Err(anyhow!(
                "Min value {} is greater than max value {}",
                min,
                max
            ));
        }

        let result_candle = self.candle_tensor().clamp(min as f64, max as f64)?;

        Ok(Tensor::from_candle(
            result_candle,
            self.dtype(),
            self.layout(),
        ))
    }

    /// Apply ReLU activation function.
    pub fn relu(&self) -> Result<Tensor> {
        self.clamp(0.0, f32::INFINITY)
    }

    /// Apply Sigmoid activation function.
    pub fn sigmoid(&self) -> Result<Tensor> {
        // sigmoid(x) = 1 / (1 + exp(-x))
        let neg_x = self.neg()?;
        let exp_neg_x = neg_x.exp()?;
        let one = Tensor::ones(vec![1], self.dtype(), self.layout())?;
        let one_plus_exp = one.add(&exp_neg_x)?;
        one.div(&one_plus_exp)
    }

    /// Apply Tanh activation function.
    pub fn tanh(&self) -> Result<Tensor> {
        let result_candle = self.candle_tensor().tanh()?;

        Ok(Tensor::from_candle(
            result_candle,
            self.dtype(),
            self.layout(),
        ))
    }

    /// Apply GELU activation function.
    pub fn gelu(&self) -> Result<Tensor> {
        // GELU(x) = x * Φ(x) where Φ is the standard Gaussian CDF
        // Approximation: GELU(x) ≈ 0.5 * x * (1 + tanh(sqrt(2/π) * (x + 0.044715 * x³)))
        let x = self;
        let x_cubed = x.pow(3.0)?;
        let term1 = x_cubed.mul_scalar(0.044715)?;
        let term2 = x.add(&term1)?;
        let sqrt_2_over_pi = (2.0 / std::f32::consts::PI).sqrt();
        let term3 = term2.mul_scalar(sqrt_2_over_pi)?;
        let tanh_term = term3.tanh()?;
        let one = Tensor::ones(vec![1], self.dtype(), self.layout())?;
        let one_plus_tanh = one.add(&tanh_term)?;
        let half = Tensor::from_data(vec![0.5], vec![1], self.dtype(), self.layout())?;
        let result = x.mul(&half)?.mul(&one_plus_tanh)?;
        Ok(result)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::types::{DataType, TensorLayout};

    #[test]
    fn test_arithmetic_operations() -> Result<()> {
        let a = Tensor::from_data(
            vec![1.0, 2.0, 3.0, 4.0],
            vec![2, 2],
            DataType::F32,
            TensorLayout::RowMajor,
        )?;
        let b = Tensor::from_data(
            vec![2.0, 1.0, 1.0, 2.0],
            vec![2, 2],
            DataType::F32,
            TensorLayout::RowMajor,
        )?;

        // Test addition
        let sum = a.add(&b)?;
        let sum_data = sum.to_vec()?;
        assert_eq!(sum_data, vec![3.0, 3.0, 4.0, 6.0]);

        // Test subtraction
        let diff = a.sub(&b)?;
        let diff_data = diff.to_vec()?;
        assert_eq!(diff_data, vec![-1.0, 1.0, 2.0, 2.0]);

        // Test multiplication
        let product = a.mul(&b)?;
        let product_data = product.to_vec()?;
        assert_eq!(product_data, vec![2.0, 2.0, 3.0, 8.0]);

        // Test division
        let quotient = a.div(&b)?;
        let quotient_data = quotient.to_vec()?;
        assert_eq!(quotient_data, vec![0.5, 2.0, 3.0, 2.0]);

        Ok(())
    }

    #[test]
    fn test_scalar_operations() -> Result<()> {
        let a = Tensor::from_data(
            vec![1.0, 2.0, 3.0, 4.0],
            vec![2, 2],
            DataType::F32,
            TensorLayout::RowMajor,
        )?;

        // Test scalar addition
        let sum = a.add_scalar(5.0)?;
        let sum_data = sum.to_vec()?;
        assert_eq!(sum_data, vec![6.0, 7.0, 8.0, 9.0]);

        // Test scalar multiplication
        let product = a.mul_scalar(2.0)?;
        let product_data = product.to_vec()?;
        assert_eq!(product_data, vec![2.0, 4.0, 6.0, 8.0]);

        Ok(())
    }

    #[test]
    fn test_broadcasting() -> Result<()> {
        let a = Tensor::from_data(
            vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0],
            vec![2, 3],
            DataType::F32,
            TensorLayout::RowMajor,
        )?;
        let b = Tensor::from_data(
            vec![10.0, 20.0, 30.0],
            vec![3],
            DataType::F32,
            TensorLayout::RowMajor,
        )?;

        let sum = a.add(&b)?;
        let sum_data = sum.to_vec()?;
        assert_eq!(sum_data, vec![11.0, 22.0, 33.0, 14.0, 25.0, 36.0]);

        Ok(())
    }

    #[test]
    fn test_activation_functions() -> Result<()> {
        let a = Tensor::from_data(
            vec![-1.0, 0.0, 1.0, 2.0],
            vec![4],
            DataType::F32,
            TensorLayout::RowMajor,
        )?;

        // Test ReLU
        let relu_result = a.relu()?;
        let relu_data = relu_result.to_vec()?;
        assert_eq!(relu_data, vec![0.0, 0.0, 1.0, 2.0]);

        // Test absolute value
        let abs_result = a.abs()?;
        let abs_data = abs_result.to_vec()?;
        assert_eq!(abs_data, vec![1.0, 0.0, 1.0, 2.0]);

        // Test negation
        let neg_result = a.neg()?;
        let neg_data = neg_result.to_vec()?;
        assert_eq!(neg_data, vec![1.0, 0.0, -1.0, -2.0]);

        Ok(())
    }

    #[test]
    fn test_sigmoid() -> Result<()> {
        let x = Tensor::from_data(vec![0.0], vec![1], DataType::F32, TensorLayout::RowMajor)?;
        let sigmoid_result = x.sigmoid()?;
        let sigmoid_data = sigmoid_result.to_vec()?;

        // sigmoid(0) should be 0.5
        assert!((sigmoid_data[0] - 0.5).abs() < 1e-6);

        Ok(())
    }

    #[test]
    fn test_error_handling() {
        let a = Tensor::from_data(
            vec![1.0, 2.0],
            vec![2],
            DataType::F32,
            TensorLayout::RowMajor,
        )
        .unwrap();
        let b = Tensor::from_data(
            vec![1.0, 2.0, 3.0],
            vec![3],
            DataType::F32,
            TensorLayout::RowMajor,
        )
        .unwrap();

        // Should fail because shapes are not broadcastable
        assert!(a.add(&b).is_err());

        // Division by zero should fail
        assert!(a.div_scalar(0.0).is_err());

        // Invalid clamp should fail
        assert!(a.clamp(5.0, 1.0).is_err());
    }
}