operator.cpp 4.8 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
  }
  return it->second.second;
}

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

朔-望's avatar
朔-望 已提交
41 42 43 44 45 46
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
朔-望 已提交
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() {
L
liuruilong 已提交
60
  DLOG << " begin run " << type_;
61
  RunImpl();
L
liuruilong 已提交
62
  DLOG << " end run " << type_;
L
liuruilong 已提交
63
  return;
W
wangliu 已提交
64
#ifdef PADDLE_MOBILE_DEBUG
xiebaiyuan's avatar
xiebaiyuan 已提交
65
  DLOG << "-------------" << type_ << "----------------------------";
66 67
  vector<string> input_keys = GetInputKeys();
  for (const auto key : input_keys) {
68 69 70
    auto var_vec_in = inputs_.at(key);
    for (int i = 0; i < var_vec_in.size(); ++i) {
      auto vari = scope_->FindVar(var_vec_in[i]);
71
      if (vari->IsInitialized()) {
Y
yangfei 已提交
72 73 74 75 76 77
#ifdef PADDLE_MOBILE_CL
        if (type_ == "feed") {
          Tensor *tensor = vari->template GetMutable<framework::LoDTensor>();
          if (tensor) DLOG << type_ << " input- " << key << "=" << *tensor;
        } else {
          CLImage *cl_image = vari->template GetMutable<framework::CLImage>();
Y
yangfei 已提交
78 79 80 81 82 83
          //                        cl_command_queue commandQueue =
          //                        scope_->GetCLScpoe()->CommandQueue(); Tensor
          //                        *tmp ;
          //                        CLImageToTensor(cl_image,tmp,commandQueue);
          //                        tmp->Resize(cl_image->dims());
          const float *input = cl_image->data<float>();
Y
yangfei 已提交
84 85
          if (cl_image) {
            DLOG << type_ << " input- " << key << "=" << cl_image->dims();
Y
yangfei 已提交
86 87
            //              if(input)
            //              DLOG<<type_<<" input- "<<key<<"="<<*input;
Y
yangfei 已提交
88 89 90 91
          }
        }

#else
92 93
        Tensor *tensor = vari->template GetMutable<framework::LoDTensor>();
        if (tensor) DLOG << type_ << " input- " << key << "=" << *tensor;
Y
yangfei 已提交
94
#endif
95
      }
96
    }
97
  }
98 99 100 101
  for (const auto key : GetOutKeys()) {
    auto var_vec_out = outputs_.at(key);
    for (int i = 0; i < var_vec_out.size(); ++i) {
      auto vari = scope_->FindVar(var_vec_out[i]);
102
      if (vari->IsInitialized()) {
Y
yangfei 已提交
103
#ifdef PADDLE_MOBILE_CL
Y
yangfei 已提交
104 105
        if (type_ == "fetch") {
          Tensor *tensor = vari->template GetMutable<framework::LoDTensor>();
L
liuruilong 已提交
106
          if (tensor) {
Y
yangfei 已提交
107
            DLOG << type_ << " output- " << key << "=" << tensor->dims();
L
liuruilong 已提交
108
          }
Y
yangfei 已提交
109 110 111 112 113 114 115 116 117 118 119 120
        } else {
          CLImage *cl_image = vari->template GetMutable<framework::CLImage>();
          //          cl_command_queue commandQueue =
          //          scope_->GetCLScpoe()->CommandQueue(); Tensor *tmp ;
          //          CLImageToTensor(cl_image,tmp,commandQueue);
          //          tmp->Resize(cl_image->dims());
          if (cl_image) {
            const float *output = cl_image->data<float>();
            DLOG << type_ << " output- " << key << "=" << cl_image->dims();
            //                  if(output)
            //                  DLOG<<type_<<" output- "<<key<<"="<<*output;
          }
Y
yangfei 已提交
121
        }
Y
yangfei 已提交
122

Y
yangfei 已提交
123
#else
124 125
        Tensor *tensor = vari->template GetMutable<framework::LoDTensor>();
        if (tensor) DLOG << type_ << " output- " << key << "=" << *tensor;
Y
yangfei 已提交
126
#endif
127
      }
128
    }
129 130 131 132
  }
#endif
}

朔-望's avatar
朔-望 已提交
133
template class OperatorBase<CPU>;
L
for  
liuruilong 已提交
134 135
template class OperatorBase<FPGA>;
template class OperatorBase<GPU_MALI>;
L
liuruilong 已提交
136
template class OperatorBase<GPU_CL>;
L
for  
liuruilong 已提交
137

朔-望's avatar
朔-望 已提交
138 139
}  // namespace framework
}  // namespace paddle_mobile