executor_for_test.h 4.3 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

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

L
liuruilong 已提交
32
using paddle_mobile::Executor;
L
liuruilong 已提交
33 34 35 36 37 38 39 40
using paddle_mobile::framework::BlockDesc;
using paddle_mobile::framework::DDim;
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 已提交
41
using std::vector;
L
liuruilong 已提交
42 43 44 45
template <typename DeviceType, typename OpType>
class Executor4Test : public Executor<DeviceType> {
 public:
  Executor4Test(Program<DeviceType> p, string op_type)
L
liuruilong 已提交
46 47 48 49 50 51 52 53
      : Executor<DeviceType>() {
    this->program_ = p;
    if (this->use_optimize_) {
      this->to_predict_program_ = this->program_.optimizeProgram;
    } else {
      this->to_predict_program_ = this->program_.originProgram;
    }

L
liuruilong 已提交
54 55 56 57 58 59 60 61 62 63
    if (this->program_.originProgram == nullptr) {
      LOG(paddle_mobile::LogLevel::kLOG_ERROR)
          << "to_predict_program_ == nullptr";
    }
    const std::vector<std::shared_ptr<BlockDesc>> blocks =
        this->to_predict_program_->Blocks();
    for (std::shared_ptr<BlockDesc> block_desc : blocks) {
      std::vector<std::shared_ptr<OpDesc>> ops = block_desc->Ops();
      for (std::shared_ptr<OpDesc> op : ops) {
        if (op->Type() == op_type) {
E
eclipsess 已提交
64
          /// test first meeting op in program
Z
zhaojiaying01 已提交
65 66 67 68 69 70
          std::shared_ptr<paddle_mobile::framework::OperatorBase<DeviceType>>
              op_ptr = paddle_mobile::framework::OpRegistry<
                  paddle_mobile::CPU>::CreateOp(op->Type(), op->GetInputs(),
                                                op->GetOutputs(),
                                                op->GetAttrMap(),
                                                this->program_.scope);
L
liuruilong 已提交
71 72 73 74 75 76 77
          this->ops_of_block_[*block_desc.get()].push_back(op_ptr);
          break;
        }
      }
    }
  }

E
eclipsess 已提交
78 79 80 81 82
  template <typename T = LoDTensor>
  vector<std::shared_ptr<Tensor>> predict(const vector<Tensor> &ts,
                                          const vector<string> &input_names,
                                          const vector<string> &output_names,
                                          const vector<DDim> &ddims) {
L
liuruilong 已提交
83
    auto scope = this->program_.scope;
E
eclipsess 已提交
84 85
    size_t input_size = input_names.size();
    size_t out_size = output_names.size();
L
liuruilong 已提交
86

E
eclipsess 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    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 已提交
106

L
liuruilong 已提交
107 108 109 110 111 112 113 114
    std::shared_ptr<paddle_mobile::framework::BlockDesc> to_predict_block =
        this->to_predict_program_->Block(0);
    for (int j = 0; j < this->ops_of_block_[*to_predict_block.get()].size();
         ++j) {
      auto op = this->ops_of_block_[*to_predict_block.get()][j];
      op->Run();
    }

E
eclipsess 已提交
115
    return output_tensor_sptrs;
L
liuruilong 已提交
116 117
  }
};