data_layout_transform_test.cc 2.4 KB
Newer Older
1
//   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2 3 4 5 6 7 8 9 10 11 12 13
//
// 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.
14

Y
Yi Wang 已提交
15
#include "paddle/fluid/framework/data_layout_transform.h"
16 17

#include "gtest/gtest.h"
Y
Yi Wang 已提交
18
#include "paddle/fluid/platform/device_context.h"
19 20

TEST(DataTransform, DataLayoutFunction) {
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
  auto place = paddle::platform::CPUPlace();
  paddle::framework::Tensor in = paddle::framework::Tensor();
  paddle::framework::Tensor out = paddle::framework::Tensor();
  in.mutable_data<double>(paddle::framework::make_ddim({2, 3, 1, 2}), place);
  in.set_layout(paddle::framework::DataLayout::kNHWC);

  auto kernel_nhwc = paddle::framework::OpKernelType(
      paddle::framework::proto::VarType::FP32, place,
      paddle::framework::DataLayout::kNHWC,
      paddle::framework::LibraryType::kPlain);
  auto kernel_ncwh = paddle::framework::OpKernelType(
      paddle::framework::proto::VarType::FP32, place,
      paddle::framework::DataLayout::kNCHW,
      paddle::framework::LibraryType::kPlain);

  paddle::framework::TransDataLayout(kernel_nhwc, kernel_ncwh, in, &out);

  EXPECT_TRUE(out.layout() == paddle::framework::DataLayout::kNCHW);
  EXPECT_TRUE(out.dims() == paddle::framework::make_ddim({2, 2, 3, 1}));
40 41 42

  TransDataLayout(kernel_ncwh, kernel_nhwc, in, &out);

43 44
  EXPECT_TRUE(in.layout() == paddle::framework::DataLayout::kNHWC);
  EXPECT_TRUE(in.dims() == paddle::framework::make_ddim({2, 3, 1, 2}));
45
}
46 47 48 49 50 51 52 53 54 55 56 57 58 59

#ifdef PADDLE_WITH_MKLDNN
TEST(DataTransform, GetDataFromTensorDNNL) {
  auto place = paddle::platform::CPUPlace();
  paddle::framework::Tensor in = paddle::framework::Tensor();
  in.mutable_data<paddle::platform::bfloat16>(
      paddle::framework::make_ddim({2, 3, 1, 2}), place);

  void* in_data =
      paddle::framework::GetDataFromTensor(in, dnnl::memory::data_type::bf16);
  EXPECT_EQ(in_data, paddle::platform::to_void_cast(
                         in.data<paddle::platform::bfloat16>()));
}
#endif