test_mkldnn_op_inplace.cc 5.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Copyright (c) 2020 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 <algorithm>
#include <cstdlib>
#include <memory>
#include <random>
19

20 21 22 23 24
#include "gtest/gtest.h"
#include "paddle/fluid/framework/lod_tensor.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
#include "paddle/fluid/framework/scope.h"
25 26
#include "paddle/phi/common/place.h"
#include "paddle/phi/core/enforce.h"
27
#include "paddle/phi/core/kernel_registry.h"
28

29
USE_OP_ITSELF(elementwise_add);
30
PD_DECLARE_KERNEL(add_raw, OneDNN, ONEDNN);
31
USE_OP_ITSELF(relu);
32
PD_DECLARE_KERNEL(relu, OneDNN, ONEDNN);
33
USE_OP_ITSELF(softmax);
34
PD_DECLARE_KERNEL(softmax, OneDNN, ONEDNN);
35 36
PD_DECLARE_KERNEL(softmax, CPU, ALL_LAYOUT);

37 38 39
namespace paddle {
namespace operators {

40 41
struct InputVars {
  std::string name;
42
  phi::DenseTensor *tensor;
43 44
};

45
template <typename T>
46 47 48 49
bool TestMain(const platform::Place &place,
              const std::string &op_type,
              const framework::DDim &dims,
              const int num_inputs) {
50 51
  framework::Scope scope;

52
  std::vector<InputVars> input_names = {
53
      {"x", scope.Var("x")->GetMutable<phi::DenseTensor>()},
54
      {"x1",
55
       num_inputs > 1 ? scope.Var("x1")->GetMutable<phi::DenseTensor>()
56 57
                      : nullptr},
      {"x2",
58
       num_inputs > 2 ? scope.Var("x2")->GetMutable<phi::DenseTensor>()
59 60
                      : nullptr},
      {"x3",
61
       num_inputs > 3 ? scope.Var("x3")->GetMutable<phi::DenseTensor>()
62 63
                      : nullptr},
      {"x4",
64
       num_inputs > 4 ? scope.Var("x4")->GetMutable<phi::DenseTensor>()
65
                      : nullptr}};
66
  auto *y = scope.Var("y")->GetMutable<phi::DenseTensor>();
67

68
  // Initialize input data
69 70 71
  std::uniform_real_distribution<T> dist(static_cast<T>(10.0),
                                         static_cast<T>(20.0));
  std::mt19937 engine;
72
  size_t numel = static_cast<size_t>(phi::product(dims));
73 74 75 76 77 78 79
  for (int i = 0; i < num_inputs; ++i) {
    input_names[i].tensor->Resize(dims);
    auto data_ptr = input_names[i].tensor->mutable_data<T>(place);
    for (size_t i = 0; i < numel; ++i) {
      data_ptr[i] = dist(engine);
    }
  }
80

81 82 83
  // Initialize output
  y->Resize(dims);
  auto y_ptr = y->mutable_data<T>(place);
84 85 86 87 88 89 90
  for (size_t i = 0; i < numel; ++i) {
    y_ptr[i] = static_cast<T>(0);
  }

  auto &pool = platform::DeviceContextPool::Instance();

  // Out of place (reference) computation
91 92 93 94 95 96 97 98 99 100
  auto op_ref =
      num_inputs > 1
          ? framework::OpRegistry::CreateOp(op_type,
                                            {{"X", {"x"}}, {"Y", {"x1"}}},
                                            {{"Out", {"y"}}},
                                            {{"use_mkldnn", {true}}})
          : framework::OpRegistry::CreateOp(op_type,
                                            {{"X", {"x"}}},
                                            {{"Out", {"y"}}},
                                            {{"use_mkldnn", {true}}});
101

102 103 104 105
  op_ref->Run(scope, place);
  pool.Get(place)->Wait();

  // Get reference (out of place) result
106
  auto &ref_tensor = scope.FindVar("y")->Get<phi::DenseTensor>();
107 108

  // In-place (to be tested) computation
109 110 111 112 113 114 115 116 117
  auto op = num_inputs > 1
                ? framework::OpRegistry::CreateOp(op_type,
                                                  {{"X", {"x"}}, {"Y", {"x1"}}},
                                                  {{"Out", {"x"}}},
                                                  {{"use_mkldnn", {true}}})
                : framework::OpRegistry::CreateOp(op_type,
                                                  {{"X", {"x"}}},
                                                  {{"Out", {"x"}}},
                                                  {{"use_mkldnn", {true}}});
118

119 120 121 122
  op->Run(scope, place);
  platform::DeviceContextPool::Instance().Get(place)->Wait();

  // Get in-place result
123
  auto &out_tensor = scope.FindVar("x")->Get<phi::DenseTensor>();
124
  PADDLE_ENFORCE_EQ(
125 126
      &out_tensor,
      input_names[0].tensor,
127 128 129 130 131 132 133 134 135 136 137 138
      platform::errors::InvalidArgument(
          "Input and output vars should share tensor for In-place test"));

  // compare results
  auto *ref_ptr = ref_tensor.data<T>();
  auto *out_ptr = out_tensor.data<T>();
  bool is_equal = std::equal(out_ptr, out_ptr + numel, ref_ptr);
  return is_equal;
}

TEST(test_softmax_inplace, cpu_place) {
  framework::DDim dims({32, 64});
139
  phi::CPUPlace p;
140 141 142
  ASSERT_TRUE(TestMain<float>(p, "softmax", dims, 1));
}

143 144
TEST(test_relu_inplace, cpu_place) {
  framework::DDim dims({1, 12, 20, 20});
145
  phi::CPUPlace p;
146 147 148
  ASSERT_TRUE(TestMain<float>(p, "relu", dims, 1));
}

149 150
}  // namespace operators
}  // namespace paddle