test_gemm_accuracy.cpp 3.8 KB
Newer Older
Z
zhaojiaying01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

Y
yanantao78 已提交
15 16
#include <cstdlib>
#include <ctime>
Y
yanantao78 已提交
17
#include <iostream>
Z
zhaojiaying01 已提交
18
#include "../test_helper.h"
Z
zhaojiaying01 已提交
19
#include "common/log.h"
Z
zhaojiaying01 已提交
20
#include "memory/t_malloc.h"
Z
zhaojiaying01 已提交
21
#include "operators/math/gemm.h"
Z
zhaojiaying01 已提交
22

Z
zhaojiaying01 已提交
23 24
#define a(i, j) a[(i)*lda + (j)]
#define b(i, j) b[(i)*ldb + (j)]
Y
yanantao78 已提交
25
#define c(i, j) c[(i)*ldc + (j)]
Z
zhaojiaying01 已提交
26 27
#define c1(i, j) c1[(i)*ldc + (j)]

Y
yanantao78 已提交
28
void print_matirx(int m, int n, int ldc, float *c) {
Y
yanantao78 已提交
29 30 31 32
  for (int i = 0; i < m; ++i) {
    std::cout << c(i, 0);
    for (int j = 1; j < n; ++j) {
      std::cout << " | " << c(i, j);
Y
yanantao78 已提交
33 34
    }
    std::cout << std::endl;
Y
yanantao78 已提交
35 36
  }
  std::cout << std::endl;
Y
yanantao78 已提交
37 38 39
}

int do_sgemm(int m, int n, int k, bool relu, int t1, int t2, int pr) {
Z
zhaojiaying01 已提交
40 41 42
  int lda = k;
  int ldb = n;
  int ldc = n;
Z
zhaojiaying01 已提交
43

Y
yanantao78 已提交
44 45 46 47 48 49 50 51 52 53 54 55
  float *a =
      static_cast<float *>(paddle_mobile::memory::Alloc(sizeof(float) * m * k));
  float *b =
      static_cast<float *>(paddle_mobile::memory::Alloc(sizeof(float) * k * n));
  float *c =
      static_cast<float *>(paddle_mobile::memory::Alloc(sizeof(float) * m * n));
  float *c1 =
      static_cast<float *>(paddle_mobile::memory::Alloc(sizeof(float) * m * n));
  float *scale =
      static_cast<float *>(paddle_mobile::memory::Alloc(sizeof(float) * m));
  float *bias =
      static_cast<float *>(paddle_mobile::memory::Alloc(sizeof(float) * m));
Y
do trim  
yanantao78 已提交
56

Y
yanantao78 已提交
57
  srand(unsigned(time(0)));
Z
zhaojiaying01 已提交
58
  for (int i = 0; i < m * k; ++i) {
Y
yanantao78 已提交
59
    a[i] = t1 + rand() % t2;
Z
zhaojiaying01 已提交
60 61
  }
  for (int i = 0; i < k * n; ++i) {
Y
yanantao78 已提交
62
    b[i] = t1 + rand() % t2;
Z
zhaojiaying01 已提交
63
  }
Y
yanantao78 已提交
64
  for (int i = 0; i < m; ++i) {
Y
yanantao78 已提交
65
    scale[i] = t1 + rand() % t2;
66
  }
Y
yanantao78 已提交
67
  for (int i = 0; i < m; ++i) {
Y
yanantao78 已提交
68
    bias[i] = t1 + rand() % t2;
Z
zhaojiaying01 已提交
69
  }
Y
do trim  
yanantao78 已提交
70

Y
yanantao78 已提交
71 72 73 74 75 76 77 78 79 80
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      float r = 0;
      for (int p = 0; p < k; p++) {
        r += a(i, p) * b(p, j);
      }
      r *= scale[i];
      r += bias[i];
      if (relu && (r < 0)) {
        r = 0;
Z
zhaojiaying01 已提交
81
      }
Y
yanantao78 已提交
82
      c1(i, j) = r;
Z
zhaojiaying01 已提交
83 84
    }
  }
Y
do trim  
yanantao78 已提交
85

86
  paddle_mobile::operators::math::Gemm gemm;
Z
Zhen Wang 已提交
87
  gemm.SgemmWithBn(m, n, k, 1, a, lda, b, ldb, 0.3, c, ldc, relu, scale, bias,
88
                   nullptr);
Y
yanantao78 已提交
89 90
  int eq = 0;
  int neq = 0;
Z
zhaojiaying01 已提交
91
  for (int i = 0; i < m * n; ++i) {
Y
yanantao78 已提交
92
    if (static_cast<int>(c[i]) == static_cast<int>(c1[i])) {
Y
yanantao78 已提交
93
      ++eq;
Y
yanantao78 已提交
94
    } else {
Y
yanantao78 已提交
95
      ++neq;
Z
zhaojiaying01 已提交
96 97
    }
  }
Y
do trim  
yanantao78 已提交
98

Y
yanantao78 已提交
99 100 101 102 103 104 105 106 107 108
  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);
  }
Y
do trim  
yanantao78 已提交
109

Y
yanantao78 已提交
110 111
  std::cout << "mnk=" << m << " " << n << " " << k << " relu=" << relu
            << "   eq=" << eq << " neq=" << neq << std::endl;
Y
yanantao78 已提交
112 113 114 115 116 117 118

  paddle_mobile::memory::Free(a);
  paddle_mobile::memory::Free(b);
  paddle_mobile::memory::Free(c);
  paddle_mobile::memory::Free(c1);
  paddle_mobile::memory::Free(scale);
  paddle_mobile::memory::Free(bias);
Y
do trim  
yanantao78 已提交
119

Y
yanantao78 已提交
120 121 122 123 124 125 126 127 128 129
  return 0;
}

int main() {
  do_sgemm(9, 9, 9, true, 10, 10, 10);
  do_sgemm(10, 6, 12, false, 10, 10, 0);
  do_sgemm(512, 256, 384, false, 10, 10, 0);
  do_sgemm(1366, 768, 256, false, 10, 10, 0);
  do_sgemm(1255, 755, 333, false, 10, 10, 0);
  do_sgemm(555, 777, 999, false, 10, 10, 0);
Y
do trim  
yanantao78 已提交
130

Y
yanantao78 已提交
131 132 133 134 135
  do_sgemm(10, 6, 12, true, -4, 10, 0);
  do_sgemm(512, 256, 384, true, -4, 10, 0);
  do_sgemm(1366, 768, 256, true, -4, 10, 0);
  do_sgemm(1255, 755, 333, true, -4, 10, 0);
  do_sgemm(555, 777, 999, true, -4, 10, 0);
Z
zhaojiaying01 已提交
136 137
  return 0;
}