feed_op.cc 4.2 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Q
qijun 已提交
2

L
Luo Tao 已提交
3 4 5
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
Q
qijun 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
Q
qijun 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
Q
qijun 已提交
14

Y
Yi Wang 已提交
15 16
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
Q
qijun 已提交
17

W
wanghuancoder 已提交
18 19 20 21 22 23 24 25 26 27 28 29
namespace paddle {
namespace framework {
class OpDesc;
class Scope;
template <typename T>
class EmptyGradOpMaker;
}  // namespace framework
namespace imperative {
class OpBase;
}  // namespace imperative
}  // namespace paddle

Q
qijun 已提交
30 31
namespace paddle {
namespace operators {
Y
Yu Yang 已提交
32
class FeedOp : public framework::OperatorBase {
Q
qijun 已提交
33
 public:
Y
Yu Yang 已提交
34 35 36 37
  FeedOp(const std::string &type, const framework::VariableNameMap &inputs,
         const framework::VariableNameMap &outputs,
         const framework::AttributeMap &attrs)
      : OperatorBase(type, inputs, outputs, attrs) {}
38 39 40 41

 private:
  void RunImpl(const framework::Scope &scope,
               const platform::Place &place) const override {
42 43
    OP_INOUT_CHECK(HasInputs("X"), "Input", "X", "Feed");
    OP_INOUT_CHECK(HasOutputs("Out"), "Output", "Out", "Feed");
44

Y
Yu Yang 已提交
45
    auto feed_var_name = Input("X");
Y
Yu Yang 已提交
46
    auto *feed_var = scope.FindVar(feed_var_name);
47 48 49 50 51
    PADDLE_ENFORCE_NOT_NULL(
        feed_var,
        platform::errors::NotFound(
            "Input varibale(%s) cannot be found in scope for operator 'Feed'.",
            feed_var_name));
Y
Yu Yang 已提交
52 53 54

    auto out_name = this->Output("Out");
    auto *out_var = scope.FindVar(out_name);
55 56 57 58 59
    PADDLE_ENFORCE_NOT_NULL(
        out_var,
        platform::errors::NotFound(
            "Output variable(%s) cannot be found in scope for operator 'Feed'",
            out_name));
Y
Yu Yang 已提交
60 61

    auto col = Attr<int>("col");
62 63 64 65 66 67
    PADDLE_ENFORCE_GE(col, 0,
                      platform::errors::InvalidArgument(
                          "Expected the column index (the attribute 'col' of "
                          "operator 'Feed') of current feeding variable to be "
                          "no less than 0. But received column index = %d.",
                          col));
Y
Yu Yang 已提交
68

69 70
    VLOG(3) << "Feed variable " << feed_var_name << "'s " << col
            << " column to variable " << out_name;
Y
Yu Yang 已提交
71

72
    auto &feed_list = feed_var->Get<framework::FeedList>();
73 74 75 76 77 78 79 80
    PADDLE_ENFORCE_LT(
        static_cast<size_t>(col), feed_list.size(),
        platform::errors::InvalidArgument(
            "The column index of current feeding variable is expected to be "
            "less than the length of feeding list. But received column index = "
            "%d, the length of feeding list = %d",
            col, feed_list.size()));

Y
Yu Yang 已提交
81
    auto &feed_item = feed_list.at(static_cast<size_t>(col));
82
    auto *out_item = out_var->GetMutable<framework::FeedType>();
D
dzhwinter 已提交
83

C
chengduoZH 已提交
84 85 86
    if (platform::is_same_place(feed_item.place(), place)) {
      out_item->ShareDataWith(feed_item);
    } else {
87
      auto *dev_ctx = platform::DeviceContextPool::Instance().Get(place);
88
      framework::TensorCopy(feed_item, place, *dev_ctx, out_item);
C
chengduoZH 已提交
89
    }
Y
Yu Yang 已提交
90
    out_item->set_lod(feed_item.lod());
Q
qijun 已提交
91 92 93
  }
};

Y
Yu Yang 已提交
94 95
class FeedOpInfoMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
96
  void Make() override {
97 98 99 100 101 102 103
    AddInput("X",
             "(vector<LoDTensor>) A feeding list of LoDTensor, which may have "
             "different dimension and data type.");
    AddOutput("Out",
              "(LoDTensor) The LoDTensor which is a copy of the col-th feeding "
              "object.");
    AddAttr<int>("col", "(int) The column index of current feeding object.");
K
kexinzhao 已提交
104 105 106 107 108 109
    AddComment(R"DOC(
Feed Operator.

It should not be configured by users directly.

)DOC");
Y
Yu Yang 已提交
110 111 112
  }
};

Q
qijun 已提交
113 114 115
}  // namespace operators
}  // namespace paddle

H
hong 已提交
116 117 118 119 120
REGISTER_OPERATOR(
    feed, paddle::operators::FeedOp,
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
    paddle::operators::FeedOpInfoMaker);