operator.cc 3.1 KB
Newer Older
Q
Qiao Longfei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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. */

Y
Yan Chunwei 已提交
15 16
#include <algorithm>

Q
Qiao Longfei 已提交
17 18 19 20 21
#include "paddle/framework/operator.h"

namespace paddle {
namespace framework {

Q
qijun 已提交
22
template <>
23
Eigen::DefaultDevice& ExecutionContext::GetEigenDevice<
Q
qijun 已提交
24
    platform::CPUPlace, Eigen::DefaultDevice>() const {
D
dongzhihong 已提交
25
  return *device_context_->get_eigen_device<Eigen::DefaultDevice>();
Q
qijun 已提交
26 27 28 29
}

#ifndef PADDLE_ONLY_CPU
template <>
30
Eigen::GpuDevice&
31
ExecutionContext::GetEigenDevice<platform::GPUPlace, Eigen::GpuDevice>() const {
D
dongzhihong 已提交
32
  return *device_context_->get_eigen_device<Eigen::GpuDevice>();
Q
qijun 已提交
33 34 35
}
#endif

Y
Yan Chunwei 已提交
36
const std::string& OperatorBase::Input(const std::string& name) const {
Y
Yu Yang 已提交
37 38
  auto it = inputs_.find(name);
  PADDLE_ENFORCE(it != inputs_.end(), "Op %s does not have output %s", type_,
Y
Yu Yang 已提交
39
                 name);
Y
Yu Yang 已提交
40 41 42 43
  PADDLE_ENFORCE_EQ(it->second.size(), 1UL,
                    "Op %s input %s should contain only one variable", type_,
                    name);
  return it->second[0];
Y
Yan Chunwei 已提交
44 45
}

Y
Yu Yang 已提交
46 47 48
const std::vector<std::string>& OperatorBase::Inputs(
    const std::string& name) const {
  return inputs_.at(name);
Y
Yan Chunwei 已提交
49 50 51
}

const std::string& OperatorBase::Output(const std::string& name) const {
Y
Yu Yang 已提交
52 53
  auto it = outputs_.find(name);
  PADDLE_ENFORCE(it != outputs_.end(), "Op %s does not have output %s", type_,
Y
Yu Yang 已提交
54
                 name);
Y
Yu Yang 已提交
55 56 57 58
  PADDLE_ENFORCE_EQ(it->second.size(), 1UL,
                    "Op %s input %s should contain only one variable", type_,
                    name);
  return it->second[0];
Y
Yan Chunwei 已提交
59 60
}

Y
Yu Yang 已提交
61 62 63
const std::vector<std::string>& OperatorBase::Outputs(
    const std::string& name) const {
  return outputs_.at(name);
Y
Yan Chunwei 已提交
64 65
}

Q
Qiao Longfei 已提交
66 67
std::string OperatorBase::DebugString() const {
  std::stringstream ss;
Y
Yu Yang 已提交
68 69 70 71 72 73 74 75
  ss << "Op(" << type_ << "), inputs:{";
  for (auto& input : inputs_) {
    ss << input.first << "[";
    for (size_t i = 0; i < input.second.size(); ++i) {
      ss << input.second[i];
      if (i != input.second.size() - 1) {
        ss << ", ";
      }
76
    }
Y
Yu Yang 已提交
77
    ss << "]";
Q
Qiao Longfei 已提交
78
  }
Y
Yu Yang 已提交
79 80 81 82 83 84 85 86
  ss << "}, outputs:{";
  for (auto& output : outputs_) {
    ss << output.first << "[";
    for (size_t i = 0; i < output.second.size(); ++i) {
      ss << output.second[i];
      if (i != output.second.size() - 1) {
        ss << ", ";
      }
87
    }
Y
Yu Yang 已提交
88
    ss << "]";
Q
Qiao Longfei 已提交
89
  }
Y
Yu Yang 已提交
90
  ss << "}.";
Q
Qiao Longfei 已提交
91 92 93
  return ss.str();
}

D
dongzhihong 已提交
94 95
void OperatorBase::Rename(const std::string& old_name,
                          const std::string& new_name) {
Y
Yu Yang 已提交
96 97 98 99 100 101 102
  for (auto& input : inputs_) {
    std::replace(input.second.begin(), input.second.end(), old_name, new_name);
  }
  for (auto& output : outputs_) {
    std::replace(output.second.begin(), output.second.end(), old_name,
                 new_name);
  }
D
dongzhihong 已提交
103 104
}

Q
Qiao Longfei 已提交
105
}  // namespace framework
L
liaogang 已提交
106
}  // namespace paddle