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

Y
Yu Yang 已提交
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

Y
Yu Yang 已提交
7
   http://www.apache.org/licenses/LICENSE-2.0
Q
qijun 已提交
8

Y
Yu Yang 已提交
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
Yu Yang 已提交
15 16 17
#include "paddle/framework/feed_fetch_type.h"
#include "paddle/framework/op_registry.h"
#include "paddle/framework/operator.h"
Q
qijun 已提交
18 19 20

namespace paddle {
namespace operators {
Y
Yu Yang 已提交
21
class FeedOp : public framework::OperatorBase {
Q
qijun 已提交
22
 public:
Y
Yu Yang 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
  FeedOp(const std::string &type, const framework::VariableNameMap &inputs,
         const framework::VariableNameMap &outputs,
         const framework::AttributeMap &attrs)
      : OperatorBase(type, inputs, outputs, attrs) {}
  void Run(const framework::Scope &scope,
           const platform::DeviceContext &dev_ctx) const override {
    auto feed_var_name = Input("Input");
    auto *feed_var = scope.FindVar(feed_var_name);
    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");

    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>();
    out_item->CopyFromTensor(feed_item, dev_ctx.GetPlace(), dev_ctx);
    out_item->set_lod(feed_item.lod());
Q
qijun 已提交
48 49 50 51 52 53
  }
};

}  // namespace operators
}  // namespace paddle

Y
Yu Yang 已提交
54 55 56 57
// We do not need to register OpInfoMaker,
// since feed operator will not be used by end users directly
REGISTER_OPERATOR(feed, paddle::operators::FeedOp,
                  paddle::framework::EmptyGradOpMaker);