This repository has been archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
mxnet_op.h
1251 lines (1167 loc) · 51.8 KB
/
mxnet_op.h
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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*!
* \file mxnet_op.h
* \brief
* \author Junyuan Xie
*/
#ifndef MXNET_OPERATOR_MXNET_OP_H_
#define MXNET_OPERATOR_MXNET_OP_H_
#include <dmlc/omp.h>
#include <mxnet/base.h>
#include <mxnet/engine.h>
#include <mxnet/op_attr_types.h>
#include <algorithm>
#include <limits>
#include "./operator_tune.h"
#include "../engine/openmp.h"
#ifdef __CUDACC__
#include "../common/cuda/utils.h"
#endif // __CUDACC__
namespace mxnet {
namespace op {
namespace mxnet_op {
using namespace mshadow;
#ifdef __CUDA_ARCH__
__constant__ const float PI = 3.14159265358979323846;
#else
const float PI = 3.14159265358979323846;
using std::isnan;
#endif
template <typename xpu>
int get_num_threads(const int N);
#ifdef __CUDACC__
#define CUDA_KERNEL_LOOP(i, n) \
for (int i = blockIdx.x * blockDim.x + threadIdx.x; i < (n); i += blockDim.x * gridDim.x)
inline cudaDeviceProp cuda_get_device_prop() {
int device;
CUDA_CALL(cudaGetDevice(&device));
cudaDeviceProp deviceProp;
CUDA_CALL(cudaGetDeviceProperties(&deviceProp, device));
return deviceProp;
}
/*!
* \brief Get the number of blocks for cuda kernel given N
*/
inline int cuda_get_num_blocks(const int N) {
using namespace mshadow::cuda;
return std::min(kMaxGridNum, (N + kBaseThreadNum - 1) / kBaseThreadNum);
}
template <>
inline int get_num_threads<gpu>(const int N) {
using namespace mshadow::cuda;
return kBaseThreadNum * cuda_get_num_blocks(N);
}
#endif // __CUDACC__
template <>
inline int get_num_threads<cpu>(const int N) {
return engine::OpenMP::Get()->GetRecommendedOMPThreadCount();
}
/*! \brief operator request type switch */
#define MXNET_ASSIGN_REQ_SWITCH(req, ReqType, ...) \
switch (req) { \
case kNullOp: \
break; \
case kWriteInplace: \
case kWriteTo: { \
const OpReqType ReqType = kWriteTo; \
{ __VA_ARGS__ } \
} break; \
case kAddTo: { \
const OpReqType ReqType = kAddTo; \
{ __VA_ARGS__ } \
} break; \
default: \
break; \
}
/*! \brief operator request type switch */
#define MXNET_REQ_TYPE_SWITCH(req, ReqType, ...) \
switch (req) { \
case kNullOp: { \
const OpReqType ReqType = kNullOp; \
{ __VA_ARGS__ } \
} break; \
case kWriteInplace: \
case kWriteTo: { \
const OpReqType ReqType = kWriteTo; \
{ __VA_ARGS__ } \
} break; \
case kAddTo: { \
const OpReqType ReqType = kAddTo; \
{ __VA_ARGS__ } \
} break; \
default: \
break; \
}
#define MXNET_NDIM_SWITCH(NDim, ndim, ...) \
if (NDim == 0) { \
} else if (NDim == 1) { \
const int ndim = 1; \
{ __VA_ARGS__ } \
} else if (NDim == 2) { \
const int ndim = 2; \
{ __VA_ARGS__ } \
} else if (NDim == 3) { \
const int ndim = 3; \
{ __VA_ARGS__ } \
} else if (NDim == 4) { \
const int ndim = 4; \
{ __VA_ARGS__ } \
} else if (NDim == 5) { \
const int ndim = 5; \
{ __VA_ARGS__ } \
} else { \
LOG(FATAL) << "ndim=" << NDim << "too large "; \
}
#define MXNET_NDIM_SWITCH_EX(NDim, ndim, ...) \
if (NDim == 0) { \
} else if (NDim == 1) { \
const int ndim = 1; \
{ __VA_ARGS__ } \
} else if (NDim == 2) { \
const int ndim = 2; \
{ __VA_ARGS__ } \
} else if (NDim == 3) { \
const int ndim = 3; \
{ __VA_ARGS__ } \
} else if (NDim == 4) { \
const int ndim = 4; \
{ __VA_ARGS__ } \
} else if (NDim == 5) { \
const int ndim = 5; \
{ __VA_ARGS__ } \
} else if (NDim == 6) { \
const int ndim = 6; \
{ __VA_ARGS__ } \
} else if (NDim == 7) { \
const int ndim = 7; \
{ __VA_ARGS__ } \
} else if (NDim == 8) { \
const int ndim = 8; \
{ __VA_ARGS__ } \
} else if (NDim == 9) { \
const int ndim = 9; \
{ __VA_ARGS__ } \
} else if (NDim == 10) { \
const int ndim = 10; \
{ __VA_ARGS__ } \
} else { \
LOG(FATAL) << "ndim=" << NDim << "too large "; \
}
#define MXNET_NO_INT8_TYPE_SWITCH(type, DType, ...) \
switch (type) { \
case mshadow::kFloat32: { \
typedef float DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kFloat64: { \
typedef double DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kFloat16: \
case mshadow::kBfloat16: { \
typedef mshadow::half::half_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kUint8: \
LOG(FATAL) << "This operation does not " \
"support int8 or uint8"; \
break; \
case mshadow::kInt8: \
LOG(FATAL) << "This operation does not " \
"support int8 or uint8"; \
break; \
case mshadow::kInt32: { \
typedef int32_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kInt64: { \
typedef int64_t DType; \
{ __VA_ARGS__ } \
} break; \
default: \
LOG(FATAL) << "Unknown type enum " << type; \
}
#define MXNET_NO_BFLOAT16_TYPE_SWITCH(type, DType, ...) \
switch (type) { \
case mshadow::kFloat32: { \
typedef float DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kFloat64: { \
typedef double DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kFloat16: { \
typedef mshadow::half::half_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kBfloat16: \
LOG(FATAL) << "This operation does not " \
"support bfloat16"; \
break; \
case mshadow::kInt8: { \
typedef int32_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kInt32: { \
typedef int32_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kInt64: { \
typedef int64_t DType; \
{ __VA_ARGS__ } \
} break; \
default: \
LOG(FATAL) << "Unknown type enum " << type; \
}
#define MXNET_NO_FLOAT16_TYPE_SWITCH(type, DType, ...) \
switch (type) { \
case mshadow::kFloat32: { \
typedef float DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kFloat64: { \
typedef double DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kFloat16: \
LOG(FATAL) << "This operation does not " \
"support float16"; \
break; \
case mshadow::kUint8: { \
typedef uint8_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kInt8: { \
typedef int8_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kInt32: { \
typedef int32_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kInt64: { \
typedef int64_t DType; \
{ __VA_ARGS__ } \
} break; \
default: \
LOG(FATAL) << "Unknown type enum " << type; \
}
template <typename T>
struct AccType {
using type = T;
};
template <>
struct AccType<mshadow::half::half_t> {
using type = float;
};
#define MXNET_REAL_ACC_TYPE_SWITCH(type, DType, AType, ...) \
switch (type) { \
case mshadow::kFloat32: { \
typedef float DType; \
typedef double AType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kFloat64: { \
typedef double DType; \
typedef double AType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kFloat16: { \
typedef mshadow::half::half_t DType; \
typedef float AType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kUint8: { \
LOG(FATAL) << "This operation only support " \
"floating point types not uint8"; \
} break; \
case mshadow::kInt8: { \
LOG(FATAL) << "This operation only support " \
"floating point types not int8"; \
} break; \
case mshadow::kInt32: { \
LOG(FATAL) << "This operation only support " \
"floating point types, not int32"; \
} break; \
case mshadow::kInt64: { \
LOG(FATAL) << "This operation only support " \
"floating point types, not int64"; \
} break; \
case mshadow::kBool: { \
LOG(FATAL) << "This operation only support " \
"floating point types, not bool"; \
} break; \
default: \
LOG(FATAL) << "Unknown type enum " << type; \
}
#define MXNET_ACC_TYPE_SWITCH(type, DType, AType, ...) \
switch (type) { \
case mshadow::kFloat32: { \
typedef float DType; \
typedef double AType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kFloat64: { \
typedef double DType; \
typedef double AType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kFloat16: { \
typedef mshadow::half::half_t DType; \
typedef float AType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kUint8: { \
typedef uint8_t DType; \
typedef uint32_t AType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kInt8: { \
typedef int8_t DType; \
typedef int32_t AType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kInt32: { \
typedef int32_t DType; \
typedef int64_t AType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kInt64: { \
typedef int64_t DType; \
typedef int64_t AType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kBool: { \
typedef bool DType; \
typedef int64_t AType; \
{ __VA_ARGS__ } \
} break; \
default: \
LOG(FATAL) << "Unknown type enum " << type; \
}
#define MXNET_INT_TYPE_SWITCH(type, DType, ...) \
switch (type) { \
case mshadow::kFloat32: { \
LOG(FATAL) << "This operation only support " \
"integer types, not float32"; \
} break; \
case mshadow::kFloat64: { \
LOG(FATAL) << "This operation only support " \
"integer types, not float64"; \
} break; \
case mshadow::kFloat16: { \
LOG(FATAL) << "This operation only support " \
"integer types, not float16"; \
} break; \
case mshadow::kUint8: { \
typedef uint8_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kInt8: { \
typedef int8_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kInt32: { \
typedef int32_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kInt64: { \
typedef int64_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kBool: { \
typedef bool DType; \
{ __VA_ARGS__ } \
} break; \
default: \
LOG(FATAL) << "Unknown type enum " << type; \
}
#define MXNET_INT_TYPE_SWITCH_EXT_WITH_BOOL(type, DType, ...) \
switch (type) { \
case mshadow::kFloat32: { \
LOG(FATAL) << "This operation only support " \
"integer and bool types, not float32"; \
} break; \
case mshadow::kFloat64: { \
LOG(FATAL) << "This operation only support " \
"integer and bool types, not float64"; \
} break; \
case mshadow::kFloat16: { \
LOG(FATAL) << "This operation only support " \
"integer and boo; types, not float16"; \
} break; \
case mshadow::kUint8: { \
typedef uint8_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kInt8: { \
typedef int8_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kInt32: { \
typedef int32_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kInt64: { \
typedef int64_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kInt16: { \
typedef int16_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kUint16: { \
typedef uint16_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kUint32: { \
typedef uint32_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kUint64: { \
typedef uint64_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kBool: { \
typedef bool DType; \
{ __VA_ARGS__ } \
} break; \
default: \
LOG(FATAL) << "Unknown type enum " << type; \
}
#define MXNET_INT_TYPE_SWITCH_EXT(type, DType, ...) \
switch (type) { \
case mshadow::kFloat32: { \
LOG(FATAL) << "This operation only support " \
"integer types, not float32"; \
} break; \
case mshadow::kFloat64: { \
LOG(FATAL) << "This operation only support " \
"integer types, not float64"; \
} break; \
case mshadow::kFloat16: { \
LOG(FATAL) << "This operation only support " \
"integer types, not float16"; \
} break; \
case mshadow::kUint8: { \
typedef uint8_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kInt8: { \
typedef int8_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kInt32: { \
typedef int32_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kInt64: { \
typedef int64_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kInt16: { \
typedef int16_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kUint16: { \
typedef uint16_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kUint32: { \
typedef uint32_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kUint64: { \
typedef uint64_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kBool: { \
LOG(FATAL) << "This operation only support " \
"integer types, not bool type"; \
} break; \
default: \
LOG(FATAL) << "Unknown type enum " << type; \
}
#define MXNET_INT32_INT64_TYPE_SWITCH(type, DType, ...) \
switch (type) { \
case mshadow::kFloat32: { \
LOG(FATAL) << "This operation only support " \
"integer types, not float32"; \
} break; \
case mshadow::kFloat64: { \
LOG(FATAL) << "This operation only support " \
"integer types, not float64"; \
} break; \
case mshadow::kFloat16: { \
LOG(FATAL) << "This operation only support " \
"integer types, not float16"; \
} break; \
case mshadow::kUint8: { \
LOG(FATAL) << "This operation only support " \
"integer types, not uint8"; \
} break; \
case mshadow::kInt8: { \
LOG(FATAL) << "This operation only support " \
"integer types, not int8"; \
} break; \
case mshadow::kInt32: { \
typedef int32_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kInt64: { \
typedef int64_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kBool: { \
LOG(FATAL) << "This operation only support " \
"integer types, not bool"; \
} break; \
default: \
LOG(FATAL) << "Unknown type enum " << type; \
}
#define MXNET_LOAD_TYPE_SWITCH(type, DType, ...) \
switch (type) { \
case mshadow::kFloat32: { \
typedef float DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kFloat64: { \
typedef double DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kFloat16: { \
typedef mshadow::half::half_t DType; \
{ __VA_ARGS__ } \
} break; \
case mshadow::kUint8: { \
typedef uint8_t DType; \
{ __VA_ARGS__ } \
} break; \
default: \
LOG(FATAL) << "Invalid loading enum type " << type; \
}
/*!
* \brief assign the val to out according
* to request in Kernel::Launch
* \param out the data to be assigned
* \param req the assignment request
* \param val the value to be assigned to out
* \tparam OType output type
* \tparam VType value type
*/
#define KERNEL_ASSIGN(out, req, val) \
{ \
switch (req) { \
case kNullOp: \
break; \
case kWriteTo: \
case kWriteInplace: \
(out) = (val); \
break; \
case kAddTo: \
(out) += (val); \
break; \
default: \
break; \
} \
}
#define MXNET_ADD_ALL_TYPES \
.add_enum("float32", mshadow::kFloat32) \
.add_enum("float64", mshadow::kFloat64) \
.add_enum("float16", mshadow::kFloat16) \
.add_enum("bfloat16", mshadow::kBfloat16) \
.add_enum("uint8", mshadow::kUint8) \
.add_enum("int8", mshadow::kInt8) \
.add_enum("int32", mshadow::kInt32) \
.add_enum("int64", mshadow::kInt64)
#define MXNET_ADD_ALL_TYPES_WITH_BOOL \
.add_enum("float32", mshadow::kFloat32) \
.add_enum("float64", mshadow::kFloat64) \
.add_enum("float16", mshadow::kFloat16) \
.add_enum("bfloat16", mshadow::kBfloat16) \
.add_enum("uint8", mshadow::kUint8) \
.add_enum("int8", mshadow::kInt8) \
.add_enum("int32", mshadow::kInt32) \
.add_enum("int64", mshadow::kInt64) \
.add_enum("bool", mshadow::kBool)
#define MXNET_ADD_ALL_TYPES_EXT \
.add_enum("float32", mshadow::kFloat32) \
.add_enum("float64", mshadow::kFloat64) \
.add_enum("float16", mshadow::kFloat16) \
.add_enum("bfloat16", mshadow::kBfloat16) \
.add_enum("uint8", mshadow::kUint8) \
.add_enum("int8", mshadow::kInt8) \
.add_enum("int32", mshadow::kInt32) \
.add_enum("int64", mshadow::kInt64) \
.add_enum("int16", mshadow::kInt16) \
.add_enum("uint16", mshadow::kUint16) \
.add_enum("uint32", mshadow::kUint32) \
.add_enum("uint64", mshadow::kUint64)
#define MXNET_ADD_ALL_TYPES_EXT_WITH_BOOL \
.add_enum("float32", mshadow::kFloat32) \
.add_enum("float64", mshadow::kFloat64) \
.add_enum("float16", mshadow::kFloat16) \
.add_enum("bfloat16", mshadow::kBfloat16) \
.add_enum("uint8", mshadow::kUint8) \
.add_enum("int8", mshadow::kInt8) \
.add_enum("int32", mshadow::kInt32) \
.add_enum("int64", mshadow::kInt64) \
.add_enum("bool", mshadow::kBool) \
.add_enum("int16", mshadow::kInt16) \
.add_enum("uint16", mshadow::kUint16) \
.add_enum("uint32", mshadow::kUint32) \
.add_enum("uint64", mshadow::kUint64)
/* \brief Compute flattened index given coordinates and shape. */
template <int ndim>
MSHADOW_XINLINE index_t ravel(const Shape<ndim>& coord, const Shape<ndim>& shape) {
index_t ret = 0;
#pragma unroll
for (int i = 0; i < ndim; ++i) {
ret = ret * shape[i] + (shape[i] > coord[i]) * coord[i];
}
return ret;
}
/* Compute coordinates from flattened index given shape */
template <int ndim>
MSHADOW_XINLINE Shape<ndim> unravel(const index_t idx, const Shape<ndim>& shape) {
Shape<ndim> ret;
#pragma unroll
for (index_t i = ndim - 1, j = idx; i >= 0; --i) {
auto tmp = j / shape[i];
ret[i] = j - tmp * shape[i];
j = tmp;
}
return ret;
}
/* Compute dot product of two vector */
template <int ndim>
MSHADOW_XINLINE index_t dot(const Shape<ndim>& coord, const Shape<ndim>& stride) {
index_t ret = 0;
#pragma unroll
for (int i = 0; i < ndim; ++i) {
ret += coord[i] * stride[i];
}
return ret;
}
/* Combining unravel and dot */
template <int ndim>
MSHADOW_XINLINE index_t unravel_dot(const index_t idx,
const Shape<ndim>& shape,
const Shape<ndim>& stride) {
index_t ret = 0;
#pragma unroll
for (index_t i = ndim - 1, j = idx; i >= 0; --i) {
auto tmp = j / shape[i];
ret += (j - tmp * shape[i]) * stride[i];
j = tmp;
}
return ret;
}
/* Calculate stride of each dim from shape */
template <int ndim>
MSHADOW_XINLINE Shape<ndim> calc_stride(const Shape<ndim>& shape) {
Shape<ndim> stride;
index_t cumprod = 1;
#pragma unroll
for (int i = ndim - 1; i >= 0; --i) {
stride[i] = (shape[i] > 1) ? cumprod : 0;
cumprod *= shape[i];
}
return stride;
}
/* Increment coordinates */
template <int ndim>
MSHADOW_XINLINE bool inc(Shape<ndim>* coord, const Shape<ndim>& shape) {
++(*coord)[ndim - 1];
#pragma unroll
for (int i = ndim - 1; i > 0 && (*coord)[i] >= shape[i]; --i) {
(*coord)[i] -= shape[i];
++(*coord)[i - 1];
}
return (*coord)[0] < shape[0];
}
/* Increment coordinates and modify index */
template <int ndim>
MSHADOW_XINLINE void inc(Shape<ndim>* coord,
const Shape<ndim>& shape,
index_t* idx,
const Shape<ndim>& stride) {
++(*coord)[ndim - 1];
*idx += stride[ndim - 1];
#pragma unroll
for (int i = ndim - 1; i > 0 && (*coord)[i] >= shape[i]; --i) {
(*coord)[i] -= shape[i];
++(*coord)[i - 1];
*idx = *idx + stride[i - 1] - shape[i] * stride[i];
}
}
/* Increment coordinates and modify index */
template <int ndim>
MSHADOW_XINLINE void inc(Shape<ndim>* coord,
const Shape<ndim>& shape,
index_t* idx1,
const Shape<ndim>& stride1,
index_t* idx2,
const Shape<ndim>& stride2) {
++(*coord)[ndim - 1];
*idx1 += stride1[ndim - 1];
*idx2 += stride2[ndim - 1];
#pragma unroll
for (int i = ndim - 1; i > 0 && (*coord)[i] >= shape[i]; --i) {
(*coord)[i] -= shape[i];
++(*coord)[i - 1];
*idx1 = *idx1 + stride1[i - 1] - shape[i] * stride1[i];
*idx2 = *idx2 + stride2[i - 1] - shape[i] * stride2[i];
}
}
/*!
* \brief Simple copy data from one blob to another
* \param to Destination blob
* \param from Source blob
*/
template <typename xpu>
MSHADOW_CINLINE void copy(mshadow::Stream<xpu>* s, const TBlob& to, const TBlob& from) {
CHECK_EQ(from.Size(), to.Size());
CHECK_EQ(from.dev_mask(), to.dev_mask());
MSHADOW_TYPE_SWITCH_EXT_WITH_BOOL(to.type_flag_, DType, {
if (to.type_flag_ == from.type_flag_) {
mshadow::Copy(to.FlatTo1D<xpu, DType>(s), from.FlatTo1D<xpu, DType>(s), s);
} else {
MSHADOW_TYPE_SWITCH_EXT_WITH_BOOL(from.type_flag_, SrcDType, {
to.FlatTo1D<xpu, DType>(s) = mshadow::expr::tcast<DType>(from.FlatTo1D<xpu, SrcDType>(s));
})
}
})
}
/*! \brief Binary op backward gradient OP wrapper */
template <typename GRAD_OP>
struct backward_grad {
/* \brief Backward calc with grad
* \param a - output grad
* \param args... - data to grad calculation op (what this is -- input, output, etc. -- varies)
* \return input grad
*/
template <typename DType, typename... Args>
MSHADOW_XINLINE static DType Map(DType a, Args... args) {
return DType(a * GRAD_OP::Map(args...));
}
};
template <typename OP, int req>
struct mixed_type_unary_op {
typedef OP Operation;
/*! \brief input is one tensor */
template <typename OType, typename IType>
MSHADOW_XINLINE static void Map(index_t i, OType* out, const IType* in) {
KERNEL_ASSIGN(out[i], req, OP::Map(OType(in[i])));
}
};
/*! \brief Binary op backward gradient OP wrapper (tuned) */
template <typename GRAD_OP>
struct backward_grad_tuned : public backward_grad<GRAD_OP>, public tunable {
using backward_grad<GRAD_OP>::Map;
};
/*! \brief Select assignment operation based upon the req value
* Also useful for mapping mshadow Compute (F<OP>) to Kernel<OP>::Launch
*/
template <typename OP, int req>
struct op_with_req {
typedef OP Operation;
/*! \brief input is one tensor */
template <typename DType>
MSHADOW_XINLINE static void Map(index_t i, DType* out, const DType* in) {
KERNEL_ASSIGN(out[i], req, OP::Map(in[i]));
}
/*! \brief inputs are two tensors */
template <typename DType>
MSHADOW_XINLINE static void Map(index_t i, DType* out, const DType* lhs, const DType* rhs) {
KERNEL_ASSIGN(out[i], req, OP::Map(lhs[i], rhs[i]));
}
/*! \brief input is tensor and a scalar value */
template <typename DType>
MSHADOW_XINLINE static void Map(index_t i, DType* out, const DType* in, const DType value) {
KERNEL_ASSIGN(out[i], req, OP::Map(in[i], value));
}
/*! \brief input is tensor and two scalar value */
template <typename DType>
MSHADOW_XINLINE static void Map(index_t i,
DType* out,
const DType* in,
const DType value_1,
const DType value_2) {
KERNEL_ASSIGN(out[i], req, OP::Map(in[i], value_1, value_2));
}
/*! \brief No inputs (ie fill to constant value) */
template <typename DType>
MSHADOW_XINLINE static void Map(index_t i, DType* out) {
KERNEL_ASSIGN(out[i], req, OP::Map());
}
/*! \brief input is single scalar value */
template <typename DType>
MSHADOW_XINLINE static void Map(index_t i, DType* out, const DType value) {
KERNEL_ASSIGN(out[i], req, OP::Map(value));
}
/*! \brief inputs are two tensors and a scalar value */
template <typename DType>
MSHADOW_XINLINE static void Map(index_t i,
DType* out,
const DType* input_1,
const DType* input_2,
const DType value) {
KERNEL_ASSIGN(out[i], req, OP::Map(input_1[i], input_2[i], value));
}
/*! \brief inputs are three tensors (ie backward grad with binary grad function) */
template <typename DType>
MSHADOW_XINLINE static void Map(index_t i,
DType* out,
const DType* input_1,
const DType* input_2,
const DType* input_3) {
KERNEL_ASSIGN(out[i], req, OP::Map(input_1[i], input_2[i], input_3[i]));
}
/*! \brief input is a tensor and the output is a boolean tensor */
template <typename DType,
typename std::enable_if<!std::is_same<DType, bool>::value, int>::type = 0>
MSHADOW_XINLINE static void Map(index_t i, bool* out, const DType* in) {
KERNEL_ASSIGN(out[i], req, OP::Map(in[i]));
}
/*! \brief inputs are two tensors with a boolean output tensor */
template <typename DType,
typename std::enable_if<!std::is_same<DType, bool>::value, int>::type = 0>
MSHADOW_XINLINE static void Map(index_t i, bool* out, const DType* lhs, const DType* rhs) {
KERNEL_ASSIGN(out[i], req, OP::Map(lhs[i], rhs[i]));
}
/*! \brief input is tensor and two scalar value with a boolean output tensor */
template <typename DType,
typename std::enable_if<!std::is_same<DType, bool>::value, int>::type = 0>
MSHADOW_XINLINE static void Map(index_t i, bool* out, const DType* in, const DType value) {
KERNEL_ASSIGN(out[i], req, OP::Map(in[i], value));
}
/*! \brief input is two tensors with different type and with a boolean output tensor */
template <typename LType,
typename RType,
typename std::enable_if<!std::is_same<LType, RType>::value, int>::type = 0>
MSHADOW_XINLINE static void Map(index_t i, bool* out, const LType* lhs, const RType* rhs) {
KERNEL_ASSIGN(out[i], req, OP::Map(lhs[i], rhs[i]));
}
/*! \brief inputs are two tensors with a half_t output tensor */
template <typename DType, typename std::enable_if<std::is_integral<DType>::value, int>::type = 0>
MSHADOW_XINLINE static void Map(index_t i,
mshadow::half::half_t* out,
const DType* lhs,
const mshadow::half::half_t* rhs) {
KERNEL_ASSIGN(out[i], req, OP::Map(lhs[i], rhs[i]));
}
/*! \brief inputs are two tensors with a float output tensor */
template <typename DType,
typename std::enable_if<std::is_same<DType, mshadow::half::half_t>::value ||
std::is_same<DType, mshadow::bfloat::bf16_t>::value ||
std::is_integral<DType>::value,
int>::type = 0>
MSHADOW_XINLINE static void Map(index_t i, float* out, const DType* lhs, const float* rhs) {
KERNEL_ASSIGN(out[i], req, OP::Map(lhs[i], rhs[i]));
}
/*! \brief inputs are two tensors with a double output tensor */
template <typename DType,
typename std::enable_if<std::is_same<DType, mshadow::half::half_t>::value ||
std::is_same<DType, mshadow::bfloat::bf16_t>::value ||
std::is_same<DType, float>::value ||
std::is_integral<DType>::value,
int>::type = 0>
MSHADOW_XINLINE static void Map(index_t i, double* out, const DType* lhs, const double* rhs) {
KERNEL_ASSIGN(out[i], req, OP::Map(lhs[i], rhs[i]));
}
/*! \brief inputs are two tensors with a half_t output tensor */
template <typename DType, typename std::enable_if<std::is_integral<DType>::value, int>::type = 0>
MSHADOW_XINLINE static void Map(index_t i,
mshadow::half::half_t* out,
const DType* lhs,
const mshadow::half::half_t value) {
KERNEL_ASSIGN(out[i], req, OP::Map(lhs[i], value));
}
/*! \brief inputs are two tensors with a float output tensor */
template <typename DType,
typename std::enable_if<std::is_same<DType, mshadow::half::half_t>::value ||
std::is_integral<DType>::value,
int>::type = 0>
MSHADOW_XINLINE static void Map(index_t i, float* out, const DType* lhs, const float value) {
KERNEL_ASSIGN(out[i], req, OP::Map(lhs[i], value));
}
/*! \brief inputs are two tensors with a double output tensor */
template <typename DType,
typename std::enable_if<std::is_same<DType, mshadow::half::half_t>::value ||
std::is_same<DType, float>::value ||
std::is_integral<DType>::value,
int>::type = 0>
MSHADOW_XINLINE static void Map(index_t i, double* out, const DType* lhs, const double value) {
KERNEL_ASSIGN(out[i], req, OP::Map(lhs[i], value));
}
/*! \brief inputs are two tensors with a float output tensor */
template <typename DType, typename std::enable_if<std::is_integral<DType>::value, int>::type = 0>
MSHADOW_XINLINE static void Map(index_t i, float* out, const DType* lhs, const DType* rhs) {
KERNEL_ASSIGN(out[i], req, OP::Map(lhs[i], rhs[i]));
}
/*! \brief input is a tensor and a scalar value with a float output tensor */
template <typename DType, typename std::enable_if<std::is_integral<DType>::value, int>::type = 0>
MSHADOW_XINLINE static void Map(index_t i, float* out, const DType* in, const DType value) {
KERNEL_ASSIGN(out[i], req, OP::Map(in[i], value));
}
};
template <typename OP, typename xpu>
struct Kernel;
/*!
* \brief CPU Kernel launcher
* \tparam OP Operator to launch