phi_utils_test.cc 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2021 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. */

15
#include "paddle/fluid/framework/phi_utils.h"
16

17 18
#include "gtest/gtest.h"
#include "paddle/fluid/framework/lod_tensor.h"
19
#include "paddle/fluid/framework/selected_rows_utils.h"
20 21
#include "paddle/fluid/framework/variable.h"

22
TEST(PhiUtils, TransPhiKernelKeyToOpKernelType) {
23 24
  phi::KernelKey kernel_key(
      phi::Backend::CPU, phi::DataLayout::NCHW, phi::DataType::FLOAT32);
25
  auto op_kernel_type =
26
      paddle::framework::TransPhiKernelKeyToOpKernelType(kernel_key);
27 28 29 30 31 32 33
  ASSERT_EQ(op_kernel_type.data_type_, paddle::framework::proto::VarType::FP32);
  ASSERT_EQ(op_kernel_type.data_layout_, paddle::framework::DataLayout::kNCHW);
  ASSERT_TRUE(paddle::platform::is_cpu_place(op_kernel_type.place_));
  ASSERT_EQ(op_kernel_type.library_type_,
            paddle::framework::LibraryType::kPlain);

#ifdef PADDLE_WITH_MKLDNN
34
  phi::KernelKey kernel_key_mkldnn(
35
      phi::Backend::ONEDNN, phi::DataLayout::NCHW, phi::DataType::FLOAT32);
36
  op_kernel_type =
37
      paddle::framework::TransPhiKernelKeyToOpKernelType(kernel_key_mkldnn);
38 39 40 41 42 43 44 45
  ASSERT_EQ(op_kernel_type.data_type_, paddle::framework::proto::VarType::FP32);
  ASSERT_EQ(op_kernel_type.data_layout_, paddle::framework::DataLayout::kNCHW);
  ASSERT_TRUE(paddle::platform::is_cpu_place(op_kernel_type.place_));
  ASSERT_EQ(op_kernel_type.library_type_,
            paddle::framework::LibraryType::kMKLDNN);
#endif

#ifdef PADDLE_WITH_CUDA
46 47
  phi::KernelKey kernel_key_cudnn(
      phi::Backend::GPUDNN, phi::DataLayout::NCHW, phi::DataType::FLOAT32);
48
  op_kernel_type =
49
      paddle::framework::TransPhiKernelKeyToOpKernelType(kernel_key_cudnn);
50 51 52 53 54 55 56
  ASSERT_EQ(op_kernel_type.data_type_, paddle::framework::proto::VarType::FP32);
  ASSERT_EQ(op_kernel_type.data_layout_, paddle::framework::DataLayout::kNCHW);
  ASSERT_TRUE(paddle::platform::is_gpu_place(op_kernel_type.place_));
  ASSERT_EQ(op_kernel_type.library_type_,
            paddle::framework::LibraryType::kCUDNN);
#endif
}
57

58
TEST(PhiUtils, TransOpKernelTypeToPhiKernelKey) {
59
  paddle::framework::OpKernelType op_kernel_type(
60 61
      paddle::framework::proto::VarType::FP32,
      paddle::platform::CPUPlace(),
62 63
      paddle::framework::DataLayout::kNCHW);
  auto kernel_key =
64
      paddle::framework::TransOpKernelTypeToPhiKernelKey(op_kernel_type);
65 66 67 68 69 70
  ASSERT_EQ(kernel_key.dtype(), phi::DataType::FLOAT32);
  ASSERT_EQ(kernel_key.layout(), phi::DataLayout::NCHW);
  ASSERT_EQ(kernel_key.backend(), phi::Backend::CPU);

#ifdef PADDLE_WITH_MKLDNN
  paddle::framework::OpKernelType op_kernel_type_mkldnn(
71 72
      paddle::framework::proto::VarType::FP32,
      paddle::platform::CPUPlace(),
73 74
      paddle::framework::DataLayout::kMKLDNN,
      paddle::framework::LibraryType::kMKLDNN);
75 76
  auto kernel_key_mkldnn =
      paddle::framework::TransOpKernelTypeToPhiKernelKey(op_kernel_type_mkldnn);
77 78
  ASSERT_EQ(kernel_key_mkldnn.dtype(), phi::DataType::FLOAT32);
  ASSERT_EQ(kernel_key_mkldnn.layout(), phi::DataLayout::MKLDNN);
79
  ASSERT_EQ(kernel_key_mkldnn.backend(), phi::Backend::ONEDNN);
80 81 82 83
#endif

#ifdef PADDLE_WITH_CUDA
  paddle::framework::OpKernelType op_kernel_type_cudnn(
84 85
      paddle::framework::proto::VarType::FP32,
      paddle::platform::CPUPlace(),
86 87 88
      paddle::framework::DataLayout::kNCHW,
      paddle::framework::LibraryType::kCUDNN);
  auto kernel_key_cudnn =
89
      paddle::framework::TransOpKernelTypeToPhiKernelKey(op_kernel_type_cudnn);
90 91 92 93 94
  ASSERT_EQ(kernel_key_cudnn.dtype(), phi::DataType::FLOAT32);
  ASSERT_EQ(kernel_key_cudnn.layout(), phi::DataLayout::NCHW);
  ASSERT_EQ(kernel_key_cudnn.backend(), phi::Backend::GPUDNN);
#endif
}