executor_test.cc 8.1 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 17 18
#include <memory>  // for unique_ptr
#include <mutex>   // for call_once
#include <vector>
Y
Yang Yang 已提交
19
#include "gtest/gtest.h"
Y
Yang Yang 已提交
20
#include "paddle/framework/attribute.h"
Y
Yang Yang 已提交
21 22 23 24 25
#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 已提交
26
USE_OP(gaussian_random);
Q
qijun 已提交
27
USE_OP(feed);
Q
qijun 已提交
28
USE_OP(fetch);
Q
qijun 已提交
29

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

Y
Yang Yang 已提交
34 35 36 37 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
typedef paddle::framework::BlockDesc proto_block;
typedef paddle::framework::OpDesc proto_op;

void add_gaussian_random_op(string var_name, proto_block* block) {
  std::vector<int> dim{2, 3};

  // 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);
}

Q
qijun 已提交
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
void add_feed_op(string var_name, int index, proto_block* block) {
  std::vector<int> dim{3};

  // 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);
}

Q
qijun 已提交
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 128 129 130 131 132
void add_fetch_op(string var_name, int index, proto_block* block) {
  std::vector<int> dim{3};

  // 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 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
std::once_flag set_variable_flag;

template <typename T>
void set_feed_variable(const std::vector<std::vector<T>>& inputs) {
  typedef std::vector<paddle::framework::Tensor> FeedInputs;
  Variable* g_feed_value = GetScope()->FindVar("feed_value");
  FeedInputs& feed_inputs = *(g_feed_value->GetMutable<FeedInputs>());
  auto size = inputs.size();

  std::call_once(set_variable_flag, [&]() {
    feed_inputs.reserve(size);
    for (size_t i = 0; i < size; i++) {
      paddle::framework::Tensor tmp;
      tmp.mutable_data<T>(make_ddim({static_cast<int64_t>(inputs[i].size())}),
                          CPUPlace());
      feed_inputs.push_back(tmp);
    }
  });

  for (size_t i = 0; i < size; i++) {
    memcpy(feed_inputs[i].data<T>(), inputs[i].data(),
           inputs[i].size() * sizeof(T));
  }
}

Q
qijun 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
template <typename T>
std::vector<std::vector<T>> get_fetch_variable() {
  typedef std::vector<paddle::framework::Tensor> FetchOutputs;
  Variable* g_fetch_value = GetScope()->FindVar("fetch_value");
  FetchOutputs& fetch_outputs = *(g_fetch_value->GetMutable<FetchOutputs>());
  auto size = fetch_outputs.size();

  std::vector<std::vector<T>> result;
  result.reserve(size);

  for (size_t i = 0; i < size; i++) {
    std::vector<T> tmp;
    tmp.reserve(fetch_outputs[i].numel());
    memcpy(tmp.data(), fetch_outputs[i].data<T>(),
           fetch_outputs[i].numel() * sizeof(T));
    result.push_back(tmp);
  }

  return result;
}

Q
qijun 已提交
179
class ExecutorTesterRandom : public ::testing::Test {
Q
qijun 已提交
180 181 182 183 184 185
 public:
  virtual void SetUp() override {
    auto root_block = pdesc_.add_blocks();
    root_block->set_idx(0);
    root_block->set_parent_idx(-1);

Y
Yang Yang 已提交
186 187
    add_gaussian_random_op("a", root_block);
    add_gaussian_random_op("b", root_block);
Q
qijun 已提交
188 189 190 191 192 193

    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 已提交
194 195 196
    auto op = root_block->add_ops();
    op->set_type("elementwise_add");
    auto X = op->add_inputs();
Q
qijun 已提交
197 198
    X->set_parameter("X");
    X->add_arguments("a");
Y
Yang Yang 已提交
199
    auto Y = op->add_inputs();
Q
qijun 已提交
200 201
    Y->set_parameter("Y");
    Y->add_arguments("b");
Y
Yang Yang 已提交
202 203 204
    auto Out = op->add_outputs();
    Out->set_parameter("Out");
    Out->add_arguments("c");
Q
qijun 已提交
205 206

    scope_ = GetScope();
Q
qijun 已提交
207
  }
Y
Yang Yang 已提交
208

Q
qijun 已提交
209 210
 protected:
  ProgramDesc pdesc_;
Q
qijun 已提交
211
  Scope* scope_;
Q
qijun 已提交
212 213
};

Q
qijun 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
class ExecutorTesterFeed : public ::testing::Test {
 public:
  virtual void SetUp() override {
    auto root_block = pdesc_.add_blocks();
    root_block->set_idx(0);
    root_block->set_parent_idx(-1);

    add_feed_op("a", 0, root_block);
    add_feed_op("b", 1, root_block);

    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);

    auto op = root_block->add_ops();
    op->set_type("elementwise_add");
    auto X = op->add_inputs();
    X->set_parameter("X");
    X->add_arguments("a");
    auto Y = op->add_inputs();
    Y->set_parameter("Y");
    Y->add_arguments("b");
    auto Out = op->add_outputs();
    Out->set_parameter("Out");
    Out->add_arguments("c");

Q
qijun 已提交
241 242
    add_fetch_op("c", 0, root_block);

Q
qijun 已提交
243 244 245 246 247 248 249 250 251 252 253 254
    std::vector<float> vec1 = {1.0, 2.0, 3.0};
    std::vector<float> vec2 = {4.0, 5.0, 6.0};
    inputs_.push_back(vec1);
    inputs_.push_back(vec2);
  }

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

TEST_F(ExecutorTesterRandom, CPU) {
Q
qijun 已提交
255
  std::vector<Place> places;
Q
qijun 已提交
256
  CPUPlace cpu_place1, cpu_place2;
Q
qijun 已提交
257 258 259
  places.push_back(cpu_place1);
  places.push_back(cpu_place2);

Y
Yang Yang 已提交
260
  Executor* executor = new Executor(places);
Q
qijun 已提交
261 262 263 264 265 266 267 268 269 270 271 272 273 274
  executor->Run(pdesc_, scope_);
  delete executor;
}

TEST_F(ExecutorTesterFeed, CPU) {
  std::vector<Place> places;
  CPUPlace cpu_place;
  places.push_back(cpu_place);

  Executor* executor = new Executor(places);

  // 3 mini-batch
  for (int i = 0; i < 3; i++) {
    // need to set feed variable before Executor::Run
Q
qijun 已提交
275
    std::cout << "start mini-batch " << i << std::endl;
Q
qijun 已提交
276 277
    set_feed_variable<float>(inputs_);
    executor->Run(pdesc_, GetScope());
Q
qijun 已提交
278 279 280 281 282 283 284
    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 已提交
285 286
  }

Q
qijun 已提交
287 288
  delete executor;
}
Y
Yang Yang 已提交
289

Q
qijun 已提交
290
#ifdef PADDLE_WITH_GPU
Q
qijun 已提交
291 292 293 294 295 296 297 298 299 300 301
TEST_F(ExecutorTesterRandom, GPU) {
  std::vector<Place> places;
  GPUPlace gpu_place(0);
  places.push_back(gpu_place);

  Executor* executor = new Executor(places);
  executor->Run(pdesc_, scope_);
  delete executor;
}

TEST_F(ExecutorTesterFeed, GPU) {
Q
qijun 已提交
302
  std::vector<Place> places;
Q
qijun 已提交
303 304
  GPUPlace gpu_place(0);
  places.push_back(gpu_place);
Y
Yang Yang 已提交
305

Q
qijun 已提交
306
  Executor* executor = new Executor(places);
Q
qijun 已提交
307 308 309 310 311

  // need to set feed variable before Executor::Run
  set_feed_variable<float>(inputs_);
  executor->Run(pdesc_, scope_);

Q
qijun 已提交
312
  delete executor;
Y
Yang Yang 已提交
313
}
Q
qijun 已提交
314
#endif