test_gemm_accuracy.cpp 3.7 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"
21
#include "operators/math/gemm/cblas.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)]

Z
ZhenWang 已提交
28
void print_matrix(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 40 41 42 43 44
int do_sgemm(int m, int n, int k, int pr) {
  const float alpha = 1.f;
  const float beta = 0.f;
  const int lda = k;
  const int ldb = n;
  const int ldc = n;
Z
zhaojiaying01 已提交
45

Y
yanantao78 已提交
46 47 48 49 50 51 52 53
  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));
Y
do trim  
yanantao78 已提交
54

55 56 57 58 59
  std::mt19937 rng(111);
  std::uniform_real_distribution<double> uniform_dist(0, 1);
  const float lower = -10.f;
  const float upper = 10.f;

Z
zhaojiaying01 已提交
60
  for (int i = 0; i < m * k; ++i) {
61
    a[i] = static_cast<float>(uniform_dist(rng) * (upper - lower) + lower);
Z
zhaojiaying01 已提交
62 63
  }
  for (int i = 0; i < k * n; ++i) {
64
    b[i] = static_cast<float>(uniform_dist(rng) * (upper - lower) + lower);
Z
zhaojiaying01 已提交
65
  }
66
  memcpy(c, c1, sizeof(float) * m * n);
Y
do trim  
yanantao78 已提交
67

Y
yanantao78 已提交
68 69 70 71 72 73
  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);
      }
74
      c1(i, j) = alpha * r;
Z
zhaojiaying01 已提交
75 76
    }
  }
Y
do trim  
yanantao78 已提交
77

78 79 80 81 82
  std::cout << "run cblas_sgemm..." << std::endl;
  paddle_mobile::operators::math::cblas_sgemm(false, false, m, n, k, alpha, a,
                                              lda, b, ldb, 0.f, c, ldc);

  std::cout << "compare results..." << std::endl;
Z
zhaojiaying01 已提交
83
  for (int i = 0; i < m * n; ++i) {
84 85 86 87
    if (abs(c[i] - c1[i]) >= 1e-2) {
      std::cout << "c[" << i << "] != c1[" << i << "]: " << c[i] << " vs "
                << c1[i] << std::endl;
      exit(1);
Z
zhaojiaying01 已提交
88 89
    }
  }
Y
do trim  
yanantao78 已提交
90

Y
yanantao78 已提交
91 92
  if (pr > 0) {
    std::cout << "A:" << std::endl;
Z
ZhenWang 已提交
93
    print_matrix(m, k, lda, a);
Y
yanantao78 已提交
94
    std::cout << "B:" << std::endl;
Z
ZhenWang 已提交
95
    print_matrix(k, n, ldb, b);
Y
yanantao78 已提交
96
    std::cout << "C:" << std::endl;
Z
ZhenWang 已提交
97
    print_matrix(m, n, ldc, c);
Y
yanantao78 已提交
98
    std::cout << "C1:" << std::endl;
Z
ZhenWang 已提交
99
    print_matrix(m, n, ldc, c1);
Y
yanantao78 已提交
100
  }
Y
do trim  
yanantao78 已提交
101

Y
yanantao78 已提交
102 103 104 105
  paddle_mobile::memory::Free(a);
  paddle_mobile::memory::Free(b);
  paddle_mobile::memory::Free(c);
  paddle_mobile::memory::Free(c1);
Y
do trim  
yanantao78 已提交
106

Y
yanantao78 已提交
107 108 109
  return 0;
}

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
int main(int argc, char *argv[]) {
  do_sgemm(1, 1, 1, 1);

  do_sgemm(9, 9, 1, 1);
  do_sgemm(999, 99, 1, 0);
  do_sgemm(999, 1, 1, 0);
  do_sgemm(1, 9, 9, 1);
  do_sgemm(1, 99, 999, 0);
  do_sgemm(1, 1, 999, 0);

  do_sgemm(9, 9, 9, 1);
  do_sgemm(10, 6, 12, 1);
  do_sgemm(512, 256, 384, 0);
  do_sgemm(1366, 768, 256, 0);
  do_sgemm(1255, 755, 333, 0);
  do_sgemm(555, 777, 999, 0);

  do_sgemm(10, 6, 12, 1);
  do_sgemm(512, 256, 384, 0);
  do_sgemm(1366, 768, 256, 0);
  do_sgemm(1255, 755, 333, 0);
  do_sgemm(555, 777, 999, 0);

Z
zhaojiaying01 已提交
133 134
  return 0;
}