executor_test.cc 9.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 20 21 22 23
#include "paddle/framework/grad_op_builder.h"
#include "paddle/framework/op_registry.h"
#include "paddle/framework/operator.h"

USE_OP(elementwise_add);
Y
Yang Yang 已提交
24
USE_OP(gaussian_random);
Q
qijun 已提交
25
USE_OP(feed);
Q
qijun 已提交
26
USE_OP(fetch);
Q
qijun 已提交
27

Y
Yang Yang 已提交
28
using std::string;
Q
qijun 已提交
29 30 31
using namespace paddle::platform;
using namespace paddle::framework;

Y
Yang Yang 已提交
32 33 34
typedef paddle::framework::BlockDesc proto_block;
typedef paddle::framework::OpDesc proto_op;

35 36
void add_gaussian_random_op(string var_name, std::vector<int>& dim,
                            proto_block* block) {
Y
Yang Yang 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  // insert variable
  auto a = block->add_vars();
  a->set_name(var_name);
  auto a_lt = a->mutable_lod_tensor();
  a_lt->set_data_type(paddle::framework::DataType::FP32);
  for (int i : dim) {
    a_lt->add_dims(i);
  }

  // insert operation
  auto op = block->add_ops();
  op->set_type("gaussian_random");
  auto dims = op->add_attrs();
  dims->set_name("dims");
  dims->set_type(paddle::framework::AttrType::INTS);
  for (int i : dim) {
    dims->add_ints(i);
  }
  auto Out = op->add_outputs();
  Out->set_parameter("Out");
  Out->add_arguments(var_name);
}

60 61
void add_feed_op(string var_name, std::vector<int>& dim, int index,
                 proto_block* block) {
Q
qijun 已提交
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
  // insert variable
  auto a = block->add_vars();
  a->set_name(var_name);
  auto a_lt = a->mutable_lod_tensor();
  a_lt->set_data_type(paddle::framework::DataType::FP32);
  for (int i : dim) {
    a_lt->add_dims(i);
  }

  // insert operation
  auto op = block->add_ops();
  op->set_type("feed");

  // set dims attr
  auto dims = op->add_attrs();
  dims->set_name("dims");
  dims->set_type(paddle::framework::AttrType::INTS);
  for (int i : dim) {
    dims->add_ints(i);
  }

  // set col attr
  auto col = op->add_attrs();
  col->set_name("col");
  col->set_type(paddle::framework::AttrType::INT);
  col->set_i(index);

  auto Out = op->add_outputs();
  Out->set_parameter("Out");
  Out->add_arguments(var_name);
}

94 95
void add_fetch_op(string var_name, std::vector<int>& dim, int index,
                  proto_block* block) {
Q
qijun 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  // insert variable
  auto a = block->add_vars();
  a->set_name(var_name);
  auto a_lt = a->mutable_lod_tensor();
  a_lt->set_data_type(paddle::framework::DataType::FP32);
  for (int i : dim) {
    a_lt->add_dims(i);
  }

  // insert operation
  auto op = block->add_ops();
  op->set_type("fetch");

  // set dims attr
  auto dims = op->add_attrs();
  dims->set_name("dims");
  dims->set_type(paddle::framework::AttrType::INTS);
  for (int i : dim) {
    dims->add_ints(i);
  }

  // set col attr
  auto col = op->add_attrs();
  col->set_name("col");
  col->set_type(paddle::framework::AttrType::INT);
  col->set_i(index);

  auto Out = op->add_inputs();
  Out->set_parameter("Input");
  Out->add_arguments(var_name);
}

Q
qijun 已提交
128 129
std::once_flag set_variable_flag;

Y
Yang Yang 已提交
130 131
// Tensors in feed value variable will only be in CPUPlace
// So we can  memcpy the data from vector<T> to feed_value
Q
qijun 已提交
132 133 134
template <typename T>
void set_feed_variable(const std::vector<std::vector<T>>& inputs) {
  typedef std::vector<paddle::framework::Tensor> FeedInputs;
135
  Variable* g_feed_value = GetGlobalScope()->FindVar("feed_value");
Q
qijun 已提交
136 137
  FeedInputs& feed_inputs = *(g_feed_value->GetMutable<FeedInputs>());
  auto size = inputs.size();
138
  feed_inputs.resize(size);
Q
qijun 已提交
139
  for (size_t i = 0; i < size; i++) {
140 141 142
    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 已提交
143 144 145
  }
}

Y
Yang Yang 已提交
146 147
// Tensors in fetch value variable will only be in CPUPlace
// So we can memcpy the data from fetch_value to vector<T>
Q
qijun 已提交
148 149 150
template <typename T>
std::vector<std::vector<T>> get_fetch_variable() {
  typedef std::vector<paddle::framework::Tensor> FetchOutputs;
151
  Variable* g_fetch_value = GetGlobalScope()->FindVar("fetch_value");
Q
qijun 已提交
152 153
  FetchOutputs& fetch_outputs = *(g_fetch_value->GetMutable<FetchOutputs>());

154
  auto size = fetch_outputs.size();
Q
qijun 已提交
155 156 157 158
  std::vector<std::vector<T>> result;
  result.reserve(size);
  for (size_t i = 0; i < size; i++) {
    std::vector<T> tmp;
159
    tmp.resize(fetch_outputs[i].numel());
Q
qijun 已提交
160 161 162 163
    memcpy(tmp.data(), fetch_outputs[i].data<T>(),
           fetch_outputs[i].numel() * sizeof(T));
    result.push_back(tmp);
  }
Y
Yang Yang 已提交
164

Q
qijun 已提交
165 166 167
  return result;
}

Q
qijun 已提交
168
class ExecutorTesterRandom : public ::testing::Test {
Q
qijun 已提交
169 170 171 172 173 174
 public:
  virtual void SetUp() override {
    auto root_block = pdesc_.add_blocks();
    root_block->set_idx(0);
    root_block->set_parent_idx(-1);

175 176 177
    std::vector<int> dim{2, 3};
    add_gaussian_random_op("a", dim, root_block);
    add_gaussian_random_op("b", dim, root_block);
Q
qijun 已提交
178 179 180 181 182 183

    auto c = root_block->add_vars();
    c->set_name("c");
    auto c_lt = c->mutable_lod_tensor();
    c_lt->set_data_type(paddle::framework::DataType::FP32);

Y
Yang Yang 已提交
184 185 186
    auto op = root_block->add_ops();
    op->set_type("elementwise_add");
    auto X = op->add_inputs();
Q
qijun 已提交
187 188
    X->set_parameter("X");
    X->add_arguments("a");
Y
Yang Yang 已提交
189
    auto Y = op->add_inputs();
Q
qijun 已提交
190 191
    Y->set_parameter("Y");
    Y->add_arguments("b");
Y
Yang Yang 已提交
192 193 194
    auto Out = op->add_outputs();
    Out->set_parameter("Out");
    Out->add_arguments("c");
Q
qijun 已提交
195

196
    add_fetch_op("c", dim, 0, root_block);
Q
qijun 已提交
197
  }
Y
Yang Yang 已提交
198

Q
qijun 已提交
199 200 201 202
 protected:
  ProgramDesc pdesc_;
};

Y
Yang Yang 已提交
203
class ExecutorTesterFeedAndFetch : public ::testing::Test {
Q
qijun 已提交
204 205 206 207 208 209
 public:
  virtual void SetUp() override {
    auto root_block = pdesc_.add_blocks();
    root_block->set_idx(0);
    root_block->set_parent_idx(-1);

210 211 212 213
    std::vector<int> dim{6};

    add_feed_op("a", dim, 0, root_block);
    add_feed_op("b", dim, 1, root_block);
Y
Yang Yang 已提交
214
    add_fetch_op("a", dim, 0, root_block);
Y
Yang Yang 已提交
215
    add_fetch_op("b", dim, 1, root_block);
Q
qijun 已提交
216

217 218
    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 已提交
219 220 221 222 223 224 225 226 227
    inputs_.push_back(vec1);
    inputs_.push_back(vec2);
  }

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

Q
qijun 已提交
228
#ifndef PADDLE_WITH_CUDA
Q
qijun 已提交
229
TEST_F(ExecutorTesterRandom, CPU) {
Q
qijun 已提交
230
  std::vector<Place> places;
231 232 233 234 235 236 237 238
  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 已提交
239

Y
Yang Yang 已提交
240
  Executor* executor = new Executor(places);
241
  executor->Run(pdesc_, GetGlobalScope());
242
  std::vector<std::vector<float>> result = get_fetch_variable<float>();
Y
Yang Yang 已提交
243

244 245 246 247 248 249
  for (auto& vec : result) {
    for (auto& num : vec) {
      std::cout << num << " ";
    }
    std::cout << std::endl;
  }
Q
qijun 已提交
250 251 252
  delete executor;
}

Y
Yang Yang 已提交
253
TEST_F(ExecutorTesterFeedAndFetch, CPU) {
Q
qijun 已提交
254 255 256 257
  std::vector<Place> places;
  CPUPlace cpu_place;
  places.push_back(cpu_place);

258 259 260 261 262 263
  // 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 已提交
264 265 266 267 268
  Executor* executor = new Executor(places);

  // 3 mini-batch
  for (int i = 0; i < 3; i++) {
    set_feed_variable<float>(inputs_);
269
    executor->Run(pdesc_, GetGlobalScope());
Q
qijun 已提交
270
    std::vector<std::vector<float>> result = get_fetch_variable<float>();
Y
Yang Yang 已提交
271 272 273 274 275
    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 已提交
276 277
      }
    }
Q
qijun 已提交
278 279
  }

Q
qijun 已提交
280 281
  delete executor;
}
Q
qijun 已提交
282
#else
Q
qijun 已提交
283 284 285 286 287
TEST_F(ExecutorTesterRandom, GPU) {
  std::vector<Place> places;
  GPUPlace gpu_place(0);
  places.push_back(gpu_place);

Q
qijun 已提交
288 289 290 291 292 293 294
  // 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());
295 296
  paddle::memory::Used(gpu_place);

Q
qijun 已提交
297
  Executor* executor = new Executor(places);
298
  executor->Run(pdesc_, GetGlobalScope());
Q
qijun 已提交
299 300 301
  delete executor;
}

Y
Yang Yang 已提交
302
TEST_F(ExecutorTesterFeedAndFetch, GPU) {
Q
qijun 已提交
303
  std::vector<Place> places;
Q
qijun 已提交
304 305
  GPUPlace gpu_place(0);
  places.push_back(gpu_place);
Q
qijun 已提交
306 307 308 309 310 311 312
  // 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());
313 314
  paddle::memory::Used(gpu_place);

Q
qijun 已提交
315
  Executor* executor = new Executor(places);
Q
qijun 已提交
316

Q
qijun 已提交
317 318 319
  // 3 mini-batch
  for (int i = 0; i < 3; i++) {
    set_feed_variable<float>(inputs_);
320
    executor->Run(pdesc_, GetGlobalScope());
Q
qijun 已提交
321
    std::vector<std::vector<float>> result = get_fetch_variable<float>();
Y
Yang Yang 已提交
322 323 324 325 326
    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 已提交
327 328 329
      }
    }
  }
Q
qijun 已提交
330
  delete executor;
Y
Yang Yang 已提交
331
}
Q
qijun 已提交
332
#endif