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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
use crate::AsIndex;
use crate::FloatDType;
use crate::Tensor;
use crate::cast::ToElement;
use crate::check;
use crate::check::TensorCheck;
use crate::ops::GridSampleOptions;
use crate::quantization::{QuantScheme, QuantizationParameters};
use crate::tensor::backend::Backend;
use crate::tensor::stats;
use crate::tensor::{Distribution, TensorData};
use crate::{Bool, Int, TensorPrimitive};
use burn_backend::tensor::quantization::QuantizationParametersPrimitive;
/// 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.
///
#[cfg_attr(doc, doc = "$y_i = e^{x_i}$")]
#[cfg_attr(not(doc), doc = "`y = e^x`")]
pub fn exp(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_exp(
self.primitive.tensor(),
)))
}
/// Applies element wise natural log operation *ln*.
///
#[cfg_attr(doc, doc = r#"$y_i = \log_e\(x_i\)$"#)]
#[cfg_attr(not(doc), doc = "`y_i = log(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.
///
#[cfg_attr(doc, doc = r#"$y_i = \log_e\(x_i + 1\)$"#)]
#[cfg_attr(not(doc), doc = "`y_i = log(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.
///
#[cfg_attr(
doc,
doc = r#"
$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$$
"#
)]
#[cfg_attr(not(doc), doc = "`y_i = erf(x_i)`")]
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.
///
#[cfg_attr(doc, doc = r#"$y_i = \frac{1}{x_i}$"#)]
#[cfg_attr(not(doc), doc = "`y_i = 1/x_i`")]
pub fn recip(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_recip(
self.primitive.tensor(),
)))
}
/// Applies element wise square operation.
///
#[cfg_attr(doc, doc = r#"$y_i = x_i * x_i$"#)]
#[cfg_attr(not(doc), doc = "`y_i = x_i * x_i`")]
pub fn square(self) -> Self {
self.powi_scalar(2)
}
/// Applies element wise root square operation.
///
#[cfg_attr(doc, doc = r#"$y_i = \sqrt{x_i}$"#)]
#[cfg_attr(not(doc), doc = "`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.
///
#[cfg_attr(doc, doc = r#"$y_i = \cos\(x_i\)$"#)]
#[cfg_attr(not(doc), doc = "`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.
///
#[cfg_attr(doc, doc = r#"$y_i = \sin\(x_i\)$"#)]
#[cfg_attr(not(doc), doc = "`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.
///
#[cfg_attr(doc, doc = r#"$y_i = \tan\(x_i\)$"#)]
#[cfg_attr(not(doc), doc = "`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.
///
#[cfg_attr(doc, doc = r#"$y_i = \cosh\(x_i\)$"#)]
#[cfg_attr(not(doc), doc = "`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, 1>::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.
///
#[cfg_attr(doc, doc = r#"$y_i = \sinh\(x_i\)$"#)]
#[cfg_attr(not(doc), doc = "`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, 1>::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.
///
#[cfg_attr(doc, doc = r#"$y_i = \tanh\(x_i\)$"#)]
#[cfg_attr(not(doc), doc = "`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, 1>::from_data([0.0, -1.0, 2.0], &device);
/// println!("{}", tensor.tanh()); // [0.0, -0.7616, 0.9640]
/// }
/// ```
pub fn tanh(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_tanh(
self.primitive.tensor(),
)))
}
/// Applies element wise inverse sine operation.
///
#[cfg_attr(doc, doc = r#"$y_i = \asin\(x_i\)$"#)]
#[cfg_attr(not(doc), doc = "`y_i = asin(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, 1>::from_data([0.0, -1.0, 1.0], &device);
/// println!("{}", tensor.asin()); // [ 0.0000, -1.5708, 1.5708]
/// }
/// ```
pub fn asin(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_asin(
self.primitive.tensor(),
)))
}
/// Applies element wise inverse hyperbolic sine operation.
///
#[cfg_attr(doc, doc = r#"$y_i = \asinh\(x_i\)$"#)]
#[cfg_attr(not(doc), doc = "`y_i = asinh(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, 1>::from_data([0.0, -1.0, 1.0], &device);
/// println!("{}", tensor.asinh()); // [ 0.0000, -0.8814, 0.8814]
/// }
/// ```
pub fn asinh(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_asinh(
self.primitive.tensor(),
)))
}
/// Applies element wise inverse cosine operation.
///
#[cfg_attr(doc, doc = r#"$y_i = \acos\(x_i\)$"#)]
#[cfg_attr(not(doc), doc = "`y_i = acos(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, 1>::from_data([0.0, -1.0, 1.0], &device);
/// println!("{}", tensor.acos()); // [1.5708, 3.1416, 0.0]
/// }
/// ```
pub fn acos(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_acos(
self.primitive.tensor(),
)))
}
/// Applies element wise inverse hyperbolic cosine operation.
///
#[cfg_attr(doc, doc = r#"$y_i = \acosh\(x_i\)$"#)]
#[cfg_attr(not(doc), doc = "`y_i = acosh(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, 1>::from_data([1.0, 2.0, 3.0], &device);
/// println!("{}", tensor.sinh()); // [0.0000, 1.3170, 1.7627]
/// }
/// ```
pub fn acosh(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_acosh(
self.primitive.tensor(),
)))
}
/// Applies element wise inverse tangent operation.
///
#[cfg_attr(doc, doc = r#"$y_i = \atan\(x_i\)$"#)]
#[cfg_attr(not(doc), doc = "`y_i = atan(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, 1>::from_data([0.0, -1.0, 2.0], &device);
/// println!("{}", tensor.sinh()); // [ 0.0, -0.7854, 1.1071]
/// }
/// ```
pub fn atan(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_atan(
self.primitive.tensor(),
)))
}
/// Applies element wise inverse hyperbolic tangent operation.
///
#[cfg_attr(doc, doc = r#"$y_i = \atan\(x_i\)$"#)]
#[cfg_attr(not(doc), doc = "`y_i = atan(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, 1>::from_data([0.0, -0.5, 0.5], &device);
/// println!("{}", tensor.sinh()); // [ 0.0, -0.5493, 0.5493]
/// }
/// ```
pub fn atanh(self) -> Self {
Self::new(TensorPrimitive::Float(B::float_atanh(
self.primitive.tensor(),
)))
}
/// Applies element wise inverse tangent operation using the signs of arguments to determine the correct quadrant.
///
#[cfg_attr(doc, doc = r#"$z_i = \atan2\(y_i, x_i\)$"#)]
#[cfg_attr(not(doc), doc = "`z_i = atan2(y_i, x_i)`")]
///
/// # Example
///
/// ```rust
/// use burn_tensor::backend::Backend;
/// use burn_tensor::Tensor;
///
/// fn example<B: Backend>() {
/// let device = Default::default();
///
/// let lhs = Tensor::<B, 1>::from_data([-2.0, 2.0, -2.0], &device);
/// let rhs = Tensor::<B, 1>::from_data([1.0, -1.0, -1.0], &device);
/// println!("{}", lhs.atan2(rhs)); // [-1.1071, 2.0344, -2.0344]
/// }
/// ```
pub fn atan2(self, other: Self) -> Self {
Self::new(TensorPrimitive::Float(B::float_atan2(
self.primitive.tensor(),
other.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, dtype, and device as the current tensor filled random
/// values sampled from the given distribution.
pub fn random_like(&self, distribution: Distribution) -> Self {
Self::new(TensorPrimitive::Float(B::float_random(
self.shape(),
distribution,
&self.device(),
)))
.cast(self.dtype())
}
/// 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.
///
/// This is always a no-op when casting to the current dtype.
///
/// # 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> {
let dtype = dtype.into();
let self_type: FloatDType = self.dtype().into();
if dtype == self_type {
// no-op.
return self;
}
Tensor::new(TensorPrimitive::Float(B::float_cast(
self.primitive.tensor(),
dtype,
)))
}
/// 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,
QuantizationParametersPrimitive {
scales: qparams.scales.primitive.tensor(),
},
)))
}
/// 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> {
Tensor::new(B::float_is_nan(self.primitive.tensor()))
}
/// 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> {
Tensor::new(B::float_is_inf(self.primitive.tensor()))
}
/// 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())
}
/// Samples tensor as a two-dimensional spatial grid of (possibly multi-channel) values,
/// using the given locations in [-1, 1].
///
/// # Arguments
///
/// * `grid` - A tensor of locations, with shape (N, H_out, W_out, 2). Values are [-1, 1].
/// A [x = -1, y = -1] means top-left, and [x = 1, y = 1] means bottom-right
/// * `options` - Grid sampling options (mode, padding_mode, align_corners)
///
/// # Returns
///
/// A tensor with shape (N, C, H_out, W_out)
///
/// # Example
///
/// ```ignore
/// use burn_tensor::ops::{GridSampleOptions, GridSamplePaddingMode, InterpolateMode};
///
/// // Default options (bilinear, zeros padding, align_corners=false)
/// let output = tensor.grid_sample_2d(grid, GridSampleOptions::default());
///
/// // Custom options
/// let options = GridSampleOptions::new(InterpolateMode::Bilinear)
/// .with_padding_mode(GridSamplePaddingMode::Border)
/// .with_align_corners(true);
/// let output = tensor.grid_sample_2d(grid, options);
/// ```
pub fn grid_sample_2d(
self,
grid: Tensor<B, D>,
options: impl Into<GridSampleOptions>,
) -> Tensor<B, D> {
Tensor::new(TensorPrimitive::Float(B::float_grid_sample_2d(
self.primitive.tensor(),
grid.primitive.tensor(),
options.into(),
)))
}
/// Computes the cross product of `self` and another tensor along a given dimension.
///
/// Both `self` and `other` **must have size 3** along the specified `dim`,
/// because the cross product is only defined in three-dimensional space.
///
/// # Arguments
///
/// * `other` - The other tensor to take the cross product with.
/// * `dim` - The dimension along which to compute the cross product.
///
/// # Returns
///
/// A tensor containing the cross product of `self` and `other` along `dim`.
pub fn cross<Dim: AsIndex>(self, other: Tensor<B, D>, dim: Dim) -> Tensor<B, D> {
let dim = dim.expect_dim_index(D);
check!(TensorCheck::cross(&self, &other, dim));
Tensor::new(TensorPrimitive::Float(B::float_cross(
self.primitive.tensor(),
other.primitive.tensor(),
dim,
)))
}
}