-
Notifications
You must be signed in to change notification settings - Fork 1k
/
type_helpers.hpp
1296 lines (1139 loc) · 45.7 KB
/
type_helpers.hpp
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
/*******************************************************************************
* Copyright 2016-2024 Intel Corporation
*
* Licensed 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.
*******************************************************************************/
#ifndef COMMON_TYPE_HELPERS_HPP
#define COMMON_TYPE_HELPERS_HPP
#include <algorithm>
#include <assert.h>
#include <math.h>
#include "oneapi/dnnl/dnnl.h"
#include "bit_cast.hpp"
#include "c_types_map.hpp"
#include "dnnl_traits.hpp"
#include "memory_desc.hpp"
#include "nstl.hpp"
#include "utils.hpp"
namespace dnnl {
namespace impl {
// Global zero memory descriptor. Mostly used for queries to return
extern memory_desc_t DNNL_API glob_zero_md;
template <typename base_type, typename derived_type>
status_t safe_ptr_assign(base_type *&lhs, derived_type *rhs) {
if (rhs == nullptr) return status::out_of_memory;
lhs = rhs;
return status::success;
}
template <typename base_type, typename derived_type>
status_t safe_ptr_assign(std::unique_ptr<base_type> &lhs, derived_type *rhs) {
if (rhs == nullptr) return status::out_of_memory;
lhs.reset(rhs);
return status::success;
}
template <typename base_type, typename base_type_deleter, typename derived_type>
status_t safe_ptr_assign(
std::unique_ptr<base_type, base_type_deleter> &lhs, derived_type *rhs) {
if (rhs == nullptr) return status::out_of_memory;
lhs.reset(rhs);
return status::success;
}
template <typename T, typename U>
struct is_subset {
static constexpr bool value = false;
};
template <typename T>
struct is_subset<T, T> {
static constexpr bool value = true;
};
template <typename T>
struct is_subset<T,
typename utils::enable_if<nstl::is_integral<T>::value, float>::type> {
static constexpr bool value = true;
};
#define ISSPEC(t1, t2) \
template <> \
struct is_subset<t1, t2> { \
static constexpr bool value = true; \
}
ISSPEC(int16_t, int32_t);
ISSPEC(int8_t, int32_t);
ISSPEC(uint8_t, int32_t);
ISSPEC(int8_t, int16_t);
ISSPEC(uint8_t, int16_t);
#undef ISSPEC
inline bool operator==(const memory_desc_t &lhs, const memory_desc_t &rhs);
namespace types {
inline size_t data_type_size(data_type_t data_type) {
using namespace data_type;
switch ((int)data_type) {
case f4_e2m1: return sizeof(prec_traits<f4_e2m1>::type);
case e8m0: return sizeof(prec_traits<e8m0>::type);
case f8_e5m2: return sizeof(prec_traits<f8_e5m2>::type);
case f8_e4m3: return sizeof(prec_traits<f8_e4m3>::type);
case f16: return sizeof(prec_traits<f16>::type);
case bf16: return sizeof(prec_traits<bf16>::type);
case tf32: // the tf32 type is an f32
case f32: return sizeof(prec_traits<f32>::type);
case f64: return sizeof(prec_traits<f64>::type);
case s32: return sizeof(prec_traits<s32>::type);
case s8: return sizeof(prec_traits<s8>::type);
case u8: return sizeof(prec_traits<u8>::type);
case s4: return sizeof(prec_traits<s4>::type);
case u4: return sizeof(prec_traits<u4>::type);
case boolean: return sizeof(prec_traits<boolean>::type);
case data_type::undef:
default: assert(!"unknown data_type");
}
return (size_t)-1; /* not supposed to be reachable */
}
inline size_t elements_to_bytes(data_type_t data_type, size_t count) {
using namespace data_type;
switch ((int)data_type) {
case s4:
case u4: return (count + 1) >> 1;
default: return data_type_size(data_type) * count;
}
}
inline size_t bytes_to_elements(data_type_t data_type, size_t bytes) {
using namespace data_type;
switch ((int)data_type) {
case s4:
case u4: return bytes * 2;
default: return utils::div_up(bytes, data_type_size(data_type));
}
}
template <typename T>
inline T min_value(data_type_t data_type) {
using namespace data_type;
#define CASE(x) \
case x: \
return static_cast<T>(nstl::numeric_limits<prec_traits<x>::type>::min())
switch (data_type) {
CASE(f4_e2m1);
CASE(e8m0);
CASE(f8_e5m2);
CASE(f8_e4m3);
CASE(f16);
CASE(bf16);
CASE(f32);
CASE(f64);
CASE(s32);
CASE(s8);
CASE(u8);
CASE(s4);
CASE(u4);
case data_type::undef:
default: assert(!"unknown data_type");
}
return static_cast<T>(0); /* not supposed to be reachable */
#undef CASE
}
template <typename T>
inline T max_value(data_type_t data_type) {
using namespace data_type;
#define CASE(x) \
case x: \
return static_cast<T>(nstl::numeric_limits<prec_traits<x>::type>::max())
switch (data_type) {
CASE(f4_e2m1);
CASE(e8m0);
CASE(f8_e5m2);
CASE(f8_e4m3);
CASE(f16);
CASE(bf16);
CASE(f32);
CASE(s32);
CASE(s8);
CASE(u8);
CASE(s4);
CASE(u4);
case f64: return nstl::numeric_limits<T>::max();
case data_type::undef:
default: assert(!"unknown data_type");
}
return static_cast<T>(0); /* not supposed to be reachable */
#undef CASE
}
// This is a hack to comply with a big comment below.
template <>
inline float max_value(data_type_t data_type) {
using namespace data_type;
#define CASE(x) \
case x: \
return static_cast<float>( \
nstl::numeric_limits<prec_traits<x>::type>::max())
switch (data_type) {
CASE(f4_e2m1);
CASE(e8m0);
CASE(f8_e5m2);
CASE(f8_e4m3);
CASE(f16);
CASE(bf16);
CASE(f32);
CASE(s8);
CASE(u8);
CASE(s4);
CASE(u4);
// INT_MAX is not representable in float. The nearest float to it is
// INT_MAX + 1 = 2^31 (0x4f000000). Regular conversion instructions such
// as `cvtps2dq` or `cvtss2si` will convert this number to INT_MIN
// making the result negative. We on purpose choose the previous float
// number (0x4effffff) to return leaving the output close to INT_MAX but
// still positive. In addition, we adjust validation of this approach.
// The main concern against `real` saturation is performance, which
// likely to drop (but it was not proved). The only drawback of current
// approach is saturating on some integer values before it should happen
// in the reality.
case s32: return 2147483520.f;
case f64: return nstl::numeric_limits<float>::max();
case data_type::undef:
default: assert(!"unknown data_type");
}
return 0.f; /* not supposed to be reachable */
#undef CASE
}
template <typename T>
inline T lowest_value(data_type_t data_type) {
using namespace data_type;
#define CASE(x) \
case x: \
return static_cast<T>( \
nstl::numeric_limits<prec_traits<x>::type>::lowest())
switch (data_type) {
CASE(f4_e2m1);
CASE(e8m0);
CASE(f8_e5m2);
CASE(f8_e4m3);
CASE(f16);
CASE(bf16);
CASE(f32);
CASE(s32);
CASE(s8);
CASE(u8);
CASE(s4);
CASE(u4);
case f64: return nstl::numeric_limits<T>::lowest();
case data_type::undef:
default: assert(!"unknown data_type");
}
return static_cast<T>(0); /* not supposed to be reachable */
#undef CASE
}
template <typename T>
inline T digits(data_type_t data_type) {
using namespace data_type;
#define CASE(x) \
case x: \
return static_cast<T>( \
nstl::numeric_limits<prec_traits<x>::type>::digits)
switch (data_type) {
CASE(f4_e2m1);
CASE(e8m0);
CASE(f8_e5m2);
CASE(f8_e4m3);
CASE(f16);
CASE(bf16);
CASE(f32);
CASE(f64);
CASE(s32);
CASE(s8);
CASE(u8);
CASE(s4);
CASE(u4);
case data_type::undef:
default: assert(!"unknown data_type");
}
return static_cast<T>(0); /* not supposed to be reachable */
#undef CASE
}
inline format_kind_t format_tag_to_kind(format_tag_t tag) {
switch (tag) {
case format_tag::undef: return format_kind::undef;
case format_tag::any: return format_kind::any;
case format_tag::last: return format_kind::undef;
default: return format_kind::blocked;
}
assert(!"unreachable");
return format_kind::undef;
}
// Currently rnn_s8s8_compensation has common bits with rnn_u8s8_compensation
// and scale_adjust constants so we have to perform additional checks to
// separate these two cases
inline bool extra_flag_rnn_s8s8_compensation_is_set(uint64_t flags) {
return ((flags & memory_extra_flags::rnn_s8s8_compensation)
^ memory_extra_flags::rnn_s8s8_compensation)
== 0;
}
inline bool memory_extra_desc_is_equal(
const memory_extra_desc_t &lhs, const memory_extra_desc_t &rhs) {
using namespace memory_extra_flags;
return true && lhs.flags == rhs.flags
&& IMPLICATION(lhs.flags & compensation_conv_s8s8,
lhs.compensation_mask == rhs.compensation_mask)
&& IMPLICATION((lhs.flags & rnn_u8s8_compensation)
&& !extra_flag_rnn_s8s8_compensation_is_set(
lhs.flags),
lhs.compensation_mask == rhs.compensation_mask)
&& IMPLICATION((lhs.flags & scale_adjust)
&& !extra_flag_rnn_s8s8_compensation_is_set(
lhs.flags),
lhs.scale_adjust == rhs.scale_adjust)
&& IMPLICATION(lhs.flags & compensation_conv_asymmetric_src,
lhs.asymm_compensation_mask == rhs.asymm_compensation_mask);
}
inline bool blocking_desc_is_equal(const memory_desc_t &lhs_md,
const memory_desc_t &rhs_md, bool ignore_strides = false) {
using dnnl::impl::utils::array_cmp;
auto is_sparse_packed_desc = [](const memory_desc_t &md) {
return md.format_kind == format_kind::sparse
&& md.format_desc.sparse_desc.encoding
== sparse_encoding::packed;
};
const bool lhs_is_sparse_packed_desc = is_sparse_packed_desc(lhs_md);
const bool rhs_is_sparse_packed_desc = is_sparse_packed_desc(rhs_md);
if (lhs_md.format_kind != format_kind::blocked
&& !lhs_is_sparse_packed_desc)
return false;
if (rhs_md.format_kind != format_kind::blocked
&& !rhs_is_sparse_packed_desc)
return false;
const auto &lhs = lhs_md.format_kind == format_kind::sparse
? lhs_md.format_desc.sparse_desc.packed_desc
: lhs_md.format_desc.blocking;
const auto &rhs = rhs_md.format_kind == format_kind::sparse
? rhs_md.format_desc.sparse_desc.packed_desc
: rhs_md.format_desc.blocking;
bool equal = lhs.inner_nblks == rhs.inner_nblks
&& array_cmp(lhs.inner_blks, rhs.inner_blks, lhs.inner_nblks)
&& array_cmp(lhs.inner_idxs, rhs.inner_idxs, lhs.inner_nblks);
if (ignore_strides) return equal;
// Check the strides.
// Note: for dimensions of size `1` the stride doesn't really matter.
for (int d = 0; d < lhs_md.ndims; ++d) {
if (lhs_md.dims[d] == 1 && lhs_md.padded_dims[d] == 1) continue;
equal = equal && lhs.strides[d] == rhs.strides[d];
}
return equal;
}
inline bool wino_desc_is_equal(const wino_desc_t &lhs, const wino_desc_t &rhs) {
return lhs.wino_format == rhs.wino_format && lhs.alpha == rhs.alpha
&& lhs.ic == rhs.ic && lhs.oc == rhs.oc
&& lhs.ic_block == rhs.ic_block && lhs.oc_block == rhs.oc_block
&& lhs.ic2_block == rhs.ic2_block && lhs.oc2_block == rhs.oc2_block
&& lhs.r == rhs.r;
}
inline bool cublaslt_blocked_desc_is_equal(const cublaslt_blocked_desc_t &lhs,
const cublaslt_blocked_desc_t &rhs) {
return lhs.cublaslt_format == rhs.cublaslt_format && lhs.size == rhs.size;
}
inline bool rnn_packed_desc_is_equal(
const rnn_packed_desc_t &lhs, const rnn_packed_desc_t &rhs) {
bool ok = true && lhs.format == rhs.format && lhs.ldb == rhs.ldb
&& lhs.n_parts == rhs.n_parts
&& lhs.offset_compensation == rhs.offset_compensation
&& lhs.size == rhs.size && lhs.n == rhs.n;
if (!ok) return false;
for (int i = 0; i < rhs.n_parts; i++)
ok = ok && lhs.parts[i] == rhs.parts[i];
for (int i = 0; i < rhs.n_parts; i++)
ok = ok && lhs.part_pack_size[i] == rhs.part_pack_size[i];
return ok;
}
inline bool sparse_desc_is_equal(
const sparse_desc_t &lhs, const sparse_desc_t &rhs) {
bool ok = lhs.encoding == rhs.encoding && lhs.nnz == rhs.nnz;
if (!ok) return false;
for (int i = 0; i < sparse_desc_t::max_metadata_types; i++)
ok = ok && lhs.metadata_types[i] == rhs.metadata_types[i];
return ok;
}
inline memory_desc_t zero_md() {
auto zero = memory_desc_t();
return zero;
}
inline bool is_zero_md(const memory_desc_t *md) {
return md == nullptr || *md == zero_md();
}
inline data_type_t default_accum_data_type(
data_type_t src_dt, data_type_t dst_dt, bool strict = true) {
using namespace utils;
using namespace data_type;
// we allow to use f32 accumulation type only when the
// accumulation chain is small. Otherwise, strict should be set to
// true
if (one_of(src_dt, s8, u8, u4, s4) && (dst_dt != f32 || strict)) return s32;
if (one_of(f4_e2m1, src_dt, dst_dt)) return f32;
if (one_of(f8_e5m2, src_dt, dst_dt)) return f32;
if (one_of(f8_e4m3, src_dt, dst_dt)) return f32;
if (one_of(f16, src_dt, dst_dt)) return f32;
if (one_of(bf16, src_dt, dst_dt)) return f32;
if (one_of(f32, src_dt, dst_dt)) return f32;
if (one_of(f64, src_dt, dst_dt)) return f64;
if (one_of(s32, src_dt, dst_dt)) return s32;
if (one_of(s8, src_dt, dst_dt) || one_of(u8, src_dt, dst_dt)
|| one_of(s4, src_dt, dst_dt) || one_of(u4, src_dt, dst_dt))
return s32;
return data_type::undef;
}
inline data_type_t default_accum_data_type(data_type_t src_dt,
data_type_t wei_dt, data_type_t dst_dt, prop_kind_t prop_kind) {
using namespace utils;
using namespace data_type;
using namespace prop_kind;
/* prop_kind doesn't matter */
if (everyone_is(f32, src_dt, wei_dt)) return f32;
if (everyone_is(f64, src_dt, wei_dt)) return f64;
if (one_of(prop_kind, forward_training, forward_inference)) {
if (one_of(src_dt, u8, s8) && one_of(wei_dt, u8, s8, s4, u4))
return s32;
if (one_of(f16, src_dt, wei_dt)) return f32;
// weights decompression
if (one_of(src_dt, bf16, f32) && one_of(wei_dt, u8, s8, s4, u4))
return f32;
} else if (prop_kind == backward_data) {
if (one_of(src_dt, f32, s32, s8, u8) && wei_dt == s8
&& one_of(dst_dt, s8, u8, s32))
return s32;
if (one_of(f16, dst_dt, wei_dt)) return f32;
if (everyone_is(f32, dst_dt, wei_dt) && one_of(src_dt, s8, u8))
return f32;
}
if (one_of(f4_e2m1, src_dt, wei_dt, dst_dt)) return f32;
if (one_of(f8_e5m2, src_dt, wei_dt, dst_dt)) return f32;
if (one_of(f8_e4m3, src_dt, wei_dt, dst_dt)) return f32;
if (one_of(bf16, src_dt, wei_dt, dst_dt)) return f32;
if (one_of(f16, src_dt, wei_dt, dst_dt)) return f32;
return data_type::undef;
}
inline bool is_integral_dt(data_type_t dt) {
using namespace data_type;
return utils::one_of(dt, s32, s8, u8, u4, s4);
}
template <typename data_t>
inline void cvt_from_float(data_t *out, const float *inp, size_t nelems)
= delete;
template <typename data_t>
inline void cvt_to_float(float *out, const data_t *inp, size_t nelems) = delete;
template <>
inline void cvt_from_float<float>(float *out, const float *inp, size_t nelems) {
// This operation should be avoided as it does nothing useful
assert(!"unexpected");
for (size_t i = 0; i < nelems; i++)
out[i] = inp[i];
}
template <>
inline void cvt_to_float<float>(float *out, const float *inp, size_t nelems) {
// This operation should be avoided as it does nothing useful
assert(!"unexpected");
for (size_t i = 0; i < nelems; i++)
out[i] = inp[i];
}
template <>
inline void cvt_from_float<bfloat16_t>(
bfloat16_t *out, const float *inp, size_t nelems) {
cvt_float_to_bfloat16(out, inp, nelems);
}
template <>
inline void cvt_to_float<bfloat16_t>(
float *out, const bfloat16_t *inp, size_t nelems) {
cvt_bfloat16_to_float(out, inp, nelems);
}
template <>
inline void cvt_from_float<float16_t>(
float16_t *out, const float *inp, size_t nelems) {
cvt_float_to_float16(out, inp, nelems);
}
template <>
inline void cvt_to_float<float16_t>(
float *out, const float16_t *inp, size_t nelems) {
cvt_float16_to_float(out, inp, nelems);
}
template <>
inline void cvt_to_float<float8_e5m2_t>(
float *out, const float8_e5m2_t *inp, size_t nelems) {
cvt_f8_e5m2_to_float(out, inp, nelems);
}
template <>
inline void cvt_from_float<float8_e5m2_t>(
float8_e5m2_t *out, const float *inp, size_t nelems) {
cvt_float_to_f8_e5m2(out, inp, nelems);
}
template <>
inline void cvt_to_float<float8_e4m3_t>(
float *out, const float8_e4m3_t *inp, size_t nelems) {
cvt_f8_e4m3_to_float(out, inp, nelems);
}
template <>
inline void cvt_from_float<float8_e4m3_t>(
float8_e4m3_t *out, const float *inp, size_t nelems) {
cvt_float_to_f8_e4m3(out, inp, nelems);
}
inline void cvt_from_float(
data_type_t dt, void *out, const float *inp, size_t nelems) {
switch (dt) {
case data_type::bf16:
cvt_from_float((bfloat16_t *)out, inp, nelems);
break;
case data_type::f16:
cvt_from_float((float16_t *)out, inp, nelems);
break;
case data_type::f8_e5m2:
cvt_from_float((float8_e5m2_t *)out, inp, nelems);
break;
case data_type::f8_e4m3:
cvt_from_float((float8_e4m3_t *)out, inp, nelems);
break;
default: assert(!"unimplemented");
}
}
inline void cvt_to_float(
data_type_t dt, float *out, const void *inp, size_t nelems) {
switch (dt) {
case data_type::bf16:
cvt_to_float(out, (const bfloat16_t *)inp, nelems);
break;
case data_type::f16:
cvt_to_float(out, (const float16_t *)inp, nelems);
break;
case data_type::f8_e5m2:
cvt_to_float(out, (const float8_e5m2_t *)inp, nelems);
break;
case data_type::f8_e4m3:
cvt_to_float(out, (const float8_e4m3_t *)inp, nelems);
break;
default: assert(!"unimplemented");
}
}
} // namespace types
inline bool operator==(const memory_desc_t &lhs, const memory_desc_t &rhs) {
using namespace dnnl::impl::utils;
// quick path for zero_mds
if (utils::everyone_is(0, lhs.ndims, rhs.ndims)) return true;
bool base_equal = true && lhs.ndims == rhs.ndims
&& array_cmp(lhs.dims, rhs.dims, lhs.ndims)
&& lhs.data_type == rhs.data_type
&& array_cmp(lhs.padded_dims, rhs.padded_dims, lhs.ndims)
&& array_cmp(lhs.padded_offsets, rhs.padded_offsets, lhs.ndims)
&& lhs.offset0 == rhs.offset0 && lhs.format_kind == rhs.format_kind;
if (!base_equal) return false;
if (!types::memory_extra_desc_is_equal(lhs.extra, rhs.extra)) return false;
if (lhs.format_kind == format_kind::blocked)
return types::blocking_desc_is_equal(lhs, rhs);
else if (lhs.format_kind == format_kind::wino)
return types::wino_desc_is_equal(
lhs.format_desc.wino_desc, rhs.format_desc.wino_desc);
else if (lhs.format_kind == format_kind::rnn_packed)
return types::rnn_packed_desc_is_equal(lhs.format_desc.rnn_packed_desc,
rhs.format_desc.rnn_packed_desc);
else if (lhs.format_kind == format_kind::sparse)
return types::sparse_desc_is_equal(
lhs.format_desc.sparse_desc, rhs.format_desc.sparse_desc);
return true;
}
inline bool operator!=(const memory_desc_t &lhs, const memory_desc_t &rhs) {
return !operator==(lhs, rhs);
}
// Comparison operators for descriptors
#define COMPARE_DESC_MEMBERS(m) lhs.m == rhs.m
#define COMPARE_DESC_ARRAY_MEMBERS(m, s) utils::array_cmp(lhs.m, rhs.m, s)
#define DEREF_AND_COMPARE_DESC_MEMBERS(m) *lhs.m == *rhs.m
#define COMPARE_FLOAT_DESC_MEMBERS(m) utils::equal_with_nan(lhs.m, rhs.m)
#define COMPARE_FLOAT_DESC_ARRAY_MEMBERS(m, s) \
!std::memcmp(lhs.m, rhs.m, sizeof(float) * s)
// clang-format off
inline bool operator==(const batch_normalization_desc_t &lhs,
const batch_normalization_desc_t &rhs) {
bool ret = COMPARE_DESC_MEMBERS(primitive_kind)
&& COMPARE_DESC_MEMBERS(prop_kind)
&& COMPARE_DESC_MEMBERS(src_desc)
&& COMPARE_DESC_MEMBERS(dst_desc)
&& COMPARE_DESC_MEMBERS(diff_src_desc)
&& COMPARE_DESC_MEMBERS(diff_dst_desc)
&& COMPARE_DESC_MEMBERS(scaleshift_desc)
&& COMPARE_DESC_MEMBERS(diff_scaleshift_desc)
&& COMPARE_DESC_MEMBERS(stat_desc)
&& COMPARE_FLOAT_DESC_MEMBERS(batch_norm_epsilon)
&& COMPARE_DESC_MEMBERS(flags);
return ret;
}
inline bool operator==(const binary_desc_t &lhs, const binary_desc_t &rhs) {
bool ret = COMPARE_DESC_MEMBERS(primitive_kind)
&& COMPARE_DESC_MEMBERS(alg_kind)
&& COMPARE_DESC_MEMBERS(src_desc[0])
&& COMPARE_DESC_MEMBERS(src_desc[1])
&& COMPARE_DESC_MEMBERS(dst_desc);
return ret;
}
inline bool operator==(const concat_desc_t &lhs, const concat_desc_t &rhs) {
bool ret = COMPARE_DESC_MEMBERS(primitive_kind)
&& DEREF_AND_COMPARE_DESC_MEMBERS(dst_md)
&& COMPARE_DESC_MEMBERS(n)
&& COMPARE_DESC_MEMBERS(concat_dimension);
if (!ret) return ret;
for (int i = 0; i < lhs.n; i++) {
ret = *lhs.src_mds[i] == *rhs.src_mds[i];
if (!ret) break;
}
return ret;
}
// This function can only be used to compare the opdescs in the primitive cache.
// For comparing the opdescs outside the primitive cache please use the regular
// comparison operator (==).
inline bool compare_conv_opdesc(const convolution_desc_t &lhs, const convolution_desc_t &rhs) {
bool ret = COMPARE_DESC_MEMBERS(primitive_kind)
&& COMPARE_DESC_MEMBERS(prop_kind)
&& COMPARE_DESC_MEMBERS(src_desc)
&& COMPARE_DESC_MEMBERS(diff_src_desc)
&& COMPARE_DESC_MEMBERS(weights_desc)
&& COMPARE_DESC_MEMBERS(diff_weights_desc)
&& COMPARE_DESC_MEMBERS(bias_desc)
&& COMPARE_DESC_MEMBERS(diff_bias_desc)
&& COMPARE_DESC_MEMBERS(dst_desc)
&& COMPARE_DESC_MEMBERS(diff_dst_desc)
&& COMPARE_DESC_ARRAY_MEMBERS(strides, DNNL_MAX_NDIMS)
&& COMPARE_DESC_ARRAY_MEMBERS(dilates, DNNL_MAX_NDIMS)
&& COMPARE_DESC_ARRAY_MEMBERS(padding[0], DNNL_MAX_NDIMS)
&& COMPARE_DESC_ARRAY_MEMBERS(padding[1], DNNL_MAX_NDIMS)
&& COMPARE_DESC_MEMBERS(accum_data_type)
&& COMPARE_DESC_MEMBERS(use_inversion);
// The `alg_kind` can be `auto` only if this function is called for the
// primitive descriptor cache scenario. In this case, we ignore `alg_kind`
// and rely on `pd_iterator_offset` to fetch the first suitable
// implementation.
//
// Background: when a convolution primitive descriptor is created for
// the algorithm `auto` we overwrite `alg_kind` field in `op_desc` when
// store it in the primitive descriptor. Because of that, the `op_desc`
// stored in the primitive descriptor is different from the one user
// passed to oneDNN API. Because of the difference the requested
// primitive descriptor cannot be found in the cache if we compare
// `alg_kind`.
if (!utils::one_of(alg_kind::convolution_auto, lhs.alg_kind, rhs.alg_kind))
ret = ret && COMPARE_DESC_MEMBERS(alg_kind);
return ret;
}
inline bool operator==(
const convolution_desc_t &lhs, const convolution_desc_t &rhs) {
if (!(COMPARE_DESC_MEMBERS(alg_kind))) return false;
return compare_conv_opdesc(lhs, rhs);
}
inline bool operator==(const eltwise_desc_t &lhs, const eltwise_desc_t &rhs) {
bool ret = COMPARE_DESC_MEMBERS(primitive_kind)
&& COMPARE_DESC_MEMBERS(prop_kind)
&& COMPARE_DESC_MEMBERS(alg_kind)
&& COMPARE_DESC_MEMBERS(src_desc)
&& COMPARE_DESC_MEMBERS(dst_desc)
&& COMPARE_DESC_MEMBERS(diff_src_desc)
&& COMPARE_DESC_MEMBERS(diff_dst_desc)
&& COMPARE_FLOAT_DESC_MEMBERS(alpha)
&& COMPARE_FLOAT_DESC_MEMBERS(beta);
return ret;
}
inline bool operator==(const gemm_desc_t &lhs, const gemm_desc_t &rhs) {
bool ret = COMPARE_DESC_MEMBERS(primitive_kind)
&& COMPARE_DESC_MEMBERS(a_desc)
&& COMPARE_DESC_MEMBERS(b_desc)
&& COMPARE_DESC_MEMBERS(c_desc)
&& COMPARE_DESC_MEMBERS(bias_desc)
&& COMPARE_DESC_MEMBERS(acc_type)
&& COMPARE_DESC_MEMBERS(sum_ab)
&& COMPARE_DESC_MEMBERS(sum_ab_type);
return ret;
}
inline bool operator==(
const group_normalization_desc_t &lhs, const group_normalization_desc_t &rhs) {
bool ret = COMPARE_DESC_MEMBERS(primitive_kind)
&& COMPARE_DESC_MEMBERS(prop_kind)
&& COMPARE_DESC_MEMBERS(src_desc)
&& COMPARE_DESC_MEMBERS(diff_src_desc)
&& COMPARE_DESC_MEMBERS(scaleshift_desc)
&& COMPARE_DESC_MEMBERS(diff_scaleshift_desc)
&& COMPARE_DESC_MEMBERS(dst_desc)
&& COMPARE_DESC_MEMBERS(diff_dst_desc)
&& COMPARE_DESC_MEMBERS(stat_desc)
&& COMPARE_DESC_MEMBERS(groups)
&& COMPARE_FLOAT_DESC_MEMBERS(group_norm_epsilon)
&& COMPARE_DESC_MEMBERS(flags);
return ret;
}
inline bool operator==(
const inner_product_desc_t &lhs, const inner_product_desc_t &rhs) {
bool ret = COMPARE_DESC_MEMBERS(primitive_kind)
&& COMPARE_DESC_MEMBERS(prop_kind)
&& COMPARE_DESC_MEMBERS(src_desc)
&& COMPARE_DESC_MEMBERS(diff_src_desc)
&& COMPARE_DESC_MEMBERS(weights_desc)
&& COMPARE_DESC_MEMBERS(diff_weights_desc)
&& COMPARE_DESC_MEMBERS(bias_desc)
&& COMPARE_DESC_MEMBERS(diff_bias_desc)
&& COMPARE_DESC_MEMBERS(dst_desc)
&& COMPARE_DESC_MEMBERS(diff_dst_desc)
&& COMPARE_DESC_MEMBERS(accum_data_type);
return ret;
}
inline bool operator==(
const layer_normalization_desc_t &lhs, const layer_normalization_desc_t &rhs) {
bool ret = COMPARE_DESC_MEMBERS(primitive_kind)
&& COMPARE_DESC_MEMBERS(prop_kind)
&& COMPARE_DESC_MEMBERS(src_desc)
&& COMPARE_DESC_MEMBERS(diff_src_desc)
&& COMPARE_DESC_MEMBERS(data_scaleshift_desc)
&& COMPARE_DESC_MEMBERS(diff_data_scaleshift_desc)
&& COMPARE_DESC_MEMBERS(dst_desc)
&& COMPARE_DESC_MEMBERS(diff_dst_desc)
&& COMPARE_DESC_MEMBERS(stat_desc)
&& COMPARE_FLOAT_DESC_MEMBERS(layer_norm_epsilon)
&& COMPARE_DESC_MEMBERS(flags);
return ret;
}
inline bool operator==(const lrn_desc_t &lhs, const lrn_desc_t &rhs) {
bool ret = COMPARE_DESC_MEMBERS(primitive_kind)
&& COMPARE_DESC_MEMBERS(prop_kind)
&& COMPARE_DESC_MEMBERS(alg_kind)
&& COMPARE_DESC_MEMBERS(src_desc)
&& COMPARE_DESC_MEMBERS(dst_desc)
&& COMPARE_DESC_MEMBERS(diff_src_desc)
&& COMPARE_DESC_MEMBERS(diff_dst_desc)
&& COMPARE_DESC_MEMBERS(local_size)
&& COMPARE_FLOAT_DESC_MEMBERS(lrn_alpha)
&& COMPARE_FLOAT_DESC_MEMBERS(lrn_beta)
&& COMPARE_FLOAT_DESC_MEMBERS(lrn_k);
return ret;
}
inline bool operator==(const matmul_desc_t &lhs, const matmul_desc_t &rhs) {
bool ret = COMPARE_DESC_MEMBERS(primitive_kind)
&& COMPARE_DESC_MEMBERS(src_desc)
&& COMPARE_DESC_MEMBERS(weights_desc)
&& COMPARE_DESC_MEMBERS(bias_desc)
&& COMPARE_DESC_MEMBERS(dst_desc)
&& COMPARE_DESC_MEMBERS(accum_data_type);
return ret;
}
inline bool operator==(
const pooling_desc_t &lhs, const pooling_desc_t &rhs) {
bool ret = COMPARE_DESC_MEMBERS(primitive_kind)
&& COMPARE_DESC_MEMBERS(prop_kind)
&& COMPARE_DESC_MEMBERS(alg_kind)
&& COMPARE_DESC_MEMBERS(src_desc)
&& COMPARE_DESC_MEMBERS(diff_src_desc)
&& COMPARE_DESC_MEMBERS(dst_desc)
&& COMPARE_DESC_MEMBERS(diff_dst_desc)
&& COMPARE_DESC_ARRAY_MEMBERS(strides, DNNL_MAX_NDIMS)
&& COMPARE_DESC_ARRAY_MEMBERS(kernel, DNNL_MAX_NDIMS)
&& COMPARE_DESC_ARRAY_MEMBERS(padding[0], DNNL_MAX_NDIMS)
&& COMPARE_DESC_ARRAY_MEMBERS(padding[1], DNNL_MAX_NDIMS)
&& COMPARE_DESC_ARRAY_MEMBERS(dilation, DNNL_MAX_NDIMS)
&& COMPARE_DESC_MEMBERS(accum_data_type);
return ret;
}
inline bool operator==(const prelu_desc_t &lhs, const prelu_desc_t &rhs) {
const bool ret = COMPARE_DESC_MEMBERS(primitive_kind)
&& COMPARE_DESC_MEMBERS(prop_kind)
&& COMPARE_DESC_MEMBERS(src_desc)
&& COMPARE_DESC_MEMBERS(weights_desc)
&& COMPARE_DESC_MEMBERS(dst_desc)
&& COMPARE_DESC_MEMBERS(diff_src_desc)
&& COMPARE_DESC_MEMBERS(diff_weights_desc)
&& COMPARE_DESC_MEMBERS(diff_dst_desc);
return ret;
}
inline bool operator==(
const reduction_desc_t &lhs, const reduction_desc_t &rhs) {
bool ret = COMPARE_DESC_MEMBERS(primitive_kind)
&& COMPARE_DESC_MEMBERS(alg_kind)
&& COMPARE_DESC_MEMBERS(src_desc)
&& COMPARE_DESC_MEMBERS(dst_desc)
&& COMPARE_FLOAT_DESC_MEMBERS(p)
&& COMPARE_FLOAT_DESC_MEMBERS(eps);
return ret;
}
inline bool operator==(const reorder_desc_t &lhs, const reorder_desc_t &rhs) {
bool ret = COMPARE_DESC_MEMBERS(primitive_kind)
&& DEREF_AND_COMPARE_DESC_MEMBERS(src_md)
&& DEREF_AND_COMPARE_DESC_MEMBERS(dst_md)
&& COMPARE_DESC_MEMBERS(src_engine_kind)
&& COMPARE_DESC_MEMBERS(dst_engine_kind)
&& COMPARE_DESC_MEMBERS(is_cross_engine);
return ret;
}
inline bool operator==(
const resampling_desc_t &lhs, const resampling_desc_t &rhs) {
bool ret = COMPARE_DESC_MEMBERS(primitive_kind)
&& COMPARE_DESC_MEMBERS(alg_kind)
&& COMPARE_DESC_MEMBERS(src_desc)
&& COMPARE_DESC_MEMBERS(diff_src_desc)
&& COMPARE_DESC_MEMBERS(dst_desc)
&& COMPARE_DESC_MEMBERS(diff_dst_desc)
&& COMPARE_FLOAT_DESC_ARRAY_MEMBERS(factors, DNNL_MAX_NDIMS);
return ret;
}
inline bool operator==(const rnn_desc_t &lhs, const rnn_desc_t &rhs) {
bool ret = COMPARE_DESC_MEMBERS(primitive_kind)
&& COMPARE_DESC_MEMBERS(prop_kind)
&& COMPARE_DESC_MEMBERS(cell_kind)
&& COMPARE_DESC_MEMBERS(direction)
&& COMPARE_DESC_MEMBERS(src_layer_desc)
&& COMPARE_DESC_MEMBERS(src_iter_desc)
&& COMPARE_DESC_MEMBERS(src_iter_c_desc)
&& COMPARE_DESC_MEMBERS(weights_layer_desc)
&& COMPARE_DESC_MEMBERS(weights_iter_desc)
&& COMPARE_DESC_MEMBERS(bias_desc)
&& COMPARE_DESC_MEMBERS(dst_layer_desc)
&& COMPARE_DESC_MEMBERS(dst_iter_desc)
&& COMPARE_DESC_MEMBERS(dst_iter_c_desc)
&& COMPARE_DESC_MEMBERS(weights_peephole_desc)
&& COMPARE_DESC_MEMBERS(weights_projection_desc)
&& COMPARE_DESC_MEMBERS(diff_src_layer_desc)
&& COMPARE_DESC_MEMBERS(diff_src_iter_desc)
&& COMPARE_DESC_MEMBERS(diff_src_iter_c_desc)
&& COMPARE_DESC_MEMBERS(diff_weights_layer_desc)
&& COMPARE_DESC_MEMBERS(diff_weights_iter_desc)
&& COMPARE_DESC_MEMBERS(diff_bias_desc)
&& COMPARE_DESC_MEMBERS(diff_dst_layer_desc)
&& COMPARE_DESC_MEMBERS(diff_dst_iter_desc)
&& COMPARE_DESC_MEMBERS(diff_dst_iter_c_desc)
&& COMPARE_DESC_MEMBERS(diff_weights_peephole_desc)
&& COMPARE_DESC_MEMBERS(diff_weights_projection_desc)
&& COMPARE_DESC_MEMBERS(flags)
&& COMPARE_DESC_MEMBERS(activation_kind)
&& COMPARE_FLOAT_DESC_MEMBERS(alpha)
&& COMPARE_FLOAT_DESC_MEMBERS(beta);
return ret;
}
inline bool operator==(const shuffle_desc_t &lhs, const shuffle_desc_t &rhs) {
bool ret = COMPARE_DESC_MEMBERS(primitive_kind)
&& COMPARE_DESC_MEMBERS(prop_kind)
&& COMPARE_DESC_MEMBERS(src_desc)
&& COMPARE_DESC_MEMBERS(dst_desc)
&& COMPARE_DESC_MEMBERS(axis)
&& COMPARE_DESC_MEMBERS(group_size);
return ret;
}
inline bool operator==(
const softmax_desc_t &lhs, const softmax_desc_t &rhs) {
bool ret = COMPARE_DESC_MEMBERS(primitive_kind)
&& COMPARE_DESC_MEMBERS(prop_kind)
&& COMPARE_DESC_MEMBERS(alg_kind)
&& COMPARE_DESC_MEMBERS(src_desc)
&& COMPARE_DESC_MEMBERS(diff_src_desc)
&& COMPARE_DESC_MEMBERS(dst_desc)
&& COMPARE_DESC_MEMBERS(diff_dst_desc)
&& COMPARE_DESC_MEMBERS(softmax_axis);
return ret;
}
inline bool operator==(const sum_desc_t &lhs, const sum_desc_t &rhs) {
bool ret = COMPARE_DESC_MEMBERS(primitive_kind)
&& DEREF_AND_COMPARE_DESC_MEMBERS(dst_md)
&& COMPARE_DESC_MEMBERS(n);
if (!ret) return ret;
for (int i = 0; i < lhs.n; i++) {
ret = *lhs.src_mds[i] == *rhs.src_mds[i];
if (!ret) break;
}
if (!ret) return ret;
for (int i = 0; i < lhs.n; i++) {
ret = ret && COMPARE_FLOAT_DESC_MEMBERS(scales[i]);
if (!ret) break;
}
return ret;
}
inline bool operator==(const zero_pad_desc_t &lhs, const zero_pad_desc_t &rhs) {
bool ret = COMPARE_DESC_MEMBERS(primitive_kind);
return ret;
}
inline bool operator==(const sdpa_desc_t &lhs, const sdpa_desc_t &rhs) {
bool ret = COMPARE_DESC_MEMBERS(primitive_kind)
&& COMPARE_DESC_MEMBERS(q_desc)
&& COMPARE_DESC_MEMBERS(k_desc)
&& COMPARE_DESC_MEMBERS(v_desc)
&& COMPARE_DESC_MEMBERS(dst_desc)
&& COMPARE_DESC_MEMBERS(attn_mask_desc)
&& COMPARE_DESC_MEMBERS(scale_dt)
&& COMPARE_DESC_MEMBERS(invert_scale);
return ret;
}
// clang-format on
#undef COMPARE_DESC_MEMBERS
#undef COMPARE_DESC_ARRAY_MEMBERS
#undef DEREF_AND_COMPARE_DESC_MEMBERS
#undef COMPARE_FLOAT_DESC_MEMBERS
#undef COMPARE_FLOAT_DESC_ARRAY_MEMBERS
inline bool is_dense_format_kind(const std::vector<const memory_desc_t *> mds) {
#ifdef DNNL_EXPERIMENTAL_SPARSE
for (const auto *md : mds)
if (md->format_kind == format_kind::sparse) return false;
#endif
return true;
}
inline memory_desc_t cvt_blocked2sparse_packed(
const memory_desc_t &blocked_md, dim_t nnz) {
if (blocked_md.format_kind != format_kind::blocked) return glob_zero_md;
auto sparse_packed_md = blocked_md;
sparse_packed_md.format_kind = format_kind::sparse;
sparse_packed_md.format_desc.sparse_desc.encoding = sparse_encoding::packed;
sparse_packed_md.format_desc.sparse_desc.nnz = nnz;
sparse_packed_md.format_desc.sparse_desc.packed_desc
= blocked_md.format_desc.blocking;
return sparse_packed_md;
}
inline memory_desc_t cvt_sparse_packed2blocked(
const memory_desc_t &sparse_packed_md) {
if (sparse_packed_md.format_kind != format_kind::sparse
|| sparse_packed_md.format_desc.sparse_desc.encoding
!= sparse_encoding::packed)
return glob_zero_md;