-
Notifications
You must be signed in to change notification settings - Fork 77
/
Tensor.cs
2854 lines (2566 loc) · 102 KB
/
Tensor.cs
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
using UnityEngine.Assertions;
using System;
using System.Runtime.InteropServices;
using System.Text;
using Unity.Collections.LowLevel.Unsafe;
using UnityEngine;
namespace Unity.Barracuda {
/// <summary>
/// TensorShape are immutable representation of a Tensor dimensions and rank.
/// Depending on which constructor is used, the TensorShape will either be rank 8 and channels last (ie NHWC) or actual
/// rank with unnamed tensor dimensions when using the constructor that takes int[].
/// With legacy use (explicit named constructors) of TensorShape an axis can be of size 1. For example, a tensor
/// without spatial information will be N,1,1,C. With the use of TensorShape via the int[] constructor, then axes can
/// have values of 0.
/// </summary>
[Serializable]
public unsafe struct TensorShape
{
/// <summary>
/// Max rank
/// </summary>
public const int MaxRank = 8;
// The following dimension names are based on ONNX Dimension Denotation.
// see: https://github.com/onnx/onnx/blob/master/docs/DimensionDenotation.md
/// <summary>
/// Data channel dimension index number
/// </summary>
public const int DataChannel = 7;
/// <summary>
/// Channels dimension index number
/// </summary>
public const int C = DataChannel;
/// <summary>
/// Data feature 0 dimension index number
/// </summary>
public const int DataFeature0 = 6;
/// <summary>
/// Width dimension index number
/// </summary>
public const int W = DataFeature0;
/// <summary>
/// Data feature 1 dimension index number
/// </summary>
public const int DataFeature1 = 5;
/// <summary>
/// Height dimension index number
/// </summary>
public const int H = DataFeature1;
/// <summary>
/// Data feature 2 dimension index number
/// </summary>
public const int DataFeature2 = 4;
/// <summary>
/// Depth dimension index number
/// </summary>
public const int D = DataFeature2;
/// <summary>
/// Data feature 3 dimension index number
/// </summary>
public const int DataFeature3 = 3;
/// <summary>
/// Batch dimension index number
/// </summary>
public const int DataBatch = 2;
/// <summary>
/// Sequence length dimension index number
/// </summary>
public const int NumberOfDirections = 1;
/// <summary>
/// Sequence length dimension index number
/// </summary>
public const int SequenceLength = 0;
/// <summary>
/// Data features
/// </summary>
public static readonly int[] DataFeatures = { W, H, D, DataFeature3 };
/// <summary>
/// Kernel input channel dimension
/// </summary>
public const int KernelInChannel = 6;
/// <summary>
/// Kernel output channel dimension
/// </summary>
public const int KernelOutChannel = 7;
/// <summary>
/// Kernel spatial dimension 0
/// </summary>
public const int KernelSpatial0 = 5;
/// <summary>
/// Kernel spatial dimension 1
/// </summary>
public const int KernelSpatial1 = DataBatch; // NOTE: maps to batch
/// <summary>
/// Kernel spatial dimension 2
/// </summary>
public const int KernelSpatial2 = DataBatch-1; // NOTE: maps to numDirections
/// <summary>
/// Kernel spatial dimension 3
/// </summary>
public const int KernelSpatial3 = SequenceLength; // NOTE: maps to sequenceLength
/// <summary>
/// Kernel spatial dimensions
/// </summary>
public static readonly int[] KernelSpatials = { KernelSpatial0, KernelSpatial1, KernelSpatial2, KernelSpatial3 };
/// <summary>
/// Return the number of sequence.
/// </summary>
public int sequenceLength
{
get
{
if (hasNamedDimensions)
{
fixed (int* shape = &d0)
{
int value = shape[SequenceLength];
return value;
}
}
return 1;
}
}
/// <summary>
/// Return the number of direction.
/// </summary>
public int numberOfDirections
{
get
{
if (hasNamedDimensions)
{
fixed (int* shape = &d0)
{
int value = shape[NumberOfDirections];
return value;
}
}
return 1;
}
}
/// <summary>
/// Return the number of batch.
/// </summary>
public int batch
{
get
{
if (hasNamedDimensions)
{
fixed (int* shape = &d0)
{
int value = shape[DataBatch];
return value;
}
}
return this[0];
}
}
/// <summary>
/// Return the size of 3rd spatial dimension (axis is DataFeature3)
/// Internal for now, please use myTensorShape[DataFeature3] instead.
/// </summary>
internal int extraDimension
{
get
{
if (hasNamedDimensions)
{
fixed (int* shape = &d0)
{
int value = shape[DataFeature3];
return value;
}
}
return 1;
}
}
/// <summary>
/// Return the spatial depth (axis is DataFeature2).
/// </summary>
public int depth
{
get
{
if (hasNamedDimensions)
{
fixed (int* shape = &d0)
{
int value = shape[DataFeature2];
return value;
}
}
return 1;
}
}
/// <summary>
/// Return the spatial height (axis is DataFeature1).
/// </summary>
public int height
{
get
{
if (hasNamedDimensions)
{
fixed (int* shape = &d0)
{
int value = shape[DataFeature1];
return value;
}
}
return this[1];
}
}
/// <summary>
/// Return the spatial width (axis is DataFeature0).
/// </summary>
public int width
{
get
{
if (hasNamedDimensions)
{
fixed (int* shape = &d0)
{
int value = shape[DataFeature0];
return value;
}
}
return this[2];
}
}
/// <summary>
/// Return the number of channels.
/// </summary>
public int channels
{
get
{
if (hasNamedDimensions)
{
fixed (int* shape = &d0)
{
int value = shape[DataChannel];
return value;
}
}
return this[3];
}
}
// TODO: Use `fixed int m_Shape[MaxRank];` when debugger display works
int d0;
int d1;
int d2;
int d3;
int d4;
int d5;
int d6;
int d7;
#region Constructors
/// <summary>
/// Create a TensorShape of shape [S,R,N,T,D,H,W,C].
/// Currently seqLen must be 1.
/// </summary>
/// <param name="s">sequence</param>
/// <param name="r">direction</param>
/// <param name="n">batch</param>
/// <param name="t">time</param>
/// <param name="d">depth</param>
/// <param name="h">height</param>
/// <param name="w">width</param>
/// <param name="c">channels</param>
public TensorShape(int s, int r, int n, int t, int d, int h, int w, int c)
: this()
{
m_UsesNamedDimensions = NamedDimension.All;
m_Rank = MaxRank;
fixed (int* shape = &d0)
{
shape[SequenceLength] = s > 0 ? s : 1;
shape[NumberOfDirections] = r > 0 ? r : 1;
shape[DataBatch] = n > 0 ? n : 1;
shape[DataFeature3] = t > 0 ? t : 1;
shape[DataFeature2] = d > 0 ? d : 1;
shape[DataFeature1] = h > 0 ? h : 1;
shape[DataFeature0] = w > 0 ? w : 1;
shape[DataChannel] = c > 0 ? c : 1;
}
}
/// <summary>
/// Create a TensorShape of shape [1,1,N,1,D,H,W,C].
/// </summary>
/// <param name="n">batch</param>
/// <param name="d">depth</param>
/// <param name="h">height</param>
/// <param name="w">width</param>
/// <param name="c">channels</param>
public TensorShape(int n, int d, int h, int w, int c)
: this(1, 1, n, 1, d, h, w, c)
{
m_UsesNamedDimensions = NamedDimension.N | NamedDimension.D | NamedDimension.H | NamedDimension.W | NamedDimension.C;
}
/// <summary>
/// Create a TensorShape of shape [1,1,N,1,1,H,W,C].
/// </summary>
/// <param name="n">batch</param>
/// <param name="h">height</param>
/// <param name="w">width</param>
/// <param name="c">channels</param>
public TensorShape(int n, int h, int w, int c)
: this(n, 1, h, w, c)
{
m_UsesNamedDimensions = NamedDimension.N | NamedDimension.H | NamedDimension.W | NamedDimension.C;
}
/// <summary>
/// Create a TensorShape of shape [1,1,N,1,1,1,W,C].
/// </summary>
/// <param name="n">batch</param>
/// <param name="w">width</param>
/// <param name="c">channels</param>
public TensorShape(int n, int w, int c)
: this(n, 1, w, c)
{
m_UsesNamedDimensions = NamedDimension.N | NamedDimension.W | NamedDimension.C;
}
/// <summary>
/// Create a TensorShape of shape [1,1,N,1,1,1,1,C].
/// </summary>
/// <param name="n">batch</param>
/// <param name="c">channels</param>
public TensorShape(int n, int c)
: this(n, 1, c)
{
m_UsesNamedDimensions = NamedDimension.N | NamedDimension.C;
}
/// <summary>
/// Create a TensorShape of shape [1,1,N,1,1,1,1,1].
/// </summary>
/// <param name="n">batch</param>
public TensorShape(int n)
: this(n, 1)
{
m_UsesNamedDimensions = NamedDimension.N;
}
/// <summary>
/// Create a TensorShape of arbitrary `shape`.
/// </summary>
/// <param name="shape">shape as int array</param>
/// <param name="unnamedDimensions">create the shape with no specific, named layout</param>
public TensorShape(int[] shape, bool unnamedDimensions = false)
: this()
{
Assert.IsTrue(shape.Length <= MaxRank, $"Only shapes up to a maximum rank of {MaxRank} are supported.");
if (unnamedDimensions)
{
m_UsesNamedDimensions = NamedDimension.None;
m_Rank = shape.Length;
if (m_Rank > 0)
{
fixed (int* dst = &d0, src = &shape[0])
{
UnsafeUtility.MemCpy(dst, src, shape.Length * sizeof(int));
UnsafeUtility.MemSet(dst + shape.Length, 0, (MaxRank - shape.Length) * sizeof(int));
}
}
else
{
// Treat a scalar as a rank-1 tensor
m_Rank = 1;
fixed (int* dst = &d0)
{
UnsafeUtility.MemSet(dst, 0, MaxRank * sizeof(int));
dst[0] = 1;
}
}
}
else
{
TensorShape copy;
switch (shape.Length)
{
case 0:
// Treat a scalar as a rank-1 tensor
copy = new TensorShape(1);
break;
case 1:
copy = new TensorShape(shape[0]);
break;
case 2:
copy = new TensorShape(shape[0], shape[1]);
break;
case 3:
copy = new TensorShape(shape[0], shape[1], shape[2]);
break;
case 4:
copy = new TensorShape(shape[0], shape[1], shape[2], shape[3]);
break;
case 5:
copy = new TensorShape(shape[0], shape[1], shape[2], shape[3], shape[4]);
break;
#if UNITY_EDITOR
// Restricting this to editor-only since Burst cannot have exceptions, but this code should also not be
// run since there are no rank-6/7 named tensor constructors
case 6:
case 7:
throw new ArgumentException($"Must use unnamedDimensions = true for a rank {shape.Length} tensor");
#endif
case 8:
default:
copy = new TensorShape(shape[0], shape[1], shape[2], shape[3], shape[4], shape[5], shape[6], shape[7]);
break;
}
fixed (TensorShape* dst = &this)
{
UnsafeUtility.CopyStructureToPtr(ref copy, dst);
}
}
}
#endregion
#region Properties
[Flags]
enum NamedDimension : byte
{
S = 1 << SequenceLength,
R = 1 << NumberOfDirections,
N = 1 << DataBatch,
T = 1 << DataFeature3,
D = 1 << DataFeature2,
H = 1 << DataFeature1,
W = 1 << DataFeature0,
C = 1 << DataChannel,
None = 0,
All = S | R | N | T | D | H | W | C
}
/// <summary>
/// Whether this shape makes use of named dimensions or is nameless.
/// </summary>
public bool hasNamedDimensions => m_UsesNamedDimensions != 0;
NamedDimension m_UsesNamedDimensions;
/// <summary>
/// Kernel dimension ordering is [D,H,W,C,K] for efficiency purpose.
/// Return kernel intermediate dimension 0.
/// </summary>
public int kernelSpatialDepth => numberOfDirections;
/// <summary>
/// Kernel dimension ordering is [D,H,W,C,K] for efficiency purpose.
/// Return kernel height.
/// </summary>
public int kernelHeight => batch; //Use .batch so HWCK weight use 4D constructor for backward compatibility with 4D tensorShape.
/// <summary>
/// Kernel dimension ordering is [D,H,W,C,K] for efficiency purpose.
/// Return kernel width.
/// </summary>
public int kernelWidth => height;
/// <summary>
/// Kernel dimension ordering is [D,H,W,C,K] for efficiency purpose.
/// Return kernel depth (aka the number of input channels of the associated operator).
/// </summary>
public int kernelDepth => width;
/// <summary>
/// Kernel dimension ordering is [D,H,W,C,K] for efficiency purpose.
/// Return kernel count (aka the number of output channels of the associated operator).
/// </summary>
public int kernelCount => channels;
/// <summary>
/// Return the number of batch.
/// </summary>
public int flatHeight => batch;
/// <summary>
/// Return the T*D*H*W*C.
/// </summary>
public int flatWidth
{
get
{
int w = 1;
if (hasNamedDimensions)
{
w = extraDimension * depth * height * width * channels;
return w;
}
for (int i = 1; i < rank; i++)
{
w *= this[i];
}
return w;
}
}
/// <summary>
/// Return the total number of elements represented by this shape.
/// </summary>
public int length
{
get
{
int l = 1;
if (hasNamedDimensions)
{
l = sequenceLength * numberOfDirections * flatHeight * flatWidth;
return l;
}
for (int i = 0; i < rank; i++)
{
l *= this[i];
}
return l;
}
}
/// <summary>
/// Always 8 if legacy, named constructors are used otherwise the actual rank.
/// Look also at the `dimensions` property.
/// </summary>
public int rank => m_Rank;
int m_Rank;
/// <summary>
/// Return the count of non-unit dimension of this shape.
/// For example [N,1,1,C] dimensions is 2.
/// </summary>
public int dimensions
{
get
{
if (hasNamedDimensions) // legacy
return (sequenceLength > 1 ? 1 : 0) +
(numberOfDirections > 1 ? 1 : 0) +
(batch > 1 ? 1 : 0) +
(extraDimension > 1 ? 1 : 0) +
(depth > 1 ? 1 : 0) +
(height > 1 ? 1 : 0) +
(width > 1 ? 1 : 0) +
(channels > 1 ? 1 : 0);
return rank;
}
}
#endregion
#region Helpers
/// <summary>
/// Allow to use negative axis to access tensorShape backward.
/// `axis` should be from -rank to rank (exclusive).
/// </summary>
/// <param name="axis">axis</param>
/// <returns>adjusted axis</returns>
public int Axis(int axis)
{
Assert.IsTrue(axis > -rank && axis < rank);
return axis >= 0 ? axis: rank + axis;
}
/// <summary>
/// Given an offset in memory return the dimensions indices of the element as [_,_,N,_,_,H,W,C].
/// </summary>
/// <param name="index">one dimensional index (offset) in the memory</param>
/// <param name="n">batch</param>
/// <param name="h">height</param>
/// <param name="w">width</param>
/// <param name="c">channels</param>
public void GetPositionsFromIndex(int index, ref int n, ref int h, ref int w, ref int c)
{
var shape = this;
if (!hasNamedDimensions)
shape = AsNamed();
c = index % shape.channels;
w = (index / shape.channels) % shape.width;
h = (index / (shape.channels * shape.width)) % shape.height;
n = (index / (shape.channels * shape.width * shape.height * shape.depth * shape.extraDimension)) % shape.batch;
}
/// <summary>
/// Given an offset in memory return the dimensions indices of the element as [S,R,N,T,D,H,W,C].
/// </summary>
/// <param name="index">one dimensional index (offset) in the memory</param>
/// <param name="s">sequence</param>
/// <param name="r">direction</param>
/// <param name="n">batch</param>
/// <param name="t">time</param>
/// <param name="d">depth</param>
/// <param name="h">height</param>
/// <param name="w">width</param>
/// <param name="c">channels</param>
public void GetPositionsFromIndex(int index, ref int s, ref int r, ref int n, ref int t, ref int d, ref int h, ref int w, ref int c)
{
var shape = this;
if (!hasNamedDimensions)
shape = AsNamed();
c = index % shape.channels;
w = (index / shape.channels) % shape.width;
h = (index / (shape.channels * shape.width)) % shape.height;
d = (index / (shape.channels * shape.width * shape.height)) % shape.depth;
t = (index / (shape.channels * shape.width * shape.height * shape.depth)) % shape.extraDimension;
n = (index / (shape.channels * shape.width * shape.height * shape.depth * shape.extraDimension)) % shape.batch;
r = (index / (shape.channels * shape.width * shape.height * shape.depth * shape.extraDimension * shape.batch)) % shape.numberOfDirections;
s = (index / (shape.channels * shape.width * shape.height * shape.depth * shape.extraDimension * shape.batch * shape.numberOfDirections)) % shape.sequenceLength;
}
/// <summary>
/// Given an offset in memory return the dimensions indices of the element as [S,R,N,T,D,H,W,C] in ChannelFirst memory layout.
/// </summary>
/// <param name="index">one dimensional index (offset) in the memory</param>
/// <param name="s">sequence</param>
/// <param name="r">direction</param>
/// <param name="n">batch</param>
/// <param name="t">time</param>
/// <param name="d">depth</param>
/// <param name="h">height</param>
/// <param name="w">width</param>
/// <param name="c">channels</param>
internal void GetPositionsFromIndexChannelFirst(int index, ref int s, ref int r, ref int n, ref int t, ref int d, ref int h, ref int w, ref int c)
{
var shape = this;
if (!hasNamedDimensions)
shape = AsNamed();
w = index % shape.width;
h = (index / shape.width) % shape.height;
d = (index / (shape.width * shape.height)) % shape.depth;
t = (index / (shape.width * shape.height * shape.depth)) % shape.extraDimension;
c = (index / (shape.width * shape.height * shape.depth * shape.extraDimension)) % shape.channels;
n = (index / (shape.width * shape.height * shape.depth * shape.extraDimension * shape.channels)) % shape.batch;
r = (index / (shape.width * shape.height * shape.depth * shape.extraDimension * shape.channels * shape.batch)) % shape.numberOfDirections;
s = (index / (shape.width * shape.height * shape.depth * shape.extraDimension * shape.channels * shape.batch * shape.numberOfDirections)) % shape.sequenceLength;
}
/// <summary>
/// Given an offset in memory return the dimensions indices of the element as [_,_,N,_,_,H,W,C] in ChannelFirst format.
/// </summary>
/// <param name="index">one dimensional index (offset) in the memory</param>
/// <param name="n">batch</param>
/// <param name="h">height</param>
/// <param name="w">width</param>
/// <param name="c">channels</param>
internal void GetPositionsFromIndexChannelFirst(int index, ref int n, ref int h, ref int w, ref int c)
{
var shape = this;
if (!hasNamedDimensions)
shape = AsNamed();
w = index % shape.width;
h = (index / shape.width) % shape.height;
c = (index / (shape.width * shape.height * shape.depth * shape.extraDimension)) % shape.channels;
n = (index / (shape.width * shape.height * shape.depth * shape.extraDimension * shape.channels)) % shape.batch;
}
/// <summary>
/// Given an element dimensions indices [0,0,N,0,0,H,W,C] with broadcast support, return this element offset in memory.
/// </summary>
/// <param name="n">batch</param>
/// <param name="h">height</param>
/// <param name="w">width</param>
/// <param name="c">channels</param>
/// <returns></returns>
public int IndexWithBroadcast(int n, int h, int w, int c)
{
var shape = this;
if (!hasNamedDimensions)
shape = AsNamed();
n %= shape.batch;
h %= shape.height;
w %= shape.width;
c %= shape.channels;
return Index(n, h, w, c);
}
/// <summary>
/// Given an element dimensions indices [S,R,N,T,D,H,W,C] with broadcast support, return this element offset in memory.
/// </summary>
/// <param name="s">sequence</param>
/// <param name="r">direction</param>
/// <param name="n">batch</param>
/// <param name="t">time</param>
/// <param name="d">depth</param>
/// <param name="h">height</param>
/// <param name="w">width</param>
/// <param name="c">channels</param>
/// <returns>one dimensional index (offset in the flat memory region)</returns>
public int IndexWithBroadcast(int s, int r, int n, int t, int d, int h, int w, int c)
{
var shape = this;
if (!hasNamedDimensions)
shape = AsNamed();
s %= shape.sequenceLength;
r %= shape.numberOfDirections;
n %= shape.batch;
t %= shape.extraDimension;
d %= shape.depth;
h %= shape.height;
w %= shape.width;
c %= shape.channels;
return Index(s, r, n, t, d, h, w, c);
}
/// <summary>
/// Given an element dimensions indices [1,N,1,1,1,H,W,C] return this element offset in memory, clamping indices to tensor dimensions.
/// </summary>
/// <param name="n">batch</param>
/// <param name="h">height</param>
/// <param name="w">width</param>
/// <param name="c">channels</param>
/// <returns>one dimensional index (offset in the flat memory region)</returns>
public int IndexWithClamp(int n, int h, int w, int c)
{
var shape = this;
if (!hasNamedDimensions)
shape = AsNamed();
n = Math.Max(n, 0);
h = Math.Max(h, 0);
w = Math.Max(w, 0);
c = Math.Max(c, 0);
n = Math.Min(n, shape.batch - 1);
h = Math.Min(h, shape.height - 1);
w = Math.Min(w, shape.width - 1);
c = Math.Min(c, shape.channels - 1);
return Index(n, h, w, c);
}
/// <summary>
/// Given an element dimensions indices [1,N,1,1,D,H,W,C] return this element offset in memory, clamping indices to tensor dimensions.
/// </summary>
/// <param name="n">batch</param>
/// <param name="d">depth</param>
/// <param name="h">height</param>
/// <param name="w">width</param>
/// <param name="c">channels</param>
/// <returns>one dimensional index (offset in the flat memory region)</returns>
public int IndexWithClamp(int n, int d, int h, int w, int c)
{
var shape = this;
if (!hasNamedDimensions)
shape = AsNamed();
n = Math.Max(n, 0);
d = Math.Max(d, 0);
h = Math.Max(h, 0);
w = Math.Max(w, 0);
c = Math.Max(c, 0);
n = Math.Min(n, shape.batch - 1);
d = Math.Min(d, shape.depth - 1);
h = Math.Min(h, shape.height - 1);
w = Math.Min(w, shape.width - 1);
c = Math.Min(c, shape.channels - 1);
return Index(n, d, h, w, c);
}
/// <summary>
/// Given an element dimensions indices [S,R,N,T,D,H,W,C] return this element offset in memory, clamping indices to tensor dimensions.
/// </summary>
/// <param name="s">sequence</param>
/// <param name="r">direction</param>
/// <param name="n">batch</param>
/// <param name="t">time</param>
/// <param name="d">depth</param>
/// <param name="h">height</param>
/// <param name="w">width</param>
/// <param name="c">channels</param>
/// <returns>one dimensional index (offset in the flat memory region)</returns>
public int IndexWithClamp(int s, int r, int n, int t, int d, int h, int w, int c)
{
var shape = this;
if (!hasNamedDimensions)
shape = AsNamed();
s = Math.Max(s, 0);
r = Math.Max(r, 0);
n = Math.Max(n, 0);
t = Math.Max(t, 0);
d = Math.Max(d, 0);
h = Math.Max(h, 0);
w = Math.Max(w, 0);
c = Math.Max(c, 0);
s = Math.Min(s, shape.sequenceLength - 1);
r = Math.Min(r, shape.numberOfDirections - 1);
n = Math.Min(n, shape.batch - 1);
t = Math.Min(t, shape.extraDimension - 1);
d = Math.Min(d, shape.depth - 1);
h = Math.Min(h, shape.height - 1);
w = Math.Min(w, shape.width - 1);
c = Math.Min(c, shape.channels - 1);
return Index(s,r,n,t,d,h,w,c);
}
/// <summary>
/// Given an element dimensions indices [S,R,N,T,D,H,W,C] return this element offset in memory.
/// </summary>
/// <param name="s">sequence</param>
/// <param name="r">direction</param>
/// <param name="n">batch</param>
/// <param name="t">time</param>
/// <param name="d">depth</param>
/// <param name="h">height</param>
/// <param name="w">width</param>
/// <param name="c">channels</param>
/// <returns>one dimensional index (offset in the flat memory region)</returns>
public int Index(int s, int r, int n, int t, int d, int h, int w, int c)
{
var shape = this;
if (!hasNamedDimensions)
shape = AsNamed();
int index =
s * shape.numberOfDirections * shape.batch * shape.extraDimension * shape.depth * shape.height * shape.width * shape.channels +
r * shape.batch * shape.extraDimension * shape.depth * shape.height * shape.width * shape.channels +
n * shape.extraDimension * shape.depth * shape.height * shape.width * shape.channels +
t * shape.depth * shape.height * shape.width * shape.channels +
d * shape.height * shape.width * shape.channels +
h * shape.width * shape.channels +
w * shape.channels +
c;
return index;
}
/// <summary>
/// Given an element dimensions indices [0,0,N,0,D,H,W,C] return this element offset in memory.
/// </summary>
/// <param name="n">batch</param>
/// <param name="d">depth</param>
/// <param name="h">height</param>
/// <param name="w">width</param>
/// <param name="c">channels</param>
/// <returns>one dimensional index (offset in the flat memory region)</returns>
public int Index(int n, int d, int h, int w, int c)
{
var shape = this;
if (!hasNamedDimensions)
shape = AsNamed();
int index =
n * shape.extraDimension * shape.depth * shape.height * shape.width * shape.channels +
d * shape.height * shape.width * shape.channels +
h * shape.width * shape.channels +
w * shape.channels +
c;
return index;
}
/// <summary>
/// Given an element dimensions indices [0,0,N,0,0,H,W,C] return this element offset in memory.
/// </summary>
/// <param name="n">batch</param>
/// <param name="h">height</param>
/// <param name="w">width</param>
/// <param name="c">channels</param>
/// <returns>one dimensional index (offset in the flat memory region)</returns>
public int Index(int n, int h, int w, int c)
{
var shape = this;
if (!hasNamedDimensions)
shape = AsNamed();
int index =
n * shape.extraDimension * shape.depth * shape.height * shape.width * shape.channels +
h * shape.width * shape.channels +
w * shape.channels +
c;
return index;
}
/// <summary>
/// Given an element dimensions indices [S,R,N,T,D,H,W,C] return this element offset in memory in ChannelFirst format.
/// </summary>
/// <param name="s">sequence</param>
/// <param name="r">direction</param>
/// <param name="n">batch</param>
/// <param name="t">time</param>
/// <param name="d">depth</param>
/// <param name="h">height</param>
/// <param name="w">width</param>
/// <param name="c">channels</param>
/// <returns>one dimensional index (offset in the flat memory region)</returns>
internal int IndexChannelFirst(int s, int r, int n, int t, int d, int h, int w, int c)
{
var shape = this;
if (!hasNamedDimensions)
shape = AsNamed();
int index =
s * shape.numberOfDirections * shape.batch * shape.channels * shape.extraDimension * shape.depth * shape.height * shape.width +
r * shape.batch * shape.channels * shape.extraDimension * shape.depth * shape.height * shape.width +
n * shape.channels * shape.extraDimension * shape.depth * shape.height * shape.width +
c * shape.extraDimension * shape.depth * shape.height * shape.width +
t * shape.depth * shape.height * shape.width +
d * shape.height * shape.width +
h * shape.width +
w;
return index;
}
/// <summary>
/// Given an element dimensions indices [0,0,N,0,0,H,W,C] return this element offset in memory in ChannelFirst format.
/// </summary>
/// <param name="n">batch</param>
/// <param name="h">height</param>
/// <param name="w">width</param>
/// <param name="c">channels</param>
/// <returns>one dimensional index (offset in the flat memory region)</returns>
internal int IndexChannelFirst(int n, int h, int w, int c)
{
var shape = this;
if (!hasNamedDimensions)
shape = AsNamed();
int index =
n * shape.channels * shape.extraDimension * shape.depth * shape.height * shape.width +
c * shape.extraDimension * shape.depth * shape.height * shape.width +
h * shape.width +
w;
return index;
}
/// <summary>
/// Given an element dimensions indices [0,0,N,0,0,0,0,C] return this element offset in memory.
/// </summary>
/// <param name="n">batch</param>
/// <param name="c">channels</param>
/// <returns>one dimensional index (offset in the flat memory region)</returns>
public int Index(int n, int c)
{
var shape = this;
if (!hasNamedDimensions)
shape = AsNamed();
int index =
n * shape.flatWidth +
c;
return index;