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
//! Statistical operations for tensors with numerical stability enhancements.
//!
//! This module contains comprehensive statistical operations including:
//! - **Aggregation functions**: sum, mean with flexible axis support
//! - **Descriptive statistics**: variance, standard deviation with robust calculation
//! - **Extremal operations**: min/max with optimized SIMD implementations
//! - **Index operations**: argmax with negative axis support
//! - **Utility functions**: min_max with optimized performance
//!
//! All operations include numerical stability features, comprehensive error handling,
//! and support for F32 and F64 tensor types where applicable.
use super::super::Tensor;
use super::utilities::{simd_min_max_f32, simd_min_max_f64};
use crate::errors::{Result, TrustformersError};
use scirs2_core::ndarray::{arr0, ArrayD, Axis, IxDyn, Zip};
impl Tensor {
/// Standard deviation across all elements.
///
/// Computes the standard deviation of all elements in the tensor,
/// returning a scalar tensor containing the result.
///
/// # Returns
///
/// A scalar tensor containing the standard deviation.
pub fn std(&self) -> Result<Tensor> {
match self {
Tensor::F32(a) => {
let mean = a.mean().expect("Mean calculation failed");
let variance =
a.mapv(|x| (x - mean).powi(2)).mean().expect("Mean calculation failed");
let std = variance.sqrt();
Ok(Tensor::F32(ArrayD::from_elem(IxDyn(&[]), std)))
},
Tensor::F64(a) => {
let mean = a.mean().expect("Mean calculation failed");
let variance =
a.mapv(|x| (x - mean).powi(2)).mean().expect("Mean calculation failed");
let std = variance.sqrt();
Ok(Tensor::F64(ArrayD::from_elem(IxDyn(&[]), std)))
},
_ => Err(TrustformersError::tensor_op_error(
"Standard deviation not supported for this tensor type",
"std",
)),
}
}
/// Maximum value.
pub fn max_value(&self) -> Result<Tensor> {
match self {
Tensor::F32(a) => {
let max_val = a.iter().fold(f32::NEG_INFINITY, |acc, &x| acc.max(x));
Ok(Tensor::F32(ArrayD::from_elem(IxDyn(&[]), max_val)))
},
_ => Err(TrustformersError::tensor_op_error(
"Max not supported for this tensor type",
"max_value",
)),
}
}
/// Element-wise maximum between two tensors.
pub fn max(&self, other: &Tensor) -> Result<Tensor> {
match (self, other) {
(Tensor::F32(a), Tensor::F32(b)) => {
// Handle scalar case (0-dimensional tensor)
if a.ndim() == 0 && b.ndim() > 0 {
// a is scalar, broadcast to b's shape
let scalar_val = a.iter().next().expect("array must have at least one element");
let result = b.mapv(|x| x.max(*scalar_val));
Ok(Tensor::F32(result))
} else if b.ndim() == 0 && a.ndim() > 0 {
// b is scalar, broadcast to a's shape
let scalar_val = b.iter().next().expect("array must have at least one element");
let result = a.mapv(|x| x.max(*scalar_val));
Ok(Tensor::F32(result))
} else if a.ndim() == 0 && b.ndim() == 0 {
// Both scalars
let a_val = a.iter().next().expect("array must have at least one element");
let b_val = b.iter().next().expect("array must have at least one element");
let max_val = a_val.max(*b_val);
Ok(Tensor::F32(ArrayD::from_elem(IxDyn(&[]), max_val)))
} else {
// Regular element-wise operation
let result = Zip::from(a).and(b).map_collect(|&x, &y| x.max(y));
Ok(Tensor::F32(result))
}
},
(Tensor::F64(a), Tensor::F64(b)) => {
// Handle scalar case (0-dimensional tensor)
if a.ndim() == 0 && b.ndim() > 0 {
// a is scalar, broadcast to b's shape
let scalar_val = a.iter().next().expect("array must have at least one element");
let result = b.mapv(|x| x.max(*scalar_val));
Ok(Tensor::F64(result))
} else if b.ndim() == 0 && a.ndim() > 0 {
// b is scalar, broadcast to a's shape
let scalar_val = b.iter().next().expect("array must have at least one element");
let result = a.mapv(|x| x.max(*scalar_val));
Ok(Tensor::F64(result))
} else if a.ndim() == 0 && b.ndim() == 0 {
// Both scalars
let a_val = a.iter().next().expect("array must have at least one element");
let b_val = b.iter().next().expect("array must have at least one element");
let max_val = a_val.max(*b_val);
Ok(Tensor::F64(ArrayD::from_elem(IxDyn(&[]), max_val)))
} else {
// Regular element-wise operation
let result = Zip::from(a).and(b).map_collect(|&x, &y| x.max(y));
Ok(Tensor::F64(result))
}
},
_ => Err(TrustformersError::tensor_op_error(
"Element-wise max not supported for these tensor types",
"max",
)),
}
}
/// Find the indices of maximum values along the specified axis.
///
/// # Arguments
/// * `axis` - The axis along which to find the maximum indices. Negative values count from the last axis.
///
/// # Returns
/// A tensor containing the indices of maximum values along the specified axis.
pub fn argmax(&self, axis: i32) -> Result<Tensor> {
match self {
Tensor::F32(a) => {
let ndim = a.ndim();
let axis = if axis < 0 { (ndim as i32 + axis) as usize } else { axis as usize };
if axis >= ndim {
return Err(TrustformersError::tensor_op_error(
&format!(
"Axis {} is out of bounds for tensor with {} dimensions",
axis, ndim
),
"argmax",
));
}
let indices = a.map_axis(Axis(axis), |lane| {
lane.iter()
.enumerate()
.max_by(|(_, a), (_, b)| {
a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)
})
.map(|(i, _)| i as f32)
.unwrap_or(0.0)
});
Ok(Tensor::F32(indices))
},
Tensor::F64(a) => {
let ndim = a.ndim();
let axis = if axis < 0 { (ndim as i32 + axis) as usize } else { axis as usize };
if axis >= ndim {
return Err(TrustformersError::tensor_op_error(
&format!(
"Axis {} is out of bounds for tensor with {} dimensions",
axis, ndim
),
"argmax",
));
}
let indices = a.map_axis(Axis(axis), |lane| {
lane.iter()
.enumerate()
.max_by(|(_, a), (_, b)| {
a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)
})
.map(|(i, _)| i as f64)
.unwrap_or(0.0)
});
Ok(Tensor::F64(indices))
},
_ => Err(TrustformersError::tensor_op_error(
"Argmax not supported for this tensor type",
"argmax",
)),
}
}
/// Mean value across all elements.
///
/// Computes the arithmetic mean of all elements in the tensor,
/// returning a scalar tensor containing the result.
///
/// # Returns
///
/// A scalar tensor containing the mean value.
pub fn mean(&self) -> Result<Tensor> {
match self {
Tensor::F32(a) => {
let mean = a.mean().expect("Mean calculation failed");
Ok(Tensor::F32(ArrayD::from_elem(IxDyn(&[]), mean)))
},
Tensor::F64(a) => {
let mean = a.mean().expect("Mean calculation failed");
Ok(Tensor::F64(ArrayD::from_elem(IxDyn(&[]), mean)))
},
_ => Err(TrustformersError::tensor_op_error(
"Mean not supported for this tensor type",
"mean",
)),
}
}
/// Find minimum and maximum values.
pub fn min_max(&self) -> Result<(f32, f32)> {
match self {
Tensor::F32(a) => {
let data = a.as_slice().expect("array must have contiguous layout");
let (min_val, max_val) = simd_min_max_f32(data);
Ok((min_val, max_val))
},
Tensor::F64(a) => {
let data = a.as_slice().expect("array must have contiguous layout");
let (min_val, max_val) = simd_min_max_f64(data);
Ok((min_val as f32, max_val as f32))
},
_ => Err(TrustformersError::tensor_op_error(
"Min/max not supported for this tensor type",
"min_max",
)),
}
}
/// Sum across specified axes with robust error handling.
///
/// Computes the sum along the specified axes. The axes are processed
/// in reverse order to maintain proper axis indexing during reduction.
///
/// # Arguments
///
/// * `axes` - The axes along which to compute the sum
///
/// # Returns
///
/// A tensor with sums computed along the specified axes.
pub fn sum_axes(&self, axes: &[usize]) -> Result<Tensor> {
match self {
Tensor::F32(a) => {
let mut result = a.clone();
for &axis in axes.iter().rev() {
// Reverse to maintain axis indices
if axis >= result.ndim() {
return Err(TrustformersError::tensor_op_error(
&format!(
"Axis {} is out of bounds for tensor with {} dimensions",
axis,
result.ndim()
),
"sum_axes",
));
}
result = result.sum_axis(Axis(axis));
}
Ok(Tensor::F32(result))
},
Tensor::F64(a) => {
let mut result = a.clone();
for &axis in axes.iter().rev() {
if axis >= result.ndim() {
return Err(TrustformersError::tensor_op_error(
&format!(
"Axis {} is out of bounds for tensor with {} dimensions",
axis,
result.ndim()
),
"sum_axes",
));
}
result = result.sum_axis(Axis(axis));
}
Ok(Tensor::F64(result))
},
_ => Err(TrustformersError::tensor_op_error(
"Sum along axes not supported for this tensor type",
"sum_axes",
)),
}
}
/// Sum all elements or along specified axes.
///
/// # Arguments
///
/// * `axes` - Optional axes to sum along. If None, sum all elements.
/// * `keepdims` - Whether to keep dimensions (currently ignored for compatibility).
///
/// # Returns
///
/// A tensor with the sum result.
pub fn sum(&self, axes: Option<Vec<usize>>, _keepdims: bool) -> Result<Tensor> {
match self {
Tensor::F32(a) => {
if let Some(axes) = axes {
if axes.is_empty() {
// Sum all elements
let sum_val = a.sum();
Ok(Tensor::F32(ArrayD::from_elem(IxDyn(&[]), sum_val)))
} else {
// Sum along specified axes
let mut result = a.clone();
for &axis in axes.iter().rev() {
result = result.sum_axis(Axis(axis));
}
Ok(Tensor::F32(result))
}
} else {
// Sum all elements
let sum_val = a.sum();
Ok(Tensor::F32(ArrayD::from_elem(IxDyn(&[]), sum_val)))
}
},
Tensor::F64(a) => {
if let Some(axes) = axes {
if axes.is_empty() {
// Sum all elements
let sum_val = a.sum();
Ok(Tensor::F64(ArrayD::from_elem(IxDyn(&[]), sum_val)))
} else {
// Sum along specified axes
let mut result = a.clone();
for &axis in axes.iter().rev() {
result = result.sum_axis(Axis(axis));
}
Ok(Tensor::F64(result))
}
} else {
// Sum all elements
let sum_val = a.sum();
Ok(Tensor::F64(ArrayD::from_elem(IxDyn(&[]), sum_val)))
}
},
_ => Err(TrustformersError::tensor_op_error(
"Sum not supported for this tensor type",
"sum",
)),
}
}
/// Mean along specified axes with robust error handling.
///
/// # Arguments
///
/// * `axes` - The axes along which to compute the mean
///
/// # Returns
///
/// A tensor with means computed along the specified axes.
pub fn mean_axes(&self, axes: &[usize]) -> Result<Tensor> {
match self {
Tensor::F32(a) => {
let mut result = a.clone();
for &axis in axes.iter().rev() {
// Reverse to maintain axis indices
if axis >= result.ndim() {
return Err(TrustformersError::tensor_op_error(
&format!(
"Axis {} is out of bounds for tensor with {} dimensions",
axis,
result.ndim()
),
"mean_axes",
));
}
result = result
.mean_axis(Axis(axis))
.expect("axis must be valid for mean operation");
}
Ok(Tensor::F32(result))
},
Tensor::F64(a) => {
let mut result = a.clone();
for &axis in axes.iter().rev() {
if axis >= result.ndim() {
return Err(TrustformersError::tensor_op_error(
&format!(
"Axis {} is out of bounds for tensor with {} dimensions",
axis,
result.ndim()
),
"mean_axes",
));
}
result = result
.mean_axis(Axis(axis))
.expect("axis must be valid for mean operation");
}
Ok(Tensor::F64(result))
},
_ => Err(TrustformersError::tensor_op_error(
"Mean along axes not supported for this tensor type",
"mean_axes",
)),
}
}
/// Sum along a single axis (convenience method).
///
/// # Arguments
///
/// * `axis` - The axis to sum along
///
/// # Returns
///
/// A tensor with the sum along the specified axis.
pub fn sum_axis(&self, axis: usize) -> Result<Tensor> {
self.sum_axes(&[axis])
}
/// Python-style sum along a dimension with negative axis support.
///
/// This is a convenience method that supports negative axis indexing
/// (e.g., -1 for last axis, -2 for second-to-last, etc.)
///
/// # Arguments
///
/// * `dim` - The dimension to sum along (supports negative indexing)
/// * `keepdims` - Whether to keep dimensions (currently ignored for compatibility)
///
/// # Returns
///
/// A tensor with the sum along the specified dimension.
///
/// # Examples
///
/// ```ignore
/// let tensor = Tensor::randn(&[2, 3, 4])?;
/// let sum_last = tensor.sum_dim(-1, false)?; // Sum along last axis
/// let sum_first = tensor.sum_dim(0, false)?; // Sum along first axis
/// ```
pub fn sum_dim(&self, dim: i64, _keepdims: bool) -> Result<Tensor> {
let ndim = self.shape().len();
let normalized_axis = if dim < 0 {
let abs_dim = (-dim) as usize;
if abs_dim > ndim {
return Err(TrustformersError::tensor_op_error(
&format!(
"Dimension {} is out of bounds for tensor with {} dimensions",
dim, ndim
),
"sum_dim",
));
}
ndim - abs_dim
} else {
let dim = dim as usize;
if dim >= ndim {
return Err(TrustformersError::tensor_op_error(
&format!(
"Dimension {} is out of bounds for tensor with {} dimensions",
dim, ndim
),
"sum_dim",
));
}
dim
};
self.sum_axis(normalized_axis)
}
/// Mean along a single axis (convenience method).
///
/// # Arguments
///
/// * `axis` - The axis to compute mean along
///
/// # Returns
///
/// A tensor with the mean along the specified axis.
pub fn mean_axis(&self, axis: usize) -> Result<Tensor> {
self.mean_axes(&[axis])
}
/// Variance computation along specified axes.
///
/// Computes the sample variance using the formula: Var(X) = E[(X - μ)²]
/// where μ is the mean. Supports computation along specific axes or
/// across the entire tensor.
///
/// # Arguments
///
/// * `axes` - Optional axes along which to compute variance. If None, compute across all elements.
/// * `keepdims` - Whether to keep dimensions (currently ignored for compatibility).
///
/// # Returns
///
/// A tensor containing the variance values.
pub fn variance(&self, axes: Option<&[usize]>, _keepdims: bool) -> Result<Tensor> {
match self {
Tensor::F32(_) => {
let mean_tensor = match axes {
Some(ax) => self.mean_axes(ax)?,
None => self.mean()?,
};
let diff = self.sub(&mean_tensor)?;
let squared_diff = diff.pow(2.0)?;
match axes {
Some(ax) => squared_diff.mean_axes(ax),
None => squared_diff.mean(),
}
},
Tensor::F64(_) => {
let mean_tensor = match axes {
Some(ax) => self.mean_axes(ax)?,
None => self.mean()?,
};
let diff = self.sub(&mean_tensor)?;
let squared_diff = diff.pow(2.0)?;
match axes {
Some(ax) => squared_diff.mean_axes(ax),
None => squared_diff.mean(),
}
},
_ => Err(TrustformersError::tensor_op_error(
"Variance only supported for F32 and F64 tensors",
"variance",
)),
}
}
/// Standard deviation computation along specified axes.
///
/// Computes the standard deviation as the square root of variance.
/// This provides a measure of spread in the same units as the original data.
///
/// # Arguments
///
/// * `axes` - Optional axes along which to compute standard deviation.
/// * `keepdims` - Whether to keep dimensions (currently ignored for compatibility).
///
/// # Returns
///
/// A tensor containing the standard deviation values.
pub fn std_dev(&self, axes: Option<&[usize]>, keepdims: bool) -> Result<Tensor> {
let var = self.variance(axes, keepdims)?;
var.sqrt()
}
/// Find maximum value across specified axes.
pub fn max_axes(&self, axes: &[usize]) -> Result<Tensor> {
match self {
Tensor::F32(a) => {
let mut result = a.clone();
for &axis in axes.iter().rev() {
// reverse to maintain axis indices
// Apply max reduction along the specified axis
let reduced =
result.fold_axis(Axis(axis), f32::NEG_INFINITY, |acc, &x| acc.max(x));
result = reduced;
}
Ok(Tensor::F32(result))
},
Tensor::F64(a) => {
let mut result = a.clone();
for &axis in axes.iter().rev() {
// reverse to maintain axis indices
// Apply max reduction along the specified axis
let reduced =
result.fold_axis(Axis(axis), f64::NEG_INFINITY, |acc, &x| acc.max(x));
result = reduced;
}
Ok(Tensor::F64(result))
},
_ => Err(TrustformersError::tensor_op_error(
"Max axes not supported for this tensor type",
"max_axes",
)),
}
}
/// Find minimum value across specified axes.
pub fn min_axes(&self, axes: &[usize]) -> Result<Tensor> {
match self {
Tensor::F32(a) => {
let mut result = a.clone();
for &axis in axes.iter().rev() {
// reverse to maintain axis indices
// Apply min reduction along the specified axis
let reduced = result.fold_axis(Axis(axis), f32::INFINITY, |acc, &x| acc.min(x));
result = reduced;
}
Ok(Tensor::F32(result))
},
Tensor::F64(a) => {
let mut result = a.clone();
for &axis in axes.iter().rev() {
// reverse to maintain axis indices
// Apply min reduction along the specified axis
let reduced = result.fold_axis(Axis(axis), f64::INFINITY, |acc, &x| acc.min(x));
result = reduced;
}
Ok(Tensor::F64(result))
},
_ => Err(TrustformersError::tensor_op_error(
"Min axes not supported for this tensor type",
"min_axes",
)),
}
}
/// Find maximum value in tensor (scalar reduction).
pub fn max_scalar(&self) -> Result<Tensor> {
match self {
Tensor::F32(a) => {
let max_val = a.iter().fold(f32::NEG_INFINITY, |acc, &x| acc.max(x));
Ok(Tensor::F32(arr0(max_val).into_dyn()))
},
Tensor::F64(a) => {
let max_val = a.iter().fold(f64::NEG_INFINITY, |acc, &x| acc.max(x));
Ok(Tensor::F64(arr0(max_val).into_dyn()))
},
Tensor::I64(a) => {
let max_val = a.iter().fold(i64::MIN, |acc, &x| acc.max(x));
Ok(Tensor::I64(arr0(max_val).into_dyn()))
},
_ => Err(TrustformersError::tensor_op_error(
"max_scalar not implemented for this tensor type",
"max_scalar",
)),
}
}
/// Find minimum value in tensor (scalar reduction).
pub fn min_scalar(&self) -> Result<Tensor> {
match self {
Tensor::F32(a) => {
let min_val = a.iter().fold(f32::INFINITY, |acc, &x| acc.min(x));
Ok(Tensor::F32(arr0(min_val).into_dyn()))
},
Tensor::F64(a) => {
let min_val = a.iter().fold(f64::INFINITY, |acc, &x| acc.min(x));
Ok(Tensor::F64(arr0(min_val).into_dyn()))
},
Tensor::I64(a) => {
let min_val = a.iter().fold(i64::MAX, |acc, &x| acc.min(x));
Ok(Tensor::I64(arr0(min_val).into_dyn()))
},
_ => Err(TrustformersError::tensor_op_error(
"min_scalar not implemented for this tensor type",
"min_scalar",
)),
}
}
/// Sample from multinomial distribution.
///
/// Samples from a multinomial distribution defined by the probabilities in the input tensor.
/// This is useful for sampling tokens during text generation.
///
/// # Arguments
///
/// * `num_samples` - Number of samples to draw
/// * `replacement` - Whether to sample with replacement (must be true currently)
///
/// # Returns
///
/// A tensor containing sampled indices.
///
/// # Errors
///
/// - `TensorOpError`: If the tensor is not a probability distribution (doesn't sum to ~1.0)
///
/// # Examples
///
/// ```no_run
/// use trustformers_core::tensor::Tensor;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // Create a probability distribution
/// let probs = Tensor::from_vec(vec![0.1, 0.2, 0.3, 0.4], &[4])?;
/// let probs = probs.softmax(0)?; // Ensure it sums to 1.0
///
/// // Sample from the distribution
/// let samples = probs.multinomial(1, true)?;
/// # Ok(())
/// # }
/// ```
pub fn multinomial(&self, num_samples: usize, replacement: bool) -> Result<Tensor> {
use scirs2_core::random::*;
if !replacement {
return Err(TrustformersError::tensor_op_error(
"Sampling without replacement is not yet supported",
"multinomial",
));
}
match self {
Tensor::F32(probs) => {
let mut rng = thread_rng();
// Get the shape and flatten if needed
let shape = probs.shape();
let last_dim = shape[shape.len() - 1];
// Calculate total number of distributions
let num_dists: usize = shape[..shape.len() - 1].iter().product();
// Prepare output shape
let mut output_shape = shape[..shape.len() - 1].to_vec();
output_shape.push(num_samples);
let mut samples = Vec::with_capacity(num_dists * num_samples);
// Sample from each distribution
for dist_idx in 0..num_dists {
// Extract probabilities for this distribution
let offset = dist_idx * last_dim;
let prob_slice = &probs.as_slice().expect("array must have contiguous layout")
[offset..offset + last_dim];
// Compute cumulative distribution
let mut cumsum = Vec::with_capacity(last_dim);
let mut sum = 0.0f32;
for &p in prob_slice {
sum += p;
cumsum.push(sum);
}
// Sample using inverse transform sampling
for _ in 0..num_samples {
let u: f32 = rng.random();
let u_scaled = u * sum;
// Find the first index where cumsum >= u_scaled
let idx =
cumsum.iter().position(|&c| c >= u_scaled).unwrap_or(last_dim - 1);
samples.push(idx as i64);
}
}
Ok(Tensor::I64(ArrayD::from_shape_vec(
IxDyn(&output_shape),
samples,
)?))
},
Tensor::F64(probs) => {
let mut rng = thread_rng();
let shape = probs.shape();
let last_dim = shape[shape.len() - 1];
let num_dists: usize = shape[..shape.len() - 1].iter().product();
let mut output_shape = shape[..shape.len() - 1].to_vec();
output_shape.push(num_samples);
let mut samples = Vec::with_capacity(num_dists * num_samples);
for dist_idx in 0..num_dists {
let offset = dist_idx * last_dim;
let prob_slice = &probs.as_slice().expect("array must have contiguous layout")
[offset..offset + last_dim];
let mut cumsum = Vec::with_capacity(last_dim);
let mut sum = 0.0f64;
for &p in prob_slice {
sum += p;
cumsum.push(sum);
}
for _ in 0..num_samples {
let u: f64 = rng.random();
let u_scaled = u * sum;
let idx =
cumsum.iter().position(|&c| c >= u_scaled).unwrap_or(last_dim - 1);
samples.push(idx as i64);
}
}
Ok(Tensor::I64(ArrayD::from_shape_vec(
IxDyn(&output_shape),
samples,
)?))
},
_ => Err(TrustformersError::tensor_op_error(
"multinomial not supported for this tensor type",
"multinomial",
)),
}
}
/// Check if all elements are true (for boolean tensors) or non-zero.
///
/// Returns a scalar boolean tensor indicating whether all elements satisfy the condition.
///
/// # Returns
///
/// A scalar F32 tensor with value 1.0 if all elements are non-zero, 0.0 otherwise.
///
/// # Errors
///
/// - `TensorOpError`: If the operation is not supported for the tensor type
///
/// # Examples
///
/// ```no_run
/// use trustformers_core::tensor::Tensor;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let tensor = Tensor::from_vec(vec![1.0, 1.0, 1.0], &[3])?;
/// let result = tensor.all()?; // Should be 1.0 (true)
///
/// let tensor2 = Tensor::from_vec(vec![1.0, 0.0, 1.0], &[3])?;
/// let result2 = tensor2.all()?; // Should be 0.0 (false)
/// # Ok(())
/// # }
/// ```
pub fn all(&self) -> Result<Tensor> {
match self {
Tensor::F32(arr) => {
let all_true = arr.iter().all(|&x| x != 0.0);
let result = if all_true { 1.0f32 } else { 0.0f32 };
Ok(Tensor::F32(ArrayD::from_elem(IxDyn(&[]), result)))
},
Tensor::F64(arr) => {
let all_true = arr.iter().all(|&x| x != 0.0);
let result = if all_true { 1.0f32 } else { 0.0f32 };
Ok(Tensor::F32(ArrayD::from_elem(IxDyn(&[]), result)))
},
Tensor::I64(arr) => {
let all_true = arr.iter().all(|&x| x != 0);
let result = if all_true { 1.0f32 } else { 0.0f32 };
Ok(Tensor::F32(ArrayD::from_elem(IxDyn(&[]), result)))
},
_ => Err(TrustformersError::tensor_op_error(
"all not supported for this tensor type",
"all",
)),
}
}
}
#[cfg(test)]
mod tests {
use crate::errors::Result;
use crate::tensor::Tensor;
#[test]
fn test_std_f32() -> Result<()> {
let t = Tensor::from_data(vec![1.0, 2.0, 3.0, 4.0, 5.0], &[5])?;
let s = t.std()?;
let data = s.data()?;
// std of [1,2,3,4,5] = sqrt(2.0) ~= 1.4142
assert!((data[0] - std::f64::consts::SQRT_2 as f32).abs() < 0.01);
Ok(())
}
#[test]
fn test_std_constant() -> Result<()> {
let t = Tensor::full(5.0, vec![10])?;
let s = t.std()?;
let data = s.data()?;
assert!(data[0].abs() < 1e-5);
Ok(())
}
#[test]
fn test_max_value() -> Result<()> {
let t = Tensor::from_data(vec![1.0, 5.0, 3.0, 2.0], &[4])?;
let m = t.max_value()?;
let data = m.data()?;
assert!((data[0] - 5.0).abs() < 1e-6);
Ok(())
}
#[test]
fn test_max_elementwise() -> Result<()> {
let a = Tensor::from_data(vec![1.0, 5.0, 3.0], &[3])?;
let b = Tensor::from_data(vec![2.0, 4.0, 6.0], &[3])?;
let result = a.max(&b)?;
let data = result.data()?;
assert!((data[0] - 2.0).abs() < 1e-6);
assert!((data[1] - 5.0).abs() < 1e-6);
assert!((data[2] - 6.0).abs() < 1e-6);
Ok(())
}
#[test]
fn test_mean_f32() -> Result<()> {
let t = Tensor::from_data(vec![2.0, 4.0, 6.0, 8.0], &[4])?;
let m = t.mean()?;
let data = m.data()?;
assert!((data[0] - 5.0).abs() < 1e-6);
Ok(())
}
#[test]
fn test_mean_single_element() -> Result<()> {
let t = Tensor::from_data(vec![42.0], &[1])?;
let m = t.mean()?;
let data = m.data()?;
assert!((data[0] - 42.0).abs() < 1e-6);
Ok(())
}
#[test]
fn test_min_max() -> Result<()> {
let t = Tensor::from_data(vec![-3.0, 1.0, 7.0, -1.0, 5.0], &[5])?;
let (min_val, max_val) = t.min_max()?;
assert!((min_val - (-3.0)).abs() < 1e-6);
assert!((max_val - 7.0).abs() < 1e-6);
Ok(())
}
#[test]
fn test_sum_all() -> Result<()> {
let t = Tensor::from_data(vec![1.0, 2.0, 3.0, 4.0], &[2, 2])?;
let s = t.sum(None, false)?;
let data = s.data()?;
assert!((data[0] - 10.0).abs() < 1e-5);
Ok(())
}
#[test]
fn test_sum_axis_0() -> Result<()> {
let t = Tensor::from_data(vec![1.0, 2.0, 3.0, 4.0], &[2, 2])?;
let s = t.sum_axis(0)?;
assert_eq!(s.shape(), vec![2]);
let data = s.data()?;
assert!((data[0] - 4.0).abs() < 1e-5);
assert!((data[1] - 6.0).abs() < 1e-5);
Ok(())
}
#[test]
fn test_sum_axis_1() -> Result<()> {
let t = Tensor::from_data(vec![1.0, 2.0, 3.0, 4.0], &[2, 2])?;
let s = t.sum_axis(1)?;
assert_eq!(s.shape(), vec![2]);
let data = s.data()?;
assert!((data[0] - 3.0).abs() < 1e-5);
assert!((data[1] - 7.0).abs() < 1e-5);
Ok(())
}
#[test]
fn test_mean_axis() -> Result<()> {
let t = Tensor::from_data(vec![1.0, 3.0, 5.0, 7.0], &[2, 2])?;
let m = t.mean_axis(0)?;
assert_eq!(m.shape(), vec![2]);
let data = m.data()?;
assert!((data[0] - 3.0).abs() < 1e-5);
assert!((data[1] - 5.0).abs() < 1e-5);
Ok(())
}
#[test]
fn test_variance() -> Result<()> {
let t = Tensor::from_data(vec![2.0, 4.0, 4.0, 4.0, 5.0, 5.0, 7.0, 9.0], &[8])?;
let v = t.variance(None, false)?;
let data = v.data()?;
// variance = mean of (x - mean)^2
assert!(data[0] > 0.0);
Ok(())
}
#[test]
fn test_argmax_1d() -> Result<()> {
let t = Tensor::from_data(vec![1.0, 5.0, 3.0, 2.0], &[4])?;
let idx = t.argmax(0)?;
// argmax returns indices - check shape is correct
assert_eq!(idx.shape(), Vec::<usize>::new());
Ok(())
}
#[test]
fn test_argmax_2d_axis0() -> Result<()> {
let t = Tensor::from_data(vec![1.0, 5.0, 3.0, 2.0], &[2, 2])?;
let idx = t.argmax(0)?;
assert_eq!(idx.shape(), vec![2]);
Ok(())
}
#[test]
fn test_sum_axes_multiple() -> Result<()> {
let t = Tensor::ones(&[2, 3, 4])?;
let s = t.sum_axes(&[0, 2])?;
// After summing axes 0 and 2 from [2,3,4]: result should have shape [3]
assert_eq!(s.shape(), vec![3]);
let data = s.data()?;
for val in &data {
assert!((val - 8.0).abs() < 1e-5); // 2*4 = 8
}
Ok(())
}
#[test]
fn test_max_scalar() -> Result<()> {
let t = Tensor::from_data(vec![-1.0, 3.0, 2.0, 7.0, -5.0], &[5])?;
let m = t.max_scalar()?;
let data = m.data()?;
assert!((data[0] - 7.0).abs() < 1e-6);
Ok(())
}
#[test]
fn test_min_scalar() -> Result<()> {
let t = Tensor::from_data(vec![-1.0, 3.0, 2.0, 7.0, -5.0], &[5])?;
let m = t.min_scalar()?;
let data = m.data()?;
assert!((data[0] - (-5.0)).abs() < 1e-6);
Ok(())
}
#[test]
fn test_sum_dim_positive() -> Result<()> {
let t = Tensor::ones(&[2, 3])?;
let s = t.sum_dim(1, false)?;
assert_eq!(s.shape(), vec![2]);
let data = s.data()?;
for val in &data {
assert!((val - 3.0).abs() < 1e-5);
}
Ok(())
}
#[test]
fn test_sum_dim_negative() -> Result<()> {
let t = Tensor::ones(&[2, 3])?;
let s = t.sum_dim(-1, false)?;
assert_eq!(s.shape(), vec![2]);
Ok(())
}
#[test]
fn test_mean_axes() -> Result<()> {
let t = Tensor::ones(&[2, 3, 4])?;
let m = t.mean_axes(&[1])?;
assert_eq!(m.shape(), vec![2, 4]);
Ok(())
}
#[test]
fn test_max_axes() -> Result<()> {
let t = Tensor::from_data(vec![1.0, 5.0, 3.0, 2.0, 4.0, 6.0], &[2, 3])?;
let m = t.max_axes(&[1])?;
assert_eq!(m.shape(), vec![2]);
let data = m.data()?;
assert!((data[0] - 5.0).abs() < 1e-6);
assert!((data[1] - 6.0).abs() < 1e-6);
Ok(())
}
#[test]
fn test_min_axes() -> Result<()> {
let t = Tensor::from_data(vec![1.0, 5.0, 3.0, 2.0, 4.0, 6.0], &[2, 3])?;
let m = t.min_axes(&[1])?;
assert_eq!(m.shape(), vec![2]);
let data = m.data()?;
assert!((data[0] - 1.0).abs() < 1e-6);
assert!((data[1] - 2.0).abs() < 1e-6);
Ok(())
}
#[test]
fn test_std_dev_alias() -> Result<()> {
let t = Tensor::from_data(vec![1.0, 2.0, 3.0], &[3])?;
let s = t.std_dev(None, false)?;
let data = s.data()?;
assert!(data[0] > 0.0);
Ok(())
}
#[test]
fn test_all_ones() -> Result<()> {
let t = Tensor::ones(&[3])?;
let result = t.all()?;
let data = result.data()?;
assert!((data[0] - 1.0).abs() < 1e-6);
Ok(())
}
#[test]
fn test_all_with_zero() -> Result<()> {
let t = Tensor::from_data(vec![1.0, 0.0, 1.0], &[3])?;
let result = t.all()?;
let data = result.data()?;
assert!(data[0].abs() < 1e-6);
Ok(())
}
}