concat_test.cc 14.5 KB
Newer Older
C
chengduoZH 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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. */

#include <gtest/gtest.h>
W
wanghuancoder 已提交
16

C
chengduoZH 已提交
17
#include "paddle/fluid/framework/tensor_util.h"
C
chengduo 已提交
18
#include "paddle/fluid/operators/math/concat_and_split.h"
C
chengduoZH 已提交
19

20 21 22 23 24 25 26 27
/**
 * case 1:
 *    inputs:
 *        t_a.shape: [2, 3, 4]
 *        t_b.shape: [3, 3, 4]
 *    output:
 *        out.shape: [5, 3, 4]
 */
C
chengduoZH 已提交
28
template <typename DeviceContext, typename Place>
29
void ConcatCase1(DeviceContext* context) {
30 31 32
  paddle::framework::Tensor input_a_cpu;
  paddle::framework::Tensor input_b_cpu;
  paddle::framework::Tensor out_cpu;
33

34 35 36
  paddle::framework::Tensor input_a;
  paddle::framework::Tensor input_b;
  paddle::framework::Tensor out;
C
chengduoZH 已提交
37

38 39 40
  auto dim_a = paddle::framework::make_ddim({2, 3, 4});
  auto dim_b = paddle::framework::make_ddim({3, 3, 4});
  auto dim_out = paddle::framework::make_ddim({5, 3, 4});
C
chengduoZH 已提交
41 42 43 44 45 46

  input_a.mutable_data<int>(dim_a, Place());
  input_b.mutable_data<int>(dim_b, Place());
  out.mutable_data<int>(dim_out, Place());

  if (paddle::platform::is_gpu_place(Place())) {
47 48 49
    input_a_cpu.mutable_data<int>(dim_a, paddle::platform::CPUPlace());
    input_b_cpu.mutable_data<int>(dim_b, paddle::platform::CPUPlace());
    out_cpu.mutable_data<int>(dim_out, paddle::platform::CPUPlace());
C
chengduoZH 已提交
50 51
  }

52 53
  int* a_ptr = nullptr;
  int* b_ptr = nullptr;
C
chengduoZH 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
  if (paddle::platform::is_gpu_place(Place())) {
    a_ptr = input_a_cpu.data<int>();
    b_ptr = input_b_cpu.data<int>();
  } else {
    a_ptr = input_a.data<int>();
    b_ptr = input_b.data<int>();
  }

  for (int i = 0; i < 2 * 3 * 4; ++i) {
    a_ptr[i] = i;
  }
  for (int i = 0; i < 3 * 3 * 4; ++i) {
    b_ptr[i] = i;
  }

  if (paddle::platform::is_gpu_place(Place())) {
F
fengjiayi 已提交
70 71
    paddle::framework::TensorCopySync(input_a_cpu, Place(), &input_a);
    paddle::framework::TensorCopySync(input_b_cpu, Place(), &input_b);
C
chengduoZH 已提交
72 73
  }

74
  std::vector<paddle::framework::Tensor> input;
C
chengduoZH 已提交
75 76 77 78 79 80 81
  input.push_back(input_a);
  input.push_back(input_b);

  paddle::operators::math::ConcatFunctor<DeviceContext, int> concat_functor;
  concat_functor(*context, input, 0, &out);

  // check the dim of input_a, input_b
82 83 84 85 86 87 88 89 90 91
  PADDLE_ENFORCE_EQ(input_a.dims(), dim_a,
                    paddle::platform::errors::InvalidArgument(
                        "The dims of Input tensor should be the same as the "
                        "declared dims. Tensor dims: [%s], declared dims: [%s]",
                        input_a.dims(), dim_a));
  PADDLE_ENFORCE_EQ(input_b.dims(), dim_b,
                    paddle::platform::errors::InvalidArgument(
                        "The dims of Input tensor should be the same as the "
                        "declared dims. Tensor dims: [%s], declared dims: [%s]",
                        input_b.dims(), dim_b));
C
chengduoZH 已提交
92

93
  int* out_ptr = nullptr;
C
chengduoZH 已提交
94
  if (paddle::platform::is_gpu_place(Place())) {
F
fengjiayi 已提交
95 96
    paddle::framework::TensorCopySync(out, paddle::platform::CPUPlace(),
                                      &out_cpu);
C
chengduoZH 已提交
97 98 99 100 101 102 103 104 105
    out_ptr = out_cpu.data<int>();
  } else {
    out_ptr = out.data<int>();
  }

  int cols = 2 * 3 * 4;
  int idx_a = 0, idx_b = 0;
  for (int j = 0; j < 5 * 3 * 4; ++j) {
    if (j >= cols) {
106 107 108
      PADDLE_ENFORCE_EQ(out_ptr[j], b_ptr[idx_b],
                        paddle::platform::errors::InvalidArgument(
                            "Concat test failed, the result should be equal."));
C
chengduoZH 已提交
109 110
      ++idx_b;
    } else {
111 112 113
      PADDLE_ENFORCE_EQ(out_ptr[j], a_ptr[idx_a],
                        paddle::platform::errors::InvalidArgument(
                            "Concat test failed, the result should be equal."));
C
chengduoZH 已提交
114 115 116
      ++idx_a;
    }
  }
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
}

/**
  * case 2:
  *    inputs:
  *        t_a.shape: [2, 3, 4]
  *        t_b.shape: [2, 4, 4]
  *    output:
  *        out.shape: [2, 7, 4]
  */
template <typename DeviceContext, typename Place>
void ConcatCase2(DeviceContext* context) {
  paddle::framework::Tensor input_a_cpu;
  paddle::framework::Tensor input_b_cpu;
  paddle::framework::Tensor out_cpu;

  paddle::framework::Tensor input_a;
  paddle::framework::Tensor input_b;
  paddle::framework::Tensor out;

  auto dim_a = paddle::framework::make_ddim({2, 3, 4});
  auto dim_b = paddle::framework::make_ddim({2, 4, 4});
  auto dim_out = paddle::framework::make_ddim({2, 7, 4});

  input_a.mutable_data<int>(dim_a, Place());
  input_b.mutable_data<int>(dim_b, Place());
  out.mutable_data<int>(dim_out, Place());

C
chengduoZH 已提交
145
  if (paddle::platform::is_gpu_place(Place())) {
146 147 148
    input_a_cpu.mutable_data<int>(dim_a, paddle::platform::CPUPlace());
    input_b_cpu.mutable_data<int>(dim_b, paddle::platform::CPUPlace());
    out_cpu.mutable_data<int>(dim_out, paddle::platform::CPUPlace());
C
chengduoZH 已提交
149 150
  }

151 152
  int* a_ptr = nullptr;
  int* b_ptr = nullptr;
C
chengduoZH 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
  if (paddle::platform::is_gpu_place(Place())) {
    a_ptr = input_a_cpu.data<int>();
    b_ptr = input_b_cpu.data<int>();
  } else {
    a_ptr = input_a.data<int>();
    b_ptr = input_b.data<int>();
  }

  for (int i = 0; i < 2 * 3 * 4; ++i) {
    a_ptr[i] = i;
  }
  for (int i = 0; i < 2 * 4 * 4; ++i) {
    b_ptr[i] = i;
  }

  if (paddle::platform::is_gpu_place(Place())) {
F
fengjiayi 已提交
169 170
    paddle::framework::TensorCopySync(input_a_cpu, Place(), &input_a);
    paddle::framework::TensorCopySync(input_b_cpu, Place(), &input_b);
C
chengduoZH 已提交
171 172
  }

173
  std::vector<paddle::framework::Tensor> input;
C
chengduoZH 已提交
174 175 176
  input.push_back(input_a);
  input.push_back(input_b);

177
  paddle::operators::math::ConcatFunctor<DeviceContext, int> concat_functor;
C
chengduoZH 已提交
178 179 180
  concat_functor(*context, input, 1, &out);

  // check the dim of input_a, input_b
181 182 183 184 185 186 187 188 189 190
  PADDLE_ENFORCE_EQ(input_a.dims(), dim_a,
                    paddle::platform::errors::InvalidArgument(
                        "The dims of Input tensor should be the same as the "
                        "declared dims. Tensor dims: [%s], declared dims: [%s]",
                        input_a.dims(), dim_a));
  PADDLE_ENFORCE_EQ(input_b.dims(), dim_b,
                    paddle::platform::errors::InvalidArgument(
                        "The dims of Input tensor should be the same as the "
                        "declared dims. Tensor dims: [%s], declared dims: [%s]",
                        input_b.dims(), dim_b));
C
chengduoZH 已提交
191

192
  int* out_ptr = nullptr;
C
chengduoZH 已提交
193
  if (paddle::platform::is_gpu_place(Place())) {
F
fengjiayi 已提交
194 195
    paddle::framework::TensorCopySync(out, paddle::platform::CPUPlace(),
                                      &out_cpu);
C
chengduoZH 已提交
196 197 198 199 200
    out_ptr = out_cpu.data<int>();
  } else {
    out_ptr = out.data<int>();
  }

201 202
  int cols = 3 * 4;
  int idx_a = 0, idx_b = 0;
C
chengduoZH 已提交
203 204 205
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 28; ++j) {
      if (j >= cols) {
206 207 208 209
        PADDLE_ENFORCE_EQ(
            out_ptr[i * 28 + j], b_ptr[idx_b],
            paddle::platform::errors::InvalidArgument(
                "Concat test failed, the result should be equal."));
C
chengduoZH 已提交
210 211
        ++idx_b;
      } else {
212 213 214 215
        PADDLE_ENFORCE_EQ(
            out_ptr[i * 28 + j], a_ptr[idx_a],
            paddle::platform::errors::InvalidArgument(
                "Concat test failed, the result should be equal."));
C
chengduoZH 已提交
216 217 218 219
        ++idx_a;
      }
    }
  }
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
}

/**
  * case 3:
  *    inputs:
  *        t_a.shape: [2, 3, 5]
  *        t_b.shape: [2, 3, 4]
  *    output:
  *        out.shape: [2, 3, 9]
  */
template <typename DeviceContext, typename Place>
void ConcatCase3(DeviceContext* context) {
  paddle::framework::Tensor input_a_cpu;
  paddle::framework::Tensor input_b_cpu;
  paddle::framework::Tensor out_cpu;

  paddle::framework::Tensor input_a;
  paddle::framework::Tensor input_b;
  paddle::framework::Tensor out;

  auto dim_a = paddle::framework::make_ddim({2, 3, 4});
  auto dim_b = paddle::framework::make_ddim({2, 3, 5});
  auto dim_out = paddle::framework::make_ddim({2, 3, 9});

  input_a.mutable_data<int>(dim_a, Place());
  input_b.mutable_data<int>(dim_b, Place());
  out.mutable_data<int>(dim_out, Place());
C
chengduoZH 已提交
247 248

  if (paddle::platform::is_gpu_place(Place())) {
249 250 251
    input_a_cpu.mutable_data<int>(dim_a, paddle::platform::CPUPlace());
    input_b_cpu.mutable_data<int>(dim_b, paddle::platform::CPUPlace());
    out_cpu.mutable_data<int>(dim_out, paddle::platform::CPUPlace());
C
chengduoZH 已提交
252 253
  }

254 255
  int* a_ptr = nullptr;
  int* b_ptr = nullptr;
C
chengduoZH 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
  if (paddle::platform::is_gpu_place(Place())) {
    a_ptr = input_a_cpu.data<int>();
    b_ptr = input_b_cpu.data<int>();
  } else {
    a_ptr = input_a.data<int>();
    b_ptr = input_b.data<int>();
  }

  for (int i = 0; i < 2 * 3 * 4; ++i) {
    a_ptr[i] = i;
  }
  for (int i = 0; i < 2 * 3 * 5; ++i) {
    b_ptr[i] = i;
  }

  if (paddle::platform::is_gpu_place(Place())) {
F
fengjiayi 已提交
272 273
    paddle::framework::TensorCopySync(input_a_cpu, Place(), &input_a);
    paddle::framework::TensorCopySync(input_b_cpu, Place(), &input_b);
C
chengduoZH 已提交
274 275
  }

276
  std::vector<paddle::framework::Tensor> input;
C
chengduoZH 已提交
277 278 279
  input.push_back(input_a);
  input.push_back(input_b);

280
  paddle::operators::math::ConcatFunctor<DeviceContext, int> concat_functor;
C
chengduoZH 已提交
281 282 283
  concat_functor(*context, input, 2, &out);

  // check the dim of input_a, input_b
284 285 286 287 288 289 290 291 292 293
  PADDLE_ENFORCE_EQ(input_a.dims(), dim_a,
                    paddle::platform::errors::InvalidArgument(
                        "The dims of Input tensor should be the same as the "
                        "declared dims. Tensor dims: [%s], declared dims: [%s]",
                        input_a.dims(), dim_a));
  PADDLE_ENFORCE_EQ(input_b.dims(), dim_b,
                    paddle::platform::errors::InvalidArgument(
                        "The dims of Input tensor should be the same as the "
                        "declared dims. Tensor dims: [%s], declared dims: [%s]",
                        input_b.dims(), dim_b));
C
chengduoZH 已提交
294

295
  int* out_ptr = nullptr;
C
chengduoZH 已提交
296
  if (paddle::platform::is_gpu_place(Place())) {
F
fengjiayi 已提交
297 298
    paddle::framework::TensorCopySync(out, paddle::platform::CPUPlace(),
                                      &out_cpu);
C
chengduoZH 已提交
299 300 301 302 303 304
    out_ptr = out_cpu.data<int>();
  } else {
    out_ptr = out.data<int>();
  }

  // check the data
305 306
  int cols = 4;
  int idx_a = 0, idx_b = 0;
C
chengduoZH 已提交
307 308 309
  for (int i = 0; i < 6; ++i) {
    for (int j = 0; j < 9; ++j) {
      if (j >= cols) {
310 311 312 313
        PADDLE_ENFORCE_EQ(
            out_ptr[i * 9 + j], b_ptr[idx_b],
            paddle::platform::errors::InvalidArgument(
                "Concat test failed, the result should be equal."));
C
chengduoZH 已提交
314 315
        ++idx_b;
      } else {
316 317 318 319
        PADDLE_ENFORCE_EQ(
            out_ptr[i * 9 + j], a_ptr[idx_a],
            paddle::platform::errors::InvalidArgument(
                "Concat test failed, the result should be equal."));
C
chengduoZH 已提交
320 321 322 323
        ++idx_a;
      }
    }
  }
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
}

/**
  * case 4:
  *    inputs:
  *        axis = 1
  *        t_a.shape: [2, 3, 4]
  *        t_b.shape: [2, 3, 4]
  *    output:
  *        out.shape: [2, 6, 4]
  */
template <typename DeviceContext, typename Place>
void ConcatCase4(DeviceContext* context) {
  paddle::framework::Tensor input_a_cpu;
  paddle::framework::Tensor input_b_cpu;
  paddle::framework::Tensor out_cpu;

  paddle::framework::Tensor input_a;
  paddle::framework::Tensor input_b;
  paddle::framework::Tensor out;

  auto dim_a = paddle::framework::make_ddim({2, 3, 4});
  auto dim_b = paddle::framework::make_ddim({2, 3, 4});
  auto dim_out = paddle::framework::make_ddim({2, 6, 4});

  input_a.mutable_data<int>(dim_a, Place());
  input_b.mutable_data<int>(dim_b, Place());
  out.mutable_data<int>(dim_out, Place());
C
chengduoZH 已提交
352 353

  if (paddle::platform::is_gpu_place(Place())) {
354 355 356
    input_a_cpu.mutable_data<int>(dim_a, paddle::platform::CPUPlace());
    input_b_cpu.mutable_data<int>(dim_b, paddle::platform::CPUPlace());
    out_cpu.mutable_data<int>(dim_out, paddle::platform::CPUPlace());
C
chengduoZH 已提交
357 358
  }

359 360
  int* a_ptr = nullptr;
  int* b_ptr = nullptr;
C
chengduoZH 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
  if (paddle::platform::is_gpu_place(Place())) {
    a_ptr = input_a_cpu.data<int>();
    b_ptr = input_b_cpu.data<int>();
  } else {
    a_ptr = input_a.data<int>();
    b_ptr = input_b.data<int>();
  }

  for (int i = 0; i < 2 * 3 * 4; ++i) {
    a_ptr[i] = i;
  }
  for (int i = 0; i < 2 * 3 * 4; ++i) {
    b_ptr[i] = i;
  }

  if (paddle::platform::is_gpu_place(Place())) {
F
fengjiayi 已提交
377 378
    paddle::framework::TensorCopySync(input_a_cpu, Place(), &input_a);
    paddle::framework::TensorCopySync(input_b_cpu, Place(), &input_b);
C
chengduoZH 已提交
379 380
  }

381
  std::vector<paddle::framework::Tensor> input;
C
chengduoZH 已提交
382 383 384
  input.push_back(input_a);
  input.push_back(input_b);

385
  paddle::operators::math::ConcatFunctor<DeviceContext, int> concat_functor;
C
chengduoZH 已提交
386
  concat_functor(*context, input, 1, &out);
387
  context->Wait();
C
chengduoZH 已提交
388 389

  // check the dim of input_a, input_b
390 391 392 393 394 395 396 397 398 399
  PADDLE_ENFORCE_EQ(input_a.dims(), dim_a,
                    paddle::platform::errors::InvalidArgument(
                        "The dims of Input tensor should be the same as the "
                        "declared dims. Tensor dims: [%s], declared dims: [%s]",
                        input_a.dims(), dim_a));
  PADDLE_ENFORCE_EQ(input_b.dims(), dim_b,
                    paddle::platform::errors::InvalidArgument(
                        "The dims of Input tensor should be the same as the "
                        "declared dims. Tensor dims: [%s], declared dims: [%s]",
                        input_b.dims(), dim_b));
C
chengduoZH 已提交
400

401
  int* out_ptr = nullptr;
C
chengduoZH 已提交
402
  if (paddle::platform::is_gpu_place(Place())) {
F
fengjiayi 已提交
403 404
    paddle::framework::TensorCopySync(out, paddle::platform::CPUPlace(),
                                      &out_cpu);
C
chengduoZH 已提交
405 406 407 408 409 410
    out_ptr = out_cpu.data<int>();
  } else {
    out_ptr = out.data<int>();
  }

  // check the data
411 412
  int cols = 12;
  int idx_a = 0, idx_b = 0;
C
chengduoZH 已提交
413 414 415
  for (int i = 0; i < 2; ++i) {
    for (int j = 0; j < 24; ++j) {
      if (j >= cols) {
416 417 418 419
        PADDLE_ENFORCE_EQ(
            out_ptr[i * 24 + j], b_ptr[idx_b],
            paddle::platform::errors::InvalidArgument(
                "Concat test failed, the result should be equal."));
C
chengduoZH 已提交
420 421
        ++idx_b;
      } else {
422 423 424 425
        PADDLE_ENFORCE_EQ(
            out_ptr[i * 24 + j], a_ptr[idx_a],
            paddle::platform::errors::InvalidArgument(
                "Concat test failed, the result should be equal."));
C
chengduoZH 已提交
426 427 428 429
        ++idx_a;
      }
    }
  }
C
chengduoZH 已提交
430 431
}

432 433 434 435 436 437 438 439 440 441
template <typename DeviceContext, typename Place>
void TestConcatMain() {
  DeviceContext* context = new DeviceContext(Place());

  ConcatCase1<DeviceContext, Place>(context);
  ConcatCase2<DeviceContext, Place>(context);
  ConcatCase3<DeviceContext, Place>(context);
  ConcatCase4<DeviceContext, Place>(context);
}

C
chengduoZH 已提交
442
TEST(math, concat) {
443 444
  TestConcatMain<paddle::platform::CPUDeviceContext,
                 paddle::platform::CPUPlace>();
C
chengduoZH 已提交
445
#ifdef PADDLE_WITH_CUDA
446 447
  TestConcatMain<paddle::platform::CUDADeviceContext,
                 paddle::platform::CUDAPlace>();
C
chengduoZH 已提交
448 449
#endif
}