test_math_function.cc 11.2 KB
Newer Older
1
//  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2 3 4 5 6 7 8 9 10 11 12 13
//
// 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.
14

Q
qijun 已提交
15
#include "gtest/gtest.h"
16 17
#include "paddle/phi/kernels/funcs/blas/blas.h"
#include "paddle/phi/kernels/funcs/math_function.h"
18

19
namespace phi {
20
namespace tests {
Q
qijun 已提交
21

Y
Yu Yang 已提交
22
template <typename T>
23
inline phi::funcs::BlasT<paddle::platform::CPUDeviceContext, T> GetBlas(
24
    const paddle::platform::CPUDeviceContext& context) {
25
  return phi::funcs::GetBlas<paddle::platform::CPUDeviceContext, T>(context);
Y
Yu Yang 已提交
26 27
}

G
guosheng 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
TEST(math_function, gemm_notrans_cblas) {
  paddle::framework::Tensor input1;
  paddle::framework::Tensor input2;
  paddle::framework::Tensor input3;

  int m = 2;
  int n = 3;
  int k = 3;
  auto* cpu_place = new paddle::platform::CPUPlace();
  float* input1_ptr = input1.mutable_data<float>({2, 3}, *cpu_place);
  float arr1[6] = {0, 1, 2, 3, 4, 5};
  memcpy(input1_ptr, arr1, 6 * sizeof(float));
  float* input2_ptr = input2.mutable_data<float>({3, 4}, *cpu_place);
  float arr2[12] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
  memcpy(input2_ptr, arr2, 12 * sizeof(float));
  float* input3_ptr = input3.mutable_data<float>({2, 4}, *cpu_place);
  float arr3[8] = {0, 1, 2, 3, 4, 5, 6, 7};
  memcpy(input3_ptr, arr3, 8 * sizeof(float));

  paddle::platform::CPUDeviceContext context(*cpu_place);
48 49 50 51 52 53 54 55 56 57 58 59 60
  GetBlas<float>(context).GEMM(false,
                               false,
                               m,
                               n,
                               k,
                               1,
                               input1_ptr,
                               3,
                               input2_ptr + 1,
                               4,
                               1,
                               input3_ptr + 1,
                               4);
G
guosheng 已提交
61 62 63 64 65 66 67 68 69 70

  EXPECT_EQ(input3_ptr[0], 0);
  EXPECT_EQ(input3_ptr[1], 24);
  EXPECT_EQ(input3_ptr[2], 28);
  EXPECT_EQ(input3_ptr[3], 32);
  EXPECT_EQ(input3_ptr[4], 4);
  EXPECT_EQ(input3_ptr[5], 73);
  EXPECT_EQ(input3_ptr[6], 86);
  EXPECT_EQ(input3_ptr[7], 99);
}
T
tensor-tang 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
#ifdef PADDLE_WITH_LIBXSMM
template <typename T>
void MklSmmCompare(int m, int n, int k) {
  paddle::framework::Tensor mat_a;
  paddle::framework::Tensor mat_b;
  paddle::framework::Tensor mat_c_smm;
  paddle::framework::Tensor mat_c_mkl;
  auto* cpu_place = new paddle::platform::CPUPlace();

  T* A = mat_a.mutable_data<T>({m, k}, *cpu_place);
  T* B = mat_b.mutable_data<T>({k, n}, *cpu_place);
  T* CSMM = mat_c_smm.mutable_data<T>({m, n}, *cpu_place);
  T* CMKL = mat_c_mkl.mutable_data<T>({m, n}, *cpu_place);
  T alpha = static_cast<T>(1);
  T beta = static_cast<T>(0);
  for (int i = 0; i < mat_a.numel(); ++i) {
    A[i] = static_cast<T>(i);
  }
  for (int i = 0; i < mat_b.numel(); ++i) {
    B[i] = static_cast<T>(i);
  }
T
tensor-tang 已提交
92 93 94 95
  // lda,ldb,ldc follow RowMajor
  int lda = k;
  int ldb = n;
  int ldc = n;
T
tensor-tang 已提交
96

T
tensor-tang 已提交
97
  auto smm = [&, m, n, k, lda, ldb, ldc, alpha, beta]() {
T
tensor-tang 已提交
98 99
    const char transa = 'N';
    const char transb = 'N';
100 101 102 103 104 105 106 107 108 109 110 111 112
    phi::funcs::CBlas<T>::SMM_GEMM(&transa,
                                   &transb,
                                   &n,
                                   &m,
                                   &k,
                                   &alpha,
                                   B,
                                   &ldb,
                                   A,
                                   &lda,
                                   &beta,
                                   CSMM,
                                   &ldc);
T
tensor-tang 已提交
113 114
  };

T
tensor-tang 已提交
115
  auto mkl = [&, m, n, k, lda, ldb, ldc, alpha, beta]() {
116 117 118 119 120 121 122 123 124 125 126 127 128 129
    phi::funcs::CBlas<T>::GEMM(CblasRowMajor,
                               CblasNoTrans,
                               CblasNoTrans,
                               m,
                               n,
                               k,
                               alpha,
                               A,
                               lda,
                               B,
                               ldb,
                               beta,
                               CMKL,
                               ldc);
T
tensor-tang 已提交
130
  };
T
tensor-tang 已提交
131

T
tensor-tang 已提交
132 133 134 135 136 137 138 139 140 141
  smm();
  mkl();
  ASSERT_EQ(mat_c_mkl.numel(), mat_c_smm.numel());
  for (int i = 0; i < mat_c_mkl.numel(); ++i) {
    EXPECT_FLOAT_EQ(CSMM[i], CMKL[i]);
  }
}
TEST(math_function, gemm_mkl_vs_smm) {
  MklSmmCompare<float>(1, 2, 3);
  MklSmmCompare<double>(1, 2, 3);
T
tensor-tang 已提交
142 143
  MklSmmCompare<float>(3, 2, 1);
  MklSmmCompare<double>(3, 2, 1);
T
tensor-tang 已提交
144 145 146 147
  MklSmmCompare<float>(3, 8, 5);
  MklSmmCompare<double>(3, 8, 5);
}
#endif
G
guosheng 已提交
148

T
tensor-tang 已提交
149
TEST(math_function, gemm_trans_cblas) {
G
guosheng 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
  paddle::framework::Tensor input1;
  paddle::framework::Tensor input2;
  paddle::framework::Tensor input3;

  int m = 2;
  int n = 3;
  int k = 3;
  auto* cpu_place = new paddle::platform::CPUPlace();
  float* input1_ptr = input1.mutable_data<float>({2, 3}, *cpu_place);
  float arr1[6] = {0, 1, 2, 3, 4, 5};
  memcpy(input1_ptr, arr1, 6 * sizeof(float));
  float* input2_ptr = input2.mutable_data<float>({4, 3}, *cpu_place);
  float arr2[12] = {0, 4, 8, 1, 5, 9, 2, 6, 10, 3, 7, 11};
  memcpy(input2_ptr, arr2, 12 * sizeof(float));
  float* input3_ptr = input3.mutable_data<float>({2, 4}, *cpu_place);
  float arr3[8] = {0, 1, 2, 3, 4, 5, 6, 7};
  memcpy(input3_ptr, arr3, 8 * sizeof(float));

  paddle::platform::CPUDeviceContext context(*cpu_place);
169 170 171 172 173 174 175 176 177 178 179 180 181
  GetBlas<float>(context).GEMM(false,
                               true,
                               m,
                               n,
                               k,
                               1,
                               input1_ptr,
                               3,
                               input2_ptr + 3,
                               3,
                               1,
                               input3_ptr + 1,
                               4);
182 183
  delete cpu_place;
  cpu_place = NULL;
G
guosheng 已提交
184 185 186 187 188 189 190 191 192 193

  EXPECT_EQ(input3_ptr[0], 0);
  EXPECT_EQ(input3_ptr[1], 24);
  EXPECT_EQ(input3_ptr[2], 28);
  EXPECT_EQ(input3_ptr[3], 32);
  EXPECT_EQ(input3_ptr[4], 4);
  EXPECT_EQ(input3_ptr[5], 73);
  EXPECT_EQ(input3_ptr[6], 86);
  EXPECT_EQ(input3_ptr[7], 99);
}
194 195 196 197 198 199

TEST(math_function, zero) {
  paddle::framework::Tensor tensor;
  auto* cpu_place = new paddle::platform::CPUPlace();
  float* t = tensor.mutable_data<float>({2, 2}, *cpu_place);
  paddle::platform::CPUDeviceContext context(*cpu_place);
200
  phi::funcs::SetConstant<paddle::platform::CPUDeviceContext, float> functor;
Q
qijun 已提交
201
  functor(context, &tensor, 0);
202 203 204 205 206
  EXPECT_EQ(t[0], 0);
  EXPECT_EQ(t[1], 0);
  EXPECT_EQ(t[2], 0);
  EXPECT_EQ(t[3], 0);

Q
qijun 已提交
207
  functor(context, &tensor, 1);
208 209 210 211 212 213

  EXPECT_EQ(t[0], 1);
  EXPECT_EQ(t[1], 1);
  EXPECT_EQ(t[2], 1);
  EXPECT_EQ(t[3], 1);
}
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

template <typename T>
void GemvTest(int m, int n, bool trans) {
  paddle::framework::Tensor mat_a;
  paddle::framework::Tensor vec_b;
  paddle::framework::Tensor vec_c;
  auto* cpu_place = new paddle::platform::CPUPlace();
  int b_num = trans ? m : n;
  int c_num = trans ? n : m;

  T* data_a = mat_a.mutable_data<T>({m, n}, *cpu_place);
  T* data_b = vec_b.mutable_data<T>({b_num}, *cpu_place);
  T* data_c = vec_c.mutable_data<T>({c_num}, *cpu_place);
  for (int i = 0; i < mat_a.numel(); ++i) {
    data_a[i] = static_cast<T>(i);
  }
  for (int i = 0; i < vec_b.numel(); ++i) {
    data_b[i] = static_cast<T>(i);
  }

  paddle::platform::CPUDeviceContext context(*cpu_place);
235 236 237 238 239 240 241 242
  GetBlas<T>(context).GEMV(trans,
                           static_cast<int>(m),
                           static_cast<int>(n),
                           1.,
                           data_a,
                           data_b,
                           0.,
                           data_c);
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260

  if (!trans) {
    for (int i = 0; i < m; ++i) {
      T sum = 0.0;
      for (int j = 0; j < n; ++j) {
        sum += data_a[i * n + j] * data_b[j];
      }
      ASSERT_FLOAT_EQ(data_c[i], sum);
    }
  } else {
    for (int i = 0; i < n; ++i) {
      T sum = 0.0;
      for (int j = 0; j < m; ++j) {
        sum += data_a[j * n + i] * data_b[j];
      }
      ASSERT_FLOAT_EQ(data_c[i], sum);
    }
  }
261
  delete cpu_place;
262 263 264 265 266 267 268 269
}

TEST(math_function, gemv) {
  GemvTest<float>(3, 13, false);
  GemvTest<double>(4, 5, false);
  GemvTest<float>(12, 7, true);
  GemvTest<double>(7, 9, true);
}
270 271 272 273 274 275

TEST(math_funciton, set_constant) {
  paddle::framework::Tensor t;
  t.Resize({10, 10});
  t.mutable_data<int>(paddle::platform::CPUPlace());
  auto* ctx = new paddle::platform::CPUDeviceContext();
276
  phi::funcs::set_constant(*ctx, &t, 10);
277
  for (int64_t i = 0; i < t.numel(); ++i) {
278 279
    PADDLE_ENFORCE_EQ(10,
                      t.data<int>()[i],
280
                      paddle::platform::errors::InvalidArgument(
281 282
                          "Each value of input tensor should be 10, "
                          "but received %d.",
283
                          t.data<int>()[i]));
284 285 286
  }
  delete ctx;
}
T
tensor-tang 已提交
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314

template <typename T>
void GemmWarpTest(int m, int n, int k, T alpha, T beta) {
  paddle::framework::Tensor mat_a;
  paddle::framework::Tensor mat_b;
  paddle::framework::Tensor mat_c_ref;
  paddle::framework::Tensor mat_c_mkl;
  auto* cpu_place = new paddle::platform::CPUPlace();

  T* A = mat_a.mutable_data<T>({m, k}, *cpu_place);
  T* B = mat_b.mutable_data<T>({k, n}, *cpu_place);
  T* CREF = mat_c_ref.mutable_data<T>({m, n}, *cpu_place);
  T* CMKL = mat_c_mkl.mutable_data<T>({m, n}, *cpu_place);

  ASSERT_EQ(mat_c_mkl.numel(), mat_c_ref.numel());
  for (int i = 0; i < mat_a.numel(); ++i) {
    A[i] = static_cast<T>(i);
  }
  for (int i = 0; i < mat_b.numel(); ++i) {
    B[i] = static_cast<T>(i + 1);
  }
  for (int i = 0; i < mat_c_ref.numel(); ++i) {
    CREF[i] = static_cast<T>(i + 2);
    CMKL[i] = CREF[i];
  }

  // this would call gemm_warp
  paddle::platform::CPUDeviceContext context(*cpu_place);
315 316
  GetBlas<T>(context).GEMM(
      CblasNoTrans, CblasNoTrans, m, n, k, alpha, A, B, beta, CREF);
T
tensor-tang 已提交
317 318 319 320 321

  // lda,ldb,ldc follow RowMajor
  int lda = k;
  int ldb = n;
  int ldc = n;
322 323 324 325 326 327 328 329 330 331 332 333 334 335
  phi::funcs::CBlas<T>::GEMM(CblasRowMajor,
                             CblasNoTrans,
                             CblasNoTrans,
                             m,
                             n,
                             k,
                             alpha,
                             A,
                             lda,
                             B,
                             ldb,
                             beta,
                             CMKL,
                             ldc);
T
tensor-tang 已提交
336 337 338 339

  for (int i = 0; i < mat_c_mkl.numel(); ++i) {
    EXPECT_FLOAT_EQ(CREF[i], CMKL[i]);
  }
340
  delete cpu_place;
T
tensor-tang 已提交
341 342 343 344 345 346 347 348 349 350 351 352
}

TEST(math_function, gemm_warp) {
  GemmWarpTest<float>(3, 2, 5, 1.f, 0.f);
  GemmWarpTest<float>(3, 2, 5, 2.f, 1.f);
  GemmWarpTest<float>(8, 5, 6, 1.f, 0.f);
  GemmWarpTest<float>(8, 5, 6, 2.f, 1.f);
  GemmWarpTest<double>(3, 2, 5, 1.0, 0.0);
  GemmWarpTest<double>(3, 2, 5, 2.0, 1.0);
  GemmWarpTest<double>(8, 5, 6, 1.0, 0.0);
  GemmWarpTest<double>(8, 5, 6, 2.0, 1.0);
}
353 354

}  // namespace tests
355
}  // namespace phi