data_transform_test.cc 3.1 KB
Newer Older
Q
Qiao Longfei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2016 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. */
D
dzhwinter 已提交
14 15
#include <array>
#include <vector>
Q
Qiao Longfei 已提交
16 17 18

#include <gtest/gtest.h>

D
dzhwinter 已提交
19 20
#include "paddle/framework/data_transform.h"

Q
Qiao Longfei 已提交
21 22 23 24
namespace paddle {
namespace framework {
using namespace platform;

D
dzhwinter 已提交
25 26 27 28 29 30 31 32 33
/**
 * @brief cross validation of different kernel type transform
 *  We use four bit map represent different combination.
 *  If the field has multiple possible value, only choose two of them.
 *  For DataType, only test the FP32(float), FP64(double).
 *  e.g. 0000 -> FP32, CPUPlace, kNHWC, kPlain
 *       1111 -> FP64, GPUPlace, kNCHW, kMKLDNN
 */

Q
Qiao Longfei 已提交
34 35
std::array<proto::DataType, 2> kDataType = {
    {proto::DataType::FP32, proto::DataType::FP64}};
D
dzhwinter 已提交
36

Q
Qiao Longfei 已提交
37
std::array<Place, 2> kPlace = {{CPUPlace(), CUDAPlace(0)}};
D
dzhwinter 已提交
38 39

std::array<DataLayout, 2> kDataLayout = {
Q
Qiao Longfei 已提交
40
    {DataLayout::kNHWC, DataLayout::kNCHW}};
D
dzhwinter 已提交
41 42

std::array<LibraryType, 2> kLibraryType = {
Q
Qiao Longfei 已提交
43
    {LibraryType::kPlain, LibraryType::kMKLDNN}};
D
dzhwinter 已提交
44 45 46 47 48 49

OpKernelType GenFromBit(const std::vector<bool> bits) {
  return OpKernelType(kDataType[bits[0]], kPlace[bits[1]], kDataLayout[bits[2]],
                      kLibraryType[bits[3]]);
}

Q
Qiao Longfei 已提交
50 51
int test_value = 0;

D
dzhwinter 已提交
52 53 54 55
auto kernel0 = GenFromBit({0, 0, 0, 0});
auto kernel1 = GenFromBit({0, 0, 0, 1});
auto kernel2 = GenFromBit({0, 0, 1, 0});
auto kernel3 = GenFromBit({0, 0, 1, 1});
Q
Qiao Longfei 已提交
56

57 58
void TransDataType_t(const platform::DeviceContext* ctx, const Variable& in,
                     Variable* out) {
Q
Qiao Longfei 已提交
59 60 61
  test_value++;
}

62 63
void TransDataLayout_t(const platform::DeviceContext* ctx, const Variable& in,
                       Variable* out) {
Q
Qiao Longfei 已提交
64 65 66
  test_value--;
}

67 68
void TransLibraryType_t(const platform::DeviceContext* ctx, const Variable& in,
                        Variable* out) {
Q
Qiao Longfei 已提交
69 70 71 72 73 74 75 76
  test_value += 2;
}

}  // namespace framework
}  // namespace paddle

namespace frw = paddle::framework;

D
dzhwinter 已提交
77 78 79
REGISTER_DATA_TRANSFORM_FN(frw::kernel0, frw::kernel1, frw::TransDataType_t);
REGISTER_DATA_TRANSFORM_FN(frw::kernel1, frw::kernel2, frw::TransDataLayout_t);
REGISTER_DATA_TRANSFORM_FN(frw::kernel0, frw::kernel2, frw::TransLibraryType_t);
Q
Qiao Longfei 已提交
80 81 82 83 84 85

TEST(DataTransform, Register) {
  using namespace paddle::framework;
  using namespace paddle::platform;

  auto& instance = DataTransformFnMap::Instance();
86 87
  ASSERT_EQ(instance.Map().size(), 3UL);
  DeviceContext* ctx = nullptr;
Q
Qiao Longfei 已提交
88 89 90
  paddle::framework::Variable in;
  paddle::framework::Variable out;

D
dzhwinter 已提交
91
  instance.Get(std::make_pair(frw::kernel0, frw::kernel1))(ctx, in, &out);
Q
Qiao Longfei 已提交
92
  ASSERT_EQ(test_value, 1);
D
dzhwinter 已提交
93 94

  instance.Get(std::make_pair(frw::kernel1, frw::kernel2))(ctx, in, &out);
Q
Qiao Longfei 已提交
95
  ASSERT_EQ(test_value, 0);
D
dzhwinter 已提交
96 97

  instance.Get(std::make_pair(frw::kernel0, frw::kernel2))(ctx, in, &out);
Q
Qiao Longfei 已提交
98 99
  ASSERT_EQ(test_value, 2);
}