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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
/**
 * @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
 */

std::array<proto::DataType, 2> kDataType = {proto::DataType::FP32,
                                            proto::DataType::FP64};

std::array<Place, 2> kPlace = {CPUPlace(), CUDAPlace(0)};

std::array<DataLayout, 2> kDataLayout = {
    DataLayout::kNHWC, DataLayout::kNCHW,
};

std::array<LibraryType, 2> kLibraryType = {
    LibraryType::kPlain, LibraryType::kMKLDNN,
};

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

Q
Qiao Longfei 已提交
52 53
int test_value = 0;

D
dzhwinter 已提交
54 55 56 57
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 已提交
58

D
dzhwinter 已提交
59 60
void TransDataType_t(std::vector<platform::DeviceContext*> ctx,
                     const Variable& in, Variable* out) {
Q
Qiao Longfei 已提交
61 62 63
  test_value++;
}

D
dzhwinter 已提交
64 65
void TransDataLayout_t(std::vector<platform::DeviceContext*> ctx,
                       const Variable& in, Variable* out) {
Q
Qiao Longfei 已提交
66 67 68
  test_value--;
}

D
dzhwinter 已提交
69 70
void TransLibraryType_t(std::vector<platform::DeviceContext*> ctx,
                        const Variable& in, Variable* out) {
Q
Qiao Longfei 已提交
71 72 73 74 75 76 77 78
  test_value += 2;
}

}  // namespace framework
}  // namespace paddle

namespace frw = paddle::framework;

D
dzhwinter 已提交
79 80 81
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 已提交
82 83 84 85 86 87 88 89 90 91

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

  auto& instance = DataTransformFnMap::Instance();
  std::vector<DeviceContext*> ctx;
  paddle::framework::Variable in;
  paddle::framework::Variable out;

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

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

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