executor_test.cc 10.9 KB
Newer Older
Q
qijun 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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 "paddle/framework/executor.h"
Q
qijun 已提交
16
#include <vector>
Y
Yang Yang 已提交
17
#include "gtest/gtest.h"
Y
Yang Yang 已提交
18
#include "paddle/framework/attribute.h"
Y
Yang Yang 已提交
19
#include "paddle/framework/block_desc.h"
Y
Yang Yang 已提交
20
#include "paddle/framework/grad_op_builder.h"
Y
Yang Yang 已提交
21
#include "paddle/framework/op_desc.h"
Y
Yang Yang 已提交
22 23 24 25
#include "paddle/framework/op_registry.h"
#include "paddle/framework/operator.h"

USE_OP(elementwise_add);
Y
Yang Yang 已提交
26
USE_OP(gaussian_random);
Q
qijun 已提交
27
USE_OP(feed);
Q
qijun 已提交
28
USE_OP(fetch);
Y
Yang Yang 已提交
29
USE_OP(mul);
Q
qijun 已提交
30

Y
Yang Yang 已提交
31
using std::string;
Q
qijun 已提交
32 33 34
using namespace paddle::platform;
using namespace paddle::framework;

Y
Yang Yang 已提交
35 36 37
typedef paddle::framework::BlockDesc proto_block;
typedef paddle::framework::OpDesc proto_op;

Y
Yang Yang 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 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 101
struct SetAttrDescVisitor : public boost::static_visitor<void> {
  explicit SetAttrDescVisitor(OpDesc::Attr* attr) : attr_(attr) {}
  mutable OpDesc::Attr* attr_;
  void operator()(int v) const { attr_->set_i(v); }
  void operator()(float v) const { attr_->set_f(v); }
  void operator()(const std::string& v) const { attr_->set_s(v); }
  void operator()(bool b) const { attr_->set_b(b); }

  void operator()(const std::vector<int>& v) const {
    VectorToRepeated(v, attr_->mutable_ints());
  }
  void operator()(const std::vector<float>& v) const {
    VectorToRepeated(v, attr_->mutable_floats());
  }
  void operator()(const std::vector<std::string>& v) const {
    VectorToRepeated(v, attr_->mutable_strings());
  }
  void operator()(const std::vector<bool>& v) const {
    VectorToRepeated(v, attr_->mutable_bools());
  }
  void operator()(BlockDesc* desc) const { attr_->set_block_idx(desc->idx()); }
  void operator()(boost::blank) const { PADDLE_THROW("Unexpected branch"); }
};

void AddOp(const std::string& type, const VariableNameMap& inputs,
           const VariableNameMap& outputs, AttributeMap attrs,
           proto_block* block) {
  // insert output
  for (auto kv : outputs) {
    for (auto v : kv.second) {
      auto var = block->add_vars();
      var->set_name(v);
      auto var_lt = var->mutable_lod_tensor();
      var_lt->set_data_type(paddle::framework::DataType::FP32);
    }
  }

  // insert op
  auto op = block->add_ops();
  op->set_type(type);
  for (auto kv : inputs) {
    auto X = op->add_inputs();
    X->set_parameter(kv.first);
    for (auto argu : kv.second) {
      X->add_arguments(argu);
    }
  }
  for (auto kv : outputs) {
    auto X = op->add_outputs();
    X->set_parameter(kv.first);
    for (auto argu : kv.second) {
      X->add_arguments(argu);
    }
  }
  for (auto& attr : attrs) {
    auto* attr_desc = op->add_attrs();
    attr_desc->set_name(attr.first);
    attr_desc->set_type(
        static_cast<paddle::framework::AttrType>(attr.second.which() - 1));
    SetAttrDescVisitor visitor(attr_desc);
    boost::apply_visitor(visitor, attr.second);
  }
}

Q
qijun 已提交
102 103
std::once_flag set_variable_flag;

Y
Yang Yang 已提交
104 105
// Tensors in feed value variable will only be in CPUPlace
// So we can  memcpy the data from vector<T> to feed_value
Q
qijun 已提交
106 107 108
template <typename T>
void set_feed_variable(const std::vector<std::vector<T>>& inputs) {
  typedef std::vector<paddle::framework::Tensor> FeedInputs;
109
  Variable* g_feed_value = GetGlobalScope()->FindVar("feed_value");
Q
qijun 已提交
110 111
  FeedInputs& feed_inputs = *(g_feed_value->GetMutable<FeedInputs>());
  auto size = inputs.size();
112
  feed_inputs.resize(size);
Q
qijun 已提交
113
  for (size_t i = 0; i < size; i++) {
114 115 116
    T* dst = feed_inputs[i].mutable_data<T>(
        make_ddim({static_cast<int64_t>(inputs[i].size())}), CPUPlace());
    memcpy(dst, inputs[i].data(), inputs[i].size() * sizeof(T));
Q
qijun 已提交
117 118 119
  }
}

Y
Yang Yang 已提交
120 121
// Tensors in fetch value variable will only be in CPUPlace
// So we can memcpy the data from fetch_value to vector<T>
Q
qijun 已提交
122 123 124
template <typename T>
std::vector<std::vector<T>> get_fetch_variable() {
  typedef std::vector<paddle::framework::Tensor> FetchOutputs;
125
  Variable* g_fetch_value = GetGlobalScope()->FindVar("fetch_value");
Q
qijun 已提交
126 127
  FetchOutputs& fetch_outputs = *(g_fetch_value->GetMutable<FetchOutputs>());

128
  auto size = fetch_outputs.size();
Q
qijun 已提交
129 130 131 132
  std::vector<std::vector<T>> result;
  result.reserve(size);
  for (size_t i = 0; i < size; i++) {
    std::vector<T> tmp;
133
    tmp.resize(fetch_outputs[i].numel());
Q
qijun 已提交
134 135 136 137
    memcpy(tmp.data(), fetch_outputs[i].data<T>(),
           fetch_outputs[i].numel() * sizeof(T));
    result.push_back(tmp);
  }
Y
Yang Yang 已提交
138

Q
qijun 已提交
139 140 141
  return result;
}

Q
qijun 已提交
142
class ExecutorTesterRandom : public ::testing::Test {
Q
qijun 已提交
143 144
 public:
  virtual void SetUp() override {
Y
Yang Yang 已提交
145 146 147 148 149 150 151 152 153 154 155
    int input_dim = 5, batch_size = 2, embed_dim = 5;

    // init pdesc
    auto init_root_block = init_pdesc_.add_blocks();
    init_root_block->set_idx(0);
    init_root_block->set_parent_idx(-1);
    AddOp("gaussian_random", {}, {{"Out", {"w1"}}},
          {{"dims", std::vector<int>{input_dim, embed_dim}}}, init_root_block);
    AddOp("gaussian_random", {}, {{"Out", {"w2"}}},
          {{"dims", std::vector<int>{embed_dim, input_dim}}}, init_root_block);
    AddOp("fetch", {{"Input", {"w1"}}}, {},
Y
Yang Yang 已提交
156 157
          {{"dims", std::vector<int>{input_dim, embed_dim}}, {"col", 0}},
          init_root_block);
Y
Yang Yang 已提交
158
    AddOp("fetch", {{"Input", {"w2"}}}, {},
Y
Yang Yang 已提交
159 160
          {{"dims", std::vector<int>{embed_dim, input_dim}}, {"col", 1}},
          init_root_block);
Y
Yang Yang 已提交
161 162

    // run pdesc
Q
qijun 已提交
163 164 165 166
    auto root_block = pdesc_.add_blocks();
    root_block->set_idx(0);
    root_block->set_parent_idx(-1);

Y
Yang Yang 已提交
167 168 169 170 171 172
    AddOp("gaussian_random", {}, {{"Out", {"a"}}},
          {{"dims", std::vector<int>{batch_size, input_dim}}}, root_block);
    AddOp("mul", {{"X", {"a"}}, {"Y", {"w1"}}}, {{"Out", {"b"}}}, {},
          root_block);
    AddOp("mul", {{"X", {"b"}}, {"Y", {"w2"}}}, {{"Out", {"a_out"}}}, {},
          root_block);
Q
qijun 已提交
173

Y
Yang Yang 已提交
174 175 176
    AddOp("fetch", {{"Input", {"a_out"}}}, {},
          {{"dims", std::vector<int>{input_dim, batch_size}}, {"col", 1}},
          root_block);
Q
qijun 已提交
177
  }
Y
Yang Yang 已提交
178

Q
qijun 已提交
179 180
 protected:
  ProgramDesc pdesc_;
Y
Yang Yang 已提交
181
  ProgramDesc init_pdesc_;
Q
qijun 已提交
182 183
};

Y
Yang Yang 已提交
184
class ExecutorTesterFeedAndFetch : public ::testing::Test {
Q
qijun 已提交
185 186 187 188 189 190
 public:
  virtual void SetUp() override {
    auto root_block = pdesc_.add_blocks();
    root_block->set_idx(0);
    root_block->set_parent_idx(-1);

191 192
    std::vector<int> dim{6};

Y
Yang Yang 已提交
193 194 195 196 197 198 199 200
    AddOp("feed", {}, {{"Out", {"a"}}}, {{"dims", dim}, {"col", 0}},
          root_block);
    AddOp("feed", {}, {{"Out", {"b"}}}, {{"dims", dim}, {"col", 1}},
          root_block);
    AddOp("fetch", {{"Input", {"a"}}}, {}, {{"dims", dim}, {"col", 0}},
          root_block);
    AddOp("fetch", {{"Input", {"b"}}}, {}, {{"dims", dim}, {"col", 1}},
          root_block);
Q
qijun 已提交
201

202 203
    std::vector<float> vec1 = {1.0, 2.0, 3.0, 4.0, 5.0, 6.0};
    std::vector<float> vec2 = {4.0, 5.0, 6.0, 7.0, 8.0, 9.0};
Q
qijun 已提交
204 205 206 207 208 209 210 211 212
    inputs_.push_back(vec1);
    inputs_.push_back(vec2);
  }

 protected:
  ProgramDesc pdesc_;
  std::vector<std::vector<float>> inputs_;
};

Q
qijun 已提交
213
#ifndef PADDLE_WITH_CUDA
Q
qijun 已提交
214
TEST_F(ExecutorTesterRandom, CPU) {
Q
qijun 已提交
215
  std::vector<Place> places;
216 217 218 219 220 221 222 223
  CPUPlace cpu_place;
  places.push_back(cpu_place);

  // We have a global Scope and BuddyAllocator, and we must ensure
  // global BuddyAllocator is initialized before global Scope. Thus,
  // global Scope will deconstruct before BuddyAllocator. Otherwise,
  // "pointer being freed was not allocated" error will appear.
  paddle::memory::Used(cpu_place);
Q
qijun 已提交
224

Y
Yang Yang 已提交
225
  Executor* executor = new Executor(places);
Y
Yang Yang 已提交
226
  executor->Run(init_pdesc_, GetGlobalScope());
227
  executor->Run(pdesc_, GetGlobalScope());
228
  std::vector<std::vector<float>> result = get_fetch_variable<float>();
Y
Yang Yang 已提交
229

230 231 232 233 234 235
  for (auto& vec : result) {
    for (auto& num : vec) {
      std::cout << num << " ";
    }
    std::cout << std::endl;
  }
Q
qijun 已提交
236 237 238
  delete executor;
}

Y
Yang Yang 已提交
239
TEST_F(ExecutorTesterFeedAndFetch, CPU) {
Q
qijun 已提交
240 241 242 243
  std::vector<Place> places;
  CPUPlace cpu_place;
  places.push_back(cpu_place);

244 245 246 247 248 249
  // We have a global Scope and BuddyAllocator, and we must ensure
  // global BuddyAllocator is initialized before global Scope. Thus,
  // global Scope will deconstruct before BuddyAllocator. Otherwise,
  // "pointer being freed was not allocated" error will appear.
  paddle::memory::Used(cpu_place);

Q
qijun 已提交
250 251 252 253 254
  Executor* executor = new Executor(places);

  // 3 mini-batch
  for (int i = 0; i < 3; i++) {
    set_feed_variable<float>(inputs_);
255
    executor->Run(pdesc_, GetGlobalScope());
Q
qijun 已提交
256
    std::vector<std::vector<float>> result = get_fetch_variable<float>();
Y
Yang Yang 已提交
257 258 259 260 261
    PADDLE_ENFORCE_EQ(result.size(), inputs_.size());
    for (size_t i = 0; i < result.size(); ++i) {
      PADDLE_ENFORCE_EQ(result[i].size(), inputs_[i].size());
      for (size_t j = 0; j < result[i].size(); ++j) {
        PADDLE_ENFORCE_EQ(result[i][j], inputs_[i][j]);
Q
qijun 已提交
262 263
      }
    }
Q
qijun 已提交
264 265
  }

Q
qijun 已提交
266 267
  delete executor;
}
Q
qijun 已提交
268
#else
Q
qijun 已提交
269 270 271 272 273
TEST_F(ExecutorTesterRandom, GPU) {
  std::vector<Place> places;
  GPUPlace gpu_place(0);
  places.push_back(gpu_place);

Q
qijun 已提交
274 275 276 277 278 279 280
  // We have a global Scope and BuddyAllocator, and we must ensure
  // global BuddyAllocator is initialized before global Scope. Thus,
  // global Scope will deconstruct before BuddyAllocator. Otherwise,
  // "pointer being freed was not allocated" error will appear.
  // If paddle is compiled with GPU, both CPU and GPU BuddyAllocator
  // need to be used at first.
  paddle::memory::Used(CPUPlace());
281 282
  paddle::memory::Used(gpu_place);

Q
qijun 已提交
283
  Executor* executor = new Executor(places);
Y
Yang Yang 已提交
284 285 286 287

  LOG(INFO) << "Run Init";
  executor->Run(init_pdesc_, GetGlobalScope());
  LOG(INFO) << "Run";
288
  executor->Run(pdesc_, GetGlobalScope());
Y
Yang Yang 已提交
289 290 291 292 293 294 295 296
  std::vector<std::vector<float>> result = get_fetch_variable<float>();

  for (auto& vec : result) {
    for (auto& num : vec) {
      std::cout << num << " ";
    }
    std::cout << std::endl;
  }
Q
qijun 已提交
297 298 299
  delete executor;
}

Y
Yang Yang 已提交
300
TEST_F(ExecutorTesterFeedAndFetch, GPU) {
Q
qijun 已提交
301
  std::vector<Place> places;
Q
qijun 已提交
302 303
  GPUPlace gpu_place(0);
  places.push_back(gpu_place);
Q
qijun 已提交
304 305 306 307 308 309 310
  // We have a global Scope and BuddyAllocator, and we must ensure
  // global BuddyAllocator is initialized before global Scope. Thus,
  // global Scope will deconstruct before BuddyAllocator. Otherwise,
  // "pointer being freed was not allocated" error will appear.
  // If paddle is compiled with GPU, both CPU and GPU BuddyAllocator
  // need to be used at first.
  paddle::memory::Used(CPUPlace());
311 312
  paddle::memory::Used(gpu_place);

Q
qijun 已提交
313
  Executor* executor = new Executor(places);
Q
qijun 已提交
314

Q
qijun 已提交
315 316 317
  // 3 mini-batch
  for (int i = 0; i < 3; i++) {
    set_feed_variable<float>(inputs_);
318
    executor->Run(pdesc_, GetGlobalScope());
Q
qijun 已提交
319
    std::vector<std::vector<float>> result = get_fetch_variable<float>();
Y
Yang Yang 已提交
320 321 322 323 324
    PADDLE_ENFORCE_EQ(result.size(), inputs_.size());
    for (size_t i = 0; i < result.size(); ++i) {
      PADDLE_ENFORCE_EQ(result[i].size(), inputs_[i].size());
      for (size_t j = 0; j < result[i].size(); ++j) {
        PADDLE_ENFORCE_EQ(result[i][j], inputs_[i][j]);
Q
qijun 已提交
325 326 327
      }
    }
  }
Q
qijun 已提交
328
  delete executor;
Y
Yang Yang 已提交
329
}
Q
qijun 已提交
330
#endif