text
stringlengths
0
2.2M
mem_fp.set_elem(idx, value);
}
});
SAFE(mem_dt.reorder(mem_fp), WARN);
return OK;
}
static int compare(const prb_t *prb, data_kind_t kind, const dnn_mem_t &fp_mem,
const dnn_mem_t &dt_mem, res_t *res, const dnn_mem_t *ss = nullptr,
const dnn_mem_t *sh = nullptr) {
const char *skind = data_kind2str(kind);
const int f32_mant_digits = 24;
const float eps_coeff = (1 << (f32_mant_digits - digits_dt(prb->dt)));
float eps = eps_coeff * (kind == DATA ? 5e-7 : 0);
if ((kind == SS || kind == SC || kind == SH) && prb->dir & FLAG_BWD)
eps = eps_coeff * 5e-6;
if (is_nvidia_gpu()) {
// cuDNN stores unbiased variance which requires rescaling by
// `(N - 1) / N`, where `N = MB * Spatial`. Hence, we cannot set the
// threshold to 0...
// Also the mean could also be rounded incorrectly (how?!)
if (kind == MEAN) eps = 1e-7;
if (kind == VAR) eps = 4e-7;
}
#ifdef DNNL_EXPERIMENTAL
const bool use_relaxed_validation
= dnnl::impl::experimental::use_bnorm_stats_one_pass();
#else
const bool use_relaxed_validation = false;
#endif
if (use_relaxed_validation) {
// On Intel GPUs mean and variance could be rounded incorrectly because
// they are calculated using fast but potentially unstable formula.
if (kind == MEAN) eps = 1e-7;
if (kind == VAR) eps = 4e-7;
}
// Since bwd testing is done using results from forward which are random
// fp32 values, diff_ss starts fluctuating, so we check norm for both data
// and SS, SC and SH.
const bool rely_on_norm = prb->dir & FLAG_BWD;
const int64_t N = kind == DATA ? prb->mb : 1;
const int64_t C = kind == DATA ? prb->ic : prb->ic * (kind == SS ? 2 : 1);
const int64_t SP = kind == DATA ? prb->id * prb->ih * prb->iw : 1;
const bool use_sc = prb->use_sc();
const bool use_sh = prb->use_sh();
const auto nelems = N * C * SP;
if (nelems == 0) return res->state = PASSED, OK;
res->total += rely_on_norm ? 1 : nelems;
diff_norm_t diff_norm;
for_(int64_t n = 0; n < N; n++)
for_(int64_t c = 0; c < C; c++)
for (int64_t sp = 0; sp < SP; ++sp) {
int64_t i = (n * C + c) * SP + sp;
const float dt = dt_mem.get_elem(i);
const float fp0 = fp_mem.get_elem(i);
const float fp = kind == DATA
? round_to_nearest_representable(prb->dt, fp0)
: fp0;
float diff = 0.f, rel_diff = 0.f;
bool ok = true;
if (rely_on_norm) {
diff_norm.update(fp, dt);
} else {
diff = fabsf(fp - dt);
rel_diff = diff / (fabsf(fp) > FLT_MIN ? fabsf(fp) : 1);
ok = (fabsf(fp) > 1e-5 ? rel_diff : diff) <= eps;
/* When the error is larger than eps, It could be
* due to catastrophic cancellation in final result
* which is computed as `Y = a * X + b`.
* When `a * X` is close to `b` and `sign(a * X) = - sign(b)`.
* 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.*/
if (!ok && (prb->dir & FLAG_FWD) && kind == DATA && ss && sh) {
const float beta = (use_sc || use_sh)
? ((const float *)*sh)[c]
: ((const float *)*ss)[prb->ic + c];
/* Using an empirically derived threshold,
* check if cancellation error
* in `|Y| = |a*X - (-b)|` is huge.*/
bool maybe_cancellation_error
= (fabsf(fp - beta)
/ (fabsf(fp) > FLT_MIN ? fabsf(fp) : 1))
> 1.0f;
if (maybe_cancellation_error) {
/* Check for error in `a * X` */
float diff_aX = fabsf((fp - beta) - (dt - beta));