operator.cpp 2.3 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"
16
#include "operators/op_param.h"
朔-望's avatar
朔-望 已提交
17 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 31 32 33 34 35 36 37 38 39 40 41 42
  }
  return it->second.second;
}

template <typename T>
static T *GetVarValue(const string &key, const VariableNameMap &var_map,
                      const Scope &scope) {
  auto var_vec = var_map.at(key);
  if (!var_vec.empty()) {
    auto var = scope.FindVar(var_vec[0]);
    return var->GetMutable<T>();
  } else {
    return nullptr;
  }
}

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

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

60 61 62 63 64 65 66 67 68 69 70 71
template <typename Dtype>
void OperatorBase<Dtype>::Run() const {
  RunImpl();
#ifdef PADDLE_MOBILE_DEBUG
  vector<string> output_keys = GetOutKeys();
  for (const auto key : output_keys) {
    Tensor *out_ = GetVarValue<framework::LoDTensor>(key, outputs_, *scope_);
    DLOG << type_ << " output- " << key << "=" << *out_;
  }
#endif
}

朔-望's avatar
朔-望 已提交
72 73
template class OperatorBase<CPU>;
template class OperatorWithKernel<CPU>;
朔-望's avatar
朔-望 已提交
74

朔-望's avatar
朔-望 已提交
75 76
}  // namespace framework
}  // namespace paddle_mobile