math_function_test.cu 8.8 KB
Newer Older
D
dzhwinter 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
//  Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
//
// 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.
Q
qijun 已提交
14
#include "gtest/gtest.h"
Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/math/math_function.h"
Q
qijun 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28

TEST(math_function, notrans_mul_trans) {
  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));

D
dzhwinter 已提交
29
  auto* gpu_place = new paddle::platform::CUDAPlace(0);
Q
qijun 已提交
30 31
  paddle::platform::CUDADeviceContext context(*gpu_place);

32 33
  paddle::framework::Copy(input1, *gpu_place, context, &input1_gpu);
  paddle::framework::Copy(input1, *gpu_place, context, &input2_gpu);
Q
qijun 已提交
34 35 36

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

Q
QI JUN 已提交
37
  paddle::operators::math::matmul<paddle::platform::CUDADeviceContext, float>(
Q
qijun 已提交
38 39
      context, input1_gpu, false, input2_gpu, true, 1, &out_gpu, 0);

40
  paddle::framework::Copy(out_gpu, *cpu_place, context, &out);
Q
qijun 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

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

TEST(math_function, trans_mul_notrans) {
  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));

D
dzhwinter 已提交
63
  auto* gpu_place = new paddle::platform::CUDAPlace(0);
Q
qijun 已提交
64 65
  paddle::platform::CUDADeviceContext context(*gpu_place);

66 67
  paddle::framework::Copy(input1, *gpu_place, context, &input1_gpu);
  paddle::framework::Copy(input1, *gpu_place, context, &input2_gpu);
Q
qijun 已提交
68 69 70

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

Q
QI JUN 已提交
71
  paddle::operators::math::matmul<paddle::platform::CUDADeviceContext, float>(
Q
qijun 已提交
72 73
      context, input1_gpu, true, input2_gpu, false, 1, &out_gpu, 0);

74
  paddle::framework::Copy(out_gpu, *cpu_place, context, &out);
Q
qijun 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

  float* out_ptr = out.data<float>();
  context.Wait();
  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);
  delete gpu_place;
}

TEST(math_function, gemm_notrans_cublas) {
  paddle::framework::Tensor input1;
  paddle::framework::Tensor input2;
  paddle::framework::Tensor input3;
  paddle::framework::Tensor input1_gpu;
  paddle::framework::Tensor input2_gpu;
  paddle::framework::Tensor input3_gpu;

  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));

D
dzhwinter 已提交
112
  auto* gpu_place = new paddle::platform::CUDAPlace(0);
Q
qijun 已提交
113 114
  paddle::platform::CUDADeviceContext context(*gpu_place);

115 116 117
  paddle::framework::Copy(input1, *gpu_place, context, &input1_gpu);
  paddle::framework::Copy(input2, *gpu_place, context, &input2_gpu);
  paddle::framework::Copy(input3, *gpu_place, context, &input3_gpu);
Q
qijun 已提交
118 119 120 121
  float* a = input1_gpu.data<float>();
  float* b = input2_gpu.data<float>();
  float* c = input3_gpu.mutable_data<float>(*gpu_place);

Q
QI JUN 已提交
122
  paddle::operators::math::gemm<paddle::platform::CUDADeviceContext, float>(
Q
qijun 已提交
123 124
      context, false, false, m, n, k, 1, a, 3, b + 1, 4, 1, c + 1, 4);

125
  paddle::framework::Copy(input3_gpu, *cpu_place, context, &input3);
Q
qijun 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166

  // numpy code:
  // a = np.arange(6).reshape(2, 3)
  // b = np.arange(12).reshape(3, 4)[:, 1:]
  // c = np.arange(8).reshape(2, 4)[:, 1:]
  // out = np.arange(8).reshape(2, 4)
  // out[:, 1:] = np.dot(a, b) + c
  context.Wait();
  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);
  delete gpu_place;
}

TEST(math_function, gemm_trans_cublas) {
  paddle::framework::Tensor input1;
  paddle::framework::Tensor input2;
  paddle::framework::Tensor input3;
  paddle::framework::Tensor input1_gpu;
  paddle::framework::Tensor input2_gpu;
  paddle::framework::Tensor input3_gpu;

  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));

D
dzhwinter 已提交
167
  auto* gpu_place = new paddle::platform::CUDAPlace(0);
Q
qijun 已提交
168 169
  paddle::platform::CUDADeviceContext context(*gpu_place);

170 171 172
  paddle::framework::Copy(input1, *gpu_place, context, &input1_gpu);
  paddle::framework::Copy(input2, *gpu_place, context, &input2_gpu);
  paddle::framework::Copy(input3, *gpu_place, context, &input3_gpu);
Q
qijun 已提交
173 174 175 176
  float* a = input1_gpu.data<float>();
  float* b = input2_gpu.data<float>();
  float* c = input3_gpu.mutable_data<float>(*gpu_place);

Q
QI JUN 已提交
177
  paddle::operators::math::gemm<paddle::platform::CUDADeviceContext, float>(
Q
qijun 已提交
178 179
      context, false, true, m, n, k, 1, a, 3, b + 3, 3, 1, c + 1, 4);

180
  paddle::framework::Copy(input3_gpu, *cpu_place, context, &input3);
Q
qijun 已提交
181 182 183 184 185 186 187 188 189 190 191 192
  context.Wait();

  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);
  delete gpu_place;
}
193 194 195 196 197 198 199 200 201 202 203 204

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();

  T* data_a = mat_a.mutable_data<T>({m, n}, *cpu_place);
  T* data_b = vec_b.mutable_data<T>({trans ? m : n}, *cpu_place);
  T* data_c = vec_c.mutable_data<T>({trans ? n : m}, *cpu_place);

D
dzhwinter 已提交
205
  auto* gpu_place = new paddle::platform::CUDAPlace(0);
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
  paddle::framework::Tensor g_mat_a;
  paddle::framework::Tensor g_vec_b;
  paddle::framework::Tensor g_vec_c;
  T* g_data_a = g_mat_a.mutable_data<T>(mat_a.dims(), *gpu_place);
  T* g_data_b = g_vec_b.mutable_data<T>(vec_b.dims(), *gpu_place);
  T* g_data_c = g_vec_c.mutable_data<T>(vec_c.dims(), *gpu_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::CUDADeviceContext context(*gpu_place);
221 222
  paddle::framework::Copy(mat_a, *gpu_place, context, &g_mat_a);
  paddle::framework::Copy(vec_b, *gpu_place, context, &g_vec_b);
223

Q
QI JUN 已提交
224
  paddle::operators::math::gemv<paddle::platform::CUDADeviceContext, T>(
225 226 227
      context, trans, static_cast<int>(m), static_cast<int>(n), 1., g_data_a,
      g_data_b, 0., g_data_c);

228 229
  paddle::framework::Copy(g_vec_c, paddle::platform::CPUPlace(), context,
                          &vec_c);
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255

  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);
    }
  }
}

TEST(math_function, gemv) {
  GemvTest<float>(3, 13, false);
  GemvTest<double>(3, 13, false);
  GemvTest<float>(3, 13, true);
  GemvTest<double>(3, 13, true);
}