test_prepare_op.cc 9.1 KB
Newer Older
J
Jiabin Yang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Copyright (c) 2019 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.

//
// Created by Jiabin on 2019-08-19.
//

#include <paddle/fluid/framework/op_registry.h>
20

J
Jiabin Yang 已提交
21 22 23
#include <memory>
#include <string>
#include <vector>
24

J
Jiabin Yang 已提交
25 26 27 28
#include "gtest/gtest.h"
#include "paddle/fluid/framework/op_info.h"
#include "paddle/fluid/imperative/prepared_operator.h"
#include "paddle/fluid/imperative/type_defs.h"
29 30 31 32 33 34 35
#include "paddle/phi/core/kernel_registry.h"

PD_DECLARE_KERNEL(split, CPU, ALL_LAYOUT);
PD_DECLARE_KERNEL(relu, CPU, ALL_LAYOUT);
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
PD_DECLARE_KERNEL(relu, GPU, ALL_LAYOUT);
#endif
J
Jiabin Yang 已提交
36 37 38 39 40 41 42 43

namespace imperative = paddle::imperative;
namespace platform = paddle::platform;
namespace framework = paddle::framework;

namespace paddle {
namespace imperative {

44 45 46
extern void TestHandleComplexGradToRealGradEager(
    const NameVarMap<egr::EagerVariable>& outs);

J
Jiabin Yang 已提交
47
static framework::VariableNameMap CreateVarNameMap(
48 49 50 51
    const framework::OpInfo& op_info,
    const std::string& op_type,
    const NameVarBaseMap& varbase_map,
    bool is_input) {
J
Jiabin Yang 已提交
52 53 54 55 56 57 58 59 60 61 62
  if (op_info.proto_ == nullptr) {
    return {};
  }

  framework::VariableNameMap result;

  for (auto& var :
       is_input ? op_info.Proto().inputs() : op_info.Proto().outputs()) {
    auto it = varbase_map.find(var.name());
    if (it == varbase_map.end()) {
      PADDLE_ENFORCE_EQ(
63 64
          var.dispensable(),
          true,
65 66 67
          platform::errors::NotFound("Variable %s is not dispensable and "
                                     "there are no such var in inputs",
                                     var.name()));
J
Jiabin Yang 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
      result[var.name()] = {};
    } else {
      auto& var_vector = it->second;
      std::vector<std::string> args;
      args.reserve(var_vector.size());
      for (auto& var_base : var_vector) {
        args.emplace_back(var_base->Name());
      }
      result[var.name()] = std::move(args);
    }
  }
  return result;
}

using vb_vector = std::vector<std::shared_ptr<imperative::VarBase>>;

using var_pair = std::pair<std::string, vb_vector>;

TEST(test_prepare_op, test_prepare_op) {
  std::shared_ptr<imperative::VarBase> vin(
      new imperative::VarBase(false, "vin"));
  std::shared_ptr<imperative::VarBase> vout(
      new imperative::VarBase(false, "vout"));
  framework::OpDesc desc;
  platform::CPUPlace place;
  vin->MutableVar()->GetMutable<framework::LoDTensor>()->mutable_data<float>(
      place);
  var_pair x_pair = var_pair("X", vb_vector(1, vin));
  var_pair out_pair = var_pair("Out", vb_vector(1, vout));
  imperative::NameVarBaseMap ins = {x_pair};
  imperative::NameVarBaseMap outs = {out_pair};
  framework::AttributeMap split_attr_map;
  const auto& info = framework::OpInfoMap::Instance().Get("split");
101
  if (info.Checker()) info.Checker()->Check(&split_attr_map);
J
Jiabin Yang 已提交
102 103 104 105
  framework::VariableNameMap var_in_map =
      CreateVarNameMap(info, "split", ins, true);
  framework::VariableNameMap var_out_map =
      CreateVarNameMap(info, "split", outs, false);
106 107
  auto op = framework::OpRegistry::CreateOp(
      "split", var_in_map, var_out_map, split_attr_map);
H
hong 已提交
108
  ASSERT_NO_FATAL_FAILURE(PreparedOp preparedOp = PreparedOp::Prepare(
109 110
                              ins,
                              outs,
111
                              dynamic_cast<framework::OperatorWithKernel&>(*op),
112 113 114
                              place,
                              split_attr_map,
                              {}));
J
Jiabin Yang 已提交
115 116
}

117
const phi::DenseTensor* GetTensorFromVar(const framework::Variable& var);
J
Jiabin Yang 已提交
118 119 120 121

TEST(test_prepare_op, test_get_tensor_from_var) {
  std::shared_ptr<imperative::VarBase> vout_error(
      new imperative::VarBase(false, "vout_error"));
122
  vout_error->MutableVar()->GetMutable<phi::SelectedRows>();
J
Jiabin Yang 已提交
123 124 125
  auto* ts = GetTensorFromVar(*vout_error->MutableVar());
  ASSERT_TRUE(ts != nullptr);
}
126

127
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
128 129 130 131 132 133 134 135 136 137 138 139 140 141
TEST(test_prepare_op, test_prepare_data) {
  std::shared_ptr<imperative::VarBase> vin(
      new imperative::VarBase(false, "vin"));
  std::shared_ptr<imperative::VarBase> vout(
      new imperative::VarBase(false, "vout"));

  framework::OpDesc desc;
  platform::CPUPlace cpu_place;
  platform::CUDAPlace gpu_place(0);
  std::vector<float> src_data(10, 2.0);
  std::vector<int64_t> dims = {2, 5};

  // prepare an cpu only input
  auto* vin_tensor = vin->MutableVar()->GetMutable<framework::LoDTensor>();
142
  vin_tensor->Resize(phi::make_ddim(dims));
143
  auto* vin_mutable_tensor = vin_tensor->mutable_data<float>(cpu_place);
144 145 146 147 148
  paddle::memory::Copy(cpu_place,
                       vin_mutable_tensor,
                       cpu_place,
                       src_data.data(),
                       sizeof(float) * src_data.size());
J
Jiabin Yang 已提交
149

150 151 152 153
  var_pair x_pair = var_pair("X", vb_vector(1, vin));
  var_pair out_pair = var_pair("Out", vb_vector(1, vout));
  imperative::NameVarBaseMap ins = {x_pair};
  imperative::NameVarBaseMap outs = {out_pair};
154 155 156 157
  const std::string op_type = "relu";
  framework::AttributeMap attr_map;
  const auto& info = framework::OpInfoMap::Instance().Get(op_type);
  if (info.Checker()) info.Checker()->Check(&attr_map);
158
  framework::VariableNameMap var_in_map =
159
      CreateVarNameMap(info, op_type, ins, true);
160
  framework::VariableNameMap var_out_map =
161
      CreateVarNameMap(info, op_type, outs, false);
162 163
  auto op = framework::OpRegistry::CreateOp(
      op_type, var_in_map, var_out_map, attr_map);
164 165

  // test if it can be transformed to GPU place
166 167 168 169 170 171 172
  auto prepared_op =
      PreparedOp::Prepare(ins,
                          outs,
                          dynamic_cast<framework::OperatorWithKernel&>(*op),
                          gpu_place,
                          attr_map,
                          {});
173
  PrepareData<imperative::VarBase>(
174 175
      dynamic_cast<framework::OperatorWithKernel&>(*op),
      ins,
176 177
      prepared_op.kernel_type());
  for (const auto& name_pair : ins) {
178 179 180 181 182 183 184 185
    for (const auto& vb : name_pair.second) {
      ASSERT_TRUE(platform::is_same_place(
          vb->Var().Get<framework::LoDTensor>().place(), gpu_place));
    }
  }
}
#endif

186
void TestPrepareDataSamePlace(framework::AttributeMap attr_map) {
187 188 189 190 191 192 193 194 195 196 197 198
  std::shared_ptr<imperative::VarBase> vin(
      new imperative::VarBase(false, "vin"));
  std::shared_ptr<imperative::VarBase> vout(
      new imperative::VarBase(false, "vout"));

  framework::OpDesc desc;
  platform::CPUPlace cpu_place;
  std::vector<float> src_data(10, 2.0);
  std::vector<int64_t> dims = {2, 5};

  // prepare an cpu only input
  auto* vin_tensor = vin->MutableVar()->GetMutable<framework::LoDTensor>();
199
  vin_tensor->Resize(phi::make_ddim(dims));
200
  auto* vin_mutable_tensor = vin_tensor->mutable_data<float>(cpu_place);
201 202 203 204 205
  paddle::memory::Copy(cpu_place,
                       vin_mutable_tensor,
                       cpu_place,
                       src_data.data(),
                       sizeof(float) * src_data.size());
206 207 208 209 210

  var_pair x_pair = var_pair("X", vb_vector(1, vin));
  var_pair out_pair = var_pair("Out", vb_vector(1, vout));
  imperative::NameVarBaseMap ins = {x_pair};
  imperative::NameVarBaseMap outs = {out_pair};
211 212 213
  const std::string op_type = "relu";
  const auto& info = framework::OpInfoMap::Instance().Get(op_type);
  if (info.Checker()) info.Checker()->Check(&attr_map);
214
  framework::VariableNameMap var_in_map =
215
      CreateVarNameMap(info, op_type, ins, true);
216
  framework::VariableNameMap var_out_map =
217 218
      CreateVarNameMap(info, op_type, outs, false);

219 220
  auto op = framework::OpRegistry::CreateOp(
      op_type, var_in_map, var_out_map, attr_map);
221

T
tianshuo78520a 已提交
222
  // test if it never transferred on GPU place
223 224 225 226 227 228 229
  auto prepared_op =
      PreparedOp::Prepare(ins,
                          outs,
                          dynamic_cast<framework::OperatorWithKernel&>(*op),
                          cpu_place,
                          attr_map,
                          {});
230
  PrepareData<imperative::VarBase>(
231 232
      dynamic_cast<framework::OperatorWithKernel&>(*op),
      ins,
233 234
      prepared_op.kernel_type());
  for (const auto& name_pair : ins) {
235 236 237 238 239 240
    for (const auto& vb : name_pair.second) {
      ASSERT_TRUE(platform::is_same_place(
          vb->Var().Get<framework::LoDTensor>().place(), cpu_place));
    }
  }
}
241 242 243 244 245

TEST(test_prepare_op, test_prepare_data_same_place) {
  TestPrepareDataSamePlace({});
}

246 247 248 249 250
TEST(test_prepare_op, test_complex_eager) {
  NameVarMap<egr::EagerVariable> outs = {};
  TestHandleComplexGradToRealGradEager(outs);
}

251 252 253 254 255
#ifdef PADDLE_WITH_MKLDNN
TEST(test_prepare_op, test_prepare_data_cpu_mkldnn) {
  TestPrepareDataSamePlace({{"use_mkldnn", true}});
}
#endif
J
Jiabin Yang 已提交
256 257 258
}  // namespace imperative
}  // namespace paddle

C
chentianyu03 已提交
259
USE_OP_ITSELF(split);
260
USE_OP_ITSELF(relu);
261
#ifdef PADDLE_WITH_MKLDNN
262
PD_DECLARE_KERNEL(relu, OneDNN, ONEDNN);
263
#endif