feed_op.cc 6.0 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
L
Luo Tao 已提交
2 3 4 5 6 7 8 9 10
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. */
Q
qijun 已提交
11

Y
Yi Wang 已提交
12 13
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/framework/operator.h"
14
#include "paddle/phi/core/tensor_utils.h"
Q
qijun 已提交
15

W
wanghuancoder 已提交
16 17 18 19 20 21 22 23 24 25 26 27
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 已提交
28 29
namespace paddle {
namespace operators {
S
Steffy-zxf 已提交
30 31

// FeedVariableVisitor is to feed the variable data
32
// according to data type (phi::DenseTensor or  Strings).
33
class FeedVariableVisitor {
S
Steffy-zxf 已提交
34 35 36 37 38
 public:
  explicit FeedVariableVisitor(framework::Variable *out_var,
                               const platform::Place &place)
      : out_var_(out_var), place_(place) {}

39 40
  void operator()(const phi::DenseTensor &in_tensor) const {
    phi::DenseTensor *out_tensor = out_var_->GetMutable<phi::DenseTensor>();
S
Steffy-zxf 已提交
41 42
    if (platform::is_same_place(in_tensor.place(), place_)) {
      out_tensor->ShareDataWith(in_tensor);
A
Allen Guo 已提交
43 44 45 46 47 48 49
#ifdef PADDLE_WITH_IPU
    } else if (platform::is_ipu_place(place_)) {
      // For ipu, both in_tensor and out_tensor are allocated on cpu,
      // PopART will copy tensor from host automatically,
      // no TensorCopy() is required here.
      out_tensor->ShareDataWith(in_tensor);
#endif
S
Steffy-zxf 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63
    } else {
      platform::DeviceContext *context =
          platform::DeviceContextPool::Instance().Get(place_);
      framework::TensorCopy(in_tensor, place_, *context, out_tensor);
    }
    out_tensor->set_lod(in_tensor.lod());
  }

  void operator()(const framework::Strings &in_str) const {
    framework::Strings *out_str = out_var_->GetMutable<framework::Strings>();
    out_str->resize(in_str.size());
    *out_str = in_str;
  }

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
  void operator()(const phi::SparseCooTensor &in_tensor) const {
    phi::SparseCooTensor *out_tensor =
        out_var_->GetMutable<phi::SparseCooTensor>();
    if (platform::is_same_place(in_tensor.place(), place_)) {
      *out_tensor = in_tensor;
    } else {
      platform::DeviceContext *context =
          platform::DeviceContextPool::Instance().Get(place_);

      phi::DenseTensor indices, values;
      framework::TensorCopy(in_tensor.indices(), place_, *context, &indices);
      framework::TensorCopy(in_tensor.values(), place_, *context, &values);
      out_tensor->SetMember(indices, values, in_tensor.meta());
    }
  }

S
Steffy-zxf 已提交
80 81 82 83 84
 private:
  framework::Variable *out_var_;
  const platform::Place &place_;
};

Y
Yu Yang 已提交
85
class FeedOp : public framework::OperatorBase {
Q
qijun 已提交
86
 public:
87 88
  FeedOp(const std::string &type,
         const framework::VariableNameMap &inputs,
Y
Yu Yang 已提交
89 90 91
         const framework::VariableNameMap &outputs,
         const framework::AttributeMap &attrs)
      : OperatorBase(type, inputs, outputs, attrs) {}
92 93 94 95

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

Y
Yu Yang 已提交
99
    auto feed_var_name = Input("X");
Y
Yu Yang 已提交
100
    auto *feed_var = scope.FindVar(feed_var_name);
101 102 103 104 105
    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 已提交
106 107 108

    auto out_name = this->Output("Out");
    auto *out_var = scope.FindVar(out_name);
109 110 111 112 113
    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 已提交
114 115

    auto col = Attr<int>("col");
116 117
    PADDLE_ENFORCE_GE(col,
                      0,
118 119 120 121 122
                      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 已提交
123

124 125
    VLOG(3) << "Feed variable " << feed_var_name << "'s " << col
            << " column to variable " << out_name;
Y
Yu Yang 已提交
126

127
    auto &feed_list = feed_var->Get<framework::FeedList>();
128
    PADDLE_ENFORCE_LT(
129 130
        static_cast<size_t>(col),
        feed_list.size(),
131 132 133 134
        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",
135 136
            col,
            feed_list.size()));
137

Y
Yu Yang 已提交
138
    auto &feed_item = feed_list.at(static_cast<size_t>(col));
D
dzhwinter 已提交
139

S
Steffy-zxf 已提交
140
    FeedVariableVisitor visitor(out_var, place);
R
Ruibiao Chen 已提交
141
    paddle::visit(visitor, feed_item);
Q
qijun 已提交
142 143 144
  }
};

Y
Yu Yang 已提交
145 146
class FeedOpInfoMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
147
  void Make() override {
148
    AddInput("X",
149 150
             "(vector<phi::DenseTensor>) "
             "A feeding list of phi::DenseTensor, which may have "
151 152
             "different dimension and data type.");
    AddOutput("Out",
153
              "(phi::DenseTensor) The phi::DenseTensor which is a copy "
S
Steffy-zxf 已提交
154
              "of the col-th feeding "
155 156
              "object.");
    AddAttr<int>("col", "(int) The column index of current feeding object.");
K
kexinzhao 已提交
157 158 159 160
    AddComment(R"DOC(
Feed Operator.
It should not be configured by users directly.
)DOC");
Y
Yu Yang 已提交
161 162 163
  }
};

Q
qijun 已提交
164 165 166
}  // namespace operators
}  // namespace paddle

H
hong 已提交
167
REGISTER_OPERATOR(
168 169
    feed,
    paddle::operators::FeedOp,
H
hong 已提交
170 171 172
    paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
    paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
    paddle::operators::FeedOpInfoMaker);