feed_op.cc 3.0 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 17
#include "paddle/fluid/framework/feed_fetch_type.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
18
#include "paddle/fluid/platform/profiler.h"
Q
qijun 已提交
19 20 21

namespace paddle {
namespace operators {
Y
Yu Yang 已提交
22
class FeedOp : public framework::OperatorBase {
Q
qijun 已提交
23
 public:
Y
Yu Yang 已提交
24 25 26 27
  FeedOp(const std::string &type, const framework::VariableNameMap &inputs,
         const framework::VariableNameMap &outputs,
         const framework::AttributeMap &attrs)
      : OperatorBase(type, inputs, outputs, attrs) {}
28 29 30 31

 private:
  void RunImpl(const framework::Scope &scope,
               const platform::Place &place) const override {
32 33 34 35
    // get device context from pool
    auto *dev_ctx = platform::DeviceContextPool::Instance().Get(place);
    platform::RecordEvent record_event(Type(), dev_ctx);

Y
Yu Yang 已提交
36
    auto feed_var_name = Input("X");
Y
Yu Yang 已提交
37
    auto *feed_var = scope.FindVar(feed_var_name);
Y
Yu Yang 已提交
38

Y
Yu Yang 已提交
39 40 41 42 43 44 45 46 47 48 49 50
    PADDLE_ENFORCE(feed_var != nullptr,
                   "Cannot find feed_var in scope, feed_var_name is %s",
                   feed_var_name);

    auto out_name = this->Output("Out");
    auto *out_var = scope.FindVar(out_name);
    PADDLE_ENFORCE(out_var != nullptr,
                   "Cannot find out_var in scope, out_var_name is %s",
                   out_name);

    auto col = Attr<int>("col");

51
    VLOG(3) << "Feed Var " << feed_var_name << "'s " << col << " column to var "
Y
Yu Yang 已提交
52 53
            << out_name;

Y
Yu Yang 已提交
54 55 56
    auto &feed_list = feed_var->Get<framework::FeedFetchList>();
    auto &feed_item = feed_list.at(static_cast<size_t>(col));
    auto *out_item = out_var->GetMutable<framework::FeedFetchType>();
D
dzhwinter 已提交
57

C
chengduoZH 已提交
58 59 60
    if (platform::is_same_place(feed_item.place(), place)) {
      out_item->ShareDataWith(feed_item);
    } else {
61
      framework::TensorCopy(feed_item, place, *dev_ctx, out_item);
C
chengduoZH 已提交
62
    }
Y
Yu Yang 已提交
63
    out_item->set_lod(feed_item.lod());
Q
qijun 已提交
64 65 66
  }
};

Y
Yu Yang 已提交
67 68
class FeedOpInfoMaker : public framework::OpProtoAndCheckerMaker {
 public:
69
  FeedOpInfoMaker(OpProto *proto, OpAttrChecker *op_checker)
Y
Yu Yang 已提交
70 71 72
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddInput("X", "The input of feed op");
    AddOutput("Out", "The output of feed op");
K
kexinzhao 已提交
73 74 75 76 77 78 79
    AddAttr<int>("col", "(int) The column of feed");
    AddComment(R"DOC(
Feed Operator.

It should not be configured by users directly.

)DOC");
Y
Yu Yang 已提交
80 81 82
  }
};

Q
qijun 已提交
83 84 85
}  // namespace operators
}  // namespace paddle

Y
Yu Yang 已提交
86
REGISTER_OPERATOR(feed, paddle::operators::FeedOp,
Y
Yu Yang 已提交
87 88
                  paddle::framework::EmptyGradOpMaker,
                  paddle::operators::FeedOpInfoMaker);