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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
use crate::FloatDType;
use crate::Tensor;
use crate::cast::ToElement;
use crate::quantization::{QuantScheme, QuantizationParameters};
use crate::tensor::backend::Backend;
use crate::tensor::stats;
use crate::tensor::{Distribution, TensorData};
use crate::{Int, TensorPrimitive};
use super::Bool;
/// Default RTOL value for `is_close` and `all_close`.
pub const DEFAULT_RTOL: f64 = 1e-5;
/// Default ATOL value for `is_close` and `all_close`.
pub const DEFAULT_ATOL: f64 = 1e-8;
impl<const D: usize, B> Tensor<B, D>
where
B: Backend,
{
/// Applies element wise exponential operation.
///
/// $y_i = e^{x_i}$
pub fn exp(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_exp(
self.primitive.tensor(),
)))
}
/// Applies element wise natural log operation *ln*.
///
/// $y_i = \log_e\(x_i\)$
pub fn log(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_log(
self.primitive.tensor(),
)))
}
/// Applies the natural logarithm of one plus the input tensor, element-wise.
///
/// $y_i = \log_e\(x_i + 1\)$
pub fn log1p(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_log1p(
self.primitive.tensor(),
)))
}
/// Applies the [error function](https://en.wikipedia.org/wiki/Error_function) element wise.
///
/// $y_i = \text{erf}\(x_i\)$
///
/// The error function is defined as:
///
/// $$\text{erf}\(x\) = \frac{2}{\sqrt{\pi}} \int_0^x e^{-t^2} dt$$
pub fn erf(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_erf(
self.primitive.tensor(),
)))
}
/// Applies [reciprocal operation](https://en.wikipedia.org/wiki/Multiplicative_inverse)
/// (or multiplicative inverse) element wise.
///
/// $y_i = \frac{1}{x_i}$
pub fn recip(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_recip(
self.primitive.tensor(),
)))
}
/// Applies element wise root square operation.
///
/// $y_i = \sqrt{x_i}$
pub fn sqrt(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_sqrt(
self.primitive.tensor(),
)))
}
/// Applies element wise cosine operation.
///
/// $y_i = \cos\(x_i\)$
pub fn cos(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_cos(
self.primitive.tensor(),
)))
}
/// Applies element wise sine operation.
///
/// $y_i = \sin\(x_i\)$
pub fn sin(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_sin(
self.primitive.tensor(),
)))
}
/// Applies element wise tangent operation.
///
/// $y_i = \tan\(x_i\)$
pub fn tan(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_tan(
self.primitive.tensor(),
)))
}
/// Applies element wise hyperbolic cosine operation.
///
/// $y_i = \cosh\(x_i\)$
///
/// # Example
///
/// ```rust
/// use burn_tensor::backend::Backend;
/// use burn_tensor::Tensor;
///
/// fn example<B: Backend>() {
/// let device = Default::default();
///
/// let tensor = Tensor::<B, 3>::from_data([0.0, -1.0, 2.0], &device);
/// println!("{}", tensor.cosh()); // [1.0, 1.5430, 3.7621]
/// }
/// ```
pub fn cosh(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_cosh(
self.primitive.tensor(),
)))
}
/// Applies element wise hyperbolic sine operation.
///
/// $y_i = \sinh\(x_i\)$
///
/// # Example
///
/// ```rust
/// use burn_tensor::backend::Backend;
/// use burn_tensor::Tensor;
///
/// fn example<B: Backend>() {
/// let device = Default::default();
///
/// let tensor = Tensor::<B, 3>::from_data([0.0, -1.0, 2.0], &device);
/// println!("{}", tensor.sinh()); // [0.0, -1.1752, 3.6269]
/// }
/// ```
pub fn sinh(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_sinh(
self.primitive.tensor(),
)))
}
/// Applies element wise hyperbolic tangent operation.
///
/// $y_i = \tanh\(x_i\)$
///
/// # Example
///
/// ```rust
/// use burn_tensor::backend::Backend;
/// use burn_tensor::Tensor;
///
/// fn example<B: Backend>() {
/// let device = Default::default();
///
/// let tensor = Tensor::<B, 3>::from_data([0.0, -1.0, 2.0], &device);
/// println!("{}", tensor.sinh()); // [0.0, -0.7616, 0.9640]
/// }
/// ```
pub fn tanh(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_tanh(
self.primitive.tensor(),
)))
}
/// Applies element wise round operation.
///
/// This function implements the [round half to even](https://en.wikipedia.org/wiki/Rounding#Rounding_half_to_even)
/// strategy, with halfway cases rounded to the nearest even integer value.
pub fn round(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_round(
self.primitive.tensor(),
)))
}
/// Applies element wise floor operation.
pub fn floor(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_floor(
self.primitive.tensor(),
)))
}
/// Applies element wise ceil operation.
pub fn ceil(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_ceil(
self.primitive.tensor(),
)))
}
/// Create a tensor from floats (f32) on a given device.
///
/// # Example
///
/// ```rust
/// use burn_tensor::backend::Backend;
/// use burn_tensor::Tensor;
///
/// fn example<B: Backend>() {
/// let device = B::Device::default();
/// let _ = Tensor::<B, 1>::from_floats([1.0, 2.0], &device);
/// let _ = Tensor::<B, 2>::from_floats([[1.0, 2.0], [3.0, 4.0]], &device);
/// }
/// ```
pub fn from_floats<A: Into<TensorData>>(floats: A, device: &B::Device) -> Self {
Self::from_data(floats.into().convert::<f32>(), device)
}
/// Returns a new tensor with the same shape and device as the current tensor and the data
/// cast to Integer.
///
/// # Example
///
/// ```rust
/// use burn_tensor::backend::Backend;
/// use burn_tensor::Tensor;
///
/// fn example<B: Backend>() {
/// let device = Default::default();
/// let float_tensor = Tensor::<B, 1>::from_floats([1.0, 2.0], &device);
/// let int_tensor = float_tensor.int();
/// }
/// ```
pub fn int(self) -> Tensor<B, D, Int> {
Tensor::new(B::float_into_int(self.primitive.tensor()))
}
/// Returns a new tensor with the same shape and device as the current tensor filled random
/// values sampled from the given distribution.
pub fn random_like(&self, distribution: Distribution) -> Self {
Tensor::new(TensorPrimitive::Float(B::float_random(
self.shape(),
distribution,
&self.device(),
)))
}
/// Calculate the variance along the given dimension.
pub fn var(self, dim: usize) -> Self {
stats::var(self, dim)
}
/// Calculate the variance along the given dimension without applying the Bessel’s correction.
pub fn var_bias(self, dim: usize) -> Self {
stats::var_bias(self, dim)
}
/// Calculate the variance along the given dimension and also returns the mean.
pub fn var_mean(self, dim: usize) -> (Self, Self) {
let mean = self.clone().mean_dim(dim);
let var = stats::var_with_mean(self, mean.clone(), dim);
(var, mean)
}
/// Calculate the variance along the given dimension without applying the Bessel’s correction and also returns the mean.
pub fn var_mean_bias(self, dim: usize) -> (Self, Self) {
let mean = self.clone().mean_dim(dim);
let var = stats::var_with_mean_bias(self, mean.clone(), dim);
(var, mean)
}
/// Converts a tensor to the specified floating point data type.
///
/// # Warning
/// Most backends don't have automatic type promotion at this time, so make sure that all tensors
/// have the same floating point precision data type for operations multiple input tensors (e.g., binary ops).
pub fn cast<F: Into<FloatDType>>(self, dtype: F) -> Tensor<B, D> {
Tensor::new(TensorPrimitive::Float(B::float_cast(
self.primitive.tensor(),
dtype.into(),
)))
}
/// Detach the current tensor from the autodiff graph.
///
/// This function does nothing when autodiff is not enabled.
/// This can be used in batchers or elsewhere to ensure that previous operations are not
/// considered in the autodiff graph.
pub fn detach(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_detach(
self.primitive.tensor(),
)))
}
/// Mark the tensor to keep gradients during the backward pass.
///
/// This function does nothing when autodiff is not enabled.
pub fn require_grad(self) -> Self {
self.set_require_grad(true)
}
/// Returns true if the tensor requires gradients during the backward pass.
pub fn is_require_grad(&self) -> bool {
match &self.primitive {
TensorPrimitive::Float(tensor) => B::float_is_require_grad(tensor),
TensorPrimitive::QFloat(tensor) => B::q_is_require_grad(tensor),
}
}
/// Mark the tensor as tracked or untracked depending on the require_grad argument.
/// When tracked, the gradients will be available after the backward pass.
///
/// This function does nothing when autodiff is not enabled.
pub fn set_require_grad(self, require_grad: bool) -> Self {
let primitive = match self.primitive {
TensorPrimitive::Float(tensor) => {
TensorPrimitive::Float(B::float_set_require_grad(tensor, require_grad))
}
TensorPrimitive::QFloat(tensor) => {
TensorPrimitive::QFloat(B::q_set_require_grad(tensor, require_grad))
}
};
Self::new(primitive)
}
/// Applies the relu function to the tensor.
pub(crate) fn relu(self) -> Self {
Self::new(TensorPrimitive::Float(B::relu(self.primitive.tensor())))
}
/// Calculate covaraince matrix between different entries alongside a given dimension.
///
/// # Arguments
///
/// * `size` - The size of the square matrix.
/// * `correction_factor` - Is usually 1 for samples and 0 for population.
pub fn cov(self, dim: usize, correction_factor: usize) -> Tensor<B, D> {
let n = self.dims()[dim];
let centered = (self.clone() - self.mean_dim(dim)).swap_dims(dim, 0);
centered
.clone()
.transpose()
.matmul(centered)
.div_scalar(n as f32 - correction_factor as f32)
}
/// Convert the tensor to a lower precision data type based on the quantization scheme.
///
/// # Arguments
///
/// * `scheme` - The quantization scheme.
/// * `qparams` - The pre-computed quantization parameters.
///
/// # Returns
///
/// The quantized tensor.
pub fn quantize(
self,
scheme: &QuantScheme,
qparams: QuantizationParameters<B>,
) -> Tensor<B, D> {
Tensor::new(TensorPrimitive::QFloat(B::quantize(
self.primitive.tensor(),
scheme,
qparams.into(),
)))
}
/// Dynamically convert the tensor to a lower precision data type based on the quantization scheme.
///
/// # Arguments
///
/// * `scheme` - The quantization scheme.
///
/// # Returns
///
/// The quantized tensor.
///
/// # Notes
/// This uses [min-max calibration](crate::quantization::Calibration::MinMax).
pub fn quantize_dynamic(self, scheme: &QuantScheme) -> Tensor<B, D> {
Tensor::new(TensorPrimitive::QFloat(B::quantize_dynamic(
self.primitive.tensor(),
scheme,
)))
}
/// Convert the tensor back to a higher precision data type.
///
/// If the tensor is not quantized, its value is simply returned.
///
/// # Returns
///
/// The dequantized tensor.
pub fn dequantize(self) -> Tensor<B, D> {
Tensor::new(TensorPrimitive::Float(self.primitive.tensor()))
}
/// Checks element wise if the tensor is close to another tensor.
///
/// The tolerance is defined by the following equation:
///
/// ```text
/// abs(a - b) <= (atol + rtol * abs(b))
///
/// where `a` is the first tensor, `b` is the second tensor, `rtol` is the relative tolerance,
/// and `atol` is the absolute tolerance.
/// ```
///
/// # Arguments
///
/// * `other` - The tensor to compare with.
/// * `rtol` - Optional relative tolerance. Default is 1e-5; see `DEFAULT_RTOL`.
/// * `atol` - Optional absolute tolerance. Default is 1e-8; see `DEFAULT_ATOL`.
///
/// # Returns
///
/// A boolean tensor with the same shape as the input tensors.
///
/// # Example
///
/// ```rust
/// use burn_tensor::backend::Backend;
/// use burn_tensor::{Tensor, Shape};
///
/// fn example<B: Backend>() {
/// let device = B::Device::default();
/// let tensor1 = Tensor::<B, 2>::from_data([[1.0, -2.0, 3.0], [5.0, 9.0, 6.0]], &device);
/// let tensor2 = Tensor::<B, 2>::from_data([[1.0, -2.0, 3.0], [5.0, 9.0, 6.0]], &device);
/// let tensor = tensor1.is_close(tensor2, None, None);
/// println!("{tensor}");
/// // [[true, true, true], [true, true, true]]
/// }
/// ```
pub fn is_close(self, other: Self, rtol: Option<f64>, atol: Option<f64>) -> Tensor<B, D, Bool> {
let rtol = rtol.unwrap_or(DEFAULT_RTOL);
let atol = atol.unwrap_or(DEFAULT_ATOL);
// check finite difference is close
let is_close_finite_val = self
.clone()
.sub(other.clone())
.abs()
.lower_equal(other.clone().abs().mul_scalar(rtol).add_scalar(atol))
.bool_and(self.clone().is_finite())
.bool_and(other.clone().is_finite());
// check if both are infinite and have same sign
let inf_same_sign = self
.clone()
.is_finite()
.bool_not()
.bool_and(other.clone().is_finite().bool_not())
.bool_and(self.equal(other));
is_close_finite_val.bool_or(inf_same_sign)
}
/// Checks if all elements are close to another tensor.
///
/// The tolerance is defined by the following equation:
///
/// ```text
///
/// abs(a - b) <= (atol + rtol * abs(b))
///
/// where `a` is the first tensor, `b` is the second tensor, `rtol` is the relative tolerance,
/// and `atol` is the absolute tolerance.
///
/// ```
///
/// # Arguments
///
/// * `other` - The tensor to compare with.
/// * `rtol` - Optional relative tolerance. Default is 1e-5; see `DEFAULT_RTOL`.
/// * `atol` - Optional absolute tolerance. Default is 1e-8; see `DEFAULT_ATOL`.
///
/// # Returns
///
/// A boolean scalar.
///
/// # Remarks
///
/// # Example
///
/// ```rust
/// use burn_tensor::backend::Backend;
/// use burn_tensor::{Tensor, Shape};
///
/// fn example<B: Backend>() {
/// let device = B::Device::default();
/// let tensor1 = Tensor::<B, 2>::from_data([[1.0, -2.0, 3.0], [5.0, 9.0, 6.0]], &device);
/// let tensor2 = Tensor::<B, 2>::from_data([[1.0, -2.0, 3.0], [5.0, 9.0, 6.0]], &device);
/// let result = tensor1.all_close(tensor2, None, None);
/// println!("{}", result);
/// // true
/// }
/// ```
pub fn all_close(self, other: Self, rtol: Option<f64>, atol: Option<f64>) -> bool {
self.is_close(other, rtol, atol)
.all()
.into_scalar()
.to_bool()
}
/// Returns a new tensor with boolean elements indicating whether each element of the input is NaN.
///
/// # Returns
///
/// A boolean tensor where `true` indicates NaN and `false` indicates a non-NaN value.
///
/// # Example
///
/// ```rust
/// use burn_tensor::backend::Backend;
/// use burn_tensor::{Tensor, Bool, Shape};
///
/// fn example<B: Backend>() {
/// let device = B::Device::default();
/// let tensor = Tensor::<B, 2>::from_data([[1.0, f64::NAN, 3.0], [5.0, 9.0, 6.0]], &device);
/// let tensor = tensor.is_nan();
/// println!("{tensor}");
/// // [[false, true, false], [false, false, false]]
/// }
/// ```
pub fn is_nan(self) -> Tensor<B, D, Bool> {
// Check if the input tensor is NaN by comparing it to itself
// NaN is the only value that is not equal to itself
self.clone().not_equal(self)
}
/// Checks if the tensor contains any NaN values.
///
/// # Returns
///
/// A boolean tensor with a single element indicating whether the tensor contains any NaN values.
///
/// # Example
///
/// ```rust
/// use burn_tensor::backend::Backend;
/// use burn_tensor::{Tensor, Bool, Shape};
///
/// fn example<B: Backend>() {
/// let device = B::Device::default();
/// let tensor = Tensor::<B, 2>::from_data([[1.0, -2.0, 3.0], [f64::NAN, 9.0, 6.0]], &device);
/// let tensor = tensor.contains_nan();
/// println!("{tensor}");
/// // [true]
/// let tensor = Tensor::<B, 2>::from_data([[1.0, -2.0, 3.0], [5.0, 9.0, 6.0]], &device);
/// let tensor = tensor.contains_nan();
/// println!("{tensor}");
/// // [false]
/// }
/// ```
pub fn contains_nan(self) -> Tensor<B, 1, Bool> {
// Summing the tensor will result in NaN if the tensor contains any NaN values
// This is faster than checking each element individually
// because it rolls up the NaN values into a single value
let sum = self.sum();
sum.is_nan()
}
/// Returns a new tensor with boolean elements indicating whether each element of the input is infinite (either +INF or -INF).
///
/// # Returns
///
/// A boolean tensor where `true` indicates that the value is infinite
///
/// # Example
///
/// ```rust
/// use burn_tensor::backend::Backend;
/// use burn_tensor::{Tensor, Bool, Shape};
///
/// fn example<B: Backend>() {
/// let device = B::Device::default();
/// let tensor = Tensor::<B, 2>::from_data([[1.0, f64::INFINITY, 3.0], [f64::NAN, 9.0, 6.0]], &device);
/// let tensor = tensor.is_finite();
/// println!("{tensor}");
/// // [[false, true, false], [false, false, false]]
/// }
/// ```
pub fn is_inf(self) -> Tensor<B, D, Bool> {
self.abs().equal_elem(f64::INFINITY)
}
/// Returns a new tensor with boolean elements indicating whether each element of the input is finite
///
/// # Returns
///
/// A boolean tensor where `true` indicates that the value is finite and `false` indicates
/// either INF, -INF or NAN
///
/// # Example
///
/// ```rust
/// use burn_tensor::backend::Backend;
/// use burn_tensor::{Tensor, Bool, Shape};
///
/// fn example<B: Backend>() {
/// let device = B::Device::default();
/// let tensor = Tensor::<B, 2>::from_data([[1.0, f64::INFINITY, 3.0], [f64::NAN, 9.0, 6.0]], &device);
/// let tensor = tensor.is_finite();
/// println!("{tensor}");
/// // [[true, false, true], [false, true, true]]
/// }
/// ```
pub fn is_finite(self) -> Tensor<B, D, Bool> {
self.clone()
.is_nan()
.bool_not()
.bool_and(self.is_inf().bool_not())
}
}