executor_for_test.h 4.9 KB
Newer Older
L
liuruilong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* 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. */

#pragma once

#include <string>
E
eclipsess 已提交
18
#include <vector>
L
liuruilong 已提交
19

L
liuruilong 已提交
20
#include "common/log.h"
21
#include "framework/executor.h"
Z
zhaojiaying01 已提交
22
#include "framework/op_registry.h"
23
#include "operators/activation_op.h"
L
liuruilong 已提交
24
#include "operators/conv_op.h"
E
eclipsess 已提交
25
#include "operators/elementwise_add_op.h"
L
liuruilong 已提交
26
#include "operators/pool_op.h"
E
eclipsess 已提交
27
#include "operators/reshape_op.h"
L
liuruilong 已提交
28
#include "operators/softmax_op.h"
E
eclipsess 已提交
29
#include "operators/transpose_op.h"
L
liuruilong 已提交
30 31 32

using paddle_mobile::framework::BlockDesc;
using paddle_mobile::framework::DDim;
33
using paddle_mobile::framework::Executor;
L
liuruilong 已提交
34 35 36 37 38 39
using paddle_mobile::framework::LoDTensor;
using paddle_mobile::framework::OpDesc;
using paddle_mobile::framework::Program;
using paddle_mobile::framework::Tensor;
using paddle_mobile::framework::Variable;
using std::string;
E
eclipsess 已提交
40
using std::vector;
41

L
liuruilong 已提交
42 43 44
template <typename DeviceType, typename OpType>
class Executor4Test : public Executor<DeviceType> {
 public:
L
liuruilong 已提交
45
  Executor4Test(Program<DeviceType> p, string op_type,
L
liuruilong 已提交
46
                bool use_optimize = false)
L
liuruilong 已提交
47
      : Executor<DeviceType>() {
L
liuruilong 已提交
48
    this->use_optimize_ = use_optimize;
L
liuruilong 已提交
49 50
    this->program_ = p;
    if (this->use_optimize_) {
51
      this->program_desc_ = this->program_.optimizeProgram;
L
liuruilong 已提交
52
    } else {
53
      this->program_desc_ = this->program_.originProgram;
L
liuruilong 已提交
54 55
    }

L
liuruilong 已提交
56
    if (this->program_.originProgram == nullptr) {
57
      LOG(paddle_mobile::LogLevel::kLOG_ERROR) << "program_desc_ == nullptr";
L
liuruilong 已提交
58
    }
59

L
liuruilong 已提交
60
    const std::vector<std::shared_ptr<BlockDesc>> blocks =
61 62 63
        this->program_desc_->Blocks();
    for (int block_id = 0; block_id < blocks.size(); ++block_id) {
      std::vector<std::shared_ptr<OpDesc>> ops = blocks[block_id]->Ops();
64 65
      for (int i = 0; i < ops.size(); ++i) {
        auto op = ops[i];
L
liuruilong 已提交
66
        if (op->Type() == op_type) {
L
liuruilong 已提交
67 68
          DLOG << "匹配到: " << op->Type();

E
eclipsess 已提交
69
          /// test first meeting op in program
Z
zhaojiaying01 已提交
70
          std::shared_ptr<paddle_mobile::framework::OperatorBase<DeviceType>>
L
liuruilong 已提交
71 72 73 74
              op_ptr =
                  paddle_mobile::framework::OpRegistry<DeviceType>::CreateOp(
                      op->Type(), op->GetInputs(), op->GetOutputs(),
                      op->GetAttrMap(), this->program_.scope);
75
          this->ops_of_block_[block_id].push_back(op_ptr);
L
liuruilong 已提交
76
          break;
L
liuruilong 已提交
77 78 79
        }
      }
    }
80
    this->InitMemory();
81 82 83 84
    for (const auto &ops : this->ops_of_block_) {
      for (const auto &op : ops) {
        op->Init();
      }
85
    }
L
liuruilong 已提交
86 87
  }

E
eclipsess 已提交
88
  template <typename T = LoDTensor>
L
liuruilong 已提交
89
  vector<std::shared_ptr<Tensor>> Predict(const vector<Tensor> &ts,
E
eclipsess 已提交
90 91 92
                                          const vector<string> &input_names,
                                          const vector<string> &output_names,
                                          const vector<DDim> &ddims) {
L
liuruilong 已提交
93
    auto scope = this->program_.scope;
E
eclipsess 已提交
94 95
    size_t input_size = input_names.size();
    size_t out_size = output_names.size();
L
liuruilong 已提交
96

E
eclipsess 已提交
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
    vector<Variable *> input_vars(input_size);
    vector<LoDTensor *> input_tensors(input_size);
    for (int i = 0; i < input_size; i++) {
      input_vars[i] = scope->Var(input_names[i]);
      input_tensors[i] = input_vars[i]->GetMutable<T>();
      input_tensors[i]->ShareDataWith(ts[i]);
    }

    vector<Variable *> output_vars(out_size);
    vector<LoDTensor *> output_tensors(out_size);
    vector<std::shared_ptr<Tensor>> output_tensor_sptrs(out_size);

    for (int i = 0; i < out_size; i++) {
      output_vars[i] = scope->Var(output_names[i]);
      output_tensors[i] = output_vars[i]->GetMutable<T>();
      output_tensors[i]->mutable_data<float>(ddims[i]);
      output_tensor_sptrs[i] = std::make_shared<LoDTensor>();
      output_tensor_sptrs[i].reset(output_tensors[i]);
    }
L
liuruilong 已提交
116

117 118 119 120
    for (auto &ops : this->ops_of_block_) {
      for (auto &op : ops) {
        op->Run();
      }
L
liuruilong 已提交
121 122
    }

E
eclipsess 已提交
123
    return output_tensor_sptrs;
L
liuruilong 已提交
124
  }
125

L
liuruilong 已提交
126
  std::shared_ptr<Tensor> Predict(const Tensor &t, string input, string output,
127 128 129 130 131 132 133 134 135 136
                                  const DDim &dDim) {
    auto scope = this->program_.scope;
    Variable *g_feed_value = scope->Var(input);
    auto tensor = g_feed_value->GetMutable<LoDTensor>();
    tensor->ShareDataWith(t);

    Variable *con_output = scope->Var(output);
    auto *output_tensor = con_output->GetMutable<LoDTensor>();
    output_tensor->mutable_data<float>(dDim);

137 138 139 140
    for (auto &ops : this->ops_of_block_) {
      for (auto &op : ops) {
        op->Run();
      }
141
    }
142 143
    return std::make_shared<paddle_mobile::framework::Tensor>(
        paddle_mobile::framework::Tensor(*output_tensor));
144
  }
L
liuruilong 已提交
145
};