math_function_test.cc 2.4 KB
Newer Older
Q
qijun 已提交
1 2 3 4
#include "paddle/operators/math/math_function.h"
#include "gtest/gtest.h"

#ifndef PADDLE_ONLY_CPU
Q
qijun 已提交
5
TEST(math_function, notrans_mul_trans) {
Q
qijun 已提交
6 7 8 9 10 11 12
  paddle::framework::Tensor input1;
  paddle::framework::Tensor input1_gpu;
  paddle::framework::Tensor input2_gpu;
  paddle::framework::Tensor out_gpu;
  paddle::framework::Tensor out;

  auto* cpu_place = new paddle::platform::CPUPlace();
Q
qijun 已提交
13 14 15
  float* input1_ptr = input1.mutable_data<float>({2, 3}, *cpu_place);
  float arr[6] = {0, 1, 2, 3, 4, 5};
  memcpy(input1_ptr, arr, 6 * sizeof(float));
Q
qijun 已提交
16 17

  auto* gpu_place = new paddle::platform::GPUPlace(0);
Q
qijun 已提交
18
  paddle::platform::DeviceContext* context =
Q
qijun 已提交
19
      new paddle::platform::CUDADeviceContext(*gpu_place);
Q
qijun 已提交
20 21 22

  input1_gpu.CopyFrom<float>(input1, *gpu_place);
  input2_gpu.CopyFrom<float>(input1, *gpu_place);
Q
qijun 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37

  out_gpu.mutable_data<float>({2, 2}, *gpu_place);

  paddle::operators::math::matmul<paddle::platform::GPUPlace, float>(
      input1_gpu, false, input2_gpu, true, 1, &out_gpu, 0, context);

  out.CopyFrom<float>(out_gpu, *cpu_place);

  float* out_ptr = out.data<float>();
  EXPECT_EQ(out_ptr[0], 5);
  EXPECT_EQ(out_ptr[1], 14);
  EXPECT_EQ(out_ptr[2], 14);
  EXPECT_EQ(out_ptr[3], 50);
}

Q
qijun 已提交
38
TEST(math_function, trans_mul_notrans) {
Q
qijun 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
  paddle::framework::Tensor input1;
  paddle::framework::Tensor input1_gpu;
  paddle::framework::Tensor input2_gpu;
  paddle::framework::Tensor out_gpu;
  paddle::framework::Tensor out;

  auto* cpu_place = new paddle::platform::CPUPlace();
  float* input1_ptr = input1.mutable_data<float>({2, 3}, *cpu_place);
  float arr[6] = {0, 1, 2, 3, 4, 5};
  memcpy(input1_ptr, arr, 6 * sizeof(float));

  auto* gpu_place = new paddle::platform::GPUPlace(0);
  paddle::platform::DeviceContext* context =
      new paddle::platform::CUDADeviceContext(*gpu_place);

  input1_gpu.CopyFrom<float>(input1, *gpu_place);
  input2_gpu.CopyFrom<float>(input1, *gpu_place);

  out_gpu.mutable_data<float>({3, 3}, *gpu_place);
Q
qijun 已提交
58

Q
qijun 已提交
59
  paddle::operators::math::matmul<paddle::platform::GPUPlace, float>(
Q
qijun 已提交
60
      input1_gpu, true, input2_gpu, false, 1, &out_gpu, 0, context);
Q
qijun 已提交
61 62 63 64

  out.CopyFrom<float>(out_gpu, *cpu_place);

  float* out_ptr = out.data<float>();
Q
qijun 已提交
65 66 67 68 69 70 71 72 73
  EXPECT_EQ(out_ptr[0], 9);
  EXPECT_EQ(out_ptr[1], 12);
  EXPECT_EQ(out_ptr[2], 15);
  EXPECT_EQ(out_ptr[3], 12);
  EXPECT_EQ(out_ptr[4], 17);
  EXPECT_EQ(out_ptr[5], 22);
  EXPECT_EQ(out_ptr[6], 15);
  EXPECT_EQ(out_ptr[7], 22);
  EXPECT_EQ(out_ptr[8], 29);
Q
qijun 已提交
74
}
Q
qijun 已提交
75
#endif