/* 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 #include #include #include #include #include "../test_helper.h" #include "common/log.h" #include "memory/t_malloc.h" #include "operators/math/gemm.h" #ifdef _OPENMP #include #endif // _OPENMP #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; void print_matirx(int m, int n, int ldc, int32_t *c) { for (int i = 0; i < m; ++i) { std::cout << c(i, 0); for (int j = 1; j < n; ++j) { std::cout << " | " << c(i, j); } std::cout << std::endl; } std::cout << std::endl; } void print_matirx(int m, int n, int ldc, int8_t *c) { for (int i = 0; i < m; ++i) { std::cout << static_cast(c(i, 0)); for (int j = 1; j < n; ++j) { std::cout << " | " << static_cast(c(i, j)); } std::cout << std::endl; } std::cout << std::endl; } int32_t qadd_int32(int32_t l, int32_t r) { int64_t res = static_cast(l) + static_cast(r); if (res > INT_MAX) return INT_MAX; else if (res < INT_MIN) return INT_MIN; else return static_cast(res); } int8_t qscale_int32(int32_t v, float scale) { float res = static_cast(v) * scale; if (res > 0) res = std::floor(res); else if (res < 0) res = std::ceil(res); // round to zero if (res > 127) return static_cast(127); else if (res < -127) return static_cast(-127); else return static_cast(res); } 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 pixel(-127, 127); int8_t *a = static_cast( paddle_mobile::memory::Alloc(sizeof(int8_t) * m * k)); int8_t *b = static_cast( paddle_mobile::memory::Alloc(sizeof(int8_t) * k * n)); int32_t *c = static_cast( paddle_mobile::memory::Alloc(sizeof(int32_t) * m * n)); int32_t *c1 = static_cast( paddle_mobile::memory::Alloc(sizeof(int32_t) * m * n)); 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(a(i, p)) * static_cast(b(p, j)); } c1(i, j) = r; } } paddle_mobile::operators::math::Gemm gemm; #ifdef _OPENMP gemm.Sgemm_omp(m, n, k, static_cast(1), a, lda, b, ldb, static_cast(0), c, ldc, relu, nullptr); #else gemm.Sgemm(m, n, k, static_cast(1), a, lda, b, ldb, static_cast(0), c, ldc, relu, nullptr); #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; print_matirx(m, k, lda, a); std::cout << "B:" << std::endl; print_matirx(k, n, ldb, b); std::cout << "C:" << std::endl; print_matirx(m, n, ldc, c); std::cout << "C1:" << std::endl; print_matirx(m, n, ldc, c1); } std::cout << "mnk=" << m << " " << n << " " << k << " relu=" << relu << " eq=" << eq << " neq=" << neq << std::endl; paddle_mobile::memory::Free(a); paddle_mobile::memory::Free(b); paddle_mobile::memory::Free(c); paddle_mobile::memory::Free(c1); return 0; } int do_sgemm_with_bias(int m, int n, int k, bool relu, int pr) { int lda = k; int ldb = n; int ldc = n; float scale = 0.00628; default_random_engine e; uniform_int_distribution pixel(-127, 127); int8_t *a = static_cast( paddle_mobile::memory::Alloc(sizeof(int8_t) * m * k)); int8_t *b = static_cast( paddle_mobile::memory::Alloc(sizeof(int8_t) * k * n)); int8_t *c = static_cast( paddle_mobile::memory::Alloc(sizeof(int8_t) * m * n)); int8_t *c1 = static_cast( paddle_mobile::memory::Alloc(sizeof(int8_t) * m * n)); int32_t *bias = static_cast(paddle_mobile::memory::Alloc(sizeof(int32_t) * m)); 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) { bias[i] = static_cast(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(a(i, p)) * static_cast(b(p, j)); } r = qadd_int32(r, bias_v); if (relu) r = std::max(0, r); c1(i, j) = qscale_int32(r, scale); } } paddle_mobile::operators::math::Gemm gemm; #ifdef _OPENMP // TODO(wzzju):gemm.Sgemm_omp_with_bias, now use single thread instead. gemm.Sgemm(m, n, k, scale, a, lda, b, ldb, static_cast(0), c, ldc, relu, bias); #else gemm.Sgemm(m, n, k, scale, a, lda, b, ldb, static_cast(0), c, ldc, relu, bias); #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; print_matirx(m, k, lda, a); std::cout << "B:" << std::endl; print_matirx(k, n, ldb, b); std::cout << "Bias:" << std::endl; print_matirx(m, 1, 1, bias); std::cout << "C:" << std::endl; print_matirx(m, n, ldc, c); std::cout << "C1:" << std::endl; print_matirx(m, n, ldc, c1); } std::cout << "mnk=" << m << " " << n << " " << k << " relu=" << relu << " eq=" << eq << " neq=" << neq << std::endl; 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; } int main() { #ifdef _OPENMP omp_set_num_threads(4); #endif std::cout << "\n\n******************************************************\n\n" << std::endl; std::cout << "Test gemm without bias:" << std::endl; do_sgemm(9, 9, 9, false, 1); 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); do_sgemm(599, 1133, 393, false, 0); do_sgemm(777, 555, 999, false, 0); do_sgemm(333, 797, 939, false, 0); do_sgemm(1024, 1024, 1024, false, 0); std::cout << "\n\n******************************************************\n\n" << std::endl; std::cout << "Test gemm with bias:" << std::endl; 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); 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); return 0; }