executor_test.cc 2.7 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"
Y
Yang Yang 已提交
16
#include "gtest/gtest.h"
Y
Yang Yang 已提交
17 18
#include "paddle/framework/attribute.h"

Y
Yang Yang 已提交
19 20 21 22
#include "paddle/framework/grad_op_builder.h"
#include "paddle/framework/op_registry.h"
#include "paddle/framework/operator.h"

Y
Yang Yang 已提交
23 24
#include <vector>

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

Q
qijun 已提交
28 29 30
using namespace paddle::platform;
using namespace paddle::framework;

Y
Yang Yang 已提交
31 32 33 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
typedef paddle::framework::BlockDesc proto_block;
typedef paddle::framework::OpDesc proto_op;

using std::string;

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 已提交
62 63
TEST(Executor, Init) {
  ProgramDesc pdesc;
Y
Yang Yang 已提交
64 65 66 67 68

  auto root_block = pdesc.add_blocks();
  root_block->set_idx(0);
  root_block->set_parent_idx(-1);

Y
Yang Yang 已提交
69 70
  add_gaussian_random_op("a", root_block);
  add_gaussian_random_op("b", root_block);
Y
Yang Yang 已提交
71 72 73 74 75 76

  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 已提交
77 78 79
  auto op = root_block->add_ops();
  op->set_type("elementwise_add");
  auto X = op->add_inputs();
Y
Yang Yang 已提交
80 81
  X->set_parameter("X");
  X->add_arguments("a");
Y
Yang Yang 已提交
82
  auto Y = op->add_inputs();
Y
Yang Yang 已提交
83 84
  Y->set_parameter("Y");
  Y->add_arguments("b");
Y
Yang Yang 已提交
85 86 87
  auto Out = op->add_outputs();
  Out->set_parameter("Out");
  Out->add_arguments("c");
Y
Yang Yang 已提交
88

Q
qijun 已提交
89 90 91 92 93
  CPUPlace cpu_place1, cpu_place2;
  std::vector<Place> places;
  places.push_back(cpu_place1);
  places.push_back(cpu_place2);

Y
Yang Yang 已提交
94
  Executor* executor = new Executor(places);
Q
qijun 已提交
95 96 97
  Scope s;
  std::vector<Tensor>* outputs{nullptr};
  executor->Run(pdesc, &s, outputs);
Y
Yang Yang 已提交
98

Q
qijun 已提交
99
  delete executor;
Y
Yang Yang 已提交
100
}