test_gemm_int8_accuracy.cpp 10.2 KB
Newer Older
Z
Zhen Wang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include <cstdlib>
#include <ctime>
#include <iostream>
18
#include <limits>
Z
Zhen Wang 已提交
19
#include <random>
Z
ZhenWang 已提交
20
#include <type_traits>
Z
Zhen Wang 已提交
21 22 23 24
#include "../test_helper.h"
#include "common/log.h"
#include "memory/t_malloc.h"
#include "operators/math/gemm.h"
Z
Zhen Wang 已提交
25 26 27
#ifdef _OPENMP
#include <omp.h>
#endif  // _OPENMP
Z
Zhen Wang 已提交
28 29 30 31 32 33 34 35 36

#define a(i, j) a[(i)*lda + (j)]
#define b(i, j) b[(i)*ldb + (j)]
#define c(i, j) c[(i)*ldc + (j)]
#define c1(i, j) c1[(i)*ldc + (j)]

using std::default_random_engine;
using std::uniform_int_distribution;

Z
ZhenWang 已提交
37 38
template <typename T>
void print_matrix(int m, int n, int ldc, T *c) {
Z
Zhen Wang 已提交
39
  for (int i = 0; i < m; ++i) {
Z
ZhenWang 已提交
40 41 42 43 44 45 46 47
    if (std::is_same<T, int8_t>::value) {
      std::cout.setf(std::ios::left);
      std::cout.width(4);
      std::cout << static_cast<int32_t>(c(i, 0));
    } else {
      std::cout.setf(std::ios::left);
      std::cout.width(6);
      std::cout << c(i, 0);
Z
Zhen Wang 已提交
48 49
    }
    for (int j = 1; j < n; ++j) {
Z
ZhenWang 已提交
50 51 52 53 54 55 56 57 58 59 60
      if (std::is_same<T, int8_t>::value) {
        std::cout << " | ";
        std::cout.setf(std::ios::left);
        std::cout.width(4);
        std::cout << static_cast<int32_t>(c(i, j));
      } else {
        std::cout << " | ";
        std::cout.setf(std::ios::left);
        std::cout.width(6);
        std::cout << c(i, j);
      }
Z
Zhen Wang 已提交
61
    }
Z
ZhenWang 已提交
62
    std::cout << "\n";
Z
Zhen Wang 已提交
63 64 65 66
  }
  std::cout << std::endl;
}

67 68
int32_t qadd_int32(int32_t l, int32_t r) {
  int64_t res = static_cast<int64_t>(l) + static_cast<int64_t>(r);
69 70 71 72
  if (res > std::numeric_limits<int32_t>::max())
    return std::numeric_limits<int32_t>::max();
  else if (res < std::numeric_limits<int32_t>::min())
    return std::numeric_limits<int32_t>::min();
73 74 75 76
  else
    return static_cast<int32_t>(res);
}

77 78 79 80 81 82 83 84 85 86
// round to zero
float round2zero(float v) {
  float res;
  if (v > 0)
    res = std::floor(v);
  else if (v < 0)
    res = std::ceil(v);
  return res;
}

87 88
int8_t qscale_int32(int32_t v, float scale) {
  float res = static_cast<float>(v) * scale;
89
  res = round2zero(res);
90 91 92 93 94 95 96 97
  if (res > 127)
    return static_cast<int8_t>(127);
  else if (res < -127)
    return static_cast<int8_t>(-127);
  else
    return static_cast<int8_t>(res);
}

Z
Zhen Wang 已提交
98 99 100 101 102 103
int do_sgemm(int m, int n, int k, bool relu, int pr) {
  int lda = k;
  int ldb = n;
  int ldc = n;
  default_random_engine e;
  uniform_int_distribution<int8_t> pixel(-127, 127);
Z
Zhen Wang 已提交
104 105 106 107 108 109 110 111
  int8_t *a = static_cast<int8_t *>(
      paddle_mobile::memory::Alloc(sizeof(int8_t) * m * k));
  int8_t *b = static_cast<int8_t *>(
      paddle_mobile::memory::Alloc(sizeof(int8_t) * k * n));
  int32_t *c = static_cast<int32_t *>(
      paddle_mobile::memory::Alloc(sizeof(int32_t) * m * n));
  int32_t *c1 = static_cast<int32_t *>(
      paddle_mobile::memory::Alloc(sizeof(int32_t) * m * n));
Z
Zhen Wang 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

  for (int i = 0; i < m * k; ++i) {
    a[i] = pixel(e);
  }
  for (int i = 0; i < k * n; ++i) {
    b[i] = pixel(e);
  }

  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      int32_t r = 0;
      for (int p = 0; p < k; p++) {
        r += static_cast<int32_t>(a(i, p)) * static_cast<int32_t>(b(p, j));
      }
      c1(i, j) = r;
    }
  }

  paddle_mobile::operators::math::Gemm gemm;
Z
Zhen Wang 已提交
131 132 133 134
#ifdef _OPENMP
  gemm.Sgemm_omp(m, n, k, static_cast<int8_t>(1), a, lda, b, ldb,
                 static_cast<int8_t>(0), c, ldc, relu, nullptr);
#else
Z
Zhen Wang 已提交
135 136
  gemm.Sgemm(m, n, k, static_cast<int8_t>(1), a, lda, b, ldb,
             static_cast<int8_t>(0), c, ldc, relu, nullptr);
Z
Zhen Wang 已提交
137
#endif
Z
Zhen Wang 已提交
138 139 140 141 142 143 144 145 146 147 148 149
  int eq = 0;
  int neq = 0;
  for (int i = 0; i < m * n; ++i) {
    if (c[i] == c1[i]) {
      ++eq;
    } else {
      ++neq;
    }
  }

  if (pr > 0) {
    std::cout << "A:" << std::endl;
Z
ZhenWang 已提交
150
    print_matrix(m, k, lda, a);
Z
Zhen Wang 已提交
151
    std::cout << "B:" << std::endl;
Z
ZhenWang 已提交
152
    print_matrix(k, n, ldb, b);
Z
Zhen Wang 已提交
153
    std::cout << "C:" << std::endl;
Z
ZhenWang 已提交
154
    print_matrix(m, n, ldc, c);
Z
Zhen Wang 已提交
155
    std::cout << "C1:" << std::endl;
Z
ZhenWang 已提交
156
    print_matrix(m, n, ldc, c1);
Z
Zhen Wang 已提交
157 158 159 160 161
  }

  std::cout << "mnk=" << m << " " << n << " " << k << " relu=" << relu
            << "   eq=" << eq << " neq=" << neq << std::endl;

Z
ZhenWang 已提交
162 163
  PADDLE_MOBILE_ENFORCE(neq == 0, "The execution of do_sgemm is failed!");

Z
Zhen Wang 已提交
164 165 166 167 168 169 170 171
  paddle_mobile::memory::Free(a);
  paddle_mobile::memory::Free(b);
  paddle_mobile::memory::Free(c);
  paddle_mobile::memory::Free(c1);

  return 0;
}

Z
ZhenWang 已提交
172 173
int do_sgemm_with_bias(int m, int n, int k, bool relu, int pr,
                       bool addOnRow = false) {
174 175 176
  int lda = k;
  int ldb = n;
  int ldc = n;
177
  float scale = 0.00628f;
178 179 180 181 182 183 184 185 186 187 188
  default_random_engine e;
  uniform_int_distribution<int8_t> pixel(-127, 127);
  int8_t *a = static_cast<int8_t *>(
      paddle_mobile::memory::Alloc(sizeof(int8_t) * m * k));
  int8_t *b = static_cast<int8_t *>(
      paddle_mobile::memory::Alloc(sizeof(int8_t) * k * n));
  int8_t *c = static_cast<int8_t *>(
      paddle_mobile::memory::Alloc(sizeof(int8_t) * m * n));
  int8_t *c1 = static_cast<int8_t *>(
      paddle_mobile::memory::Alloc(sizeof(int8_t) * m * n));

Z
ZhenWang 已提交
189 190 191 192 193 194 195 196
  int32_t *bias = nullptr;
  if (addOnRow) {
    bias = static_cast<int32_t *>(
        paddle_mobile::memory::Alloc(sizeof(int32_t) * n));
  } else {
    bias = static_cast<int32_t *>(
        paddle_mobile::memory::Alloc(sizeof(int32_t) * m));
  }
197 198 199 200 201 202 203

  for (int i = 0; i < m * k; ++i) {
    a[i] = pixel(e);
  }
  for (int i = 0; i < k * n; ++i) {
    b[i] = pixel(e);
  }
Z
ZhenWang 已提交
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

  if (addOnRow) {
    for (int i = 0; i < n; ++i) {
      bias[i] = static_cast<int32_t>(pixel(e));
    }
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        int32_t bias_v = bias[j];
        int32_t r = 0;
        for (int p = 0; p < k; p++) {
          r += static_cast<int32_t>(a(i, p)) * static_cast<int32_t>(b(p, j));
        }
        r = qadd_int32(r, bias_v);
        if (relu) r = std::max(0, r);
        c1(i, j) = qscale_int32(r, scale);
      }
    }
  } else {
    for (int i = 0; i < m; ++i) {
      bias[i] = static_cast<int32_t>(pixel(e));
    }
    for (int i = 0; i < m; ++i) {
      int32_t bias_v = bias[i];
      for (int j = 0; j < n; ++j) {
        int32_t r = 0;
        for (int p = 0; p < k; p++) {
          r += static_cast<int32_t>(a(i, p)) * static_cast<int32_t>(b(p, j));
        }
        r = qadd_int32(r, bias_v);
        if (relu) r = std::max(0, r);
        c1(i, j) = qscale_int32(r, scale);
235 236 237 238 239 240
      }
    }
  }

  paddle_mobile::operators::math::Gemm gemm;
#ifdef _OPENMP
Z
ZhenWang 已提交
241
  gemm.Sgemm_omp(m, n, k, scale, a, lda, b, ldb, static_cast<float>(0), c, ldc,
Z
ZhenWang 已提交
242
                 relu, bias, addOnRow);
243 244
#else
  gemm.Sgemm(m, n, k, scale, a, lda, b, ldb, static_cast<float>(0), c, ldc,
Z
ZhenWang 已提交
245
             relu, bias, addOnRow);
246 247 248 249 250 251 252 253 254 255 256 257 258
#endif
  int eq = 0;
  int neq = 0;
  for (int i = 0; i < m * n; ++i) {
    if (c[i] == c1[i]) {
      ++eq;
    } else {
      ++neq;
    }
  }

  if (pr > 0) {
    std::cout << "A:" << std::endl;
Z
ZhenWang 已提交
259
    print_matrix(m, k, lda, a);
260
    std::cout << "B:" << std::endl;
Z
ZhenWang 已提交
261
    print_matrix(k, n, ldb, b);
262
    std::cout << "Bias:" << std::endl;
Z
ZhenWang 已提交
263 264 265 266 267
    if (addOnRow) {
      print_matrix(1, n, n, bias);
    } else {
      print_matrix(m, 1, 1, bias);
    }
268
    std::cout << "C:" << std::endl;
Z
ZhenWang 已提交
269
    print_matrix(m, n, ldc, c);
270
    std::cout << "C1:" << std::endl;
Z
ZhenWang 已提交
271
    print_matrix(m, n, ldc, c1);
272 273 274 275 276
  }

  std::cout << "mnk=" << m << " " << n << " " << k << " relu=" << relu
            << "   eq=" << eq << " neq=" << neq << std::endl;

Z
ZhenWang 已提交
277 278 279
  PADDLE_MOBILE_ENFORCE(neq == 0,
                        "The execution of do_sgemm_with_bias is failed!");

280 281 282 283 284 285 286 287 288
  paddle_mobile::memory::Free(a);
  paddle_mobile::memory::Free(b);
  paddle_mobile::memory::Free(c);
  paddle_mobile::memory::Free(c1);
  paddle_mobile::memory::Free(bias);

  return 0;
}

Z
Zhen Wang 已提交
289
int main() {
Z
Zhen Wang 已提交
290
#ifdef _OPENMP
291
  omp_set_num_threads(4);
Z
Zhen Wang 已提交
292
#endif
293 294 295
  std::cout << "\n\n******************************************************\n\n"
            << std::endl;
  std::cout << "Test gemm without bias:" << std::endl;
Z
Zhen Wang 已提交
296
  do_sgemm(9, 9, 9, false, 1);
Z
Zhen Wang 已提交
297 298 299 300
  do_sgemm(10, 6, 12, false, 0);
  do_sgemm(512, 256, 384, false, 0);
  do_sgemm(1366, 768, 256, false, 0);
  do_sgemm(1255, 755, 333, false, 0);
Z
Zhen Wang 已提交
301 302 303
  do_sgemm(599, 1133, 393, false, 0);
  do_sgemm(777, 555, 999, false, 0);
  do_sgemm(333, 797, 939, false, 0);
Z
Zhen Wang 已提交
304
  do_sgemm(1024, 1024, 1024, false, 0);
Z
Zhen Wang 已提交
305

306 307
  std::cout << "\n\n******************************************************\n\n"
            << std::endl;
Z
ZhenWang 已提交
308
  std::cout << "Test gemm with bias(bias is added on column):" << std::endl;
309 310 311 312 313 314 315 316 317 318
  do_sgemm_with_bias(9, 9, 9, false, 1);
  do_sgemm_with_bias(10, 6, 12, false, 0);
  do_sgemm_with_bias(512, 256, 384, false, 0);
  do_sgemm_with_bias(1366, 768, 256, false, 0);
  do_sgemm_with_bias(1255, 755, 333, false, 0);
  do_sgemm_with_bias(599, 1133, 393, false, 0);
  do_sgemm_with_bias(777, 555, 999, false, 0);
  do_sgemm_with_bias(333, 797, 939, false, 0);
  do_sgemm_with_bias(1024, 1024, 1024, false, 0);

Z
ZhenWang 已提交
319 320 321 322 323 324 325 326 327 328 329 330 331
  std::cout << "\n\n******************************************************\n\n"
            << std::endl;
  std::cout << "Test gemm with bias(bias is added on row):" << std::endl;
  do_sgemm_with_bias(9, 9, 9, false, 1, true);
  do_sgemm_with_bias(10, 6, 12, false, 0, true);
  do_sgemm_with_bias(512, 256, 384, false, 0, true);
  do_sgemm_with_bias(1366, 768, 256, false, 0, true);
  do_sgemm_with_bias(1255, 755, 333, false, 0, true);
  do_sgemm_with_bias(599, 1133, 393, false, 0, true);
  do_sgemm_with_bias(777, 555, 999, false, 0, true);
  do_sgemm_with_bias(333, 797, 939, false, 0, true);
  do_sgemm_with_bias(1024, 1024, 1024, false, 0, true);

332 333 334 335 336 337 338 339 340 341 342 343 344
  std::cout << "\n\n******************************************************\n\n"
            << std::endl;
  std::cout << "Test gemm with relu and bias:" << std::endl;
  do_sgemm_with_bias(9, 9, 9, true, 1);
  do_sgemm_with_bias(10, 6, 12, true, 0);
  do_sgemm_with_bias(512, 256, 384, true, 0);
  do_sgemm_with_bias(1366, 768, 256, true, 0);
  do_sgemm_with_bias(1255, 755, 333, true, 0);
  do_sgemm_with_bias(599, 1133, 393, true, 0);
  do_sgemm_with_bias(777, 555, 999, true, 0);
  do_sgemm_with_bias(333, 797, 939, true, 0);
  do_sgemm_with_bias(1024, 1024, 1024, true, 0);

Z
Zhen Wang 已提交
345 346
  return 0;
}