assign_op.cc 4.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yu Yang 已提交
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
Y
Yu Yang 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
Y
Yu Yang 已提交
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. */
Y
Yu Yang 已提交
14

15 16 17 18
#include "paddle/fluid/operators/assign_op.h"

#include <memory>
#include <string>
Y
Yu Yang 已提交
19 20 21 22

namespace paddle {
namespace operators {

23
class AssignOp : public framework::OperatorWithKernel {
Y
Yu Yang 已提交
24 25 26 27
 public:
  AssignOp(const std::string &type, const framework::VariableNameMap &inputs,
           const framework::VariableNameMap &outputs,
           const framework::AttributeMap &attrs)
28
      : OperatorWithKernel(type, inputs, outputs, attrs) {}
29

30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
  void InferShape(framework::InferShapeContext *ctx) const override {
    if (ctx->HasInput("X")) {
      auto type = ctx->GetInputsVarType("X")[0];
      if (type == framework::proto::VarType::SELECTED_ROWS ||
          type == framework::proto::VarType::LOD_TENSOR) {
        ctx->SetOutputDim("Out", ctx->GetInputDim("X"));
        if (type == framework::proto::VarType::LOD_TENSOR) {
          ctx->ShareLoD("X", /*->*/ "Out");
        }
      }
    }
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext &ctx) const override {
46 47 48
    return framework::OpKernelType(
        OperatorWithKernel::IndicateVarDataType(ctx, "X"),
        ctx.device_context());
49 50 51 52 53 54 55
  }
};

class AssignKernel {
 public:
  void operator()(const framework::ExecutionContext &ctx) const {
    auto *x = ctx.InputVar("X");
Y
Yu Yang 已提交
56 57 58
    if (x == nullptr) {
      return;
    }
59
    auto *out = ctx.OutputVar("Out");
Y
Yu Yang 已提交
60 61 62
    PADDLE_ENFORCE(
        out != nullptr,
        "The Output(Out) should not be null if the Input(X) is set.");
Y
Yu Yang 已提交
63
    platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance();
64
    auto &dev_ctx = *pool.Get(ctx.GetPlace());
D
dzhwinter 已提交
65

Y
Yu Yang 已提交
66 67 68 69 70 71
    framework::VisitVarType(*x, AssignFunctor(out, dev_ctx));
  }
};

class AssignOpProtoMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
72
  void Make() override {
Y
Yu Yang 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    AddInput("X",
             "(LoDTensor, SelectedRows or LoDTensorArray) The input variable "
             "could be LoDTensor, SelectedRows or LoDTensorArray.")
        .AsDispensable();
    AddOutput("Out",
              "(LoDTensor, SelectedRows or LoDTensorArray) The type of output "
              "is the same as input X.");
    AddComment(R"DOC(Assign Operator

Out = X,  when type in [LoDTensor/SelectedRows/LoDTensorArray]
raise error if the type is not listed above.
)DOC");
  }
};

H
hong 已提交
88 89
template <typename T>
class AssignGradMaker : public framework::SingleGradOpMaker<T> {
Y
Yu Yang 已提交
90
 public:
H
hong 已提交
91
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
Y
Yu Yang 已提交
92 93

 protected:
H
hong 已提交
94 95
  std::unique_ptr<T> Apply() const override {
    auto *op = new T();
Y
Yu Yang 已提交
96
    op->SetType("assign");
H
hong 已提交
97 98 99
    op->SetInput("X", this->OutputGrad("Out"));
    op->SetOutput("Out", this->InputGrad("X"));
    return std::unique_ptr<T>(op);
Y
Yu Yang 已提交
100 101 102
  }
};

103 104
DECLARE_INPLACE_OP_INFERER(AssignOpInplaceInferer, {"X", "Out"});

Y
Yu Yang 已提交
105 106 107 108
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
H
hong 已提交
109 110 111
REGISTER_OPERATOR(assign, ops::AssignOp,
                  ops::AssignGradMaker<paddle::framework::OpDesc>,
                  ops::AssignGradMaker<paddle::imperative::OpBase>,
112
                  ops::AssignOpProtoMaker, ops::AssignOpInplaceInferer);
H
hong 已提交
113

114 115
REGISTER_OP_CPU_KERNEL_FUNCTOR(assign, float, ops::AssignKernel, double,
                               ops::AssignKernel, int, ops::AssignKernel,
G
Guo Sheng 已提交
116 117
                               int64_t, ops::AssignKernel, bool,
                               ops::AssignKernel);
118 119 120 121

#ifdef PADDLE_WITH_CUDA
REGISTER_OP_CUDA_KERNEL_FUNCTOR(assign, float, ops::AssignKernel, double,
                                ops::AssignKernel, int, ops::AssignKernel,
G
Guo Sheng 已提交
122 123
                                int64_t, ops::AssignKernel, bool,
                                ops::AssignKernel);
124
#endif