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
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
use super::cat::cat_with_slice_assign;
use super::grid_sample::float_grid_sample_2d_ref;
use super::repeat_dim::repeat_with_slice_assign;
use super::sort::{argsort, sort, sort_with_indices};
use crate::ops::GridSampleOptions;
use crate::tensor::{BoolTensor, Device, Float, FloatElem, FloatTensor, IntElem, IntTensor};
use crate::{Backend, Distribution, TensorData, element::ElementConversion};
use crate::{ExecutionError, TensorMetadata, TensorPrimitive};
use alloc::vec::Vec;
use burn_std::{FloatDType, Shape, Slice};
/// Operations on float tensors.
pub trait FloatTensorOps<B: Backend> {
/// Creates a new tensor from the data structure.
///
/// # Arguments
///
/// * `data` - The data structure.
/// * `device` - The device to create the tensor on.
///
/// # Returns
///
/// The tensor with the given data.
fn float_from_data(data: TensorData, device: &Device<B>) -> FloatTensor<B>;
/// Creates a new tensor with random values.
///
/// # Arguments
///
/// * `shape` - The shape of the tensor.
/// * `distribution` - The distribution to sample from.
/// * `device` - The device to create the tensor on.
///
/// # Returns
///
/// The tensor with the given shape and random values.
fn float_random(shape: Shape, distribution: Distribution, device: &Device<B>)
-> FloatTensor<B>;
/// Creates a new tensor with zeros.
///
/// # Arguments
///
/// * `shape` - The shape of the tensor.
/// * `device` - The device to create the tensor on.
/// * `dtype` - The target data type.
///
/// # Returns
///
/// The tensor with the given shape and zeros.
fn float_zeros(shape: Shape, device: &Device<B>, dtype: FloatDType) -> FloatTensor<B> {
Self::float_from_data(TensorData::full_dtype(shape, 0, dtype.into()), device)
}
/// Creates a new tensor with ones.
///
/// # Arguments
///
/// * `shape` - The shape of the tensor.
/// * `device` - The device to create the tensor on.
/// * `dtype` - The target data type.
///
/// # Returns
///
/// The tensor with the given shape and ones.
fn float_ones(shape: Shape, device: &Device<B>, dtype: FloatDType) -> FloatTensor<B> {
Self::float_from_data(TensorData::full_dtype(shape, 1, dtype.into()), device)
}
/// Creates a tensor filled with given value.
///
/// # Arguments
///
/// * `shape` - The shape of the tensor.
/// * `fill_value` - The value with which to fill the tensor.
/// * `device` - The device to create the tensor on.
/// * `dtype` - The target data type.
///
/// # Returns
///
/// The tensor filled with given value
fn float_full(
shape: Shape,
fill_value: FloatElem<B>,
device: &Device<B>,
dtype: FloatDType,
) -> FloatTensor<B> {
Self::float_from_data(
TensorData::full_dtype(shape, fill_value, dtype.into()),
device,
)
}
/// Converts the tensor to a data structure.
///
/// # Arguments
///
/// * `tensor` - The tensor.
///
/// # Returns
///
/// The data structure with the tensor's data.
fn float_into_data(
tensor: FloatTensor<B>,
) -> impl Future<Output = Result<TensorData, ExecutionError>> + Send;
/// Gets the device of the tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor.
///
/// # Returns
///
/// The device of the tensor.
fn float_device(tensor: &FloatTensor<B>) -> Device<B>;
/// Moves the tensor to the given device.
///
/// # Arguments
///
/// * `tensor` - The tensor.
/// * `device` - The device to move the tensor to.
///
/// # Returns
///
/// The tensor on the given device.
fn float_to_device(tensor: FloatTensor<B>, device: &Device<B>) -> FloatTensor<B>;
/// Converts float tensor to int tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor.
///
/// # Returns
///
/// The int tensor with the same data as the float tensor.
fn float_into_int(tensor: FloatTensor<B>) -> IntTensor<B>;
/// Creates an empty tensor with the given shape.
///
/// # Arguments
///
/// * `shape` - The shape of the tensor.
/// * `device` - The device to create the tensor on.
/// * `dtype` - The target data type.
///
/// # Returns
///
/// The empty tensor with the given shape.
fn float_empty(shape: Shape, device: &Device<B>, dtype: FloatDType) -> FloatTensor<B>;
/// Repeat the tensor along the given dimension.
///
/// # Arguments
///
/// * `tensor` - The tensor.
/// * `dim` - The dimension to repeat.
/// * `times` - The number of times to repeat the dimension.
///
/// # Returns
///
/// The tensor with the given dimension repeated.
fn float_repeat_dim(tensor: FloatTensor<B>, dim: usize, times: usize) -> FloatTensor<B> {
repeat_with_slice_assign::<B, Float>(TensorPrimitive::Float(tensor), dim, times).tensor()
}
/// Adds two tensors together.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side tensor.
///
/// # Returns
///
/// The result of adding the two tensors together.
fn float_add(lhs: FloatTensor<B>, rhs: FloatTensor<B>) -> FloatTensor<B>;
/// Adds a scalar to a tensor.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side scalar.
///
/// # Returns
///
/// The result of adding the scalar to the tensor.
fn float_add_scalar(lhs: FloatTensor<B>, rhs: FloatElem<B>) -> FloatTensor<B>;
/// Clamps a tensor under a minimum value.
///
/// # Arguments
///
/// * `tensor` - The tensor to clamp.
/// * `min` - The minimum value.
///
/// # Returns
///
/// The clamped tensor.
fn float_clamp_min(tensor: FloatTensor<B>, min: FloatElem<B>) -> FloatTensor<B> {
// Default implementation
let mask = Self::float_lower_elem(tensor.clone(), min);
B::float_mask_fill(tensor, mask, min)
}
/// Clamps a tensor over a maximum value.
///
/// # Arguments
///
/// * `tensor` - The tensor to clamp.
/// * `max` - The maximum value.
///
/// # Returns
///
/// The clamped tensor.
fn float_clamp_max(tensor: FloatTensor<B>, max: FloatElem<B>) -> FloatTensor<B> {
// Default implementation
let mask = Self::float_greater_elem(tensor.clone(), max);
B::float_mask_fill(tensor, mask, max)
}
/// Clamps a tensor between a minimum and maximum value.
///
/// # Arguments
///
/// * `tensor` - The tensor to clamp.
/// * `min` - The minimum value.
/// * `max` - The maximum value.
///
/// # Returns
///
/// The clamped tensor.
fn float_clamp(tensor: FloatTensor<B>, min: FloatElem<B>, max: FloatElem<B>) -> FloatTensor<B> {
// Default implementation
Self::float_clamp_min(Self::float_clamp_max(tensor, max), min)
}
/// Subtracts two tensors.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side tensor.
///
/// # Returns
///
/// The result of subtracting the two tensors.
fn float_sub(lhs: FloatTensor<B>, rhs: FloatTensor<B>) -> FloatTensor<B>;
/// Subtracts a scalar from a tensor.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side scalar.
///
/// # Returns
///
/// The result of subtracting the scalar from the tensor.
fn float_sub_scalar(lhs: FloatTensor<B>, rhs: FloatElem<B>) -> FloatTensor<B>;
/// Multiplies two tensors together element-wise.
fn float_mul(lhs: FloatTensor<B>, rhs: FloatTensor<B>) -> FloatTensor<B>;
/// Multiplies a tensor by a scalar.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side scalar.
///
/// # Returns
///
/// The result of multiplying the tensor by the scalar.
fn float_mul_scalar(lhs: FloatTensor<B>, rhs: FloatElem<B>) -> FloatTensor<B>;
/// Divides two tensors element-wise.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side tensor.
///
/// # Returns
///
/// The result of dividing the two tensors.
fn float_div(lhs: FloatTensor<B>, rhs: FloatTensor<B>) -> FloatTensor<B>;
/// Divides a tensor by a scalar.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side scalar.
///
/// # Returns
///
/// The result of dividing the tensor by the scalar.
fn float_div_scalar(lhs: FloatTensor<B>, rhs: FloatElem<B>) -> FloatTensor<B>;
/// Computes the remainder of division between two tensors element-wise.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side tensor.
///
/// # Returns
///
/// The element-wise remainder when dividing `lhs` by `rhs`.
fn float_remainder(lhs: FloatTensor<B>, rhs: FloatTensor<B>) -> FloatTensor<B>;
/// Computes the modulus of a tensor given a scalar.
///
/// # Arguments
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side scalar.
///
/// # Returns
///
/// The result of applying the modulus of the scalar to the tensor.
fn float_remainder_scalar(lhs: FloatTensor<B>, rhs: FloatElem<B>) -> FloatTensor<B>;
/// Multiplies two tensors together using matrix multiplication.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side tensor.
///
/// # Returns
///
/// The result of multiplying the two tensors together using matrix multiplication.
fn float_matmul(lhs: FloatTensor<B>, rhs: FloatTensor<B>) -> FloatTensor<B>;
/// Computes the cross product of two tensors along a given dimension.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side tensor.
/// * `dim` - The dimension to compute the cross product along.
///
/// # Returns
///
/// The cross product of the two tensors.
fn float_cross(lhs: FloatTensor<B>, rhs: FloatTensor<B>, dim: usize) -> FloatTensor<B>;
/// Negates a tensor element-wise.
fn float_neg(tensor: FloatTensor<B>) -> FloatTensor<B> {
Self::float_mul_scalar(tensor, (-1.0_f32).elem::<FloatElem<B>>())
}
/// Calculates the reciprocals element-wise
fn float_recip(tensor: FloatTensor<B>) -> FloatTensor<B>;
/// Transposes a tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor to transpose.
///
/// # Returns
///
/// The transposed tensor.
fn float_transpose(tensor: FloatTensor<B>) -> FloatTensor<B> {
let ndims = tensor.shape().num_dims();
Self::float_swap_dims(tensor, ndims - 2, ndims - 1)
}
/// Swaps two dimensions of a tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor to swap the dimensions of.
/// * `dim1` - The first dimension to swap.
/// * `dim2` - The second dimension to swap.
///
/// # Returns
///
/// The tensor with the dimensions swapped.
fn float_swap_dims(tensor: FloatTensor<B>, dim1: usize, dim2: usize) -> FloatTensor<B>;
/// Permutes the dimensions of a tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor to permute the dimensions of.
/// * `axes` - The new order of the dimensions.
/// # Returns
///
/// The tensor with the dimensions permuted.
fn float_permute(tensor: FloatTensor<B>, axes: &[usize]) -> FloatTensor<B>;
/// Reverse the order of elements in a tensor along the given axes.
///
/// # Arguments
///
/// * `tensor` - The tensor to reverse.
/// * `axes` - The axes to reverse.
///
/// The tensor with the elements reversed.
fn float_flip(tensor: FloatTensor<B>, axes: &[usize]) -> FloatTensor<B>;
/// Reshapes a tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor to reshape.
/// * `shape` - The new shape of the tensor.
///
/// # Returns
///
/// The tensor with the new shape.
fn float_reshape(tensor: FloatTensor<B>, shape: Shape) -> FloatTensor<B>;
/// Gather elements from a tensor.
///
/// # Arguments
///
/// * `dim` - The dimension to gather from.
/// * `tensor` - The tensor to gather from.
/// * `indices` - The indices to gather.
///
/// # Returns
///
/// The gathered elements.
fn float_gather(dim: usize, tensor: FloatTensor<B>, indices: IntTensor<B>) -> FloatTensor<B>;
/// Scatter elements into a tensor using sum reduction.
///
/// # Arguments
///
/// * `dim` - The dimension to scatter into.
/// * `tensor` - The tensor to scatter into.
/// * `indices` - The indices to scatter into.
/// * `value` - The value to scatter.
///
/// # Returns
///
/// The tensor with the scattered elements.
fn float_scatter_add(
dim: usize,
tensor: FloatTensor<B>,
indices: IntTensor<B>,
value: FloatTensor<B>,
) -> FloatTensor<B>;
/// Select tensor elements along the given dimension corresponding for the given indices.
///
/// # Arguments
///
/// * `tensor` - The tensor to select from.
/// * `dim` - The dimension to select from.
/// * `indices` - The indices to select.
///
/// # Returns
///
/// The selected elements.
fn float_select(tensor: FloatTensor<B>, dim: usize, indices: IntTensor<B>) -> FloatTensor<B>;
/// Assign the selected elements along the given dimension corresponding for the given indices
/// to the given value using sum reduction.
///
/// # Arguments
///
/// * `tensor` - The tensor to select from.
/// * `dim` - The dimension to select from.
/// * `indices` - The indices to select.
/// * `value` - The value to assign.
///
/// # Returns
///
/// The tensor with the selected elements assigned to the given value.
fn float_select_add(
tensor: FloatTensor<B>,
dim: usize,
indices: IntTensor<B>,
value: FloatTensor<B>,
) -> FloatTensor<B>;
/// Select tensor elements corresponding to the given slices.
///
/// # Arguments
///
/// * `tensor` - The tensor to select from.
/// * `slices` - The slices specifying ranges and steps for each dimension.
///
/// # Returns
///
/// The selected elements in a new tensor.
///
/// # Note
///
/// Empty slices (where start >= end) are handled at the high-level tensor API and will not
/// be passed to this method. Backend implementations do not need to handle empty slices.
fn float_slice(tensor: FloatTensor<B>, slices: &[Slice]) -> FloatTensor<B>;
/// Assign the selected elements corresponding to the given slices to the given value.
///
/// # Arguments
///
/// * `tensor` - The tensor to select from.
/// * `ranges` - The ranges to select.
/// * `value` - The value to assign.
///
/// # Returns
///
/// The tensor with the selected elements assigned to the given value.
///
/// # Note
///
/// Empty slice assignments (where any slice range produces 0 elements) are handled at the
/// high-level tensor API and will not be passed to this method. Backend implementations do
/// not need to handle empty slice assignments.
fn float_slice_assign(
tensor: FloatTensor<B>,
slices: &[Slice],
value: FloatTensor<B>,
) -> FloatTensor<B>;
/// Update the given tensor with the value tensor where the mask is true.
///
/// # Arguments
///
/// * `tensor` - The tensor to select from.
/// * `mask` - The boolean mask to select with.
/// * `value` - The value to assign to the selected elements from the value tensor.
///
/// # Returns
///
/// The tensor with the selected elements assigned to the given value.
fn float_mask_where(
tensor: FloatTensor<B>,
mask: BoolTensor<B>,
value: FloatTensor<B>,
) -> FloatTensor<B>;
/// Update the given tensor with the value where the mask is true.
///
/// # Arguments
///
/// * `tensor` - The tensor to select from.
/// * `mask` - The boolean mask to select with.
/// * `value` - The value to assign to the selected elements.
///
/// # Returns
///
/// The tensor with the selected elements assigned to the given value.
fn float_mask_fill(
tensor: FloatTensor<B>,
mask: BoolTensor<B>,
value: FloatElem<B>,
) -> FloatTensor<B>;
/// Equal comparison of two tensors.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side tensor.
///
/// # Returns
///
/// A boolean tensor with the result of the comparison.
fn float_equal(lhs: FloatTensor<B>, rhs: FloatTensor<B>) -> BoolTensor<B>;
/// Element-wise non-equality comparison.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side tensor.
///
/// # Returns
///
/// A boolean tensor with the result of the comparison.
fn float_not_equal(lhs: FloatTensor<B>, rhs: FloatTensor<B>) -> BoolTensor<B> {
let equal_tensor = B::float_equal(lhs, rhs);
B::bool_not(equal_tensor)
}
/// Equal comparison of a tensor and a scalar.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side scalar.
///
/// # Returns
///
/// A boolean tensor with the result of the comparison.
fn float_equal_elem(lhs: FloatTensor<B>, rhs: FloatElem<B>) -> BoolTensor<B>;
/// Element-wise non-equality comparison with a scalar.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side scalar.
///
/// # Returns
///
/// A boolean tensor with the result of the comparison.
fn float_not_equal_elem(lhs: FloatTensor<B>, rhs: FloatElem<B>) -> BoolTensor<B> {
let equal_tensor = B::float_equal_elem(lhs, rhs);
B::bool_not(equal_tensor)
}
/// Greater than comparison of two tensors.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side tensor.
///
/// # Returns
///
/// A boolean tensor with the result of the comparison.
fn float_greater(lhs: FloatTensor<B>, rhs: FloatTensor<B>) -> BoolTensor<B>;
/// Greater than comparison of a tensor and a scalar.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side scalar.
///
/// # Returns
///
/// A boolean tensor with the result of the comparison.
fn float_greater_elem(lhs: FloatTensor<B>, rhs: FloatElem<B>) -> BoolTensor<B>;
/// Greater than or equal comparison of two tensors.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side tensor.
///
/// # Returns
///
/// A boolean tensor with the result of the comparison.
fn float_greater_equal(lhs: FloatTensor<B>, rhs: FloatTensor<B>) -> BoolTensor<B>;
/// Greater than or equal comparison of a tensor and a scalar.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side scalar.
///
/// # Returns
///
/// A boolean tensor with the result of the comparison.
fn float_greater_equal_elem(lhs: FloatTensor<B>, rhs: FloatElem<B>) -> BoolTensor<B>;
/// Less than comparison of two tensors.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side tensor.
///
/// # Returns
///
/// A boolean tensor with the result of the comparison.
fn float_lower(lhs: FloatTensor<B>, rhs: FloatTensor<B>) -> BoolTensor<B>;
/// Less than comparison of a tensor and a scalar.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side scalar.
///
/// # Returns
///
/// A boolean tensor with the result of the comparison.
fn float_lower_elem(lhs: FloatTensor<B>, rhs: FloatElem<B>) -> BoolTensor<B>;
/// Less than or equal comparison of two tensors.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side tensor.
///
/// # Returns
///
/// A boolean tensor with the result of the comparison.
fn float_lower_equal(lhs: FloatTensor<B>, rhs: FloatTensor<B>) -> BoolTensor<B>;
/// Less than or equal comparison of a tensor and a scalar.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side scalar.
///
/// # Returns
///
/// A boolean tensor with the result of the comparison.
fn float_lower_equal_elem(lhs: FloatTensor<B>, rhs: FloatElem<B>) -> BoolTensor<B>;
/// Detaches a tensor from the computation graph.
fn float_detach(tensor: FloatTensor<B>) -> FloatTensor<B> {
// Should only be overridden by autodiff backends.
tensor
}
/// Sets the `require_grad` flag of a tensor.
fn float_set_require_grad(tensor: FloatTensor<B>, _require_grad: bool) -> FloatTensor<B> {
// Should only be overridden by autodiff backends.
tensor
}
/// Returns the `require_grad` flag of a tensor.
fn float_is_require_grad(_tensor: &FloatTensor<B>) -> bool {
// Should only be overridden by autodiff backends.
false
}
/// Sum of all elements in a tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor to sum.
///
/// # Returns
///
/// A scalar tensor with the sum of all elements in `tensor`.
fn float_sum(tensor: FloatTensor<B>) -> FloatTensor<B>;
/// Sum of all elements in a tensor along a dimension.
///
/// # Arguments
///
/// * `tensor` - The tensor to sum.
/// * `dim` - The dimension along which to sum.
///
/// # Returns
///
/// A tensor with the sum of all elements in `tensor` along `dim`.
fn float_sum_dim(tensor: FloatTensor<B>, dim: usize) -> FloatTensor<B>;
/// Product of all elements in a tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor to product.
///
/// # Returns
///
/// A scalar tensor with the product of all elements in `tensor`.
fn float_prod(tensor: FloatTensor<B>) -> FloatTensor<B> {
// Product of all elements in a tensor
B::float_exp(B::float_sum(B::float_log(tensor)))
}
/// Product of all elements in a tensor along a dimension.
///
/// # Arguments
///
/// * `tensor` - The tensor to product.
///
/// # Returns
///
/// A tensor with the product of all elements in `tensor` along `dim`.
fn float_prod_dim(tensor: FloatTensor<B>, dim: usize) -> FloatTensor<B> {
// Product of all elements in a tensor along a dimension
B::float_exp(B::float_sum_dim(B::float_log(tensor), dim))
}
/// Mean of all elements in a tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor to mean.
///
/// # Returns
///
/// A scalar tensor with the mean of all elements in `tensor`.
fn float_mean(tensor: FloatTensor<B>) -> FloatTensor<B> {
let num_elems = tensor.shape().num_elements();
B::float_div_scalar(B::float_sum(tensor), (num_elems as i64).elem())
}
/// Mean of all elements in a tensor along a dimension.
///
/// # Arguments
///
/// * `tensor` - The tensor to mean.
/// * `dim` - The dimension along which to mean.
///
/// # Returns
///
/// A tensor with the mean of all elements in `tensor` along `dim`.
fn float_mean_dim(tensor: FloatTensor<B>, dim: usize) -> FloatTensor<B>;
/// Computes the cumulative sum of elements along a dimension.
///
/// # Arguments
///
/// * `tensor` - The tensor to compute the cumulative sum of.
/// * `dim` - The dimension along which to compute the cumulative sum.
///
/// # Returns
///
/// A tensor with the same shape where each element is the cumulative sum
/// of all elements up to and including that position along the dimension.
fn float_cumsum(tensor: FloatTensor<B>, dim: usize) -> FloatTensor<B>;
/// Computes the cumulative product of elements along a dimension.
///
/// # Arguments
///
/// * `tensor` - The tensor to compute the cumulative product of.
/// * `dim` - The dimension along which to compute the cumulative product.
///
/// # Returns
///
/// A tensor with the same shape where each element is the cumulative product
/// of all elements up to and including that position along the dimension.
fn float_cumprod(tensor: FloatTensor<B>, dim: usize) -> FloatTensor<B>;
/// Computes the cumulative minimum of elements along a dimension.
///
/// # Arguments
///
/// * `tensor` - The tensor to compute the cumulative minimum of.
/// * `dim` - The dimension along which to compute the cumulative minimum.
///
/// # Returns
///
/// A tensor with the same shape where each element is the minimum
/// of all elements up to and including that position along the dimension.
fn float_cummin(tensor: FloatTensor<B>, dim: usize) -> FloatTensor<B>;
/// Computes the cumulative maximum of elements along a dimension.
///
/// # Arguments
///
/// * `tensor` - The tensor to compute the cumulative maximum of.
/// * `dim` - The dimension along which to compute the cumulative maximum.
///
/// # Returns
///
/// A tensor with the same shape where each element is the maximum
/// of all elements up to and including that position along the dimension.
fn float_cummax(tensor: FloatTensor<B>, dim: usize) -> FloatTensor<B>;
/// Converts a tensor to another floating point data type.
///
/// # Arguments
///
/// * `tensor` - The tensor to convert.
/// * `dtype` - The target data type.
///
/// # Returns
///
/// A tensor with the same values as `tensor` but in the target floating point data type.
fn float_cast(tensor: FloatTensor<B>, dtype: FloatDType) -> FloatTensor<B>;
/// Returns a new tensor with exponential values.
///
/// # Arguments
///
/// * `tensor` - The tensor to exponentiate.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with exponential values.
fn float_exp(tensor: FloatTensor<B>) -> FloatTensor<B>;
/// Returns a new tensor with natural logarithm values.
///
/// # Arguments
///
/// * `tensor` - The tensor to take the logarithm of.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with natural logarithm values.
fn float_log(tensor: FloatTensor<B>) -> FloatTensor<B>;
/// Returns a new tensor with logarithm values of (1 + Xi).
///
/// # Arguments
///
/// * `tensor` - The tensor to take the logarithm of.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with logarithm values of (1 + Xi).
fn float_log1p(tensor: FloatTensor<B>) -> FloatTensor<B>;
/// Element-wise power with a FloatTensor.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side tensor.
///
/// # Returns
///
/// The elements of `lhs` raised to the power of the elements of `rhs`.
fn float_powf(lhs: FloatTensor<B>, rhs: FloatTensor<B>) -> FloatTensor<B>;
/// Element-wise power with an IntTensor.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side floatTensor.
///
/// # Returns
///
/// The elements of `lhs` raised to the value of `rhs`. Result is an IntTensor.
fn float_powi(lhs: FloatTensor<B>, rhs: IntTensor<B>) -> FloatTensor<B> {
Self::float_powf(lhs, B::int_into_float(rhs))
}
/// Raises a tensor to the power of an int scalar.
///
/// # Backend Implementors Note
///
/// A number of common exponent cases can be implemented with operations
/// which are much cheaper than generic exponentiation.
///
/// This (`Backend` impl overridable) operation handles generic optimizations
/// for several common integer exponent cases; and then dispatches to
/// the (`Backend` impl overridable) [`Self::float_powi_scalar_impl`]
/// operation to handle the generic case.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side scalar.
///
/// # Returns
///
/// The elements of `lhs` raised to the value of `rhs`.
fn float_powi_scalar(lhs: FloatTensor<B>, rhs: IntElem<B>) -> FloatTensor<B> {
let exp = rhs.elem::<i32>();
match exp {
0 => Self::float_ones(lhs.shape(), &B::float_device(&lhs), lhs.dtype().into()),
1 => lhs,
2 => B::float_mul(lhs.clone(), lhs),
-1 => Self::float_recip(lhs),
-2 => Self::float_recip(B::float_mul(lhs.clone(), lhs)),
_ => Self::float_powi_scalar_impl(lhs, rhs),
}
}
/// Raises a tensor to the power of an int scalar.
///
/// # Backend Implementors Note
///
/// This is the generic implementation of integer exponentiation
/// called by [`Self::float_powi_scalar`] in the fallback case.
///
/// As a general rule, this should not be called directly.
///
/// # Arguments
///
/// * `lhs` - The left-hand side tensor.
/// * `rhs` - The right-hand side scalar.
///
/// # Returns
///
/// The elements of `lhs` raised to the value of `rhs`.
fn float_powi_scalar_impl(lhs: FloatTensor<B>, rhs: IntElem<B>) -> FloatTensor<B> {
// Avoid a recursive loop by deferring directly to float_powf_scalar_impl.
Self::float_powf_scalar_impl(lhs, rhs.elem::<f32>())
}
/// Returns a new tensor with values raised to the power of float `value`.
///
/// # Backend Implementors Note
///
/// This (`Backend` impl overridable) operation dispatches integer exponentiation
/// to [`Self::float_powi_scalar`], and the remaining non-integer exponent cases to
/// the (`Backend` impl overridable) [`Self::float_powf_scalar_impl`]
/// operation to handle the generic case.
///
/// # Arguments
///
/// * `tensor` - The tensor to exponentiate.
/// * `value` - The exponent.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with values raised to the power of `value`.
fn float_powf_scalar(tensor: FloatTensor<B>, value: f32) -> FloatTensor<B> {
if num_traits::Float::floor(value) == value {
// When the exponent is an integer, use the integer exponentiation implementation.
let exp = B::IntElem::from_elem(value as i32);
Self::float_powi_scalar(tensor, exp)
} else {
Self::float_powf_scalar_impl(tensor, value)
}
}
/// Returns a new tensor with values raised to the power of float `value`.
///
/// # Backend Implementors Note
///
/// This is the generic implementation of integer exponentiation
/// called by [`Self::float_powf_scalar`] in the fallback case.
///
/// This is the minimal required support a `Backend` must implement
/// for exponentiation.
///
/// As a general rule, this should not be called directly.
///
/// # Arguments
///
/// * `tensor` - The tensor to exponentiate.
/// * `value` - The exponent.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with values raised to the power of `value`.
fn float_powf_scalar_impl(tensor: FloatTensor<B>, value: f32) -> FloatTensor<B>;
/// Returns a new tensor with square root values.
///
/// # Arguments
///
/// * `tensor` - The tensor to take the square root of.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with square root values.
fn float_sqrt(tensor: FloatTensor<B>) -> FloatTensor<B>;
/// Returns a new tensor with absolute values.
///
/// # Arguments
///
/// * `tensor` - The tensor to take absolute value of.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with absolute values.
fn float_abs(tensor: FloatTensor<B>) -> FloatTensor<B>;
/// Returns a new tensor with cosine values.
///
/// # Arguments
///
/// * `tensor` - The tensor to take the cosine of.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with cosine values.
fn float_cos(tensor: FloatTensor<B>) -> FloatTensor<B>;
/// Returns a new tensor with sine values.
///
/// # Arguments
///
/// * `tensor` - The tensor to take the sine of.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with sine values.
fn float_sin(tensor: FloatTensor<B>) -> FloatTensor<B>;
/// Returns a new tensor with tangent values.
///
/// # Arguments
///
/// * `tensor` - The tensor to take the tangent of.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with tangent values.
fn float_tan(tensor: FloatTensor<B>) -> FloatTensor<B>;
/// Returns a new tensor with hyperbolic cosine values.
///
/// # Arguments
///
/// * `tensor` - The tensor to take the hyperbolic cosine of.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with hyperbolic cosine values.
fn float_cosh(tensor: FloatTensor<B>) -> FloatTensor<B>;
/// Returns a new tensor with hyperbolic sine values.
///
/// # Arguments
///
/// * `tensor` - The tensor to take the hyperbolic sine of.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with hyperbolic sine values.
fn float_sinh(tensor: FloatTensor<B>) -> FloatTensor<B>;
/// Returns a new tensor with hyperbolic tangent values.
///
/// # Arguments
///
/// * `tensor` - The tensor to take the hyperbolic tangent of.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with hyperbolic tangent values.
fn float_tanh(tensor: FloatTensor<B>) -> FloatTensor<B>;
/// Returns a new tensor with inverse cosine values.
///
/// # Arguments
///
/// * `tensor` - The input tensor.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with inverse cosine values.
fn float_acos(tensor: FloatTensor<B>) -> FloatTensor<B>;
/// Returns a new tensor with inverse hyperbolic cosine values.
///
/// # Arguments
///
/// * `tensor` - The input tensor.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with inverse hyperbolic cosine values.
fn float_acosh(tensor: FloatTensor<B>) -> FloatTensor<B>;
/// Returns a new tensor with inverse sine values.
///
/// # Arguments
///
/// * `tensor` - The input tensor.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with inverse sine values.
fn float_asin(tensor: FloatTensor<B>) -> FloatTensor<B>;
/// Returns a new tensor with inverse hyperbolic sine values.
///
/// # Arguments
///
/// * `tensor` - The input tensor.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with inverse hyperbolic sine values.
fn float_asinh(tensor: FloatTensor<B>) -> FloatTensor<B>;
/// Returns a new tensor with the inverse tangent values.
///
/// # Arguments
///
/// * `tensor` - The input tensor.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with the inverse tangent values.
fn float_atan(tensor: FloatTensor<B>) -> FloatTensor<B>;
/// Returns a new tensor with the inverse hyperbolic tangent values.
///
/// # Arguments
///
/// * `tensor` - The input tensor.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with the inverse hyperbolic tangent values.
fn float_atanh(tensor: FloatTensor<B>) -> FloatTensor<B>;
/// Returns a tensor with the four-quadrant inverse tangent values of `y` and `x`.
///
/// # Arguments
///
/// * `lhs` - The tensor with y coordinates.
/// * `rhs` - The tensor with x coordinates.
///
/// # Returns
///
/// A tensor with the four-quadrant inverse tangent values.
fn float_atan2(lhs: FloatTensor<B>, rhs: FloatTensor<B>) -> FloatTensor<B>;
/// Returns a new tensor with rounded values.
///
/// This function should implement 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.
///
/// # Arguments
///
/// * `tensor` - The tensor to be rounded.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with rounded values.
fn float_round(tensor: FloatTensor<B>) -> FloatTensor<B>;
/// Returns a new tensor with floored values.
///
/// # Arguments
///
/// * `tensor` - The tensor to be floored.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with floored values.
fn float_floor(tensor: FloatTensor<B>) -> FloatTensor<B>;
/// Returns a new tensor with ceiled values.
///
/// # Arguments
///
/// * `tensor` - The tensor to be ceiled.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with ceiled values.
fn float_ceil(tensor: FloatTensor<B>) -> FloatTensor<B>;
/// Returns a new tensor with truncated values.
///
/// # Arguments
///
/// * `tensor` - The tensor to be truncated.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with truncated values.
fn float_trunc(tensor: FloatTensor<B>) -> FloatTensor<B>;
/// Returns a new tensor with the error function values.
///
/// # Arguments
///
/// * `tensor` - The tensor to take the error function of.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` with error function values.
fn float_erf(tensor: FloatTensor<B>) -> FloatTensor<B>;
/// Concatenates tensors along a dimension.
///
/// # Arguments
///
/// * `tensors` - The tensors to concatenate.
/// * `dim` - The dimension along which to concatenate.
///
/// # Returns
///
/// A tensor with the concatenated tensors along `dim`.
///
/// # Note
///
/// Empty tensors (where the concatenation dimension has size 0) are filtered out at the
/// high-level tensor API and will not be passed to this method. Backend implementations do
/// not need to handle empty tensors.
fn float_cat(tensors: Vec<FloatTensor<B>>, dim: usize) -> FloatTensor<B> {
cat_with_slice_assign::<B, Float>(
tensors.into_iter().map(TensorPrimitive::Float).collect(),
dim,
)
.tensor()
}
/// Gets the indices of the maximum elements of a tensor along an axis.
///
/// # Arguments
///
/// * `tensor` - The tensor to get the maximum elements of.
/// * `dim` - The dimension along which to get the maximum elements.
///
/// # Returns
///
/// A tensor with the indices of the maximum elements of `tensor` along `dim`.
fn float_argmax(tensor: FloatTensor<B>, dim: usize) -> IntTensor<B>;
/// Gets the indices of the minimum elements of a tensor along an axis.
///
/// # Arguments
///
/// * `tensor` - The tensor to get the minimum elements of.
/// * `dim` - The dimension along which to get the minimum elements.
///
/// # Returns
///
/// A tensor with the indices of the minimum elements of `tensor` along `dim`.
fn float_argmin(tensor: FloatTensor<B>, dim: usize) -> IntTensor<B>;
/// Gets the maximum element of a tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor to get the maximum elements of.
///
/// # Returns
///
/// A tensor with the maximum element of `tensor`.
fn float_max(tensor: FloatTensor<B>) -> FloatTensor<B> {
let shape = tensor.shape();
let tensor = B::float_reshape(tensor, Shape::new([shape.num_elements()]));
B::float_max_dim(tensor, 0)
}
/// Gets the maximum elements of a tensor along an axis.
///
/// # Arguments
///
/// * `tensor` - The tensor to get the maximum elements of.
/// * `dim` - The dimension along which to get the maximum elements.
///
/// # Returns
///
/// A tensor with the maximum elements of `tensor` along `dim`.
fn float_max_dim(tensor: FloatTensor<B>, dim: usize) -> FloatTensor<B> {
let index = B::float_argmax(tensor.clone(), dim);
B::float_gather(dim, tensor, index)
}
/// Gets the maximum elements of a tensor along an axis and their indices.
///
/// # Arguments
///
/// * `tensor` - The tensor to get the maximum elements of.
/// * `dim` - The dimension along which to get the maximum elements.
///
/// # Returns
///
/// A tuple with the maximum elements of `tensor` along `dim` and their indices.
fn float_max_dim_with_indices(
tensor: FloatTensor<B>,
dim: usize,
) -> (FloatTensor<B>, IntTensor<B>) {
let index = B::float_argmax(tensor.clone(), dim);
let values = B::float_gather(dim, tensor, index.clone());
(values, index)
}
/// Gets the minimum element of a tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor to get the minimum elements of.
///
/// # Returns
///
/// A tensor with the minimum element of `tensor`.
fn float_min(tensor: FloatTensor<B>) -> FloatTensor<B> {
let shape = tensor.shape();
let tensor = B::float_reshape(tensor, Shape::new([shape.num_elements()]));
B::float_min_dim(tensor, 0)
}
/// Gets the minimum elements of a tensor along an axis.
///
/// # Arguments
///
/// * `tensor` - The tensor to get the minimum elements of.
/// * `dim` - The dimension along which to get the minimum elements.
///
/// # Returns
///
/// A tensor with the minimum elements of `tensor` along `dim`.
fn float_min_dim(tensor: FloatTensor<B>, dim: usize) -> FloatTensor<B> {
let index = B::float_argmin(tensor.clone(), dim);
B::float_gather(dim, tensor, index)
}
/// Gets the minimum elements of a tensor along an axis and their indices.
///
/// # Arguments
///
/// * `tensor` - The tensor to get the minimum elements of.
/// * `dim` - The dimension along which to get the minimum elements.
///
/// # Returns
///
/// A tuple with the minimum elements of `tensor` along `dim` and their indices.
fn float_min_dim_with_indices(
tensor: FloatTensor<B>,
dim: usize,
) -> (FloatTensor<B>, IntTensor<B>) {
let index = B::float_argmin(tensor.clone(), dim);
let values = B::float_gather(dim, tensor, index.clone());
(values, index)
}
/// Gets the maximum absolute element of a tensor.
///
/// # Arguments
///
/// * `tensor` - The tensor to get the maximum elements of.
///
/// # Returns
///
/// A tensor with the maximum element of `tensor`.
fn float_max_abs(tensor: FloatTensor<B>) -> FloatTensor<B> {
let shape = tensor.shape();
let tensor = B::float_reshape(tensor, Shape::new([shape.num_elements()]));
B::float_max_abs_dim(tensor, 0)
}
/// Gets the maximum absolute elements of a tensor along an axis.
///
/// # Arguments
///
/// * `tensor` - The tensor to get the maximum elements of.
/// * `dim` - The dimension along which to get the maximum elements.
///
/// # Returns
///
/// A tensor with the maximum elements of `tensor` along `dim`.
fn float_max_abs_dim(tensor: FloatTensor<B>, dim: usize) -> FloatTensor<B> {
B::float_max_dim(B::float_abs(tensor), dim)
}
/// Tests if any element in the float `tensor` evaluates to True.
///
/// # Arguments
///
/// * `tensor` - The tensor to test.
///
/// # Returns
///
/// A boolean tensor with a single element, True if any element in the tensor is True, False otherwise.
fn float_any(tensor: FloatTensor<B>) -> BoolTensor<B> {
let bool_tensor = B::float_equal_elem(tensor, 0.0f32.elem());
let bool_tensor = B::bool_not(bool_tensor);
let sum = B::float_sum(B::bool_into_float(bool_tensor));
B::float_greater_elem(sum, 0.0f32.elem())
}
/// Tests if any element in the float `tensor` evaluates to True along a given dimension `dim`.
///
/// # Arguments
///
/// * `tensor` - The tensor to test.
/// * `dim` - The axis along which to test.
///
/// # Returns
///
/// A boolean tensor `Tensor<B, D, Bool>` with the same size as input `tensor`, except in the `dim` axis
/// where the size is 1. The elem in the `dim` axis is True if any element along this dim in the
/// input evaluates to True, False otherwise.
fn float_any_dim(tensor: FloatTensor<B>, dim: usize) -> BoolTensor<B> {
let bool_tensor = B::float_equal_elem(tensor, 0.0f32.elem());
let bool_tensor = B::bool_not(bool_tensor);
let sum = B::float_sum_dim(B::bool_into_float(bool_tensor), dim);
B::float_greater_elem(sum, 0.0f32.elem())
}
/// Tests if all elements in the float `tensor` evaluate to True.
///
/// # Arguments
///
/// * `tensor` - The tensor to test.
///
/// # Returns
///
/// A boolean tensor `Tensor<B, 1, Bool>` with a single element, True if all elements in the input tensor
/// evaluate to True, False otherwise.
fn float_all(tensor: FloatTensor<B>) -> BoolTensor<B> {
let num_elems = tensor.shape().num_elements();
let bool_tensor = B::float_equal_elem(tensor, 0.0f32.elem());
let bool_tensor = B::bool_not(bool_tensor);
let sum = B::float_sum(B::bool_into_float(bool_tensor));
B::float_equal_elem(sum, (num_elems as f32).elem())
}
/// Tests if all elements in the float `tensor` evaluate to True along a given dimension `dim`.
///
/// # Arguments
///
/// * `tensor` - The tensor to test.
/// * `dim` - The axis along which to test.
///
/// # Returns
///
/// A boolean tensor `Tensor<B, D, Bool>` with the same size as input `tensor`, except in the `dim` axis
/// where the size is 1. The elem in the `dim` axis is True if all elements along this dim in the input
/// evaluates to True, False otherwise.
fn float_all_dim(tensor: FloatTensor<B>, dim: usize) -> BoolTensor<B> {
let num_elems = tensor.shape().dims[dim];
let bool_tensor = B::float_equal_elem(tensor, 0.0f32.elem());
let bool_tensor = B::bool_not(bool_tensor);
let sum = B::float_sum_dim(B::bool_into_float(bool_tensor), dim);
B::float_equal_elem(sum, (num_elems as f32).elem())
}
/// Returns the signs of the float `tensor`.
///
/// # Arguments
///
/// * `tensor` - The tensor to extract the signs from.
///
/// # Returns
///
/// A tensor with the same shape as `tensor` containing the signs of the elements of `tensor`.
fn float_sign(tensor: FloatTensor<B>) -> FloatTensor<B> {
let zeros = B::float_zeros(
tensor.shape(),
&B::float_device(&tensor),
tensor.dtype().into(),
);
let less_than_zero = B::float_lower_elem(tensor.clone(), 0.0f32.elem());
let greater_than_zero = B::float_greater_elem(tensor, 0.0f32.elem());
let mut result = B::float_mask_fill(zeros, less_than_zero, (-1.0f32).elem());
result = B::float_mask_fill(result, greater_than_zero, 1.0f32.elem());
result
}
/// Broadcasts the float `tensor` to the given `shape`.
fn float_expand(tensor: FloatTensor<B>, shape: Shape) -> FloatTensor<B>;
/// Sort the elements of the input `tensor` by value in along a given dimension.
///
/// This sort is unstable (i.e., may reorder equal elements).
///
/// # Arguments
///
/// * `tensor` - The input tensor.
/// * `dim` - The axis along which to sort.
/// * `descending` - The sorting order.
///
/// # Returns
///
/// A tensor with the same shape as the input tensor, where the elements are sorted by value.
fn float_sort(tensor: FloatTensor<B>, dim: usize, descending: bool) -> FloatTensor<B> {
sort::<B, Float>(TensorPrimitive::Float(tensor), dim, descending).tensor()
}
/// Sort the elements of the input `tensor` by value in along a given dimension.
///
/// This sort is unstable (i.e., may reorder equal elements).
///
/// # Arguments
///
/// * `tensor` - The input tensor.
/// * `dim` - The axis along which to sort.
/// * `descending` - The sorting order.
///
/// # Returns
///
/// A tensor with the same shape as the input tensor and corresponding indices, where
/// the elements are sorted by value and the indices map back to the original input tensor.
fn float_sort_with_indices(
tensor: FloatTensor<B>,
dim: usize,
descending: bool,
) -> (FloatTensor<B>, IntTensor<B>) {
let (values, indices) =
sort_with_indices::<B, Float>(TensorPrimitive::Float(tensor), dim, descending);
(values.tensor(), indices)
}
/// Returns the indices that sort the elements of the input `tensor` by value along a given dimension.
///
/// This sort is unstable (i.e., may reorder equal elements).
///
/// # Arguments
///
/// * `tensor` - The input tensor.
/// * `dim` - The axis along which to sort.
/// * `descending` - The sorting order.
///
/// # Returns
///
/// A tensor with the same shape as the input tensor the indices map back to the original input tensor.
fn float_argsort(tensor: FloatTensor<B>, dim: usize, descending: bool) -> IntTensor<B> {
argsort::<B, Float>(TensorPrimitive::Float(tensor), dim, descending)
}
/// Samples tensor as a two-dimensional spatial grid of (possibly multi-channel) values,
/// using the given locations in [-1, 1].
///
/// # Arguments
///
/// * `tensor` - The tensor being sampled from, must be contiguous with shape (N, C, H_in, W_in)
/// * `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)
fn float_grid_sample_2d(
tensor: FloatTensor<B>,
grid: FloatTensor<B>,
options: GridSampleOptions,
) -> FloatTensor<B> {
float_grid_sample_2d_ref::<B>(tensor, grid, options)
}
/// Unfold windows along a dimension.
///
/// Returns a view of the tensor with all complete windows of size `size` in dimension `dim`;
/// where windows are advanced by `step` at each index.
///
/// The number of windows is `max(0, (shape[dim] - size).ceil_div(step))`.
///
/// # Arguments
///
/// * `tensor` - The input tensor to unfold; of shape ``[pre=..., dim shape, post=...]``
/// * `dim` - the selected dim.
/// * `size` - the size of each unfolded window.
/// * `step` - the step between each window.
///
/// # Returns
///
/// A tensor view with shape ``[pre=..., windows, size, post=...]``.
fn float_unfold(tensor: FloatTensor<B>, dim: usize, size: usize, step: usize)
-> FloatTensor<B>;
/// 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.
fn float_is_nan(tensor: FloatTensor<B>) -> BoolTensor<B> {
// Check if the input tensor is NaN by comparing it to itself
// NaN is the only value that is not equal to itself
B::float_not_equal(tensor.clone(), tensor)
}
/// 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
fn float_is_inf(tensor: FloatTensor<B>) -> BoolTensor<B> {
B::float_equal_elem(B::float_abs(tensor), f64::INFINITY.elem())
}
}