executor.cc 2.0 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 <iostream>
Q
qijun 已提交
17
#include <memory>
Y
Yang Yang 已提交
18
#include <vector>
Y
Yang Yang 已提交
19
#include "paddle/framework/lod_tensor.h"
Q
qijun 已提交
20
#include "paddle/framework/op_registry.h"
Q
qijun 已提交
21
#include "paddle/framework/scope.h"
Q
qijun 已提交
22 23 24 25

namespace paddle {
namespace framework {

Q
qijun 已提交
26 27 28 29
Executor::Executor(const std::vector<platform::Place>& places) {
  devices_.resize(places.size());
  for (size_t i = 0; i < places.size(); i++) {
    devices_[i] = platform::GetDevice(places[i]);
Q
qijun 已提交
30 31 32
  }
}

Q
qijun 已提交
33 34
void Executor::Run(const ProgramDesc& pdesc, Scope* scope,
                   std::vector<Tensor>* outputs) {
Y
Yang Yang 已提交
35 36 37
  // TODO(tonyyang-svail):
  //    - only runs the first block
  //    - only runs on the first device
Y
Yang Yang 已提交
38
  //    - test on gpu
Y
Yang Yang 已提交
39 40 41
  auto& block = pdesc.blocks(0);
  auto& device = devices_[0];

Y
Yang Yang 已提交
42 43 44 45
  // TODO(tonyyang-svail):
  //    - runs on a new local scope
  // Scope& local_scope = scope->NewScope();

Y
Yang Yang 已提交
46 47 48 49 50
  for (auto& var : block.vars()) {
    scope->NewVar(var.name());
  }

  for (auto& op_desc : block.ops()) {
Y
Yang Yang 已提交
51
    auto op = paddle::framework::OpRegistry::CreateOp(op_desc);
Y
Yang Yang 已提交
52 53 54
    op->Run(*scope, *device->cpu_device_context);
  }

Y
Yang Yang 已提交
55 56 57 58 59 60 61
  // print tensor value
  for (auto& var : block.vars()) {
    std::cout << var.name() << std::endl;
    auto v = scope->FindVar(var.name());
    const LoDTensor& t = v->Get<LoDTensor>();
    for (int i = 0; i < t.numel(); ++i) std::cout << t.data<float>()[i] << " ";
    std::cout << std::endl;
Q
qijun 已提交
62
  }
Q
qijun 已提交
63 64 65 66
}

}  // namespace framework
}  // namespace paddle