test_mkldnn_caching.cc 5.7 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 <map>
#include <random>
#include <string>
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(elementwise_mul);
32
PD_DECLARE_KERNEL(multiply_raw, OneDNN, ONEDNN);
33
USE_OP_ITSELF(relu);
34
PD_DECLARE_KERNEL(relu, OneDNN, ONEDNN);
35
USE_OP_ITSELF(softmax);
36
PD_DECLARE_KERNEL(softmax, OneDNN, ONEDNN);
H
hong 已提交
37
USE_OP_ITSELF(conv2d);
38
PD_DECLARE_KERNEL(conv2d, OneDNN, ONEDNN);
39 40 41 42 43 44

namespace paddle {
namespace operators {

struct InputVars {
  std::string name;
45
  phi::DenseTensor *tensor;
46 47 48 49 50 51 52
};

class CacheTester {
 public:
  CacheTester() {
    // Clear oneDNN cache
    auto &pool = platform::DeviceContextPool::Instance();
53
    phi::CPUPlace place;
54
    onednn_dev_ctx_ = dynamic_cast<phi::OneDNNContext *>(pool.Get(place));
55
    onednn_dev_ctx_->ResetBlobMap(nullptr);
56 57
  }

H
hong 已提交
58
  bool Analyze(uint16_t num_entries) {
59 60 61 62 63
    //  Number of created objects in cache should be as expected (num_entries)
    return onednn_dev_ctx_->GetCachedObjectsNumber() == num_entries;
  }

 private:
64
  phi::OneDNNContext *onednn_dev_ctx_;
65 66 67
};

template <typename T>
68 69 70 71
void RunOperator(const platform::Place &place,
                 const std::string &op_type,
                 const framework::DDim &dims,
                 const std::string &first_input) {
72 73
  framework::Scope scope;

74 75
  std::map<const std::string, int> num_inputs = {{"softmax", 1},
                                                 {"relu", 1},
76
                                                 {"conv2d", 2},
77 78
                                                 {"elementwise_add", 2},
                                                 {"elementwise_mul", 2}};
79

80 81 82 83
  std::string first_input_var_name = (op_type == "conv2d") ? "Input" : "X";
  std::string second_input_var_name = (op_type == "conv2d") ? "Filter" : "Y";
  std::string output_var_name = (op_type == "conv2d") ? "Output" : "Out";
  std::string output_name = "output";
84 85

  std::vector<InputVars> input_names = {
86
      {first_input, scope.Var(first_input)->GetMutable<phi::DenseTensor>()},
87
      {"x1",
88 89
       num_inputs[op_type] > 1 ? scope.Var("x1")->GetMutable<phi::DenseTensor>()
                               : nullptr},
90
      {"x2",
91 92
       num_inputs[op_type] > 2 ? scope.Var("x2")->GetMutable<phi::DenseTensor>()
                               : nullptr},
93
      {"x3",
94 95
       num_inputs[op_type] > 3 ? scope.Var("x3")->GetMutable<phi::DenseTensor>()
                               : nullptr},
96
      {"x4",
97 98 99
       num_inputs[op_type] > 4 ? scope.Var("x4")->GetMutable<phi::DenseTensor>()
                               : nullptr}};
  auto *y = scope.Var(output_name)->GetMutable<phi::DenseTensor>();
100 101 102 103 104

  // Initialize input data
  std::uniform_real_distribution<T> dist(static_cast<T>(10.0),
                                         static_cast<T>(20.0));
  std::mt19937 engine;
105
  size_t numel = static_cast<size_t>(phi::product(dims));
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
  for (int i = 0; i < num_inputs[op_type]; ++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);
    }
  }

  // Initialize output
  y->Resize(dims);
  auto y_ptr = y->mutable_data<T>(place);
  for (size_t i = 0; i < numel; ++i) {
    y_ptr[i] = static_cast<T>(0);
  }

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

123 124 125 126 127 128 129 130 131 132 133 134
  auto op = num_inputs[op_type] > 1
                ? framework::OpRegistry::CreateOp(
                      op_type,
                      {{first_input_var_name, {first_input}},
                       {second_input_var_name, {"x1"}}},
                      {{output_var_name, {output_name}}},
                      {{"use_mkldnn", {true}}})
                : framework::OpRegistry::CreateOp(
                      op_type,
                      {{first_input_var_name, {first_input}}},
                      {{output_var_name, {output_name}}},
                      {{"use_mkldnn", {true}}});
135 136 137 138 139

  op->Run(scope, place);
  pool.Get(place)->Wait();
}

140 141
TEST(test_conv2d_reuse_cache, cpu_place) {
  framework::DDim dims({1, 16, 32, 64});
142
  phi::CPUPlace p;
143
  CacheTester ct;
144 145
  RunOperator<float>(p, "conv2d", dims, "input_signal");
  RunOperator<float>(p, "conv2d", dims, "input_signal");
146 147
  PADDLE_ENFORCE_EQ(ct.Analyze(9),
                    true,
148
                    platform::errors::InvalidArgument(
149
                        "Invalid number of cached oneDNN objects"));
150 151
}

152 153
TEST(test_conv2d_noreuse_cache, cpu_place) {
  framework::DDim dims({1, 16, 32, 64});
154
  phi::CPUPlace p;
155
  CacheTester ct;
156 157
  RunOperator<float>(p, "conv2d", dims, "input_signal");
  RunOperator<float>(p, "conv2d", dims, "input_signal2");
158 159
  PADDLE_ENFORCE_EQ(ct.Analyze(18),
                    true,
160
                    platform::errors::InvalidArgument(
161
                        "Invalid number of cached oneDNN objects"));
162 163 164 165
}

}  // namespace operators
}  // namespace paddle