executor_for_test.cpp 2.6 KB
Newer Older
W
wangliu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.

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. */
14 15 16 17 18 19 20 21

#include "executor_for_test.h"

template <typename DeviceType, typename OpType>
Executor4Test<DeviceType, OpType>::Executor4Test(const Program<DeviceType> p,
                                                 std::string op_type)
    : Executor<DeviceType>(p) {

22 23 24 25
  if (this->program_.originProgram == nullptr) {
    LOG(paddle_mobile::LogLevel::kLOG_ERROR)
        << "to_predict_program_ == nullptr";
  }
26

27 28
  const std::vector<std::shared_ptr<BlockDesc>> blocks =
      this->to_predict_program_->Blocks();
29

W
wangliu 已提交
30
  for (std::shared_ptr<BlockDesc> block_desc: blocks) {
31
    std::vector<std::shared_ptr<OpDesc>> ops = block_desc->Ops();
W
wangliu 已提交
32
    for (std::shared_ptr<OpDesc> op : ops) {
33 34 35 36
      if (op->Type() == op_type) {
        std::shared_ptr<OpType> op_ptr = std::make_shared<OpType>(
            op->Type(), op->GetInputs(), op->GetOutputs(), op->GetAttrMap(),
            this->program_.scope);
37

38 39 40
        this->ops_of_block_[*block_desc.get()].push_back(op_ptr);
        break;
      }
41
    }
42
  }
43 44 45 46
}

template <typename DeviceType, typename OpType>
std::shared_ptr<Tensor>
W
wangliu 已提交
47 48
Executor4Test<DeviceType, OpType>::predict(const Tensor &t, std::string input,
                                           std::string output, DDim &dDim) {
49 50 51 52
  auto scope = this->program_.scope;
  Variable *g_feed_value = scope->Var(input);
  auto tensor = g_feed_value->GetMutable<Tensor>();
  tensor->ShareDataWith(t);
53

54
  Variable *con_output = scope->Var(output);
W
wangliu 已提交
55
  auto *output_tensor = con_output->GetMutable<Tensor>();
56 57 58
  output_tensor->mutable_data<float>(dDim);
  std::shared_ptr<Tensor> out_tensor = std::make_shared<LoDTensor>();
  out_tensor.reset(output_tensor);
59

60 61
  Executor<DeviceType>::predict(t, 0);
  return out_tensor;
62 63 64 65 66 67 68 69
}

template class Executor4Test<
    paddle_mobile::CPU,
    paddle_mobile::operators::ConvOp<paddle_mobile::CPU, float>>;
template class Executor4Test<
    paddle_mobile::CPU,
    paddle_mobile::operators::PoolOp<paddle_mobile::CPU, float>>;
W
wangliu 已提交
70 71 72
template class Executor4Test<
        paddle_mobile::CPU,
        paddle_mobile::operators::SoftmaxOp<paddle_mobile::CPU, float>>;