operator.cpp 4.9 KB
Newer Older
Z
zhaojiaying01 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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. */

#include "framework/operator.h"
J
jameswu2014 已提交
16
#include <memory>
17
#include "operators/op_param.h"
朔-望's avatar
朔-望 已提交
18
namespace paddle_mobile {
朔-望's avatar
朔-望 已提交
19
namespace framework {
朔-望's avatar
朔-望 已提交
20

21 22 23 24 25
template <typename Dtype>
vector<string> OperatorBase<Dtype>::GetOutKeys() const {
  auto it = op_input_output_key.find(type_);
  if (it == op_input_output_key.end()) {
    DLOG << type_ << " has no outputs";
26
    return {};
27 28 29 30
  }
  return it->second.second;
}

31 32 33 34
template <typename Dtype>
vector<string> OperatorBase<Dtype>::GetInputKeys() const {
  auto it = op_input_output_key.find(type_);
  if (it == op_input_output_key.end()) {
35
    DLOG << type_ << " has no inputs";
36 37 38 39 40
    return {};
  }
  return it->second.first;
}

朔-望's avatar
朔-望 已提交
41 42 43 44 45
template <typename Dtype>
OperatorBase<Dtype>::OperatorBase(const std::string &type,
                                  const VariableNameMap &inputs,
                                  const VariableNameMap &outputs,
                                  const AttributeMap &attrs,
46
                                  framework::Scope *scope)
朔-望's avatar
朔-望 已提交
47 48 49 50
    : type_(type),
      inputs_(inputs),
      outputs_(outputs),
      attrs_(attrs),
朔-望's avatar
朔-望 已提交
51
      scope_(scope) {
52
  CheckAllInputOutputSet();
朔-望's avatar
朔-望 已提交
53
}
54

朔-望's avatar
朔-望 已提交
55 56
template <typename Dtype>
void OperatorBase<Dtype>::CheckAllInputOutputSet() const {}
W
wangliu 已提交
57

58
template <typename Dtype>
L
liuruilong 已提交
59
void OperatorBase<Dtype>::Run() {
60
  RunImpl();
Y
yangfei 已提交
61 62 63 64 65 66
#ifdef PADDLE_MOBILE_DEBUG
  DLOG << "-------------" << type_ << "----------------------------";
  vector<string> input_keys = GetInputKeys();
  for (const auto key : input_keys) {
    auto var_vec_in = inputs_.at(key);
    for (int i = 0; i < var_vec_in.size(); ++i) {
H
update  
hjchen2 已提交
67 68 69 70
      auto var = this->scope_->FindVar(var_vec_in[i]);
      if (var->IsInitialized() &&
          var->template IsType<framework::LoDTensor>()) {
        const Tensor *tensor = var->template Get<framework::LoDTensor>();
Y
yangfei 已提交
71
        if (tensor) DLOG << type_ << " input- " << key << "=" << *tensor;
72 73 74
#ifdef PADDLE_MOBILE_FPGA
        DLOG << var_vec_in[i];
#endif
Y
yangfei 已提交
75 76 77 78 79 80
      }
    }
  }
  for (const auto key : GetOutKeys()) {
    auto var_vec_out = outputs_.at(key);
    for (int i = 0; i < var_vec_out.size(); ++i) {
H
update  
hjchen2 已提交
81 82 83 84
      auto var = scope_->FindVar(var_vec_out[i]);
      if (var->IsInitialized() &&
          var->template IsType<framework::LoDTensor>()) {
        const Tensor *tensor = var->template Get<framework::LoDTensor>();
Y
yangfei 已提交
85
        if (tensor) DLOG << type_ << " output- " << key << "=" << *tensor;
86 87 88
#ifdef PADDLE_MOBILE_FPGA
        DLOG << var_vec_out[i];
#endif
Y
yangfei 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
      }
    }
  }
#endif
}

#ifdef PADDLE_MOBILE_CL
template <>
void OperatorBase<GPU_CL>::Run() {
  RunImpl();
#ifdef PADDLE_MOBILE_DEBUG
  DLOG << "-------------" << type_ << "----------------------------";
  vector<string> input_keys = GetInputKeys();
  for (const auto key : input_keys) {
    auto var_vec_in = inputs_.at(key);
    for (int i = 0; i < var_vec_in.size(); ++i) {
105 106 107 108 109
      auto var = scope_->FindVar(var_vec_in[i]);
      if (var->IsInitialized() && var->template IsType<framework::CLImage>()) {
        const CLImage *cl_image = var->template Get<framework::CLImage>();
        if (cl_image) {
          DLOG << type_ << " input- " << key << "=" << *cl_image;
Y
yangfei 已提交
110 111 112 113 114 115 116
        }
      }
    }
  }
  for (const auto key : GetOutKeys()) {
    auto var_vec_out = outputs_.at(key);
    for (int i = 0; i < var_vec_out.size(); ++i) {
117 118 119 120 121
      auto var = scope_->FindVar(var_vec_out[i]);
      if (var->IsInitialized() && var->template IsType<framework::CLImage>()) {
        const CLImage *cl_image = var->template Get<framework::CLImage>();
        if (cl_image) {
          DLOG << type_ << " output- " << key << "=" << *cl_image;
Y
yangfei 已提交
122 123 124 125 126
        }
      }
    }
  }
#endif
127
}
Y
yangfei 已提交
128
#endif
129

130 131
#ifdef PADDLE_MOBILE_FPGA
template <typename Dtype>
132 133 134 135 136 137 138 139 140 141 142 143 144
void OperatorBase<Dtype>::InsertTensors() {
  static int feed_num = 0;
  static int fetch_num = 0;
  if (type_ == "feed") {
    auto new_name = string("feed") + std::to_string(feed_num++);
    auto var = scope_->Var(new_name);
    var->template GetMutable<framework::LoDTensor>();
    inputs_.at("X") = {string(new_name)};
  } else if (type_ == "fetch") {
    auto new_name = string("fetch") + std::to_string(fetch_num++);
    auto var = scope_->Var(new_name);
    var->template GetMutable<framework::LoDTensor>();
    outputs_.at("Out") = {string(new_name)};
145 146 147 148
  }
}
#endif

朔-望's avatar
朔-望 已提交
149
template class OperatorBase<CPU>;
L
for  
liuruilong 已提交
150
template class OperatorBase<FPGA>;
L
liuruilong 已提交
151
template class OperatorBase<GPU_CL>;
L
for  
liuruilong 已提交
152

朔-望's avatar
朔-望 已提交
153 154
}  // namespace framework
}  // namespace paddle_mobile