oneapi-src / oneDNN

oneAPI Deep Neural Network Library (oneDNN)
https://uxlfoundation.org
Apache License 2.0
3.6k stars 993 forks source link

Mean used for lnorm benchmark is not actual mean of the data #1974

Closed ghost closed 3 months ago

ghost commented 4 months ago

Summary

The benchdnn test for lnorm fails when oneDNN is built against the ACL backend. While investigating I found that the mean used in /tests/benchdnn/lnorm/ref_lnorm.cpp does not seem to be the mean of the underlying data (a similar mismatch exists for the variance). Can someone shed some light on this discrepancy?

Version

Report oneDNN version and githash. Version information is printed to stdout in verbose mode. oneDNN v3.6.0 (commit 094cc1dda4a24ccf0a54987a34c4475e00a926f9)

Environment

oneDNN includes hardware-specific optimizations and may behave differently on depending on the compiler and build environment. Include the following information to help reproduce the issue:

Steps to reproduce

Patch /tests/benchdnn/lnorm/ref_lnorm.cpp:

--- a/tests/benchdnn/lnorm/ref_lnorm.cpp
+++ b/tests/benchdnn/lnorm/ref_lnorm.cpp
@@ -18,6 +18,8 @@

 #include "lnorm/lnorm.hpp"

+#include <stdexcept>
+
 namespace lnorm {

 void compute_ref_fwd(const prb_t *prb, const args_t &args) {
@@ -52,6 +54,19 @@ void compute_ref_fwd(const prb_t *prb, const args_t &args) {
         float svar = var.get_elem(n);
         float sqrt_var = sqrtf(svar + prb->eps);

+        float sum = 0, calc_mean, num_elements = prb->c;
+
+        for (int i = 0; i < num_elements; ++i) {
+            float data = src.get_elem(i);
+            sum += data;
+        }
+
+        calc_mean = sum / num_elements;
+
+        if (calc_mean != smean) {
+            throw std::runtime_error("smean differs from calculated mean!\n");
+        }
+
         for (int64_t c = 0; c < prb->c; ++c) {
             float gamma = (use_sc ? sc.get_elem(c) : 1.0f) / sqrt_var;
             float beta = use_sh ? sh.get_elem(c) : 0;

Then run the benchmark with OMP_NUM_THREADS=1 ONEDNN_VERBOSE=all tests/benchdnn/benchdnn -v5 --lnorm --flags=G 70x70

Observed behavior

The added exception is thrown indicating that the calculated mean of the src data does not match the mean stored in smean.

Expected behavior

I would assume that smean holds the actual mean of the data. In case I have misunderstood something I would greatly appreciate any clarification. Thank you.

dzarukin commented 4 months ago

Hi @smarm0, when the --flag=G is specified, all the library does is reads the mean and variance values (they are inputs in this case, not outputs) and uses them as the part of norm operation. It doesn't have to use the mean and variance of the data to perform the check of this mode. Thus, they don't match in benchdnn either.

ghost commented 3 months ago

@dzarukin Thank you for the clarification, much appreciated.