-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathlnorm.cpp
667 lines (559 loc) · 24.7 KB
/
lnorm.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
/*******************************************************************************
* Copyright 2019-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 <cmath>
#include <float.h>
#include <random>
#include <stdio.h>
#include <stdlib.h>
#include <sstream>
#include "oneapi/dnnl/dnnl.h"
#include "utils/fill.hpp"
#include "utils/parallel.hpp"
#include "dnnl_common.hpp"
#include "dnnl_memory.hpp"
#include "bnorm/bnorm.hpp"
#include "lnorm/lnorm.hpp"
using namespace bnorm;
namespace lnorm {
int fill_mean(const prb_t *prb, const cfg_t &cfg, dnn_mem_t &mem_fp,
dnn_mem_t &mem_dt) {
// Refer to modes documentation for filling principles.
if (has_bench_mode_bit(mode_bit_t::bitwise)) {
// Mean must be computed unless it is passed by user directly.
if (!(prb->flags & GLOB_STATS) && !(prb->dir & FLAG_BWD)) return OK;
return fill_random_real(mem_dt, mem_fp, nullptr);
}
benchdnn_parallel_nd(prb->n, [&](int64_t n) {
const float val_coeff = is_integral_dt(prb->dt[0]) ? 1.f : 0.25f;
float val = 0.f;
// For zero channels the logic relies on memory filled with zeros.
if (prb->c > 0
&& (cfg.check_alg_ != ALG_0 || (prb->flags & GLOB_STATS))) {
int64_t mean_val_shift = n % 7;
// Bump mean for u8 to keep src values in non-negative range
if (prb->dt[0] == dnnl_u8 && mean_val_shift < 2) mean_val_shift = 2;
val = val_coeff * (1LL << mean_val_shift);
}
mem_fp.set_elem(n, val);
});
if (mem_dt && IMPLICATION(prb->dir & FLAG_FWD, prb->use_stats()))
SAFE(mem_dt.reorder(mem_fp), WARN);
return OK;
}
int fill_src(const prb_t *prb, const cfg_t &cfg, dnn_mem_t &mem_fp,
dnn_mem_t &mem_dt, const dnn_mem_t &ref_mean, res_t *res) {
// Refer to modes documentation for filling principles.
if (has_bench_mode_bit(mode_bit_t::bitwise)) {
return fill_random_real(mem_dt, mem_fp, res);
}
const float val_coeff = is_integral_dt(prb->dt[0]) ? 1.f : 0.25f;
benchdnn_parallel_nd(prb->n, [&](int64_t n) {
const float m = ref_mean.get_elem(n);
// Note: we use a different seed for each chunk to avoid
// repeating patterns. We could use discard(idx_start) too but
// it has a complexity in O(idx_start). We also add 1 to avoid
// seeding with 0.
std::minstd_rand b_seed(n + 1);
b_seed.discard(2);
std::bernoulli_distribution b_dist(0.5f);
bool bigger_val = false; // Out of spatial loop.
for (int64_t c = 0; c < prb->c; ++c) {
const int64_t off = n * prb->c + c;
float val = 0.f;
if (cfg.check_alg_ == ALG_2) {
const bool is_even = c % 2 == 0;
const float sign = is_even ? 1.f : -1.f;
// Update the value for even cases.
bigger_val = is_even ? b_dist(b_seed) : bigger_val;
const float val_shift = sign * val_coeff;
// Shift left and right from mean val, shift bigger with
// probability.
val = m + val_shift + 3.f * bigger_val * val_shift;
} else {
// l[0] must be even
const int64_t l = c + n * 239 * 2;
// Shortcut for zero values.
if (cfg.check_alg_ == ALG_0
&& !flip_coin(l / 2 * 257ULL, cfg.density_)) {
mem_fp.set_elem(off, 0);
continue;
}
const int64_t gen = (l / 2 * 1637) & cfg.flex_mask_;
// s_{i} + s_{i+1} = 2 * m
const float sign = l % 2 == 0 ? 1.f : -1.f;
const float f = sign * gen / (1 << cfg.flex_bits_);
val = cfg.check_alg_ == ALG_0 ? f : m * (1.f + f);
if (prb->flags & GLOB_STATS) { val = (l % 65) - 32; }
}
// Update last element with s[c] = m.
if ((c == cfg.L_ - 1) && cfg.L_ % 2) { val = m; }
mem_fp.set_elem(
off, round_to_nearest_representable(prb->dt[0], val));
}
});
if (mem_dt) SAFE(mem_dt.reorder(mem_fp), WARN);
return OK;
}
int fill_variance_fwd(const prb_t *prb, const cfg_t &cfg, dnn_mem_t &mem_fp,
dnn_mem_t &mem_dt, const dnn_mem_t &ref_src,
const dnn_mem_t &ref_mean) {
// Refer to modes documentation for filling principles.
if (has_bench_mode_bit(mode_bit_t::bitwise)) {
// Variance must be computed unless it is passed by user directly.
if (!(prb->flags & GLOB_STATS)) return OK;
// Variance must be always positive by definition.
fill_cfg_t fill_cfg(dnnl_f32, 0.f, 16.f, /* int = */ false,
attr_t::post_ops_t::kind_t::ADD, "variance");
return fill_random_real(mem_dt, mem_fp, nullptr, fill_cfg);
}
benchdnn_parallel_nd(prb->n, [&](int64_t n) {
float val = 0.f;
// For zero channels the logic relies on memory filled with zeros.
if (prb->flags & GLOB_STATS) {
val = ((n % 7) << 1);
} else if (prb->c > 0) {
const float m = ref_mean.get_elem(n);
for (int64_t c = 0; c < prb->c; ++c) {
const int64_t off = n * prb->c + c;
const float s = ref_src.get_elem(off);
val += (s - m) * (s - m);
}
val /= cfg.L_;
}
mem_fp.set_elem(n, val);
});
if (mem_dt && prb->use_stats()) SAFE(mem_dt.reorder(mem_fp), WARN);
return OK;
}
int fill_scale(const prb_t *prb, dnn_mem_t &mem_fp, dnn_mem_t &mem_dt) {
const bool use_sc = prb->use_sc();
if (!use_sc) return OK;
// Refer to modes documentation for filling principles.
if (has_bench_mode_bit(mode_bit_t::bitwise)) {
return fill_random_real(mem_dt, mem_fp, nullptr);
}
benchdnn_parallel_nd(prb->c, [&](int64_t c) {
float val = (1.f / 8) * (1 << (c % 7));
if (prb->flags & GLOB_STATS) val *= 8.f;
mem_fp.set_elem(c, val);
});
if (mem_dt) SAFE(mem_dt.reorder(mem_fp), WARN);
return OK;
}
int fill_shift(const prb_t *prb, dnn_mem_t &mem_fp, dnn_mem_t &mem_dt) {
const bool use_sh = prb->use_sh();
if (!use_sh) return OK;
// Refer to modes documentation for filling principles.
if (has_bench_mode_bit(mode_bit_t::bitwise)) {
return fill_random_real(mem_dt, mem_fp, nullptr);
}
benchdnn_parallel_nd(prb->c, [&](int64_t c) {
float val = ((c % 3) - 1) * (1.f / 512 * (1 << (c % 7)));
if (prb->flags & GLOB_STATS) val *= 512.f;
mem_fp.set_elem(c, val);
});
if (mem_dt) SAFE(mem_dt.reorder(mem_fp), WARN);
return OK;
}
int prepare_fwd(const prb_t *prb, dnn_mem_map_t &mem_map,
dnn_mem_map_t &ref_mem_map, res_t *res) {
cfg_t cfg(prb);
auto &mean = mem_map.at(DNNL_ARG_MEAN);
auto &ref_mean = ref_mem_map.at(DNNL_ARG_MEAN);
SAFE(fill_mean(prb, cfg, ref_mean, mean), WARN);
auto &src = mem_map.at(DNNL_ARG_SRC);
auto &ref_src = ref_mem_map.at(DNNL_ARG_SRC);
SAFE(fill_src(prb, cfg, ref_src, src, ref_mean, res), WARN);
// Need a copy of source data for inplace mode for bitwise testing.
if (has_bench_mode_bit(mode_bit_t::bitwise) && prb->inplace) {
auto &src_copy = mem_map.at(-DNNL_ARG_SRC);
SAFE(bool(src_copy) ? OK : FAIL, WARN);
SAFE(src_copy.reorder(src), WARN);
}
auto &var = mem_map.at(DNNL_ARG_VARIANCE);
auto &ref_var = ref_mem_map.at(DNNL_ARG_VARIANCE);
SAFE(fill_variance_fwd(prb, cfg, ref_var, var, ref_src, ref_mean), WARN);
auto &scale = mem_map.at(DNNL_ARG_SCALE);
auto &ref_scale = ref_mem_map.at(DNNL_ARG_SCALE);
SAFE(fill_scale(prb, ref_scale, scale), WARN);
auto &shift = mem_map.at(DNNL_ARG_SHIFT);
auto &ref_shift = ref_mem_map.at(DNNL_ARG_SHIFT);
SAFE(fill_shift(prb, ref_shift, shift), WARN);
return OK;
}
int fill_variance_bwd(const prb_t *prb, dnn_mem_t &mem_fp, dnn_mem_t &mem_dt) {
const auto nelems = mem_fp.nelems();
if (nelems == 0) return OK;
// Refer to modes documentation for filling principles.
if (has_bench_mode_bit(mode_bit_t::bitwise)) {
// Variance must be always positive by definition.
fill_cfg_t fill_cfg(dnnl_f32, 0.f, 16.f, /* int = */ false,
attr_t::post_ops_t::kind_t::ADD, "variance");
return fill_random_real(mem_dt, mem_fp, nullptr, fill_cfg);
}
benchdnn_parallel_nd(prb->n, [&](int64_t n) {
// final variance = {0.25f, 1.f, 4.f}
const float val = 0.25f * (1 << ((n % 3) * 2));
mem_fp.set_elem(n, val - prb->eps);
});
if (mem_dt) SAFE(mem_dt.reorder(mem_fp), WARN);
return OK;
}
int fill_src_bwd(const prb_t *prb, dnn_mem_t &mem_fp, dnn_mem_t &mem_dt,
const dnn_mem_t &ref_mean, res_t *res) {
const auto nelems = mem_fp.nelems();
if (nelems == 0) return OK;
// Refer to modes documentation for filling principles.
if (has_bench_mode_bit(mode_bit_t::bitwise)) {
return fill_random_real(mem_dt, mem_fp, res);
}
benchdnn_parallel_nd(prb->n, [&](int64_t n) {
// Idea behind the filling is to reduce a possibility of cancellation
// when subtracting a part accumulated over N. For that, we simplify
// src data to (m+1) and (m-1) points, d_dst data is more or less
// random but we keep all values as pow2 values to have almost exact
// summation result.
const float m = ref_mean.get_elem(n);
for (int64_t c = 0; c < prb->c; ++c) {
const int64_t off = n * prb->c + c;
const float val = c % 2 == 0 ? (m - 1.f) : (m + 1.f);
mem_fp.set_elem(
off, round_to_nearest_representable(prb->dt[0], val));
}
});
if (mem_dt) SAFE(mem_dt.reorder(mem_fp), WARN);
return OK;
}
int fill_diff_dst_bwd(
const prb_t *prb, dnn_mem_t &mem_fp, dnn_mem_t &mem_dt, res_t *res) {
const auto nelems = mem_fp.nelems();
if (nelems == 0) return OK;
// Refer to modes documentation for filling principles.
if (has_bench_mode_bit(mode_bit_t::bitwise)) {
return fill_random_real(mem_dt, mem_fp, res);
}
benchdnn_parallel_nd(prb->n, [&](int64_t n) {
// Note: we use a different seed for each chunk to avoid
// repeating patterns. We could use discard(idx_start) too but
// it has a complexity in O(idx_start). We also add 1 to avoid
// seeding with 0.
std::minstd_rand int_seed(n + 1);
int_seed.discard(1);
std::minstd_rand b_seed(n + 1);
b_seed.discard(2);
// See `fill_src_bwd` comment.
std::uniform_int_distribution<> data_dist(0, 6);
std::bernoulli_distribution half_dist(0.5f);
for (int64_t c = 0; c < prb->c; ++c) {
const int64_t off = n * prb->c + c;
const float sign = half_dist(b_seed) ? 1.f : -1.f;
// d_dst = powf(2, {-4, ... , 2})
const float val = sign * 0.0625f * (1LL << data_dist(int_seed));
mem_fp.set_elem(
off, round_to_nearest_representable(prb->dt[0], val));
}
});
if (mem_dt) SAFE(mem_dt.reorder(mem_fp), WARN);
return OK;
}
int prepare_bwd(const prb_t *prb, dnn_mem_map_t &mem_map,
dnn_mem_map_t &ref_mem_map, res_t *res) {
cfg_t cfg(prb);
auto &mean = mem_map.at(DNNL_ARG_MEAN);
auto &ref_mean = ref_mem_map.at(DNNL_ARG_MEAN);
SAFE(fill_mean(prb, cfg, ref_mean, mean), WARN);
auto &var = mem_map.at(DNNL_ARG_VARIANCE);
auto &ref_var = ref_mem_map.at(DNNL_ARG_VARIANCE);
SAFE(fill_variance_bwd(prb, ref_var, var), WARN);
auto &src = mem_map.at(DNNL_ARG_SRC);
auto &ref_src = ref_mem_map.at(DNNL_ARG_SRC);
SAFE(fill_src_bwd(prb, ref_src, src, ref_mean, res), WARN);
auto &d_dst = mem_map.at(DNNL_ARG_DIFF_DST);
auto &ref_d_dst = ref_mem_map.at(DNNL_ARG_DIFF_DST);
SAFE(fill_diff_dst_bwd(prb, ref_d_dst, d_dst, res), WARN);
// Need a copy of source data for inplace mode for bitwise testing.
if (has_bench_mode_bit(mode_bit_t::bitwise) && prb->inplace) {
auto &d_dst_copy = mem_map.at(-DNNL_ARG_DIFF_DST);
SAFE(bool(d_dst_copy) ? OK : FAIL, WARN);
SAFE(d_dst_copy.reorder(d_dst), WARN);
}
auto &scale = mem_map.at(DNNL_ARG_SCALE);
auto &ref_scale = ref_mem_map.at(DNNL_ARG_SCALE);
SAFE(fill_scale(prb, ref_scale, scale), WARN);
return OK;
}
dnnl_status_t init_pd(init_pd_args_t<prb_t> &init_pd_args) {
const prb_t *prb = init_pd_args.prb;
res_t *res = init_pd_args.res;
bool force_f32_dt = init_pd_args.force_f32_dt;
auto src_d = dnn_mem_t::init_md(prb->ndims, prb->dims.data(),
force_f32_dt ? dnnl_f32 : prb->dt[0], prb->tag[0]);
benchdnn_dnnl_wrapper_t<dnnl_memory_desc_t> stat_d {};
if (prb->stat_tag != tag::undef) {
stat_d = dnn_mem_t::init_md(
prb->ndims - 1, prb->dims.data(), dnnl_f32, prb->stat_tag);
}
attr_args_t attr_args;
attr_args.prepare_post_ops_mds(
prb->attr, prb->ndims, prb->dims.data(), dnnl_layer_normalization);
auto dnnl_attr = make_benchdnn_dnnl_wrapper(
create_dnnl_attr(prb->attr, attr_args));
auto flags = (dnnl_normalization_flags_t)prb->flags;
if (prb->dir & FLAG_FWD) {
auto dst_d = dnn_mem_t::init_md(prb->ndims, prb->dims.data(),
force_f32_dt ? dnnl_f32 : prb->dt[1], prb->tag[1]);
auto prop = prb->dir & FLAG_INF ? dnnl_forward_inference
: dnnl_forward_training;
TIME_C_PD(DNN_SAFE_STATUS(
dnnl_layer_normalization_forward_primitive_desc_create_v2(
&init_pd_args.pd, init_pd_args.engine, prop,
init_pd_args.src_md ? init_pd_args.src_md : src_d,
dst_d, stat_d, prb->ss_dt, prb->eps, flags,
dnnl_attr)));
} else {
auto diff_src_d = dnn_mem_t::init_md(prb->ndims, prb->dims.data(),
force_f32_dt ? dnnl_f32 : prb->dt[0], prb->tag[0]);
auto diff_dst_d = dnn_mem_t::init_md(prb->ndims, prb->dims.data(),
force_f32_dt ? dnnl_f32 : prb->dt[1], prb->tag[1]);
auto prop = prb->dir & FLAG_WEI ? dnnl_backward : dnnl_backward_data;
TIME_C_PD(DNN_SAFE_STATUS(
dnnl_layer_normalization_backward_primitive_desc_create_v2(
&init_pd_args.pd, init_pd_args.engine, prop, diff_src_d,
diff_dst_d, src_d, stat_d, prb->ss_dt, prb->ss_dt,
prb->eps, flags, init_pd_args.hint, dnnl_attr)));
}
return dnnl_success;
}
void skip_unimplemented_prb(const prb_t *prb, res_t *res) {
skip_unimplemented_data_type(
{prb->dt[0], prb->dt[1], prb->ss_dt}, prb->dir, res);
skip_unimplemented_sum_po(
prb->attr, res, dnnl_layer_normalization, prb->dt[0]);
skip_unimplemented_prelu_po(prb->attr, res, dnnl_layer_normalization);
if (is_gpu() && prb->attr.post_ops.len() != 0) {
// GPU does not support post-ops
res->state = SKIPPED;
res->reason = skip_reason::case_not_supported;
return;
}
}
void skip_invalid_prb(const prb_t *prb, res_t *res) {
// See `skip_invalid_inplace` for details.
if (prb->inplace) {
skip_invalid_inplace(
res, prb->dt[0], prb->dt[1], prb->tag[0], prb->tag[1]);
if (res->state == SKIPPED) return;
}
}
void setup_cmp(compare::compare_t &cmp, const prb_t *prb, data_kind_t kind,
const args_t &ref_args) {
const bool compare_with_norm = (prb->dir & FLAG_BWD);
cmp.set_norm_validation_mode(compare_with_norm);
const auto dt = prb->dir & FLAG_FWD ? prb->dt[1] : prb->dt[0];
// Digits must be non-negative for safe left-shifting when `digits_dt`
// exceeds `digits_f32`.
const int safe_digits = MAX2(0, digits_dt(dnnl_f32) - digits_dt(dt));
const float trh_coeff = (1 << safe_digits);
// The following code is a workaround because a SYCL compiler bug
// that is causing precision issues.
#ifdef DNNL_WITH_SYCL
float trh = trh_coeff
* ((kind == SRC || kind == DST
|| (kind == VAR && prb->dir & FLAG_FWD))
? 5e-7
: 0);
#else
float trh = trh_coeff * ((kind == SRC || kind == DST) ? 5e-7 : 0);
#endif
if ((kind == SC || kind == SH) && prb->dir & FLAG_BWD)
trh = trh_coeff * 5e-6;
cmp.set_threshold(trh);
// u8 turns half of output into zeros.
if (prb->dt[1] == dnnl_u8) cmp.set_zero_trust_percent(60.f);
// When the error is larger than `trh`, it could be due to a catastrophic
// cancellation in final result which is computed as `Y = a * X + b`.
// When `a * X` is close to `b` and their signs are opposite, then large
// error in `a * X` could result in a final result (which has a cancellation
// i.e. `|Y| = |a*X - (-b)|`), which has no meaningful digits left in
// mantissa.
//
// Since lambda is called when stack is unavailable, need to capture `prb`
// and `kind` by value to avoid using dangling references.
const auto lnorm_add_check =
[&, kind, prb](
const compare::compare_t::driver_check_func_args_t &args) {
if (!((prb->dir & FLAG_FWD) && kind == DST && prb->use_sh()))
return false;
const auto &sh = ref_args.find(DNNL_ARG_SHIFT);
const auto &dst = ref_args.find(DNNL_ARG_DST);
const int64_t c = dst.get_idx(
args.idx, 1 << (prb->ndims - 1) /* last_dim_mask */);
const float beta = sh.get_elem(c);
// Using an empirically derived threshold, check if
// cancellation error in `|Y| = |a*X - (-b)|` is huge.
const float abs_exp = fabsf(args.exp);
const float norm_denom = abs_exp > FLT_MIN ? abs_exp : 1.f;
const float abs_exp_delta = fabsf(args.exp - beta);
bool maybe_cancel_error = abs_exp_delta / norm_denom > 1.f;
if (!maybe_cancel_error) return false;
// Check for error in `a * X`
float diff_aX = fabsf((args.exp - beta) - (args.got - beta));
float rel_diff_aX = diff_aX
/ (abs_exp_delta > FLT_MIN ? abs_exp_delta : 1.f);
return rel_diff_aX <= args.trh;
};
cmp.set_driver_check_function(lnorm_add_check);
}
std::vector<int> supported_exec_args(dir_t dir) {
static const std::vector<int> exec_fwd_args = {
DNNL_ARG_SRC,
DNNL_ARG_MEAN,
DNNL_ARG_VARIANCE,
DNNL_ARG_SCALE,
DNNL_ARG_SHIFT,
DNNL_ARG_DST,
};
static const std::vector<int> exec_bwd_args = {
DNNL_ARG_SRC,
DNNL_ARG_MEAN,
DNNL_ARG_VARIANCE,
DNNL_ARG_SCALE,
DNNL_ARG_SHIFT,
DNNL_ARG_DIFF_DST,
DNNL_ARG_DIFF_SCALE,
DNNL_ARG_DIFF_SHIFT,
DNNL_ARG_DIFF_SRC,
};
return (dir & FLAG_FWD) ? exec_fwd_args : exec_bwd_args;
};
fill_cfg_t binary_po_fill_cfg(
int exec_arg, const dnn_mem_t &mem, const attr_t &attr) {
fill_cfg_t cfg;
const int post_ops_range = DNNL_ARG_ATTR_MULTIPLE_POST_OP(31)
- DNNL_ARG_ATTR_MULTIPLE_POST_OP(0);
const bool is_post_ops_arg = (exec_arg & post_ops_range);
if (is_post_ops_arg) {
// LNorm output values are in [-1.f; 1.f] range. Using small values
// leads to the cancellation effect. Config secures only positive and
// big enough values are used.
const int bin_po_idx
= exec_arg / DNNL_ARG_ATTR_MULTIPLE_POST_OP_BASE - 1;
assert(bin_po_idx < attr.post_ops.len());
const auto alg = attr.post_ops.entry[bin_po_idx].kind;
cfg = fill_cfg_t(mem.dt(), 4.f, 16.f, /* int = */ true, alg,
"lnorm_binary_post_op");
}
return cfg;
}
int init_ref_memory_args(dnn_mem_map_t &ref_mem_map, dnn_mem_map_t &mem_map,
dnnl_primitive_t prim, const prb_t *prb, res_t *res,
dnnl_primitive_t prim_ref) {
if (has_bench_mode_modifier(mode_modifier_t::no_host_memory)) return OK;
// TODO: this function still allocates the full memory print needed to fill
// the data and each argument can't be destroyed right away since filling
// requires all of them at a time.
const auto &ref_engine = get_cpu_engine();
const bool is_fwd_prim = is_fwd_prop_kind(query_prop_kind(query_pd(prim)));
for (auto &entry : mem_map) {
const int exec_arg = entry.first;
// The function targets regular exec_args that are positive.
// Negative args are used by bitwise and are broken in the `default`
// branch due to `&` always returns `true`.
if (exec_arg <= 0) continue;
auto &mem = entry.second;
// Scratchpad memory relates to a primitive. If reference needs it,
// use switch below to define a memory desc for it.
if (exec_arg != DNNL_ARG_SCRATCHPAD) {
ref_mem_map.emplace(exec_arg,
dnn_mem_t(mem.md_, dnnl_f32, tag::abx, ref_engine));
}
auto &ref_mem = ref_mem_map[exec_arg];
switch (exec_arg) {
case DNNL_ARG_MEAN:
case DNNL_ARG_VARIANCE:
if (prb->dir & FLAG_INF) {
const auto &src_md = mem_map[DNNL_ARG_SRC].md_;
const auto stat_dims = query_md_dims(src_md);
ref_mem_map[exec_arg] = dnn_mem_t(prb->ndims - 1, stat_dims,
dnnl_f32, tag::abx, ref_engine);
}
break;
default:
const auto &binary_fill_cfg
= binary_po_fill_cfg(exec_arg, mem, prb->attr);
std::unordered_map<int, fill_cfg_t> fill_cfg_map {
{DNNL_ARG_SRC_1, binary_fill_cfg}};
SAFE(init_ref_memory_args_default_case(exec_arg, mem, ref_mem,
prb->attr, res, fill_cfg_map),
WARN);
break;
}
}
if (is_fwd_prim) {
SAFE(prepare_fwd(prb, mem_map, ref_mem_map, res), WARN);
} else {
SAFE(prepare_bwd(prb, mem_map, ref_mem_map, res), WARN);
}
// Don't keep reference memory if it is not used further.
if (!has_bench_mode_bit(mode_bit_t::corr)) ref_mem_map.clear();
return OK;
}
std::vector<data_kind_t> get_kinds_to_check(const prb_t *prb) {
std::vector<data_kind_t> check_kinds;
if (prb->dir & FLAG_FWD) {
if (!(prb->flags & GLOB_STATS) && !(prb->dir & FLAG_INF)) {
check_kinds.push_back(MEAN);
check_kinds.push_back(VAR);
}
check_kinds.push_back(DST);
} else {
if (prb->dir & FLAG_WEI) {
if (prb->use_sc()) check_kinds.push_back(SC);
if (prb->use_sh()) check_kinds.push_back(SH);
}
check_kinds.push_back(SRC);
}
assert(!check_kinds.empty());
return check_kinds;
}
int createit(std::vector<benchdnn_dnnl_wrapper_t<dnnl_primitive_t>> &v_prim,
const prb_t *prb, res_t *res) {
v_prim.resize(1);
SAFE(init_prim(prb->ctx_init, v_prim[0], init_pd, prb, res), WARN);
return OK;
}
int check_cacheit(
std::vector<benchdnn_dnnl_wrapper_t<dnnl_primitive_t>> &v_prim,
const prb_t *prb, res_t *res) {
return check_caches(v_prim[0], prb, res);
}
int doit(const std::vector<benchdnn_dnnl_wrapper_t<dnnl_primitive_t>> &v_prim,
const prb_t *prb, res_t *res) {
const auto &prim = v_prim[0];
dnn_mem_map_t mem_map, ref_mem_map;
init_memory_args<prb_t>(mem_map, prb, prim, supported_exec_args(prb->dir));
TIME_FILL(SAFE(
init_ref_memory_args(ref_mem_map, mem_map, prim, prb, res), WARN));
args_t args(mem_map), ref_args(ref_mem_map);
SAFE(execute_and_wait(prim, args, res), WARN);
check_correctness(
prb, get_kinds_to_check(prb), args, ref_args, setup_cmp, res);
SAFE(check_bitwise(prim, get_kinds_to_check(prb), args, prb->attr,
prb->inplace, res),
WARN);
return measure_perf(prb->ctx_exe, res, prim, args);
}
} // namespace lnorm