-
Notifications
You must be signed in to change notification settings - Fork 9
/
indexing.rs
1246 lines (1105 loc) · 43.6 KB
/
indexing.rs
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
use std::{
cmp::max,
iter,
marker::PhantomData,
ops::{Range, RangeFrom, RangeFull, RangeTo},
};
use crate::{
num::{Bool, CastFrom, Num},
Axes, DiffableOps, Shape, Tensor, ToCpu,
};
/// A variation of `Index` and `IndexMut`, that returns the output
/// by value. Sadly, we can't use the standard Index trait, because
/// it requires that the output be a reference. But we want to be able
/// to return new tensors, which we can't give a lifetime long enough so
/// they can be returned from the index method.
/// This means we also can't use the actual [] syntax.
pub trait BasicIndex<Idx> {
/// Basic indexing. Supports single indexing, slices, new axes, and ellipsis.
/// Always non-copying.
#[must_use]
fn ix(&self, index: Idx) -> Self;
}
/// In addition to basic indexing (slicing), allow indexing with bool and int tensors.
/// Following a proposal for numpy: <https://numpy.org/neps/nep-0021-advanced-indexing.html/>,
/// there are two methods that differ in their handling of int tensor indexes.
pub trait AdvancedIndex<Idx> {
/// Outer indexing. A straightforward generalization of slicing, with tensor indexes.
/// Copying if tensor indexes are used.
#[must_use]
fn oix(&self, index: Idx) -> Self;
/// Vectorized indexing. More powerful than oix, but also more complex. As opposed to
/// `oix`, any int tensor indexes are broadcasted together.
/// Copying if tensor indexes are used.
#[must_use]
fn vix(&self, index: Idx) -> Self;
}
/// A single index that specifies where to start counting from - the start of the axis, or the end.
/// In Python, the end is specified as a negative number, i.e. -1 means the last element.
/// -1 would be Tail(0) in this notation, which gives it a more pleasing symmetry.
#[derive(Clone, Copy, Debug)]
pub enum SingleIndex {
Head(usize),
Tail(usize),
}
#[derive(Clone)]
pub enum Fancy<I: DiffableOps> {
Full,
IntTensor(Tensor<I::Repr<i32>, i32, I>),
BoolTensor(Tensor<I::Repr<bool>, bool, I>),
}
pub enum IndexElement<I: DiffableOps> {
// A single element in an axis.
Single(SingleIndex),
// A range of elements in an axis. The second element is inclusive if the bool is true.
Slice(SingleIndex, SingleIndex, bool),
// Create a new axis with size 1.
NewAxis,
// Keep the remaining dimensions as is.
Ellipsis,
// Fancy index - mask or int tensor.
Fancy(Fancy<I>),
}
pub enum BasicIndexingWitness {}
pub enum AdvancedIndexingWitness {}
/// Specifies what to select along each axis.
#[must_use]
pub struct IndexSpec<I: DiffableOps, IndexingWitness> {
axes: Vec<IndexElement<I>>,
witness: PhantomData<IndexingWitness>,
}
/// Basic indexing is alwas non-copying. Start an index
/// specification with `IndexSpec::basic()` to ensure you don't copy.
impl<I: DiffableOps> IndexSpec<I, BasicIndexingWitness> {
pub fn basic() -> Self {
Self {
axes: Vec::new(),
witness: PhantomData,
}
}
}
/// Advanced indexing is always copying. Start an index
/// specification with `IndexSpec::advanced()` if you want to use advanced indexing.
impl<I: DiffableOps> IndexSpec<I, AdvancedIndexingWitness> {
pub fn advanced() -> Self {
Self {
axes: Vec::new(),
witness: PhantomData,
}
}
}
/// Build an [`IndexSpec`] by chaining calls to [`IndexSpecBuilder::idx`].
pub trait IndexSpecBuilder<Idx> {
#[must_use]
fn idx(self, index: Idx) -> Self;
}
impl<I: DiffableOps, W> IndexSpecBuilder<Range<usize>> for IndexSpec<I, W> {
fn idx(self, index: Range<usize>) -> Self {
self.idx(SingleIndex::Head(index.start)..SingleIndex::Head(index.end))
}
}
impl<I: DiffableOps, W> IndexSpecBuilder<Range<SingleIndex>> for IndexSpec<I, W> {
fn idx(mut self, index: Range<SingleIndex>) -> Self {
self.axes
.push(IndexElement::Slice(index.start, index.end, false));
self
}
}
impl<I: DiffableOps, W> IndexSpecBuilder<RangeFrom<usize>> for IndexSpec<I, W> {
fn idx(mut self, index: RangeFrom<usize>) -> Self {
self.axes.push(IndexElement::Slice(
SingleIndex::Head(index.start),
SingleIndex::Tail(0),
true,
));
self
}
}
impl<I: DiffableOps, W> IndexSpecBuilder<RangeFrom<SingleIndex>> for IndexSpec<I, W> {
fn idx(mut self, index: RangeFrom<SingleIndex>) -> Self {
self.axes
.push(IndexElement::Slice(index.start, SingleIndex::Tail(0), true));
self
}
}
impl<I: DiffableOps, W> IndexSpecBuilder<RangeTo<usize>> for IndexSpec<I, W> {
fn idx(mut self, index: RangeTo<usize>) -> Self {
self.axes.push(IndexElement::Slice(
SingleIndex::Head(0),
SingleIndex::Head(index.end),
false,
));
self
}
}
impl<I: DiffableOps, W> IndexSpecBuilder<RangeTo<SingleIndex>> for IndexSpec<I, W> {
fn idx(mut self, index: RangeTo<SingleIndex>) -> Self {
self.axes
.push(IndexElement::Slice(SingleIndex::Head(0), index.end, false));
self
}
}
impl<I: DiffableOps, W> IndexSpecBuilder<RangeFull> for IndexSpec<I, W> {
fn idx(mut self, _: RangeFull) -> Self {
self.axes.push(IndexElement::Slice(
SingleIndex::Head(0),
SingleIndex::Tail(0),
true,
));
self
}
}
impl<I: DiffableOps, W> IndexSpecBuilder<usize> for IndexSpec<I, W> {
fn idx(mut self, index: usize) -> Self {
self.axes
.push(IndexElement::Single(SingleIndex::Head(index)));
self
}
}
/// Select a single index along an axis, where `i` is a zero-based index
/// starting from the head, i.e. the beginning of the dimension.
#[must_use]
pub const fn hd(i: usize) -> SingleIndex {
SingleIndex::Head(i)
}
/// Select a single index along an axis, where `i` is a zero-based index
/// starting from the tail, i.e. the end of the dimension. `tl(0)` is the last element.
#[must_use]
pub const fn tl(i: usize) -> SingleIndex {
SingleIndex::Tail(i)
}
impl<I: DiffableOps, W> IndexSpecBuilder<SingleIndex> for IndexSpec<I, W> {
fn idx(mut self, element: SingleIndex) -> Self {
self.axes.push(IndexElement::Single(element));
self
}
}
/// Indicate that a new axis with size 1 should be added at this point.
pub struct NewAxis;
impl<I: DiffableOps, W> IndexSpecBuilder<NewAxis> for IndexSpec<I, W> {
fn idx(mut self, _: NewAxis) -> Self {
self.axes.push(IndexElement::NewAxis);
self
}
}
/// Indicates that any remaining dimensions should be kept as is.
/// You can only use one `Ellipsis` per index spec.
pub struct Ellipsis;
impl<I: DiffableOps, W> IndexSpecBuilder<Ellipsis> for IndexSpec<I, W> {
fn idx(mut self, _: Ellipsis) -> Self {
self.axes.push(IndexElement::Ellipsis);
self
}
}
impl SingleIndex {
/// Given the index of the last element, return the index in the array.
/// The min valid index is assumed to be zero.
fn get_index(&self, max_valid_index: usize, clamp: usize) -> usize {
match self {
SingleIndex::Head(i) => *std::cmp::min(i, &clamp),
SingleIndex::Tail(i) => max_valid_index.saturating_sub(*std::cmp::min(i, &clamp)),
}
}
}
struct BasicIndexResolution<'a, I: DiffableOps> {
limits: Vec<(usize, usize)>,
flips: Vec<bool>,
shape: Vec<usize>,
fancy: Vec<&'a Fancy<I>>,
}
impl<'a, I: DiffableOps> BasicIndexResolution<'a, I> {
fn add_full_axis(&mut self, size: usize) {
self.limits.push((0, size));
self.flips.push(false);
self.shape.push(size);
self.fancy.push(&Fancy::Full);
}
fn add_fancy_int_axis(&mut self, size: usize, fancy_element: &'a Fancy<I>) {
self.limits.push((0, size));
self.flips.push(false);
self.shape.push(size);
self.fancy.push(fancy_element);
}
fn add_fancy_bool_axis(&mut self, sizes: &[usize], fancy_element: &'a Fancy<I>, ndims: usize) {
(0..ndims).for_each(|i| {
self.limits.push((0, sizes[i]));
self.flips.push(false);
self.shape.push(sizes[i]);
});
self.fancy.push(fancy_element);
}
fn add_single(&mut self, s: usize) {
// crop to the single element
self.limits.push((s, s + 1));
// no flip necessary
self.flips.push(false);
// no change to shape - this dimension is squeezed out.
}
fn add_slice(&mut self, s: usize, e: usize) {
if e >= s {
// if the range is increasing, we have s..e. Add the limits as is.
self.limits.push((s, e));
self.flips.push(false); // no need to flip
self.shape.push(e - s); // the new shape is the size of the range
} else {
// if the range is decreasing, we have e+1..s+1 because inclusive...exclusive is flipped.
// E.g. 3..1 is 3, 2. or equivalent with (2..4).rev()
// Add the limits in reverse order.
self.limits.push((e + 1, s + 1));
self.flips.push(true); // flip the axis
self.shape.push(s - e);
}
self.fancy.push(&Fancy::Full);
}
fn add_new_axis(&mut self) {
// no limits or flips change because this is a new axis.
self.shape.push(1);
self.fancy.push(&Fancy::Full);
}
fn fixup_empty(&mut self) {
// A hack, because tensorken doesn't currently deal with empty shapes well.
// Perhaps we should return an enum Scalar/Tensor instead of a tensor as the
// indexing result.
if self.shape.is_empty() {
self.shape.push(1);
self.fancy.push(&Fancy::Full);
}
}
}
impl<W, I: DiffableOps> IndexSpec<I, W> {
fn resolve_basic(&self, shape: &[usize]) -> BasicIndexResolution<I> {
// could be more, but oh well.
let new_shape_len = std::cmp::max(shape.len(), self.axes.len());
let mut result = BasicIndexResolution {
limits: Vec::with_capacity(shape.len()),
flips: Vec::with_capacity(shape.len()),
shape: Vec::with_capacity(new_shape_len),
fancy: Vec::with_capacity(new_shape_len),
};
let axes_len = self
.axes
.iter()
.filter(|a| !matches!(a, IndexElement::NewAxis))
.count();
// the index in the IndexSpec
let mut idx_i = 0;
// the index in the shape
let mut shape_i = 0;
while shape_i < shape.len() {
let size = shape[shape_i];
match self.axes.get(idx_i) {
None => {
// if there are no more index elements, keep the axis as is.
// This is equivalent to adding implicit ellipsis at the end.
result.add_full_axis(size);
shape_i += 1;
}
Some(index_element) => match index_element {
IndexElement::Fancy(fancy_element) => {
match fancy_element {
Fancy::IntTensor(_) => {
result.add_fancy_int_axis(size, fancy_element);
shape_i += 1;
}
Fancy::BoolTensor(b) => {
result.add_fancy_bool_axis(
&shape[shape_i..shape_i + b.shape().ndims()],
fancy_element,
b.shape().ndims(),
);
shape_i += b.shape().ndims();
}
Fancy::Full => {
result.add_full_axis(size);
shape_i += 1;
}
}
idx_i += 1;
}
IndexElement::Single(idx) => {
// translate the index to the actual index in the tensor
let s = idx.get_index(size - 1, size - 1);
result.add_single(s);
idx_i += 1;
shape_i += 1;
}
IndexElement::Slice(start, end, is_end_inclusive) => {
// Get the start index. Pass in size-1 because the last element has index == size-1.
let s = start.get_index(size - 1, size - 1);
// Get the end index. Pass size, because the last element of a range is exclusive, so the max valid index is size.
let e = if *is_end_inclusive {
end.get_index(size - 1, size - 1) + 1
} else {
end.get_index(size - 1, size)
};
result.add_slice(s, e);
idx_i += 1;
shape_i += 1;
}
IndexElement::NewAxis => {
// No limits or flips change because this is a new axis.
result.add_new_axis();
idx_i += 1;
}
IndexElement::Ellipsis => {
// Add a limit if there aren't enough remaining elements in the IndexSpec
let remaining_idx_elems = axes_len.saturating_sub(idx_i + 1);
let remaining_shape_dims = shape.len() - shape_i;
if remaining_idx_elems < remaining_shape_dims {
// The ellipsis need to do something in this axis. Keep axis as is.
result.add_full_axis(size);
shape_i += 1;
// don't increment idx_i, so the ellipsis can be used again.
} else {
// This was the last axis for which we need ellipsis. Move on to the next index element.
idx_i += 1;
}
}
},
}
}
// we may have new axes to add at the end.
while idx_i < self.axes.len() {
match self.axes.get(idx_i) {
Some(IndexElement::NewAxis) => result.add_new_axis(),
Some(IndexElement::Ellipsis) => (), // ignore
_ => panic!("Invalid index spec."),
}
idx_i += 1;
}
result.fixup_empty();
result
}
}
impl<E: Bool, I: DiffableOps> BasicIndex<IndexSpec<I, BasicIndexingWitness>>
for Tensor<I::Repr<E>, E, I>
{
/// Index a tensor using basic indexing. See [`IndexSpec::basic()`] to build an [`IndexSpec`].
fn ix(&self, index: IndexSpec<I, BasicIndexingWitness>) -> Self {
// first do the basic indexing - no copy.
let resolution = index.resolve_basic(self.shape());
self.crop(&resolution.limits)
.flip(&resolution.flips)
.reshape(&resolution.shape)
}
}
impl<I: DiffableOps + Clone> IndexSpecBuilder<&Tensor<I::Repr<i32>, i32, I>>
for IndexSpec<I, AdvancedIndexingWitness>
{
fn idx(mut self, t: &Tensor<I::Repr<i32>, i32, I>) -> Self {
self.axes
.push(IndexElement::Fancy(Fancy::IntTensor(t.clone())));
self
}
}
impl<I: DiffableOps + Clone> IndexSpecBuilder<&Tensor<I::Repr<bool>, bool, I>>
for IndexSpec<I, AdvancedIndexingWitness>
{
fn idx(mut self, t: &Tensor<I::Repr<bool>, bool, I>) -> Self {
self.axes
.push(IndexElement::Fancy(Fancy::BoolTensor(t.clone())));
self
}
}
impl<
T,
E: Num + CastFrom<bool>,
I: DiffableOps<Repr<E> = T>
+ ToCpu<Repr<E> = T>
+ ToCpu<Repr<bool> = <I as DiffableOps>::Repr<bool>>,
> AdvancedIndex<IndexSpec<I, AdvancedIndexingWitness>> for Tensor<T, E, I>
{
fn oix(&self, index: IndexSpec<I, AdvancedIndexingWitness>) -> Self {
// first do the basic indexing - no copy.
let resolution = index.resolve_basic(self.shape());
let basic = self
.crop(&resolution.limits)
.flip(&resolution.flips)
.reshape(&resolution.shape);
// then any advanced indexing - always copy.
let mut result = basic;
// the dimension we're at in the resulting shape after indexing
let mut dim_result = 0;
for (fancy, size) in resolution.fancy.iter().zip(resolution.shape) {
match fancy {
Fancy::Full => {
dim_result += 1;
}
Fancy::IntTensor(i) => {
oix_int(size, i, &mut result, dim_result);
dim_result += i.shape().ndims();
}
Fancy::BoolTensor(b) => {
ix_bool(b, &mut result, dim_result);
dim_result += 1;
}
};
}
result
}
fn vix(&self, index: IndexSpec<I, AdvancedIndexingWitness>) -> Self {
// first do the basic indexing - no copy.
let resolution = index.resolve_basic(self.shape());
let basic = self
.crop(&resolution.limits)
.flip(&resolution.flips)
.reshape(&resolution.shape);
// then any advanced indexing - always copy.
let mut result = basic;
// The dimension we're at in the result, without any int tensor index dimensions added at the front.
let mut dim_result_post = 0;
// The number of dimensions added at the front of the result, by int tensor indexes.
// This is the max number of dimensions seen in any int index tensor so far.
let mut dim_result_pre = 0;
#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
for (fancy, size) in resolution.fancy.iter().zip(resolution.shape) {
match fancy {
Fancy::Full => dim_result_post += 1,
Fancy::IntTensor(i) => {
// i_range.shape is [size]
let i_range =
Tensor::new(&[size], (0..size as i32).collect::<Vec<_>>().as_slice());
let mut i_shape = i.shape().to_vec();
i_shape.push(1);
// i_shape is [i.shape, 1]
let i_one_hot = i.reshape(&i_shape).eq(&i_range).cast::<E>();
// i_one_hot is thus [i.shape, size] via broadcasting on eq.
// Now, reshape i_one_hot to add any needed 1s before and after the size:
// [i.shape, 1, 1, ..., size, 1, 1, ...]
let mut i_one_hot_shape = i.shape().to_vec();
i_one_hot_shape.extend(iter::repeat(1).take(dim_result_post));
i_one_hot_shape.push(size);
i_one_hot_shape.extend(
iter::repeat(1).take(
result
.shape()
.ndims()
.saturating_sub(dim_result_pre + dim_result_post + 1),
),
);
let i_one_hot = i_one_hot.reshape(&i_one_hot_shape);
result = &result * &i_one_hot;
dim_result_pre = max(dim_result_pre, i.shape().ndims());
let dim_result = dim_result_pre + dim_result_post;
result = result.sum(&[dim_result]).squeeze(&Axes::Axis(dim_result));
// "Moved" one dimension from the original to the prefix where int tensor indexes are.
// So no change for dim_result_post.
// dim_result_post += 0;
}
Fancy::BoolTensor(b) => {
let dim_result = dim_result_pre + dim_result_post;
ix_bool(b, &mut result, dim_result);
dim_result_post += 1;
}
};
}
result
}
}
#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
fn ix_bool<
T,
E: Num + CastFrom<bool>,
I: DiffableOps<Repr<E> = T>
+ ToCpu<Repr<E> = T>
+ ToCpu<Repr<bool> = <I as DiffableOps>::Repr<bool>>,
>(
b: &Tensor<<I as DiffableOps>::Repr<bool>, bool, I>,
result: &mut Tensor<T, E, I>,
dim_result: usize,
) {
// Create the equivalent int index tensor, then use oix_int.
let vec = b.ravel();
let i_vec: Vec<_> = vec
.iter()
.enumerate()
.filter_map(|t| if *t.1 { Some(t.0 as i32) } else { None })
.collect();
let i_tensor: Tensor<_, i32, I> = Tensor::new(&[i_vec.len()], i_vec.as_slice());
// flatten the dimensions of the result tensor that are being indexed by the bool tensor to a 1D vector.
let mut result_shape = result.shape().to_vec();
result_shape.splice(
dim_result..(dim_result + b.shape().ndims()),
[b.shape().size()],
);
*result = result.reshape(&result_shape);
oix_int(b.shape().size(), &i_tensor, result, dim_result);
}
#[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)]
fn oix_int<T, E: Num + CastFrom<bool>, I: DiffableOps<Repr<E> = T> + ToCpu<Repr<E> = T>>(
size: usize,
i: &Tensor<<I as DiffableOps>::Repr<i32>, i32, I>,
result: &mut Tensor<T, E, I>,
dim_result: usize,
) {
let mut i_range_shape = vec![size];
let ones = vec![1; i.shape().ndims()];
i_range_shape.extend(&ones);
// i_range.shape is [size, 1, 1, ...]
let i_range = Tensor::new(
&i_range_shape,
(0..i_range_shape.size() as i32)
.collect::<Vec<_>>()
.as_slice(),
);
// i_one_hot.shape is [size, i.shape] by broadcasting on eq.
let i_one_hot = i.eq(&i_range).cast::<E>();
// make room in the result for the new dimensions by adding 1s after the current dimension, dim.
// We need as many as there are dimentsions in i.
let mut result_shape = result.shape().to_vec();
result_shape.splice((dim_result + 1)..=dim_result, ones);
// also reshape the i_one_hot to add any needed 1s after the current dimension. (Any needed 1s before are added by broadcasting)
let mut i_one_hot_shape = i_one_hot.shape().to_vec();
i_one_hot_shape.extend(vec![1; result.shape().ndims() - (dim_result + 1)]);
*result = result.reshape(&result_shape);
let i_one_hot = i_one_hot.reshape(&i_one_hot_shape);
*result = &*result * &i_one_hot;
*result = result.sum(&[dim_result]).squeeze(&Axes::Axis(dim_result));
}
impl<E: Bool, I: DiffableOps> Tensor<I::Repr<E>, E, I> {
/// Shorthand for outer indexing along the first axis.
pub fn ix1<T>(&self, index: T) -> Self
where
IndexSpec<I, BasicIndexingWitness>: IndexSpecBuilder<T>,
{
self.ix(IndexSpec::basic().idx(index))
}
/// Shorthand for outer indexing along the first two axes.
pub fn ix2<T1, T2>(&self, index1: T1, index2: T2) -> Self
where
IndexSpec<I, BasicIndexingWitness>: IndexSpecBuilder<T1> + IndexSpecBuilder<T2>,
{
self.ix(IndexSpec::basic().idx(index1).idx(index2))
}
/// Shorthand for outer indexing along the first three axes.
pub fn ix3<T1, T2, T3>(&self, index1: T1, index2: T2, index3: T3) -> Self
where
IndexSpec<I, BasicIndexingWitness>:
IndexSpecBuilder<T1> + IndexSpecBuilder<T2> + IndexSpecBuilder<T3>,
{
self.ix(IndexSpec::basic().idx(index1).idx(index2).idx(index3))
}
/// Shorthand for outer indexing along the first four axes.
pub fn ix4<T1, T2, T3, T4>(&self, index1: T1, index2: T2, index3: T3, index4: T4) -> Self
where
IndexSpec<I, BasicIndexingWitness>: IndexSpecBuilder<T1>
+ IndexSpecBuilder<T2>
+ IndexSpecBuilder<T3>
+ IndexSpecBuilder<T4>,
{
self.ix(IndexSpec::basic()
.idx(index1)
.idx(index2)
.idx(index3)
.idx(index4))
}
}
impl<
T,
E: Num + CastFrom<bool>,
I: DiffableOps<Repr<E> = T>
+ ToCpu<Repr<E> = T>
+ ToCpu<Repr<bool> = <I as DiffableOps>::Repr<bool>>,
> Tensor<T, E, I>
{
/// Shorthand for outer indexing along the first axis.
pub fn oix1<T1>(&self, index: T1) -> Self
where
IndexSpec<I, AdvancedIndexingWitness>: IndexSpecBuilder<T1>,
{
self.oix(IndexSpec::advanced().idx(index))
}
/// Shorthand for outer indexing along the first two axes.
pub fn oix2<T1, T2>(&self, index1: T1, index2: T2) -> Self
where
IndexSpec<I, AdvancedIndexingWitness>: IndexSpecBuilder<T1> + IndexSpecBuilder<T2>,
{
self.oix(IndexSpec::advanced().idx(index1).idx(index2))
}
/// Shorthand for outer indexing along the first three axes.
pub fn oix3<T1, T2, T3>(&self, index1: T1, index2: T2, index3: T3) -> Self
where
IndexSpec<I, AdvancedIndexingWitness>:
IndexSpecBuilder<T1> + IndexSpecBuilder<T2> + IndexSpecBuilder<T3>,
{
self.oix(IndexSpec::advanced().idx(index1).idx(index2).idx(index3))
}
/// Shorthand for outer indexing along the first four axes.
pub fn oix4<T1, T2, T3, T4>(&self, index1: T1, index2: T2, index3: T3, index4: T4) -> Self
where
IndexSpec<I, AdvancedIndexingWitness>: IndexSpecBuilder<T1>
+ IndexSpecBuilder<T2>
+ IndexSpecBuilder<T3>
+ IndexSpecBuilder<T4>,
{
self.oix(
IndexSpec::advanced()
.idx(index1)
.idx(index2)
.idx(index3)
.idx(index4),
)
}
/// Shorthand for outer indexing along the first axis.
pub fn vix1<T1>(&self, index: T1) -> Self
where
IndexSpec<I, AdvancedIndexingWitness>: IndexSpecBuilder<T1>,
{
self.vix(IndexSpec::advanced().idx(index))
}
/// Shorthand for outer indexing along the first two axes.
pub fn vix2<T1, T2>(&self, index1: T1, index2: T2) -> Self
where
IndexSpec<I, AdvancedIndexingWitness>: IndexSpecBuilder<T1> + IndexSpecBuilder<T2>,
{
self.vix(IndexSpec::advanced().idx(index1).idx(index2))
}
/// Shorthand for outer indexing along the first three axes.
pub fn vix3<T1, T2, T3>(&self, index1: T1, index2: T2, index3: T3) -> Self
where
IndexSpec<I, AdvancedIndexingWitness>:
IndexSpecBuilder<T1> + IndexSpecBuilder<T2> + IndexSpecBuilder<T3>,
{
self.vix(IndexSpec::advanced().idx(index1).idx(index2).idx(index3))
}
/// Shorthand for outer indexing along the first four axes.
pub fn vix4<T1, T2, T3, T4>(&self, index1: T1, index2: T2, index3: T3, index4: T4) -> Self
where
IndexSpec<I, AdvancedIndexingWitness>: IndexSpecBuilder<T1>
+ IndexSpecBuilder<T2>
+ IndexSpecBuilder<T3>
+ IndexSpecBuilder<T4>,
{
self.vix(
IndexSpec::advanced()
.idx(index1)
.idx(index2)
.idx(index3)
.idx(index4),
)
}
}
#[cfg(test)]
mod tests {
use crate::{Cpu32, CpuBool, CpuI32};
use super::*;
#[test]
fn test_at_simple() {
let t = CpuI32::new(&[2, 3], &[1, 2, 3, 4, 5, 6]);
assert_eq!(t.ix2(0, 0).to_scalar(), 1);
assert_eq!(t.ix2(0, 1).to_scalar(), 2);
assert_eq!(t.ix2(0, 2).to_scalar(), 3);
assert_eq!(t.ix2(1, 0).to_scalar(), 4);
assert_eq!(t.ix2(1, 1).to_scalar(), 5);
assert_eq!(t.ix2(1, 2).to_scalar(), 6);
}
#[test]
fn test_at_single_index() {
let t = CpuI32::new(&[2, 3], &[1, 2, 3, 4, 5, 6]);
assert_eq!(t.ix2(hd(0), hd(0)).to_scalar(), 1);
assert_eq!(t.ix2(hd(0), hd(1)).to_scalar(), 2);
assert_eq!(t.ix2(hd(0), hd(2)).to_scalar(), 3);
assert_eq!(t.ix2(hd(1), hd(0)).to_scalar(), 4);
assert_eq!(t.ix2(hd(1), hd(1)).to_scalar(), 5);
assert_eq!(t.ix2(hd(1), hd(2)).to_scalar(), 6);
assert_eq!(t.ix2(tl(0), tl(0)).to_scalar(), 6);
assert_eq!(t.ix2(tl(0), tl(1)).to_scalar(), 5);
assert_eq!(t.ix2(tl(0), tl(2)).to_scalar(), 4);
assert_eq!(t.ix2(tl(1), tl(0)).to_scalar(), 3);
assert_eq!(t.ix2(tl(1), tl(1)).to_scalar(), 2);
assert_eq!(t.ix2(tl(1), tl(2)).to_scalar(), 1);
}
#[test]
fn test_at_range() {
let t = CpuI32::new(
&[2, 3],
&[
1, 2, 3, //
4, 5, 6,
],
);
let r = t.ix2(0, ..);
assert_eq!(r.shape(), &[3]);
assert_eq!(r.ravel(), &[1, 2, 3]);
let r = t.ix2(1, ..);
assert_eq!(r.shape(), &[3]);
assert_eq!(r.ravel(), &[4, 5, 6]);
let r = t.ix2(.., 0);
assert_eq!(r.shape(), &[2]);
assert_eq!(r.ravel(), &[1, 4]);
let r = t.ix2(0..1, ..);
assert_eq!(r.shape(), &[1, 3]);
assert_eq!(r.ravel(), &[1, 2, 3]);
let r = t.ix1(1..2);
assert_eq!(r.shape(), &[1, 3]);
assert_eq!(r.ravel(), &[4, 5, 6]);
let r = t.ix2(.., 1..2);
assert_eq!(r.shape(), &[2, 1]);
assert_eq!(r.ravel(), &[2, 5]);
let r = t.ix2(..tl(0), ..tl(0));
assert_eq!(r.shape(), &[1, 2]);
assert_eq!(r.ravel(), &[1, 2]);
}
#[test]
fn test_at_reverse_range() {
let t = CpuI32::new(
&[2, 3],
&[
1, 2, 3, //
4, 5, 6,
],
);
let r = t.ix2(tl(0)..hd(0), ..);
assert_eq!(r.shape(), &[1, 3]);
assert_eq!(r.ravel(), &[4, 5, 6]);
let r = t.ix2(.., tl(1)..hd(0));
assert_eq!(r.shape(), &[2, 1]);
assert_eq!(r.ravel(), &[2, 5]);
let r = t.ix2(tl(0)..hd(0), tl(0)..hd(0));
assert_eq!(r.shape(), &[1, 2]);
assert_eq!(r.ravel(), &[6, 5]);
let r = t.ix2(hd(1)..hd(0), hd(2)..hd(0));
assert_eq!(r.shape(), &[1, 2]);
assert_eq!(r.ravel(), &[6, 5]);
}
#[test]
fn test_ellipsis() {
let t = CpuI32::new(&[3, 2, 4], &(0..24).collect::<Vec<_>>());
let r = t.ix2(1, Ellipsis);
assert_eq!(r.shape(), &[2, 4]);
assert_eq!(r.ravel(), (8..16).collect::<Vec<_>>());
let r = t.ix2(Ellipsis, 1);
assert_eq!(r.shape(), &[3, 2]);
assert_eq!(r.ravel(), vec![1, 5, 9, 13, 17, 21]);
let r = t.ix3(0, Ellipsis, 1);
assert_eq!(r.shape(), &[2]);
assert_eq!(r.ravel(), vec![1, 5]);
let r = t.ix4(0, Ellipsis, 1, 1);
assert_eq!(r.shape(), &[1]);
assert_eq!(r.ravel(), vec![5]);
}
#[test]
fn test_new_axis() {
let t = CpuI32::new(&[3, 2, 4], &(0..24).collect::<Vec<_>>());
let r = t.ix2(1, NewAxis);
assert_eq!(r.shape(), &[1, 2, 4]);
let r = t.ix4(1, NewAxis, 1, ..4);
assert_eq!(r.shape(), &[1, 4]);
let r = t.ix2(Ellipsis, NewAxis);
assert_eq!(r.shape(), &[3, 2, 4, 1]);
let r = t.ix4(NewAxis, Ellipsis, NewAxis, NewAxis);
assert_eq!(r.shape(), &[1, 3, 2, 4, 1, 1]);
}
#[test]
fn test_oix_int() {
// first index - one dimensional index tensor
let t = CpuI32::linspace(1, 24, 24u8).reshape(&[4, 2, 3]);
let i = CpuI32::new(&[2], &[2, 0]);
let r = t.oix1(&i);
assert_eq!(r.shape(), &[2, 2, 3]);
assert_eq!(r.ravel(), &[13, 14, 15, 16, 17, 18, 1, 2, 3, 4, 5, 6]);
// second index - one dimensional index tensor
let i = CpuI32::new(&[2], &[1, 0]);
let r = t.oix2(.., &i);
assert_eq!(r.shape(), &[4, 2, 3]);
assert_eq!(
r.ravel(),
&[
4, 5, 6, 1, 2, 3, //
10, 11, 12, 7, 8, 9, //
16, 17, 18, 13, 14, 15, //
22, 23, 24, 19, 20, 21
]
);
// third index - one dimensional index tensor
let i = CpuI32::new(&[2], &[1, 0]);
let r = t.oix2(Ellipsis, &i);
assert_eq!(r.shape(), &[4, 2, 2]);
assert_eq!(
r.ravel(),
&[
2, 1, 5, 4, //
8, 7, 11, 10, //
14, 13, 17, 16, //
20, 19, 23, 22
]
);
// first index - two dimensional index tensor
let i = CpuI32::new(&[2, 2], &[2, 0, 1, 3]);
let r = t.oix1(&i);
assert_eq!(r.shape(), &[2, 2, 2, 3]);
assert_eq!(
r.ravel(),
&[
13, 14, 15, 16, 17, 18, //
1, 2, 3, 4, 5, 6, //
7, 8, 9, 10, 11, 12, //
19, 20, 21, 22, 23, 24
]
);
// all indexes - one-dimensional index tensors
let i0 = CpuI32::new(&[4], &[2, 0, 1, 3]);
let i1 = CpuI32::new(&[2], &[1, 0]);
let i2 = CpuI32::new(&[2], &[2, 1]);
let r = t.oix3(&i0, &i1, &i2);
assert_eq!(r.shape(), &[4, 2, 2]);
assert_eq!(
r.ravel(),
&[18, 17, 15, 14, 6, 5, 3, 2, 12, 11, 9, 8, 24, 23, 21, 20]
);
// all indexes - two-dimensional index tensor
let i0 = CpuI32::new(&[2, 2], &[2, 0, 1, 3]);
let i1 = CpuI32::new(&[2], &[1, 0]);
let i2 = CpuI32::new(&[2], &[2, 1]);
let r = t.oix3(&i0, &i1, &i2);
assert_eq!(r.shape(), &[2, 2, 2, 2]);
assert_eq!(
r.ravel(),
&[18, 17, 15, 14, 6, 5, 3, 2, 12, 11, 9, 8, 24, 23, 21, 20]
);
// all indexes - all two-dimensional index tensor
let i0 = CpuI32::new(&[2, 2], &[2, 0, 1, 3]);
let i1 = CpuI32::new(&[1, 2], &[1, 0]);
let i2 = CpuI32::new(&[2, 1], &[2, 1]);
let r = t.oix3(&i0, &i1, &i2);
assert_eq!(r.shape(), &[2, 2, 1, 2, 2, 1]);
assert_eq!(
r.ravel(),
&[18, 17, 15, 14, 6, 5, 3, 2, 12, 11, 9, 8, 24, 23, 21, 20]
);
}
#[test]
fn test_oix_bool() {
// first index - one dimensional index tensor
let t = CpuI32::linspace(1, 24, 24u8).reshape(&[4, 2, 3]);
let i = CpuBool::new(&[4], &[false, false, true, false]);
let r = t.oix1(&i);
assert_eq!(r.shape(), &[1, 2, 3]);
assert_eq!(r.ravel(), &[13, 14, 15, 16, 17, 18]);
// second index - one dimensional index tensor
let i = CpuBool::new(&[2], &[true, false]);
let r = t.oix2(.., &i);
assert_eq!(r.shape(), &[4, 1, 3]);
assert_eq!(r.ravel(), &[1, 2, 3, 7, 8, 9, 13, 14, 15, 19, 20, 21]);
// third index - one dimensional index tensor
let i = CpuBool::new(&[3], &[true, false, false]);
let r = t.oix2(Ellipsis, &i);
assert_eq!(r.shape(), &[4, 2, 1]);
assert_eq!(r.ravel(), &[1, 4, 7, 10, 13, 16, 19, 22]);
// first index - two dimensional index tensor
let i = CpuBool::new(
&[4, 2],