google / gemmlowp

Low-precision matrix multiplication

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

error result when W and X don't range from -1 to 1

pingfengluo opened this issue · comments

when I change W and X range (-20 to 20) , gemm result loss too much pricision.
diff --git a/doc/quantization_example.cc b/doc/quantization_example.cc
index d7b147d..f7178b9 100644
--- a/doc/quantization_example.cc
+++ b/doc/quantization_example.cc
@@ -157,7 +157,7 @@ class MatrixWithStorage {
: storage(rows * cols), matrix_map(storage.data(), rows, cols) {}
void MakeRandom() {
static std::mt19937 random_engine;

  • std::uniform_real_distribution distribution(-1, 1);
  • std::uniform_real_distribution distribution(-20, 20);
    for (auto& x : storage) {
    x = static_cast(distribution(random_engine));
    }

the gemm result is:
Difference between ACTUAL and REFERENCE float results:
-0.27 3.05 -0.269
-0.269 0.881 1.47

It is to be expected that as the min-max interval becomes wider, the 8bit-representable values get more diluted, so the quantization error on every value becomes larger. That is why neural network quantization needs to carefully pick the right min-max for each array.

commented

@bjacob “That is why neural network quantization needs to carefully pick the right min-max for each array.” ===> How to pick the right min-max for each array in neural network ?