text
stringlengths
0
2.2M
assert((exact_bits - ceilf(log2f(L * density))) / 2 >= flex_bits);
BENCHDNN_PRINT(6, "check_alg: %s, density = %g, flex_bits = " IFMT "\n",
check_alg2str(alg), density, flex_bits);
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) {
const float m = ((float *)mean)[c]
= alg == ALG_0 ? 0.f : 0.25f * (1 << (c % 7));
float v = 0; /* current variance */
for (int64_t mb = 0; mb < prb->mb; ++mb) {
int64_t l_base = mb * prb->id * prb->ih * prb->iw
+ c * 239 * 2; // l[0] must be even
float *s = (float *)src + data_off(prb, mb, c, 0, 0, 0);
for_(int64_t d = 0; d < prb->id; ++d)
for_(int64_t h = 0; h < prb->ih; ++h)
for (int64_t w = 0; w < prb->iw; ++w) {
const int64_t sp = d * prb->ih * prb->iw + h * prb->iw + w;
const int64_t l = l_base + sp;
if (alg == ALG_0 && !flip_coin(l / 2 * 257ULL, density)) {
s[sp] = 0;
continue;
}
const int64_t gen = (l / 2 * 1637) & flex_mask;
const int sgn = l % 2 == 0 ? 1 : -1; /* [a1] */
const float f = 1.f * sgn * gen / (1 << flex_bits);
s[sp] = alg == ALG_0 ? f : m * (1.f + f);
if (L % 2 && (mb * prb->id * prb->ih * prb->iw + sp == L - 1)) {
s[sp] = m;
}
v += (s[sp] - m) * (s[sp] - m);
}
}
((float *)var)[c] = v / (prb->mb * prb->id * prb->ih * prb->iw);
const float sc_value = 1.f / 8 * (1 << (c % 7));
const float sh_value = ((c % 3) - 1) * sc_value / 64;
if (use_sc || use_sh) {
((float *)ss)[c] = use_sc ? sc_value : 1.0f;
((float *)sh)[c] = use_sh ? sh_value : 0.0f;
} else {
((float *)ss)[c] = use_ss ? sc_value : 1.0f;
((float *)ss)[prb->ic + c] = use_ss ? sh_value : 0.0f;
}
});
return OK;
}
static int prepare_fwd(const prb_t *prb, dnn_mem_t &src, dnn_mem_t &mean,
dnn_mem_t &var, dnn_mem_t &ss, dnn_mem_t &sh) {
if (prb->flags & GLOB_STATS)
return prepare_fwd_with_stats(prb, src, mean, var, ss, sh);
else
return prepare_fwd_no_stats(prb, src, mean, var, ss, sh);
}
static int prepare_bwd(const prb_t *prb, dnn_mem_t &mem_dt, dnn_mem_t &mem_fp) {
const auto nelems = mem_fp.nelems();
if (nelems == 0) return OK;
// Idea behind filling: integer diff_dst values decrease norms unlike fp32
// values in [-1.f, 1.f] range. To decrease norms more, make data pretty
// sparse as answers sum all diff_dst values.
/* Do fixed partitioning to have same filling for any number of threads */
const int64_t n_chunks = 16;
const int64_t chunk_size = div_up(nelems, n_chunks);
dnnl::impl::parallel_nd(n_chunks, [&](int64_t idx_chunk) {
int64_t idx_start = idx_chunk * chunk_size;
int64_t idx_end = MIN2(idx_start + chunk_size, nelems);
// 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 msr(idx_start + 1);
msr.discard(1);
std::uniform_int_distribution<> igen_val(-2, 2);
std::uniform_int_distribution<> igen_coin(0, 256 * 1024);
// at least 20 non-zero elems
float sparsity = MAX2(0.05f, MIN2(1.f, 20.f / nelems));
for (int64_t idx = idx_start; idx < idx_end; ++idx) {
float value = flip_coin(igen_coin(msr), sparsity)
? round_to_nearest_representable(prb->dt, igen_val(msr))
: 0;