-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathop_executable.cpp
2369 lines (1998 loc) · 91.9 KB
/
op_executable.cpp
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 2022-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.
*******************************************************************************/
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include <type_traits>
#include <unordered_map>
#include "oneapi/dnnl/dnnl.hpp"
#include <graph/utils/utils.hpp>
#include "graph/backend/dnnl/common.hpp"
#include "graph/backend/dnnl/dnnl_constant_tensor_cache.hpp"
#include "graph/backend/dnnl/fusion_info.hpp"
#include "graph/backend/dnnl/internal_attrs.hpp"
#include "graph/backend/dnnl/op_executable.hpp"
namespace dnnl {
namespace impl {
namespace graph {
namespace dnnl_impl {
const indices_t::type_t input = indices_t::type_t::input;
const indices_t::type_t output = indices_t::type_t::output;
conv_fwd_executable_t::desc_t conv_fwd_executable_t::create_desc(
std::shared_ptr<op_t> &op, const dnnl::engine &p_engine,
fusion_info_mgr_t &mgr, pd_cache_t &pd_cache) {
// first look up the cache
if (pd_cache.find(op.get()) != pd_cache.end()) {
auto pd = graph::utils::any_cast<
dnnl::convolution_forward::primitive_desc>(
pd_cache.at(op.get()));
return {pd, true};
}
// prepare the operator attributes
auto strides = op->get_attr<dims>(op_attr::strides);
auto dilates = op->get_attr<dims>(op_attr::dilations);
auto pads_begin = op->get_attr<dims>(op_attr::pads_begin);
auto pads_end = op->get_attr<dims>(op_attr::pads_end);
dilates = get_compatible_dilates(dilates);
dnnl::primitive_attr prm_attr;
fusion_info_t fusion_info;
if (op->has_attr(op_attr::fusion_info_key)
&& op->get_attr<int64_t>(op_attr::fusion_info_key) != -1) {
int64_t key = op->get_attr<int64_t>(op_attr::fusion_info_key);
fusion_info = mgr.get_info(key);
prm_attr = make_dnnl_primitive_attr(op, fusion_info);
}
prm_attr.set_scratchpad_mode(dnnl::scratchpad_mode::user);
auto fpmath = mgr.get_fpmath_mode();
prm_attr.set_fpmath_mode(
static_cast<dnnl::fpmath_mode>(fpmath.mode_), fpmath.apply_to_int_);
const bool can_use_blocked_layout = mgr.get_use_blocked_layout();
auto src = make_dnnl_memory_desc(
op->get_input_value(0)->get_logical_tensor());
// assume constant weight is for inference scenario
const auto &wei_lt = op->get_input_value(1)->get_logical_tensor();
auto pkind = (logical_tensor_wrapper_t(wei_lt).property_type()
== property_type::constant)
? prop_kind::forward_inference
: prop_kind::forward_training;
auto weight = make_dnnl_memory_desc(wei_lt);
weight = to_format_any(weight);
auto base_conv_dst_lt = op->get_output_value(0)->get_logical_tensor();
if (fusion_info.has_post_dw_conv()) {
// when fused post depthwise conv, onednn required to use the base conv
// dst md to create the conv primitive. in the subgraph, the base conv
// dst is a intermediate output which has been fused away, so here we
// get it from fusion info
const auto &dw_conv = fusion_info.get_post_dw_conv();
base_conv_dst_lt
= dw_conv->get_op()->get_input_value(0)->get_logical_tensor();
}
auto dst = make_dnnl_memory_desc(base_conv_dst_lt);
auto create_pd = [&](const dnnl::memory::desc &src_md,
const dnnl::memory::desc &dst_md) {
if (op->has_attr(op_attr::with_bias)
&& op->get_attr<bool>(op_attr::with_bias)) {
auto bias = make_dnnl_memory_desc(
op->get_input_value(2)->get_logical_tensor());
bias = to_format_any(bias);
return dnnl::convolution_forward::primitive_desc(p_engine, pkind,
algorithm::convolution_direct, src_md, weight, bias, dst_md,
strides, dilates, pads_begin, pads_end, prm_attr);
} else {
return dnnl::convolution_forward::primitive_desc(p_engine, pkind,
algorithm::convolution_direct, src_md, weight, dst_md,
strides, dilates, pads_begin, pads_end, prm_attr);
}
};
if (!can_use_blocked_layout) {
src = to_nxc_format(src);
dst = to_nxc_format(dst);
} else {
// If the dst has been explicitly set to nxc layout or the data_format
// has been defined as NXC by users, we prefer to directly use optimal
// blocked src and plain dst to create conv pd. In the following, we
// will first query out the optimal src.
bool permute_nxc_dst = false;
if (op->get_output_value(0)->get_consumers().size() == 1) {
const auto &next_op
= op->get_output_value(0)->get_consumers()[0].get_op();
if (next_op.get_kind() == op_kind::dnnl_permute) {
auto permute_dst_lt
= next_op.get_output_value(0)->get_logical_tensor();
auto perm = get_permutation(permute_dst_lt.ndims, "NCX", "NXC");
if (next_op.get_attr<std::vector<int64_t>>(op_attr::permutation)
== perm) {
auto inverse_perm = get_permutation(
permute_dst_lt.ndims, "NXC", "NCX");
auto perm_dst = make_dnnl_memory_desc(permute_dst_lt);
dst = perm_dst.permute_axes(
dnnl_impl::utils::cast_to_int32(inverse_perm));
permute_nxc_dst = true;
}
}
}
if (!is_format(dst, "nxc") && !permute_nxc_dst) {
src = to_format_any(src);
dst = to_format_any(dst);
} else {
auto tmp_src = to_format_any(src);
auto tmp_dst = to_format_any(dst);
dnnl::convolution_forward::primitive_desc tmp_pd
= create_pd(tmp_src, tmp_dst);
src = tmp_pd.src_desc();
}
}
dnnl::convolution_forward::primitive_desc pd = create_pd(src, dst);
pd_cache.insert({op.get(), pd});
return {pd, false};
}
deconv_fwd_executable_t::desc_t deconv_fwd_executable_t::create_desc(
std::shared_ptr<op_t> &op, const dnnl::engine &p_engine,
fusion_info_mgr_t &mgr, pd_cache_t &pd_cache) {
// first look up the cache
if (pd_cache.find(op.get()) != pd_cache.end()) {
auto pd = graph::utils::any_cast<
dnnl::deconvolution_forward::primitive_desc>(
pd_cache.at(op.get()));
return {pd, true};
}
// prepare the operator attributes
auto strides = op->get_attr<dims>(op_attr::strides);
auto dilates = op->get_attr<dims>(op_attr::dilations);
auto pads_begin = op->get_attr<dims>(op_attr::pads_begin);
auto pads_end = op->get_attr<dims>(op_attr::pads_end);
dilates = get_compatible_dilates(dilates);
dnnl::primitive_attr prm_attr;
if (op->has_attr(op_attr::fusion_info_key)
&& op->get_attr<int64_t>(op_attr::fusion_info_key) != -1) {
int64_t key = op->get_attr<int64_t>(op_attr::fusion_info_key);
prm_attr = make_dnnl_primitive_attr(op, mgr.get_info(key));
}
prm_attr.set_scratchpad_mode(dnnl::scratchpad_mode::user);
auto fpmath = mgr.get_fpmath_mode();
prm_attr.set_fpmath_mode(
static_cast<dnnl::fpmath_mode>(fpmath.mode_), fpmath.apply_to_int_);
auto src = make_dnnl_memory_desc(
op->get_input_value(0)->get_logical_tensor());
src = to_format_any(src);
auto weight = make_dnnl_memory_desc(
op->get_input_value(1)->get_logical_tensor());
weight = to_format_any(weight);
auto dst = make_dnnl_memory_desc(
op->get_output_value(0)->get_logical_tensor());
dst = to_format_any(dst);
dnnl::deconvolution_forward::primitive_desc pd;
if (op->has_attr(op_attr::with_bias)
&& op->get_attr<bool>(op_attr::with_bias)) {
auto bias = make_dnnl_memory_desc(
op->get_input_value(2)->get_logical_tensor());
bias = to_format_any(bias);
pd = dnnl::deconvolution_forward::primitive_desc(p_engine,
prop_kind::forward_inference, algorithm::deconvolution_direct,
src, weight, bias, dst, strides, dilates, pads_begin, pads_end,
prm_attr);
} else {
pd = dnnl::deconvolution_forward::primitive_desc(p_engine,
prop_kind::forward_inference, algorithm::deconvolution_direct,
src, weight, dst, strides, dilates, pads_begin, pads_end,
prm_attr);
}
pd_cache.insert({op.get(), pd});
return {pd, false};
}
deconv_bwd_data_executable_t::desc_t deconv_bwd_data_executable_t::create_desc(
std::shared_ptr<op_t> &op, const dnnl::engine &p_engine,
fusion_info_mgr_t &mgr, pd_cache_t &pd_cache) {
// first look up the cache
if (pd_cache.find(op.get()) != pd_cache.end()) {
auto pd = graph::utils::any_cast<
dnnl::deconvolution_backward_data::primitive_desc>(
pd_cache.at(op.get()));
return {pd, true};
}
// prepare the operator attributes
auto strides = op->get_attr<dims>(op_attr::strides);
auto dilates = op->get_attr<dims>(op_attr::dilations);
auto pads_begin = op->get_attr<dims>(op_attr::pads_begin);
auto pads_end = op->get_attr<dims>(op_attr::pads_end);
dilates = get_compatible_dilates(dilates);
dnnl::primitive_attr prm_attr;
if (op->has_attr(op_attr::fusion_info_key)
&& op->get_attr<int64_t>(op_attr::fusion_info_key) != -1) {
int64_t key = op->get_attr<int64_t>(op_attr::fusion_info_key);
prm_attr = make_dnnl_primitive_attr(op, mgr.get_info(key));
}
prm_attr.set_scratchpad_mode(dnnl::scratchpad_mode::user);
auto fpmath = mgr.get_fpmath_mode();
prm_attr.set_fpmath_mode(
static_cast<dnnl::fpmath_mode>(fpmath.mode_), fpmath.apply_to_int_);
auto diff_dst = make_dnnl_memory_desc(
op->get_input_value(0)->get_logical_tensor());
diff_dst = to_format_any(diff_dst);
auto weight = make_dnnl_memory_desc(
op->get_input_value(1)->get_logical_tensor());
weight = to_format_any(weight);
auto diff_src = make_dnnl_memory_desc(
op->get_output_value(0)->get_logical_tensor());
diff_src = to_format_any(diff_src);
auto fwd_hints = dnnl::deconvolution_forward::primitive_desc(p_engine,
prop_kind::forward_training, algorithm::deconvolution_direct,
diff_src, weight, diff_dst, strides, dilates, pads_begin, pads_end,
prm_attr);
dnnl::deconvolution_backward_data::primitive_desc pd(p_engine,
dnnl::algorithm::deconvolution_direct, diff_src, weight, diff_dst,
strides, pads_begin, pads_end, fwd_hints);
pd_cache.insert({op.get(), pd});
return {pd, false};
}
deconv_bwd_weights_executable_t::desc_t
deconv_bwd_weights_executable_t::create_desc(std::shared_ptr<op_t> &op,
const dnnl::engine &p_engine, fusion_info_mgr_t &mgr,
pd_cache_t &pd_cache) {
// first look up the cache
if (pd_cache.find(op.get()) != pd_cache.end()) {
auto pd = graph::utils::any_cast<
dnnl::deconvolution_backward_weights::primitive_desc>(
pd_cache.at(op.get()));
return {pd, true};
}
// prepare the operator attributes
auto strides = op->get_attr<dims>(op_attr::strides);
auto dilates = op->get_attr<dims>(op_attr::dilations);
auto pads_begin = op->get_attr<dims>(op_attr::pads_begin);
auto pads_end = op->get_attr<dims>(op_attr::pads_end);
dilates = get_compatible_dilates(dilates);
dnnl::primitive_attr prm_attr;
if (op->has_attr(op_attr::fusion_info_key)) {
int64_t key = op->get_attr<int64_t>(op_attr::fusion_info_key);
prm_attr = make_dnnl_primitive_attr(op, mgr.get_info(key));
}
auto fpmath = mgr.get_fpmath_mode();
prm_attr.set_fpmath_mode(
static_cast<dnnl::fpmath_mode>(fpmath.mode_), fpmath.apply_to_int_);
auto src = make_dnnl_memory_desc(
op->get_input_value(0)->get_logical_tensor());
src = to_format_any(src);
auto diff_dst = make_dnnl_memory_desc(
op->get_input_value(1)->get_logical_tensor());
diff_dst = to_format_any(diff_dst);
auto diff_weight = make_dnnl_memory_desc(
op->get_output_value(0)->get_logical_tensor());
diff_weight = to_format_any(diff_weight);
auto fwd_hints = dnnl::deconvolution_forward::primitive_desc(p_engine,
dnnl::prop_kind::forward_training,
dnnl::algorithm::deconvolution_direct, src, diff_weight, diff_dst,
strides, dilates, pads_begin, pads_end);
dnnl::deconvolution_backward_weights::primitive_desc pd(p_engine,
dnnl::algorithm::deconvolution_direct, src, diff_weight, diff_dst,
strides, dilates, pads_begin, pads_end, fwd_hints);
pd_cache.insert({op.get(), pd});
return {pd, false};
}
matmul_executable_t::desc_t matmul_executable_t::create_desc(
std::shared_ptr<op_t> &op, const dnnl::engine &p_engine,
fusion_info_mgr_t &mgr, pd_cache_t &pd_cache) {
// first look up the cache
if (pd_cache.find(op.get()) != pd_cache.end()) {
auto pd = graph::utils::any_cast<dnnl::matmul::primitive_desc>(
pd_cache.at(op.get()));
return {pd, true};
}
const bool can_use_blocked_layout = mgr.get_use_blocked_layout();
dnnl::primitive_attr prm_attr;
if (op->has_attr(op_attr::fusion_info_key)
&& op->get_attr<int64_t>(op_attr::fusion_info_key) != -1) {
int64_t key = op->get_attr<int64_t>(op_attr::fusion_info_key);
prm_attr = make_dnnl_primitive_attr(op, mgr.get_info(key));
}
prm_attr.set_scratchpad_mode(dnnl::scratchpad_mode::user);
auto fpmath = mgr.get_fpmath_mode();
prm_attr.set_fpmath_mode(
static_cast<dnnl::fpmath_mode>(fpmath.mode_), fpmath.apply_to_int_);
auto src = make_dnnl_memory_desc(
op->get_input_value(0)->get_logical_tensor());
// For non-constant activation, create primitive desc with strided layout
// when:
// 1) activation has 4 dimensions and layout is acbd since oneDNN has
// optimized kernel
// 2) activation has 2/3 dimensions and device kind is gpu for avoiding
// blocked activation. This can reduce the cost for the reorder between
// plain and block layout, especially for users who compile partition
// with plain layout. The performance of strided primitive on GPU will be
// optimized by oneDNN.
bool const_activation
= logical_tensor_wrapper_t(
op->get_input_value(0)->get_logical_tensor())
.is_constant()
&& is_constant_cache_enabled(p_engine);
const bool use_strided_src = !const_activation
&& ((src.get_ndims() == 4
&& is_format(src, dnnl::memory::format_tag::acbd))
|| ((src.get_ndims() == 2 || src.get_ndims() == 3)
&& p_engine.get_kind() == dnnl::engine::kind::gpu));
// convert src memory desc to any when:
// 1) not the situation mentioned above
// 2) the given md is blocked and convert to queried layout is necessary
if (can_use_blocked_layout && (!use_strided_src || !is_plain(src))) {
src = to_format_any(src);
}
auto wei = make_dnnl_memory_desc(
op->get_input_value(1)->get_logical_tensor());
// For non-constant weight, create primitive desc with strided layout when:
// 1) weight has 4 dimensions and layout is adbc/abdc/acbd since oneDNN has
// optimized kernel
bool const_weight = logical_tensor_wrapper_t(
op->get_input_value(1)->get_logical_tensor())
.is_constant()
&& is_constant_cache_enabled(p_engine);
const bool use_strided_wei = wei.get_ndims() == 4
&& (is_format(wei, dnnl::memory::format_tag::adbc)
|| is_format(wei, dnnl::memory::format_tag::abdc)
|| is_format(wei, dnnl::memory::format_tag::acbd));
if (const_weight || (can_use_blocked_layout && !use_strided_wei)) {
wei = to_format_any(wei);
}
auto dst = make_dnnl_memory_desc(
op->get_output_value(0)->get_logical_tensor());
const bool keep_dst_layout = op->has_attr(op_attr::keep_dst_layout)
&& op->get_attr<bool>(op_attr::keep_dst_layout);
const bool use_strided_dst
= ((src.get_ndims() == 2 || src.get_ndims() == 3)
&& p_engine.get_kind() == dnnl::engine::kind::gpu)
|| keep_dst_layout;
if (can_use_blocked_layout && !use_strided_dst) {
dst = to_format_any(dst);
} else if (dst.get_format_kind() == dnnl::memory::format_kind::any
&& !keep_dst_layout) {
// convert to strided for avoiding blocked activation. The format kind
// of dst is possible to be any when:
// 1) It is created with internal logical tensor
// 2) It is the partition output and defined by user
dst = to_ncx_format(dst);
} else {
// do nothing
}
dnnl::matmul::primitive_desc pd;
if (op->has_attr(op_attr::with_bias)
&& op->get_attr<bool>(op_attr::with_bias)) {
auto bias = make_dnnl_memory_desc(
op->get_input_value(2)->get_logical_tensor());
bias = to_format_any(bias);
pd = dnnl::matmul::primitive_desc(
p_engine, src, wei, bias, dst, prm_attr);
} else {
pd = dnnl::matmul::primitive_desc(p_engine, src, wei, dst, prm_attr);
}
pd_cache.insert({op.get(), pd});
return {pd, false};
}
pool_executable_t::desc_t pool_executable_t::create_desc(
std::shared_ptr<op_t> &op, const dnnl::engine &p_engine,
fusion_info_mgr_t &mgr, pd_cache_t &pd_cache) {
// first look up the cache
if (pd_cache.find(op.get()) != pd_cache.end()) {
auto pd = graph::utils::any_cast<dnnl::pooling_forward::primitive_desc>(
pd_cache.at(op.get()));
return {pd, true};
}
dims strides = op->get_attr<dims>(op_attr::strides);
dims kernel = op->get_attr<dims>(op_attr::kernel);
dims pads_begin = op->get_attr<dims>(op_attr::pads_begin);
dims pads_end = op->get_attr<dims>(op_attr::pads_end);
dims dilations(strides.size(), 1);
if (op->has_attr(op_attr::dilations)
&& (op->get_attr<std::string>(op_attr::kind) == "maxpool")) {
dilations = op->get_attr<dims>(op_attr::dilations);
}
dnnl::primitive_attr prm_attr;
if (op->has_attr(op_attr::fusion_info_key)
&& op->get_attr<int64_t>(op_attr::fusion_info_key) != -1) {
int64_t key = op->get_attr<int64_t>(op_attr::fusion_info_key);
prm_attr = make_dnnl_primitive_attr(op, mgr.get_info(key));
}
prm_attr.set_scratchpad_mode(dnnl::scratchpad_mode::user);
auto src = make_dnnl_memory_desc(
op->get_input_value(0)->get_logical_tensor());
auto dst = make_dnnl_memory_desc(
op->get_output_value(0)->get_logical_tensor());
dst = to_format_any(dst);
// infer dnnl explicit padding
dims new_pads_end(pads_end);
bool adj_pad = false;
std::string rounding_type = "floor";
if (op->has_attr(op_attr::rounding_type)) {
rounding_type = op->get_attr<std::string>(op_attr::rounding_type);
}
// oneDNN pooling primitive doesn't support ceil mode, so we need to add
// additional padding right to simulate the ceil mode by using floor mode,
// and then exclude those additional paddings when doing average.
if (rounding_type == "ceil") {
dims src_sp = src.get_dims();
src_sp.erase(src_sp.begin(), src_sp.begin() + 2);
dims output_sp = dst.get_dims();
output_sp.erase(output_sp.begin(), output_sp.begin() + 2);
for (size_t i = 0; i < kernel.size(); ++i) {
dim_t dilated = dilations[i] * (kernel[i] - 1) + 1;
// calculate the expected padded input size according to floor mode
// formula: output = (padded - dilated) / strides + 1
dim_t expected_padded = (output_sp[i] - 1) * strides[i] + dilated;
dim_t cur_pads_end = expected_padded - src_sp[i] - pads_begin[i];
new_pads_end[i] = cur_pads_end;
}
adj_pad = true;
}
algorithm algo = algorithm::undef;
prop_kind prop = prop_kind::forward_inference;
if (op->get_attr<std::string>(op_attr::kind) == "maxpool") {
algo = algorithm::pooling_max;
dilations = get_compatible_dilates(dilations, src.get_ndims());
if (op->num_outputs() == 3) {
prop = prop_kind::forward_training;
op->set_attr<bool>(op_attr::is_training, true);
}
} else if (op->get_attr<std::string>(op_attr::kind) == "avgpool") {
const bool exclude_pad = op->get_attr<bool>(op_attr::exclude_pad);
dilations = dims(src.get_ndims(), 0);
algo = (exclude_pad || adj_pad)
? algorithm::pooling_avg_exclude_padding
: algorithm::pooling_avg_include_padding;
} else {
BACKEND_DNNL_ENFORCE(
0, "Currently only int8 MaxPool/AvgPool is supported.");
}
dnnl::pooling_forward::primitive_desc pd(p_engine, prop, algo, src, dst,
strides, kernel, dilations, pads_begin, new_pads_end, prm_attr);
pd_cache.insert({op.get(), pd});
return {pd, false};
}
pool_bwd_executable_t::desc_t pool_bwd_executable_t::create_desc(
std::shared_ptr<op_t> &op, const dnnl::engine &p_engine,
fusion_info_mgr_t &mgr, pd_cache_t &pd_cache) {
// first look up the cache
if (pd_cache.find(op.get()) != pd_cache.end()) {
auto pd = graph::utils::any_cast<
dnnl::pooling_backward::primitive_desc>(pd_cache.at(op.get()));
return {pd, true};
}
dims strides = op->get_attr<dims>(op_attr::strides);
dims kernel = op->get_attr<dims>(op_attr::kernel);
dims pads_begin = op->get_attr<dims>(op_attr::pads_begin);
dims pads_end = op->get_attr<dims>(op_attr::pads_end);
dims dilations(strides.size(), 0);
if (op->has_attr(op_attr::dilations)) {
dilations = op->get_attr<dims>(op_attr::dilations);
}
dnnl::primitive_attr prm_attr;
if (op->has_attr(op_attr::fusion_info_key)) {
int64_t key = op->get_attr<int64_t>(op_attr::fusion_info_key);
prm_attr = make_dnnl_primitive_attr(op, mgr.get_info(key));
}
prm_attr.set_scratchpad_mode(dnnl::scratchpad_mode::user);
auto diff_dst = make_dnnl_memory_desc(
op->get_input_value(0)->get_logical_tensor());
auto diff_src = make_dnnl_memory_desc(
op->get_output_value(0)->get_logical_tensor());
auto src = op->get_attr<std::string>(op_attr::kind) == "maxpool"
? make_dnnl_memory_desc(
op->get_input_value(2)->get_logical_tensor())
: dnnl::memory::desc(diff_src.get_dims(), diff_src.get_data_type(),
get_ncx_format(diff_src.get_dims()));
// infer dnnl explicit pad
dims new_pads_end(pads_end);
bool adj_pad = false;
std::string rounding_type = "floor";
if (op->has_attr(op_attr::rounding_type)) {
rounding_type = op->get_attr<std::string>(op_attr::rounding_type);
}
if (rounding_type == "ceil") {
dims src_sp = src.get_dims();
src_sp.erase(src_sp.begin(), src_sp.begin() + 2);
dims output_sp = diff_dst.get_dims();
output_sp.erase(output_sp.begin(), output_sp.begin() + 2);
for (size_t i = 0; i < kernel.size(); ++i) {
dim_t dilated = dilations[i] * (kernel[i] - 1) + 1;
if (op->get_attr<std::string>(op_attr::kind) == "avgpool")
dilated += 1;
dim_t cur_pads_end = (output_sp[i] - 1) * strides[i] + dilated
- src_sp[i] - pads_begin[i];
new_pads_end[i] = cur_pads_end;
}
adj_pad = true;
}
algorithm algo = algorithm::undef;
if (op->get_attr<std::string>(op_attr::kind) == "maxpool") {
algo = algorithm::pooling_max;
dilations = get_compatible_dilates(dilations, src.get_ndims());
} else if (op->get_attr<std::string>(op_attr::kind) == "avgpool") {
const bool exclude_pad = op->get_attr<bool>(op_attr::exclude_pad);
algo = (exclude_pad || adj_pad)
? algorithm::pooling_avg_exclude_padding
: algorithm::pooling_avg_include_padding;
} else {
BACKEND_DNNL_ENFORCE(0,
"Currently only MaxPoolBackprop/AvgPoolBackprop is "
"supported.");
}
if (op->get_attr<std::string>(op_attr::kind) == "maxpool") {
diff_dst = to_format_any(diff_dst);
}
dnnl::pooling_forward::primitive_desc forward_hints
= dnnl::pooling_forward::primitive_desc(p_engine,
prop_kind::forward_training, algo, src, diff_dst, strides,
kernel, dilations, pads_begin, new_pads_end);
dnnl::pooling_backward::primitive_desc pd(p_engine, algo, diff_src,
diff_dst, strides, kernel, dilations, pads_begin, new_pads_end,
forward_hints);
pd_cache.insert({op.get(), pd});
return {pd, false};
}
batchnorm_executable_t::desc_t batchnorm_executable_t::create_desc(
std::shared_ptr<op_t> &op, const dnnl::engine &p_engine,
fusion_info_mgr_t &mgr, pd_cache_t &pd_cache) {
// first look up the cache
if (pd_cache.find(op.get()) != pd_cache.end()) {
auto pd = graph::utils::any_cast<
dnnl::batch_normalization_forward::primitive_desc>(
pd_cache.at(op.get()));
return {pd, true};
}
float epsilon = op->get_attr<float>(op_attr::epsilon);
auto flags = dnnl::normalization_flags::none;
// for inference
if (!op->get_attr<bool>(op_attr::is_training)) {
flags |= dnnl::normalization_flags::use_global_stats;
flags |= dnnl::normalization_flags::use_scale;
flags |= dnnl::normalization_flags::use_shift;
} else {
// for training, inputs: [src, mean, variance, gamma, beta]
if (op->num_inputs() > 3) {
flags |= dnnl::normalization_flags::use_scale;
flags |= dnnl::normalization_flags::use_shift;
}
if (op->has_attr(op_attr::fuse_relu)
&& op->get_attr<bool>(op_attr::fuse_relu))
flags |= dnnl::normalization_flags::fuse_norm_relu;
}
dnnl::primitive_attr prm_attr;
if (op->has_attr(op_attr::fusion_info_key)
&& op->get_attr<int64_t>(op_attr::fusion_info_key) != -1) {
int64_t key = op->get_attr<int64_t>(op_attr::fusion_info_key);
prm_attr = make_dnnl_primitive_attr(op, mgr.get_info(key));
}
prm_attr.set_scratchpad_mode(dnnl::scratchpad_mode::user);
auto src = make_dnnl_memory_desc(
op->get_input_value(0)->get_logical_tensor());
auto dst = make_dnnl_memory_desc(
op->get_output_value(0)->get_logical_tensor());
dst = to_format_any(dst);
if (src.get_inner_nblks() == 1 && src.get_inner_idxs()[0] == 1
&& src.get_inner_blks()[0] == 4) {
// to default format
src = to_ncx_format(src);
}
auto pkind = op->get_attr<bool>(op_attr::is_training)
? prop_kind::forward_training
: prop_kind::forward_inference;
dnnl::batch_normalization_forward::primitive_desc pd(
p_engine, pkind, src, dst, epsilon, flags, prm_attr);
pd_cache.insert({op.get(), pd});
return {pd, false};
}
batchnorm_bwd_executable_t::desc_t batchnorm_bwd_executable_t::create_desc(
std::shared_ptr<op_t> &op, const dnnl::engine &p_engine,
fusion_info_mgr_t &mgr, pd_cache_t &pd_cache) {
// first look up the cache
if (pd_cache.find(op.get()) != pd_cache.end()) {
auto pd = graph::utils::any_cast<
dnnl::batch_normalization_backward::primitive_desc>(
pd_cache.at(op.get()));
return {pd, true};
}
float epsilon = op->get_attr<float>(op_attr::epsilon);
auto flags = dnnl::normalization_flags::none;
// [diff_src, diff_scale, diff_shift, scratchpad]
if (op->num_outputs() > 2) {
flags |= dnnl::normalization_flags::use_scale;
flags |= dnnl::normalization_flags::use_shift;
} else {
// [diff_src, scratchpad]
flags |= dnnl::normalization_flags::use_global_stats;
}
dnnl::primitive_attr prm_attr;
if (op->has_attr(op_attr::fusion_info_key)
&& op->get_attr<int64_t>(op_attr::fusion_info_key) != -1) {
int64_t key = op->get_attr<int64_t>(op_attr::fusion_info_key);
prm_attr = make_dnnl_primitive_attr(op, mgr.get_info(key));
}
prm_attr.set_scratchpad_mode(dnnl::scratchpad_mode::user);
auto src = make_dnnl_memory_desc(
op->get_input_value(0)->get_logical_tensor());
if (src.get_inner_nblks() == 1 && src.get_inner_idxs()[0] == 1
&& src.get_inner_blks()[0] == 4) {
// to default format
src = to_ncx_format(src);
}
auto forward_hints = dnnl::batch_normalization_forward::primitive_desc(
p_engine, prop_kind::forward_training, src, src, epsilon, flags);
dnnl::batch_normalization_backward::primitive_desc pd(p_engine,
prop_kind::backward, src, forward_hints.dst_desc(), src, epsilon,
flags, forward_hints);
pd_cache.insert({op.get(), pd});
return {pd, false};
}
layernorm_executable_t::desc_t layernorm_executable_t::create_desc(
std::shared_ptr<op_t> &op, const dnnl::engine &p_engine,
fusion_info_mgr_t &mgr, pd_cache_t &pd_cache) {
// first look up the cache
if (pd_cache.find(op.get()) != pd_cache.end()) {
auto pd = graph::utils::any_cast<
dnnl::layer_normalization_forward::primitive_desc>(
pd_cache.at(op.get()));
return {pd, true};
}
dnnl::primitive_attr prm_attr;
if (op->has_attr(op_attr::fusion_info_key)
&& op->get_attr<int64_t>(op_attr::fusion_info_key) != -1) {
int64_t key = op->get_attr<int64_t>(op_attr::fusion_info_key);
prm_attr = make_dnnl_primitive_attr(op, mgr.get_info(key));
}
prm_attr.set_scratchpad_mode(dnnl::scratchpad_mode::user);
float epsilon = 1e-5f;
if (op->has_attr(op_attr::epsilon))
epsilon = op->get_attr<float>(op_attr::epsilon);
bool keep_stats = true;
if (op->has_attr(op_attr::keep_stats))
keep_stats = op->get_attr<bool>(op_attr::keep_stats);
bool use_affine = true;
if (op->has_attr(op_attr::use_affine))
use_affine = op->get_attr<bool>(op_attr::use_affine);
auto flags = dnnl::normalization_flags::none;
if (use_affine)
flags |= (dnnl::normalization_flags::use_scale
| dnnl::normalization_flags::use_shift);
prop_kind pkind = keep_stats ? prop_kind::forward_training
: prop_kind::forward_inference;
auto src = make_dnnl_memory_desc(
op->get_input_value(0)->get_logical_tensor());
// onednn 3.6 spec: Implementations optimized for memory formats ab, abc,
// bac, abcd
src = to_ncx_format(src);
auto dst = make_dnnl_memory_desc(
op->get_output_value(0)->get_logical_tensor());
dst = to_format_any(dst);
dnnl::layer_normalization_forward::primitive_desc pd(
p_engine, pkind, src, dst, epsilon, flags, prm_attr);
pd_cache.insert({op.get(), pd});
return {pd, false};
}
layernorm_bwd_executable_t::desc_t layernorm_bwd_executable_t::create_desc(
std::shared_ptr<op_t> &op, const dnnl::engine &p_engine,
fusion_info_mgr_t &mgr, pd_cache_t &pd_cache) {
if (pd_cache.find(op.get()) != pd_cache.end()) {
auto pd = graph::utils::any_cast<
dnnl::layer_normalization_backward::primitive_desc>(
pd_cache.at(op.get()));
return {pd, true};
}
dnnl::primitive_attr prm_attr;
if (op->has_attr(op_attr::fusion_info_key)
&& op->get_attr<int64_t>(op_attr::fusion_info_key) != -1) {
int64_t key = op->get_attr<int64_t>(op_attr::fusion_info_key);
prm_attr = make_dnnl_primitive_attr(op, mgr.get_info(key));
}
prm_attr.set_scratchpad_mode(dnnl::scratchpad_mode::user);
auto epsilon = op->get_attr<float>(op_attr::epsilon);
auto flags = dnnl::normalization_flags::none;
const bool use_affine = op->get_attr<bool>(op_attr::use_affine);
if (use_affine) {
flags |= dnnl::normalization_flags::use_scale;
flags |= dnnl::normalization_flags::use_shift;
}
auto src = make_dnnl_memory_desc(
op->get_input_value(0)->get_logical_tensor());
auto diff_dst = make_dnnl_memory_desc(
op->get_input_value(1)->get_logical_tensor());
auto diff_src = make_dnnl_memory_desc(
op->get_output_value(0)->get_logical_tensor());
dnnl::layer_normalization_forward::primitive_desc fwd_hints(p_engine,
prop_kind::forward_training, src, diff_dst, epsilon, flags);
dnnl::layer_normalization_backward::primitive_desc pd(p_engine,
prop_kind::backward, diff_src, diff_dst, src, epsilon, flags,
fwd_hints, prm_attr);
pd_cache.insert({op.get(), pd});
return {pd, false};
}
conv_bwd_data_executable_t::desc_t conv_bwd_data_executable_t::create_desc(
std::shared_ptr<op_t> &op, const dnnl::engine &p_engine,
fusion_info_mgr_t &mgr, pd_cache_t &pd_cache) {
// first look up the cache
if (pd_cache.find(op.get()) != pd_cache.end()) {
auto pd = graph::utils::any_cast<
dnnl::convolution_backward_data::primitive_desc>(
pd_cache.at(op.get()));
return {pd, true};
}
// prepare the operator attributes
auto strides = op->get_attr<dims>(op_attr::strides);
auto dilates = op->get_attr<dims>(op_attr::dilations);
auto pads_begin = op->get_attr<dims>(op_attr::pads_begin);
auto pads_end = op->get_attr<dims>(op_attr::pads_end);
dilates = get_compatible_dilates(dilates);
dnnl::primitive_attr prm_attr;
if (op->has_attr(op_attr::fusion_info_key)
&& op->get_attr<int64_t>(op_attr::fusion_info_key) != -1) {
int64_t key = op->get_attr<int64_t>(op_attr::fusion_info_key);
prm_attr = make_dnnl_primitive_attr(op, mgr.get_info(key));
}
prm_attr.set_scratchpad_mode(dnnl::scratchpad_mode::user);
auto fpmath = mgr.get_fpmath_mode();
prm_attr.set_fpmath_mode(
static_cast<dnnl::fpmath_mode>(fpmath.mode_), fpmath.apply_to_int_);
const bool can_use_blocked_layout = mgr.get_use_blocked_layout();
auto diff_dst = make_dnnl_memory_desc(
op->get_input_value(0)->get_logical_tensor());
if (!can_use_blocked_layout)
diff_dst = to_nxc_format(diff_dst);
else
diff_dst = to_format_any(diff_dst);
auto weight = make_dnnl_memory_desc(
op->get_input_value(1)->get_logical_tensor());
weight = to_format_any(weight);
auto diff_src = make_dnnl_memory_desc(
op->get_output_value(0)->get_logical_tensor());
if (!can_use_blocked_layout)
diff_src = to_nxc_format(diff_src);
else
diff_src = to_format_any(diff_src);
auto fwd_hints = dnnl::convolution_forward::primitive_desc(p_engine,
dnnl::prop_kind::forward_training,
dnnl::algorithm::convolution_direct, diff_src, weight, diff_dst,
strides, dilates, pads_begin, pads_end);
dnnl::convolution_backward_data::primitive_desc pd(p_engine,
dnnl::algorithm::convolution_direct, diff_src, weight, diff_dst,
strides, dilates, pads_begin, pads_end, fwd_hints);
pd_cache.insert({op.get(), pd});
return {pd, false};
}
conv_bwd_weights_executable_t::desc_t
conv_bwd_weights_executable_t::create_desc(std::shared_ptr<op_t> &op,
const dnnl::engine &p_engine, fusion_info_mgr_t &mgr,
pd_cache_t &pd_cache) {
// first look up the cache
if (pd_cache.find(op.get()) != pd_cache.end()) {
auto pd = graph::utils::any_cast<
dnnl::convolution_backward_weights::primitive_desc>(
pd_cache.at(op.get()));
return {pd, true};
}
// prepare the operator attributes
auto strides = op->get_attr<dims>(op_attr::strides);
auto dilates = op->get_attr<dims>(op_attr::dilations);
auto pads_begin = op->get_attr<dims>(op_attr::pads_begin);
auto pads_end = op->get_attr<dims>(op_attr::pads_end);
dilates = get_compatible_dilates(dilates);
dnnl::primitive_attr prm_attr;
if (op->has_attr(op_attr::fusion_info_key)) {
int64_t key = op->get_attr<int64_t>(op_attr::fusion_info_key);
prm_attr = make_dnnl_primitive_attr(op, mgr.get_info(key));
}
prm_attr.set_scratchpad_mode(dnnl::scratchpad_mode::user);
auto fpmath = mgr.get_fpmath_mode();
prm_attr.set_fpmath_mode(
static_cast<dnnl::fpmath_mode>(fpmath.mode_), fpmath.apply_to_int_);
const bool can_use_blocked_layout = mgr.get_use_blocked_layout();
auto src = make_dnnl_memory_desc(
op->get_input_value(0)->get_logical_tensor());
if (!can_use_blocked_layout)
src = to_nxc_format(src);
else
src = to_format_any(src);
auto diff_dst = make_dnnl_memory_desc(
op->get_input_value(1)->get_logical_tensor());
if (!can_use_blocked_layout)
diff_dst = to_nxc_format(diff_dst);
else
diff_dst = to_format_any(diff_dst);
auto diff_weight = make_dnnl_memory_desc(
op->get_output_value(0)->get_logical_tensor());
diff_weight = to_format_any(diff_weight);
auto fwd_hints = dnnl::convolution_forward::primitive_desc(p_engine,
dnnl::prop_kind::forward_training,
dnnl::algorithm::convolution_direct, src, diff_weight, diff_dst,
strides, dilates, pads_begin, pads_end);
dnnl::convolution_backward_weights::primitive_desc pd(p_engine,
dnnl::algorithm::convolution_direct, src, diff_weight, diff_dst,
strides, dilates, pads_begin, pads_end, fwd_hints);
pd_cache.insert({op.get(), pd});
return {pd, false};
}
eltwise_executable_t::desc_t eltwise_executable_t::create_desc(
std::shared_ptr<op_t> &op, const dnnl::engine &p_engine,
fusion_info_mgr_t &mgr, pd_cache_t &pd_cache) {
// first look up the cache
if (pd_cache.find(op.get()) != pd_cache.end()) {
auto pd = graph::utils::any_cast<dnnl::eltwise_forward::primitive_desc>(
pd_cache.at(op.get()));
return {pd, true};
}
float alpha = 0.f, beta = 0.f;
if (op->has_attr(op_attr::alpha)) {
alpha = op->get_attr<float>(op_attr::alpha);
}
if (op->has_attr(op_attr::beta)) {
beta = op->get_attr<float>(op_attr::beta);
}
dnnl::primitive_attr prm_attr;
if (op->has_attr(op_attr::fusion_info_key)
&& op->get_attr<int64_t>(op_attr::fusion_info_key) != -1) {
int64_t key = op->get_attr<int64_t>(op_attr::fusion_info_key);
prm_attr = make_dnnl_primitive_attr(op, mgr.get_info(key));
}
prm_attr.set_scratchpad_mode(dnnl::scratchpad_mode::user);
auto src = make_dnnl_memory_desc(
op->get_input_value(0)->get_logical_tensor());
auto dst = make_dnnl_memory_desc(
op->get_output_value(0)->get_logical_tensor());
dst = to_format_any(dst);
const algorithm algo = static_cast<dnnl::algorithm>(
op->get_attr<int64_t>(op_attr::alg_kind));
if (algo == algorithm::undef) {
BACKEND_DNNL_ENFORCE(0, "Unsupported eltwise op.");
}
dnnl::eltwise_forward::primitive_desc pd;
pd = dnnl::eltwise_forward::primitive_desc(p_engine, prop_kind::forward,
algo, src, dst, alpha, beta, prm_attr);
pd_cache.insert({op.get(), pd});
return {pd, false};
}
eltwise_bwd_executable_t::desc_t eltwise_bwd_executable_t::create_desc(
std::shared_ptr<op_t> &op, const dnnl::engine &p_engine,
fusion_info_mgr_t &mgr, pd_cache_t &pd_cache) {
if (pd_cache.find(op.get()) != pd_cache.end()) {
auto pd = graph::utils::any_cast<
dnnl::eltwise_backward::primitive_desc>(pd_cache.at(op.get()));
return {pd, true};
}
dnnl::primitive_attr prm_attr;
if (op->has_attr(op_attr::fusion_info_key)
&& op->get_attr<int64_t>(op_attr::fusion_info_key) != -1) {
int64_t key = op->get_attr<int64_t>(op_attr::fusion_info_key);
prm_attr = make_dnnl_primitive_attr(op, mgr.get_info(key));
}
prm_attr.set_scratchpad_mode(dnnl::scratchpad_mode::user);