-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathconfig.cpp
2140 lines (1905 loc) · 78 KB
/
config.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-2025 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 "gpu/intel/jit/conv/config.hpp"
#include <cctype>
#include <cstring>
#include <mutex>
#include "common/utils.hpp"
#include "gpu/intel/jit/conv/grf_usage.hpp"
#include "gpu/intel/jit/conv/message_patterns.hpp"
#include "gpu/intel/jit/conv/normalization.hpp"
#include "gpu/intel/jit/conv/plan.hpp"
#include "gpu/intel/jit/conv/problem.hpp"
#include "gpu/intel/jit/conv/tiler.hpp"
#include "gpu/intel/jit/eltwise_injector.hpp"
#include "gpu/intel/jit/ir/gemm_schedule.hpp"
#include "gpu/intel/jit/ir/tensor_config.hpp"
#define VDISPATCH_CHECK(pd, engine, cond, msg, ...) \
VCONDCHECK(primitive, create, dispatch, convolution, (cond), \
status::unimplemented, "%s," msg, pd->info(engine), ##__VA_ARGS__)
namespace dnnl {
namespace impl {
namespace gpu {
namespace intel {
namespace jit {
// Helper functions.
bool matches_tag(const layout_t &layout, const std::string &tag,
const std::vector<dim_t> &dims) {
if (layout.is_empty()) return false;
auto tag_layout = make_layout(layout.type(), dims, tag);
if (layout != tag_layout) return false;
return true;
}
bool matches_tag(const layout_t &layout, const std::string &tag) {
return matches_tag(layout, tag, layout.dims());
}
bool matches_tag_strict(const layout_t &layout, const std::string &tag) {
if (layout.is_empty()) return false;
auto tag_layout = make_layout(layout.type(), layout.dims(), tag);
if (!layout.is_strictly_equal(tag_layout)) return false;
return true;
}
bool matches_tag(const memory_desc_t &md, const std::string &tag) {
if (md.format_kind == format_kind::any) return false;
std::vector<dim_t> dims(md.dims, md.dims + md.ndims);
return matches_tag(make_layout(md), tag, dims);
}
bool matches_tag_strict(const memory_desc_t &md, const std::string &tag) {
if (md.format_kind == format_kind::any) return false;
return matches_tag_strict(make_layout(md), tag);
}
memory_desc_t &get_src_md(convolution_pd_t *pd) {
return *const_cast<memory_desc_t *>(pd->invariant_src_md());
}
memory_desc_t &get_wei_md(convolution_pd_t *pd) {
return *const_cast<memory_desc_t *>(pd->invariant_wei_md());
}
memory_desc_t &get_dst_md(convolution_pd_t *pd) {
return *const_cast<memory_desc_t *>(pd->invariant_dst_md());
}
memory_desc_t &get_bia_md(convolution_pd_t *pd) {
return *const_cast<memory_desc_t *>(pd->invariant_bia_md());
}
layout_t init_layout(memory_desc_t &user_md, const std::string &optimal_tag) {
auto optimal = make_layout(user_md, optimal_tag);
if (user_md.format_kind != format_kind::any) {
auto user = make_layout(user_md);
// If layouts are physically different return the layout passed by
// the user and return unimplemented later.
if (user != optimal) return user;
} else {
user_md = optimal.to_dnnl(user_md.dims);
}
return optimal;
}
std::string prepend_groups_to_tag(const std::string &tag) {
auto ret = tag;
for (auto &c : ret) {
bool is_lower_dim = ('a' <= c && c < 'a' + DNNL_MAX_NDIMS);
bool is_upper_dim = ('A' <= c && c < 'A' + DNNL_MAX_NDIMS);
if (!is_lower_dim && !is_upper_dim) continue;
c += 1;
}
return "a" + ret;
}
int get_default_mad_block(const type_t &type) {
switch (type.size()) {
case 1: return 32;
case 2:
case 4: return 16;
case 8: return 8;
default: gpu_error_not_expected() << type;
}
return 1;
}
bool is_small(const type_t &type, dim_t elems) {
int block = get_default_mad_block(type);
return elems <= block / 2;
}
bool is_small_ic(const conv_problem_t &prb) {
return is_small(prb.src_data_type, prb.ic);
}
bool is_small_oc(const conv_problem_t &prb) {
return is_small(prb.src_data_type, prb.oc);
}
status_t conv_problem_t::init(
impl::engine_t *engine, const convolution_pd_t *conv_pd) {
using namespace compute;
VDISPATCH_CHECK(conv_pd, engine, !conv_pd->has_zero_dim_memory(),
VERBOSE_EMPTY_TENSOR, "");
this->conv_pd = conv_pd;
attr = conv_pd->attr();
is_fwd = conv_pd->is_fwd();
is_bwd_d = conv_pd->is_bwd_d();
is_bwd_w = conv_pd->is_bwd_w();
with_bias = conv_pd->with_bias();
with_groups = conv_pd->with_groups();
with_sum = with_sum_post_op();
memory_desc_wrapper mdw_src(conv_pd->invariant_src_md());
memory_desc_wrapper mdw_wei(conv_pd->invariant_wei_md());
memory_desc_wrapper mdw_dst(conv_pd->invariant_dst_md());
strided = (mdw_src.is_plain() && !mdw_src.is_dense())
|| (mdw_wei.is_plain() && !mdw_wei.is_dense())
|| (mdw_dst.is_plain() && !mdw_dst.is_dense());
src_data_type = conv_pd->invariant_src_md()->data_type;
wei_data_type = conv_pd->invariant_wei_md()->data_type;
bia_data_type = conv_pd->invariant_bia_md()->data_type;
dst_data_type = conv_pd->invariant_dst_md()->data_type;
fpmath_mode = attr->fpmath_.mode_;
ndims = conv_pd->ndims();
mb = conv_pd->MB();
g = conv_pd->G();
ic = ir_utils::safe_divide(conv_pd->IC(), g);
oc = ir_utils::safe_divide(conv_pd->OC(), g);
// Input spatial.
id = conv_pd->ID();
ih = conv_pd->IH();
iw = conv_pd->IW();
// Output spatial.
od = conv_pd->OD();
oh = conv_pd->OH();
ow = conv_pd->OW();
// Kernel sizes.
kd = conv_pd->KD();
kh = conv_pd->KH();
kw = conv_pd->KW();
// Strides.
sd = conv_pd->KSD();
sh = conv_pd->KSH();
sw = conv_pd->KSW();
// Padding.
pd = conv_pd->padFront();
ph = conv_pd->padT();
pw = conv_pd->padL();
// Dilation.
dd = conv_pd->KDD();
dh = conv_pd->KDH();
dw = conv_pd->KDW();
normalize_shape();
is_dw = with_groups && (g > 1) && (oc == 1) && (ic == 1);
ksp = kd * kh * kw;
isp = id * ih * iw;
osp = od * oh * ow;
hw_t hw(engine);
init_transpose(hw);
CHECK(init_abc_data_types(hw));
CHECK(init_acc_data_type());
return status::success;
}
std::string conv_problem_t::desc_str(bool print_mb) const {
std::ostringstream oss;
if (print_mb) oss << "mb" << mb;
if (g > 1) oss << "g" << g;
oss << "ic" << ic;
std::vector<dim_t> xd = {id, od, kd, sd, dd, pd};
std::vector<dim_t> xh = {ih, oh, kh, sh, dh, ph};
std::vector<dim_t> xw = {iw, ow, kw, sw, dw, pw};
std::vector<int> xdef = {1, 1, 1, 1, 0, 0};
bool has_d = !ir_utils::is_equal(xd, xdef);
bool has_h = !ir_utils::is_equal(xh, xdef);
bool is_square = !has_d && ir_utils::is_equal(xh, xw);
bool is_cubic = ir_utils::is_equal(xd, xh) && ir_utils::is_equal(xd, xw);
bool print_d = has_d;
bool print_h = has_h && !is_cubic;
bool print_w = !is_cubic && !is_square;
if (print_d) oss << "id" << id;
if (print_h) oss << "ih" << ih;
if (print_w) oss << "iw" << iw;
oss << "oc" << oc;
if (print_d) oss << "od" << od;
if (print_h) oss << "oh" << oh;
if (print_w) oss << "ow" << ow;
if (print_d) oss << "kd" << kd;
if (print_h) oss << "kh" << kh;
if (print_w) oss << "kw" << kw;
if (print_d && sd != 1) oss << "sd" << sd;
if (print_h && sh != 1) oss << "sh" << sh;
if (print_w && sw != 1) oss << "sw" << sw;
if (print_d && dd != 0) oss << "dd" << dd;
if (print_h && dh != 0) oss << "dh" << dh;
if (print_w && dw != 0) oss << "dw" << dw;
if (print_d) oss << "pd" << pd;
if (print_h) oss << "ph" << ph;
if (print_w) oss << "pw" << pw;
return oss.str();
}
int prim_config_t::sort_key(const param_t *param) const {
static const char *ordered_params[] = {
"exec-cfg",
"fma",
"l",
"T",
"i",
"P",
"p",
"s",
"src",
"wei",
"dst",
"bia",
nullptr,
};
for (const char **p = ordered_params; *p; p++) {
if (param->short_name() == *p) return into<int>(p - ordered_params);
}
return (int)(sizeof(ordered_params) / sizeof(ordered_params[0]));
}
const bool allow_global_reduction_param_t::default_value = true;
const bwd_d_optimize_kind_t bwd_d_optimize_kind_param_t::default_value
= bwd_d_optimize_kind_t::none;
const bool pad_slm_param_t::default_value = true;
std::string build_tag(const std::vector<int> &inner_blocks,
const std::vector<int> &outer_blocks, const std::vector<char> &letters,
const std::vector<int> &idxs) {
dim_idx_t n = into<dim_idx_t>(letters.size());
gpu_assert(inner_blocks.size() == n);
gpu_assert(outer_blocks.size() == n);
gpu_assert(idxs.size() == n);
std::string tag;
std::vector<bool> seen(n);
// Iterate through outer blocks.
for (int i = n - 1; i >= 0; i--) {
int idx = idxs[i];
int blk = outer_blocks[idx];
if (blk == 1) continue;
seen[idx] = true;
tag += std::to_string(blk) + letters[idx];
}
// Iterate through inner blocks.
for (int i = n - 1; i >= 0; i--) {
int idx = idxs[i];
int blk = inner_blocks[idx];
if (blk == 1) continue;
seen[idx] = true;
tag += std::to_string(blk) + letters[idx];
}
if (tag.empty()) {
// Assume this is an activations tag, use NHWC by default.
tag = "axb";
} else {
tag = 'x' + tag;
for (int i = (int)n - 1; i >= 0; i--) {
char c = letters[i];
if (c == ' ') continue;
if (seen[i]) c = static_cast<char>(std::toupper(c));
// NOLINTNEXTLINE(performance-inefficient-string-concatenation)
tag = c + tag;
}
}
return tag;
}
int pick_block_impl(bool prefer_rnd_up, dim_t dim, int b0, int b1, int b2) {
int blocks[3] = {b0, b1, b2};
int prev_blk = 1;
for (int i = 0; i < 3; i++) {
if (blocks[i] == 0) continue;
if (prefer_rnd_up) {
if (dim <= blocks[i] / 2) return prev_blk;
} else {
if (dim < blocks[i]) return prev_blk;
}
prev_blk = blocks[i];
}
return prev_blk;
}
int pick_block_rnd_up(dim_t dim, int b0, int b1 = 0, int b2 = 0) {
return pick_block_impl(true, dim, b0, b1, b2);
}
int pick_block(dim_t dim, int b0, int b1 = 0, int b2 = 0) {
return pick_block_impl(false, dim, b0, b1, b2);
}
int get_default_block(fma_kind_t fma, const type_t &type, dim_t elems) {
if (is_dp_fma(fma)) {
if (is_small(type, elems)) {
int packed_dword_elems = 4 / type.size();
return std::max(
utils::rnd_up_pow2(into<int>(elems)), packed_dword_elems);
}
return 32 / type.size();
}
if (is_small(type, elems)) return 1;
return get_default_mad_block(type);
}
fma_kind_t get_default_fma(const hw_t &hw, const type_t &type) {
switch (type.size()) {
case 1:
if (hw >= ngen::HW::XeHP) return fma_kind_t::dpas;
return hw >= ngen::HW::XeLP ? fma_kind_t::dp4a : fma_kind_t::mad;
case 2:
return hw >= ngen::HW::XeHP ? fma_kind_t::dpas : fma_kind_t::mad;
default: return fma_kind_t::mad;
}
return fma_kind_t::undef;
}
struct nc_block_t {
nc_block_t(int n_block, int c_block)
: n_block_(n_block), c_block_(c_block) {}
int n_block() const { return n_block_; }
int c_block() const { return c_block_; }
std::string tag() const {
std::vector<int> idxs = {1, 0};
return build_tag({n_block_, c_block_}, {1, 1}, {'a', 'b'}, idxs);
}
// Ideally, this should only depend on data type, direction, mb, c, and g to
// enable the same src/dst formats and avoid reorders between convolutions
static nc_block_t get_default_blocking(const hw_t &hw, fma_kind_t fma,
type_t type, bool is_dw, dim_t n, dim_t c, dim_t g,
bool is_output = false) {
// Select dst layout to align with fma kind of following conv
// for non-depthwise cases.
fma_kind_t tmp_fma
= (is_output && !is_dw) ? get_default_fma(hw, type) : fma;
int c_block = (is_dw ? get_default_block(tmp_fma, type, g)
: get_default_block(tmp_fma, type, c));
if (g > 1 && !is_dw) {
if (c % c_block != 0) c_block = 1;
// Try to use the same layout between group/non-group convolution
// to avoid reorder.
auto default_gc_blk
= get_default_block(get_default_fma(hw, type), type, g * c);
if (c_block != default_gc_blk) {
if (default_gc_blk % c == 0 && g % (default_gc_blk / c) == 0) {
c_block = default_gc_blk;
}
}
}
auto default_n_blk = (type.size() <= 2) ? 32 : 16;
int n_block = (c_block == 1) ? 1 : pick_block(n, 16, default_n_blk);
return nc_block_t(n_block, c_block);
}
private:
int n_block_;
int c_block_;
};
struct goi_block_t {
goi_block_t(fma_kind_t fma_kind, bool is_dw, bool is_bwd_d, int g_block,
int o_block, int i_block, int o_block_outer, int i_block_outer)
: fma_kind_(fma_kind)
, is_dw_(is_dw)
, is_bwd_d_(is_bwd_d)
, g_block_(g_block)
, o_block_(o_block)
, i_block_(i_block)
, o_block_outer_(o_block_outer)
, i_block_outer_(i_block_outer) {}
std::string tag() const {
std::vector<char> wei_letters(3, ' ');
char wei_letter = 'a';
for (int i = (is_dw_ ? 0 : 1); i < 3; i++) {
wei_letters[i] = wei_letter++;
}
std::vector<int> wei_idxs = {0, 1, 2}; // g, ic, oc
// dpas requires ic to go before oc in innermost blocks for weights.
if (fma_kind_ != fma_kind_t::mad) std::swap(wei_idxs[1], wei_idxs[2]);
if (is_bwd_d_) std::swap(wei_idxs[1], wei_idxs[2]);
return build_tag({g_block_, o_block_, i_block_},
{1, o_block_outer_, i_block_outer_}, wei_letters, wei_idxs);
}
static goi_block_t get_default_blocking(type_t type, int vec_size,
fma_kind_t fma_kind, bool is_fwd, bool is_bwd_d, dim_t g, dim_t o,
dim_t i, bool ab_transpose) {
dim_t x = o;
dim_t y = i;
int g_block = 1;
int o_block = 1;
int i_block = 1;
int o_block_outer = 1;
int i_block_outer = 1;
int *x_block = &o_block;
int *y_block = &i_block;
int *x_block_outer = &o_block_outer;
int *y_block_outer = &i_block_outer;
// Backward by data requires flipped ic/oc in weights.
if (is_bwd_d) {
std::swap(x, y);
std::swap(x_block, y_block);
std::swap(x_block_outer, y_block_outer);
}
get_default_blocking(type, vec_size, fma_kind, is_fwd, is_bwd_d, g, x,
y, g_block, *x_block, *y_block, *y_block_outer, ab_transpose);
return goi_block_t(fma_kind, is_dw(g, o, i), is_bwd_d, g_block, o_block,
i_block, o_block_outer, i_block_outer);
}
static void get_default_blocking(type_t type, int vec_size,
fma_kind_t fma_kind, bool is_fwd, bool is_bwd_d, dim_t g, dim_t x,
dim_t y, int &g_block, int &x_block, int &y_block,
int &y_block_outer, bool ab_transpose = false) {
if (is_dw(g, x, y)) {
g_block = vec_size;
} else if (fma_kind == fma_kind_t::mad) {
x_block = (ab_transpose && (is_fwd || is_bwd_d)) ? 1 : vec_size;
y_block = (x_block == 1 ? 1 : get_default_block(fma_kind, type, y));
} else {
int packed_dword_elems = 4 / type.size();
x_block = ab_transpose ? into<int>(utils::rnd_up_pow2(x))
: vec_size;
y_block = packed_dword_elems;
// Fixing y outer block helps to avoid extra GRF reorders however
// in small reduction cases it may result in excessive zero
// padding. In such cases fused reduction can be used. E.g. in
// non-1x1 small-ic fwd convolution kw and ic can be fused.
if (y * type.size() >= 32) {
if (ab_transpose) {
y_block *= 8;
} else {
y_block_outer = 8;
}
}
}
}
private:
static bool is_dw(dim_t g, dim_t o, dim_t i) {
return (g > 1 && o == 1 && i == 1);
}
fma_kind_t fma_kind_;
bool is_dw_;
bool is_bwd_d_;
int g_block_;
int o_block_;
int i_block_;
int o_block_outer_;
int i_block_outer_;
};
// Matches the user-provided descriptor against the list of supported plain tags.
std::string get_plain_user_tag(
const conv_problem_t &prb, const memory_desc_t &md, bool is_wei) {
memory_desc_wrapper mdw(md);
if (mdw.is_plain() && !mdw.is_dense()) return "user";
if (is_wei) {
std::vector<const char *> plain_non_group_wei_tags
= {"abx", "axb", "xba"};
std::vector<const char *> plain_group_wei_tags
= {"abcx", "abxc", "axcb"};
auto &plain_wei_tags = (prb.with_groups ? plain_group_wei_tags
: plain_non_group_wei_tags);
gpu_assert(
plain_non_group_wei_tags.size() == plain_group_wei_tags.size());
for (size_t i = 0; i < plain_wei_tags.size(); i++) {
if (matches_tag(md, plain_wei_tags[i])) {
return plain_non_group_wei_tags[i];
}
}
} else {
for (auto *t : {"axb", "abx"}) {
if (matches_tag(md, t)) return t;
}
}
return {};
}
std::string maybe_fixup_1st_conv_wei_tag(
const conv_config_t &cfg, const std::string &tag) {
auto &prb = cfg.prb();
if (!cfg.is_dp_fma()) return tag;
if (!is_small_ic(prb) || prb.is_dw) return tag;
if (prb.ab_swap_transpose) return tag;
if (!prb.is_fwd) return tag;
// Use OhwIXoYi weights for small-channel forward convolution to ensure
// c-after-w order of reduction blocks to match the source layout.
const char *patterns[] = {"ABx", "AxB", "Abx", "Axb", nullptr};
for (auto *p = patterns; *p; p += 2) {
auto pos = tag.find(*p);
if (pos == std::string::npos) continue;
auto ret = tag;
return ret.replace(pos, std::strlen(*p), *(p + 1));
}
gpu_error_not_expected() << tag;
return tag;
}
void maybe_set_plain_weights(const conv_config_t &cfg, bool src_dst_axb,
const std::string &user_wei_req, std::string &wei_tag,
std::string &user_wei_tag) {
auto &prb = cfg.prb();
// For XeHPC+ with nhwc activations always use hwio weights for
// consistency for user-facing layouts.
if (cfg.hw() >= ngen::HW::XeHPC && src_dst_axb) {
bool channels_ok = (prb.ic % 16 == 0 && prb.oc % 16 == 0);
if (prb.g == 1 && channels_ok) {
// Use plain compute layout for weights, normally they are
// supported via block 2D load.
wei_tag = (prb.is_bwd_d ? "xab" : "xba");
if (user_wei_req.empty()) user_wei_tag = "xba";
}
}
if (user_wei_tag.empty()) user_wei_tag = user_wei_req;
}
bool is_plain_tag_optimal_for_output(
const std::string &tag, const std::string &user_tag) {
// NHWC is OK with output as C is used for blocking and C is dense.
if (user_tag == "axb") return true;
// NCHW is OK only when blocked by W (not N).
if (user_tag == "abx") {
bool is_n_blocked = (tag.find("A") != std::string::npos);
return !is_n_blocked;
}
return false;
}
void init_data_tags(const conv_config_t &cfg, const memory_desc_t &src_md,
const memory_desc_t &wei_md, const memory_desc_t &dst_md,
std::string &src_tag, std::string &wei_tag, std::string &dst_tag,
std::string &user_src_tag, std::string &user_wei_tag,
std::string &user_dst_tag) {
const auto &prb = cfg.prb();
auto src_compute_type = prb.is_bwd_d ? prb.c_data_type : prb.a_data_type;
auto dst_compute_type = prb.is_fwd
? prb.c_data_type
: (prb.is_bwd_d ? prb.a_data_type : prb.b_data_type);
auto wei_compute_type = prb.is_bwd_w ? prb.c_data_type : prb.b_data_type;
auto src_blk = nc_block_t::get_default_blocking(cfg.hw(), cfg.fma_kind(),
src_compute_type, prb.is_dw, prb.mb, prb.ic, prb.g,
/*is_output=*/prb.is_bwd_d);
auto dst_blk = nc_block_t::get_default_blocking(cfg.hw(), cfg.fma_kind(),
dst_compute_type, prb.is_dw, prb.mb, prb.oc, prb.g,
/*is_output=*/prb.is_fwd);
auto wei_blk = goi_block_t::get_default_blocking(wei_compute_type,
cfg.vec_size(), cfg.fma_kind(), prb.is_fwd, prb.is_bwd_d, prb.g,
prb.oc, prb.ic, prb.ab_swap_transpose);
src_tag = src_blk.tag();
wei_tag = wei_blk.tag();
dst_tag = dst_blk.tag();
wei_tag = maybe_fixup_1st_conv_wei_tag(cfg, wei_tag);
// Handle nhwc case.
auto user_src_req = get_plain_user_tag(prb, src_md, /*is_wei=*/false);
auto user_wei_req = get_plain_user_tag(prb, wei_md, /*is_wei=*/true);
auto user_dst_req = get_plain_user_tag(prb, dst_md, /*is_wei=*/false);
bool src_axb = (user_src_req == "axb");
bool dst_axb = (user_dst_req == "axb");
bool src_abx = (user_src_req == "abx");
bool dst_abx = (user_dst_req == "abx");
bool src_matches = matches_tag(src_md, src_tag);
bool dst_matches = matches_tag(dst_md, dst_tag);
bool src_output = prb.is_bwd_d;
bool dst_output = prb.is_fwd;
bool is_small_ic_g1 = is_small_ic(prb) && (prb.g == 1);
bool is_small_oc_g1 = is_small_oc(prb) && (prb.g == 1);
// Use nhwc for compute for non-small channels to avoid reorders.
if (!src_matches && !is_small_ic_g1 && src_axb) src_tag = "axb";
if (!dst_matches && !is_small_oc_g1 && dst_axb) dst_tag = "axb";
// Use plain tags for user-facing activations for small-channel tensors.
if (!matches_tag(src_md, src_tag) && is_small_ic_g1)
user_src_tag = (user_src_req.empty() ? "axb" : user_src_req);
if (!matches_tag(dst_md, dst_tag) && is_small_oc_g1)
user_dst_tag = (user_dst_req.empty() ? "axb" : user_dst_req);
// Avoid reorder for small shapes
if (!user_src_tag.empty() && !user_dst_tag.empty() && prb.g == 1
&& prb.ic < 4 && prb.oc < 4 && prb.mb < 4 && prb.ksp == 1) {
src_tag = user_src_tag;
dst_tag = user_dst_tag;
}
maybe_set_plain_weights(
cfg, src_axb && dst_axb, user_wei_req, wei_tag, user_wei_tag);
if (user_src_tag.empty()) user_src_tag = src_tag;
if (user_wei_tag.empty()) user_wei_tag = wei_tag;
if (user_dst_tag.empty()) user_dst_tag = dst_tag;
if (src_abx && !src_matches) user_src_tag = "abx";
if (dst_abx && !dst_matches) user_dst_tag = "abx";
// Use plain tag for output to avoid extra reorders when beneficial.
if (src_output && is_plain_tag_optimal_for_output(src_tag, user_src_tag))
src_tag = user_src_tag;
if (dst_output && is_plain_tag_optimal_for_output(dst_tag, user_dst_tag))
dst_tag = user_dst_tag;
if (user_src_req == "user") src_tag = user_src_tag = "user";
if (user_wei_req == "user") wei_tag = user_wei_tag = "user";
if (user_dst_req == "user") dst_tag = user_dst_tag = "user";
}
void prepare_zp_precompute_conv(const conv_problem_t &prb, dim_t *idhw,
dim_t *odhw, dim_t *pdhw, dim_t *ddhw) {
const bool is_bwd_d = (prb.prop_kind() == prop_kind::backward_data);
using memory_dims = std::vector<dim_t>;
memory_dims I {prb.id, prb.ih, prb.iw};
memory_dims O {prb.od, prb.oh, prb.ow};
memory_dims K {prb.kd, prb.kh, prb.kw};
memory_dims S {prb.sd, prb.sh, prb.sw};
memory_dims D {prb.dd, prb.dh, prb.dw};
memory_dims P {prb.pd, prb.ph, prb.pw};
const int off = 5 - prb.ndims;
const auto *w = prb.conv_pd->weights_md();
// restore the original layout of the prb values
const auto *s
= (is_bwd_d) ? prb.conv_pd->diff_dst_md() : prb.conv_pd->src_md();
const auto *d
= (is_bwd_d) ? prb.conv_pd->diff_src_md() : prb.conv_pd->dst_md();
auto has_dim = [&](int i) {
return (s->dims[2 + i] > 1) || (d->dims[2 + i] > 1)
|| (w->dims[2 + i + prb.with_groups] > 1);
};
auto move_back = [&](int i, int off) {
if (off == 0) return;
I[i - off] = O[i - off] = K[i - off] = S[i - off] = 1;
D[i - off] = P[i - off] = 0;
std::swap(I[i - off], I[i]);
std::swap(O[i - off], O[i]);
std::swap(K[i - off], K[i]);
std::swap(S[i - off], S[i]);
std::swap(D[i - off], D[i]);
std::swap(P[i - off], P[i]);
};
bool has_d = (off <= 0) && has_dim(0 - off);
bool has_h = (off <= 1) && has_dim(1 - off);
bool has_w = (off <= 2) && has_dim(2 - off);
if (!has_d && !has_h && !has_w) has_w = true;
move_back(1, has_d * (!has_h == has_w));
move_back(2, !has_w * (!has_h + 1));
for (int i = off; i < int(K.size()); i++) {
const auto KD = (K[i] - 1) * (D[i] + 1) + 1;
gpu_assert(w->dims[2 + i + prb.with_groups - off] == K[i]);
O[i] = ir_utils::max_unique_pad_states(
O[i], I[i], KD, P[i], S[i], true);
I[i] = std::min(KD, I[i]);
}
for (int i = 0; i < 3; i++) {
idhw[i] = (i < off) ? 0 : I[i];
odhw[i] = (i < off) ? 0 : O[i];
pdhw[i] = (i < off) ? 0 : P[i];
ddhw[i] = (i < off) ? 0 : D[i];
}
}
status_t init_tensor_layouts(
conv_config_t &cfg, convolution_pd_t *pd, impl::engine_t *engine) {
const auto &prb = cfg.prb();
// Compute layout tags and user layout tags. If a compute layout is
// different from a user layout then an extra pre/post reorder will be
// executed before/after convolution.
std::string src_tag, user_src_tag;
std::string wei_tag, user_wei_tag;
std::string dst_tag, user_dst_tag;
std::string bia_tag = "a";
std::string user_bia_tag = "a";
auto &src_md = get_src_md(pd);
auto &wei_md = get_wei_md(pd);
auto &dst_md = get_dst_md(pd);
auto &bia_md = get_bia_md(pd);
// If src/dst is nhwc then set the other one with any to nhwc too (except
// 1st convolution).
bool is_small_ic_non_dw = is_small_ic(prb) && !prb.is_dw;
bool propagate_nhwc = (matches_tag(src_md, "axb") && !is_small_ic_non_dw)
|| matches_tag(dst_md, "axb");
if (propagate_nhwc) {
set_default_format(src_md, "axb");
set_default_format(dst_md, "axb");
}
init_data_tags(cfg, src_md, wei_md, dst_md, src_tag, wei_tag, dst_tag,
user_src_tag, user_wei_tag, user_dst_tag);
bool wei_prepend_groups = (prb.with_groups && !prb.is_dw);
if (wei_prepend_groups) {
wei_tag = prepend_groups_to_tag(wei_tag);
user_wei_tag = prepend_groups_to_tag(user_wei_tag);
}
auto &src = cfg.src_layout();
auto &wei = cfg.wei_layout();
auto &dst = cfg.dst_layout();
auto &bia = cfg.bia_layout();
if (src.is_overridden()) {
src_tag = src.compute_unnormalized_tag();
user_src_tag = src.user_unnormalized_tag();
}
if (wei.is_overridden()) {
wei_tag = wei.compute_unnormalized_tag();
user_wei_tag = wei.user_unnormalized_tag();
}
if (dst.is_overridden()) {
dst_tag = dst.compute_unnormalized_tag();
user_dst_tag = dst.user_unnormalized_tag();
}
// Select user layouts.
auto user_src_layout = init_layout(src_md, user_src_tag);
auto user_wei_layout = init_layout(wei_md, user_wei_tag);
auto user_dst_layout = init_layout(dst_md, user_dst_tag);
layout_t user_bia_layout;
if (prb.with_bias) user_bia_layout = init_layout(bia_md, user_bia_tag);
VDISPATCH_CHECK(pd, engine,
user_src_layout.is_strictly_equal(
make_layout(src_md, user_src_tag)),
VERBOSE_UNSUPPORTED_TAG);
VDISPATCH_CHECK(pd, engine,
user_dst_layout.is_strictly_equal(
make_layout(dst_md, user_dst_tag)),
VERBOSE_UNSUPPORTED_TAG);
VDISPATCH_CHECK(pd, engine,
user_wei_layout.is_strictly_equal(
make_layout(wei_md, user_wei_tag)),
VERBOSE_UNSUPPORTED_TAG);
auto src_layout = (src_tag != user_src_tag) ? make_layout(src_md, src_tag)
: user_src_layout;
auto wei_layout = (wei_tag != user_wei_tag) ? make_layout(wei_md, wei_tag)
: user_wei_layout;
auto dst_layout = (dst_tag != user_dst_tag) ? make_layout(dst_md, dst_tag)
: user_dst_layout;
auto bia_layout = user_bia_layout;
if (prb.is_bwd_w) {
if (utils::one_of(prb.wei_data_type, data_type::bf16, data_type::f16,
data_type::f8_e5m2, data_type::f8_e4m3))
wei_layout = wei_layout.retype(type_t::f32());
if (utils::one_of(prb.bia_data_type, data_type::bf16, data_type::f16,
data_type::f8_e5m2, data_type::f8_e4m3))
bia_layout = bia_layout.retype(type_t::f32());
}
src.set_compute_unnormalized(src_layout, src_tag);
src.set_user_unnormalized(user_src_layout, user_src_tag);
wei.set_compute_unnormalized(wei_layout, wei_tag);
wei.set_user_unnormalized(user_wei_layout, user_wei_tag);
dst.set_compute_unnormalized(dst_layout, dst_tag);
dst.set_user_unnormalized(user_dst_layout, user_dst_tag);
bia.set_compute_unnormalized(bia_layout, bia_tag);
bia.set_user_unnormalized(user_bia_layout, user_bia_tag);
// Normalize layouts: add group dimension for all layouts and reduce/fuse
// spatial dimensions when applicable.
normalize_conv_layouts(src_layout, wei_layout, dst_layout, bia_layout,
prb.with_groups, prb.g, prb.ic, prb.oc, prb.is_dw, prb.dhw_map,
/*add_groups=*/true);
normalize_conv_layouts(user_src_layout, user_wei_layout, user_dst_layout,
user_bia_layout, prb.with_groups, prb.g, prb.ic, prb.oc, prb.is_dw,
prb.dhw_map,
/*add_groups=*/true);
src.set_compute(src_layout);
src.set_user(user_src_layout);
wei.set_compute(wei_layout);
wei.set_user(user_wei_layout);
dst.set_compute(dst_layout);
dst.set_user(user_dst_layout);
bia.set_compute(bia_layout);
bia.set_user(user_bia_layout);
if (cfg.zp_cfg().needs_src_reorder_precalc) {
auto get_channels = [](const layout_t &layout) {
const dim_t min_esize = 16;
return std::max(utils::rnd_up_pow2(layout.dim(1) * layout.dim(2)),
min_esize);
};
using namespace memory_extra_flags;
prepare_zp_precompute_conv(prb, wei_md.extra.idhw, wei_md.extra.odhw,
wei_md.extra.pdhw, wei_md.extra.ddhw);
wei_md.extra.dst_size = sizeof(float);
for (const auto &o : wei_md.extra.odhw)
wei_md.extra.dst_size *= std::max(o, dim_t(1));
if (prb.prop_kind() == prop_kind::backward_data) {
wei_md.extra.flags |= compensation_gpu_conv_asymmetric_src_bwd;
wei_md.extra.dst_size *= get_channels(src_layout);
} else {
wei_md.extra.dst_size *= get_channels(dst_layout);
}
wei_md.extra.flags |= compensation_gpu_conv_asymmetric_src;
// since tmasks are used on precalc ZPs only if absolutely necessary
// (due to significant computational costs in most cases) some block
// reads can exceed the total buffer size, resulting in page faults;
// padding at the end is the easiest way to avoid that, as 1-2 KB of
// additional VRAM per precalc buffer is virtually free
// TODO: vectorize send params (in jit:ir:v2 maybe?) and add tmasks!
const dim_t max_read_blk_bytes = 2048;
wei_md.extra.dst_size += max_read_blk_bytes * 2;
}
return status::success;
}
bool hw_ok(const hw_t &hw) {
if (hw < ngen::HW::Gen9) return false;
return true;
}
bool data_types_ok(
const conv_problem_t &prb, const hw_t &hw, impl::engine_t *engine) {
auto src = prb.src_data_type;
auto wei = prb.wei_data_type;
auto dst = prb.dst_data_type;
auto bia = prb.bia_data_type;
bool is_fp8 = utils::one_of(data_type::f8_e5m2, src, wei, dst, bia)
|| utils::one_of(data_type::f8_e4m3, src, wei, dst, bia);
if (!prb.is_f64_accumulator()
&& utils::one_of(data_type::f64, src, wei, dst, bia))
return false;
auto *compute_engine
= utils::downcast<const compute::compute_engine_t *>(engine);
auto *device_info = compute_engine->device_info();
if (prb.is_f64_accumulator() && !device_info->has_native(data_type::f64))
return false;
if (is_fp8
&& !(utils::one_of(hw, ngen::HW::XeHPC) && hw.systolic_support()))
return false;
if (prb.is_fwd) return true;
if (prb.is_bwd_d) return true;
if (prb.is_bwd_w) {
bool ok = true;
data_type_t default_acc_type
= src == data_type::f64 ? data_type::f64 : data_type::f32;
ok &= utils::one_of(src, data_type::f8_e5m2, data_type::f8_e4m3,
data_type::bf16, data_type::f16, data_type::f32,
data_type::f64);
ok &= (dst == src);
ok &= (utils::one_of(wei, src, default_acc_type)
|| (utils::one_of(src, data_type::f8_e4m3, data_type::f8_e5m2)
&& utils::one_of(wei, data_type::f8_e4m3,
data_type::f8_e5m2, data_type::f32,
data_type::bf16, data_type::f16)));
if (prb.with_bias) { ok &= utils::one_of(bia, src, data_type::f32); }
return ok;
}
return false;
}
bool zero_points_ok(const conv_problem_t &prb) {
auto *pd = prb.conv_pd;
auto *attr = pd->attr();
const auto &zp = attr->zero_points_;
using namespace data_type;
const auto input_type = (prb.is_fwd) ? pd->invariant_src_md()->data_type
: pd->invariant_dst_md()->data_type;
bool ok = IMPLICATION(
!utils::one_of(input_type, s8, u8), zp.has_default_values());
if (!ok) return false;
if (!zp.has_default_values(DNNL_ARG_SRC)) {
int mask_src = zp.get_mask(DNNL_ARG_SRC);
ok = utils::one_of(mask_src, 0, (1 << 1));
if (!ok) return false;
}
if (!zp.has_default_values(DNNL_ARG_WEIGHTS)) {
int mask_wei = zp.get_mask(DNNL_ARG_WEIGHTS);
ok = mask_wei == 0;
if (!ok) return false;
if (zp.get_data_type(DNNL_ARG_WEIGHTS) != s8) return false;
if (prb.with_groups) return false;
// zp_wei implies scalar zp_src
if (zp.get_mask(DNNL_ARG_SRC) > 0) return false;
}
if (!zp.has_default_values(DNNL_ARG_DST)) {
int mask_dst = zp.get_mask(DNNL_ARG_DST);
ok = utils::one_of(mask_dst, 0, (1 << 1));
if (!ok) return false;
}
return true;
}
bool post_ops_ok(const conv_problem_t &prb, const hw_t &hw) {
auto *pd = prb.conv_pd;
auto *attr = prb.attr;
// No post-ops are supported for f64
if (prb.is_f64_accumulator() && !attr->has_default_values()) return false;
using sm = primitive_attr_t::skip_mask_t;
auto attr_skip_mask = sm::fpmath_mode | sm::accumulation_mode;
if (prb.is_fwd || prb.is_bwd_d) {
attr_skip_mask |= sm::post_ops | sm::sum_dt | sm::zero_points_data_type
| sm::rounding_mode | sm::scales_data_type;
if (!attr->has_default_values(attr_skip_mask)) return false;
} else {
if (!attr->has_default_values(attr_skip_mask)) return false;
}
using namespace data_type;
const auto input_type = (prb.is_fwd) ? pd->invariant_src_md()->data_type
: pd->invariant_dst_md()->data_type;
if (!attr->post_ops_.check_sum_consistency(
prb.c_data_type, utils::one_of(input_type, s8, u8), true))
return false;
if (!attr->scales_.has_default_values())
if (!prb.is_s32_accumulator() && !prb.is_fp8_conv()) return false;
auto scale_args = get_scale_args();
std::vector<int> scales(scale_args.size());
for (int i = 0; i < (int)scale_args.size(); i++)
scales[i] = scale_args[i].second;
if (!attr->scales_.has_default_values(scales)) return false;
for (int arg : scales) {
if (attr->scales_.has_default_values(arg)) continue;