text
stringlengths
0
2.2M
#include <float.h>
#include <math.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <random>
#include <sstream>
#include "oneapi/dnnl/dnnl.h"
#include "tests/test_thread.hpp"
#include "dnnl_common.hpp"
#include "dnnl_memory.hpp"
#include "norm.hpp"
#include "bnorm/bnorm.hpp"
namespace bnorm {
static int prepare_fwd_with_stats(const prb_t *prb, dnn_mem_t &src,
dnn_mem_t &mean, dnn_mem_t &var, dnn_mem_t &ss, dnn_mem_t &sh) {
const bool use_ss = prb->use_ss();
const bool use_sc = prb->use_sc();
const bool use_sh = prb->use_sh();
dnnl::impl::parallel_nd(prb->ic, [&](int64_t c) {
mean.set_elem(c, 4 * ((c % 5) - 2));
var.set_elem(c, ((c % 7) << 1));
const float sc_value = 1 << (c % 7);
const float sh_value = ((c % 3) - 1) * sc_value;
if (use_sc || use_sh) {
ss.set_elem(c, use_sc ? sc_value : 1.0f);
sh.set_elem(c, use_sh ? sh_value : 0.0f);
} else {
ss.set_elem(c, use_ss ? sc_value : 1.0f);
ss.set_elem(prb->ic + c, use_ss ? sh_value : 0.0f);
}
});
dnnl::impl::parallel_nd(prb->ic, prb->mb, prb->id, prb->ih, prb->iw,
[&](int64_t c, int64_t mb, int64_t d, int64_t h, int64_t w) {
int64_t l_base = mb * prb->id * prb->ih * prb->iw + c * 239 * 2;
float *s = (float *)src + data_off(prb, mb, c, 0, 0, 0);
const int64_t sp = d * prb->ih * prb->iw + h * prb->iw + w;
const int64_t l = l_base + sp;
const int64_t value = (l % 65) - 32;
s[sp] = round_to_nearest_representable(prb->dt, value);
});
return OK;
}
static int prepare_fwd_no_stats(const prb_t *prb, dnn_mem_t &src,
dnn_mem_t &mean, dnn_mem_t &var, dnn_mem_t &ss, dnn_mem_t &sh) {
/** Idea: choose src[] values so that both mean and variance are computed
* exactly (independently of the order of the computations).
*
* The `exactness` is achieved via [a1]: src[i] + src[i+1] = 2 * mean.
*
* The variation in src is allowed in the last flex_bits bits.
* If the sequence (L) is too big (flex_bits <= min_flex_bits), the mean
* value is set to 0 and src is partially filled with zeros (according to
* density so that at least want_flex_bits is reserved for src variation.
* Once src is set, variance is computed.
*
* ALG_0: mean is set to 0
* ALG_1: mean is set to 2^prb, where prb \in {-2, -1, ..., 4}
* ALG_AUTO: choose between ALG_0 and ALG_1 automatically */
const int64_t exact_bits = digits_dt(prb->dt);
const int64_t L = prb->mb * prb->id * prb->ih * prb->iw;
const int64_t logL = (int64_t)ceilf(log2f(L));
assert(logL <= 0 || (1LL << (logL - 1)) < L);
assert(L <= (1LL << logL));
const int64_t min_flex_bits = 3;
const int64_t want_flex_bits = MIN2(6, exact_bits / 2);
check_alg_t alg = prb->check_alg;
if (alg == ALG_AUTO) /* choose appropriate checking algorithm */
alg = (exact_bits - logL) / 2 - 1 >= min_flex_bits ? ALG_1 : ALG_0;
const int64_t flex_bits = alg == ALG_0
? want_flex_bits /* BFloat16 has only 7 bits of mantissa */
: MIN2(prb->dt == dnnl_bf16 ? 7 : exact_bits,
(exact_bits - logL) / 2 - 1);
if (flex_bits < min_flex_bits) return FAIL;
const int64_t flex_mask = (1 << flex_bits) - 1;
/* density: (exact_bits - log_2(L * density)) / 2 >= flex_bits */
const float density = alg == ALG_0
? 1.f * (1 << (exact_bits - 2 * flex_bits)) / L
: 1.f;