assign_op.cc 5.9 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
#include "paddle/fluid/operators/assign_op.h"

#include <string>
Y
Yu Yang 已提交
18

W
wanghuancoder 已提交
19 20 21 22 23 24 25 26 27 28
namespace paddle {
namespace framework {
class OpDesc;
class Variable;
}  // namespace framework
namespace imperative {
class OpBase;
}  // namespace imperative
}  // namespace paddle

Y
Yu Yang 已提交
29 30 31
namespace paddle {
namespace operators {

32
class AssignOp : public framework::OperatorWithKernel {
Y
Yu Yang 已提交
33 34 35 36
 public:
  AssignOp(const std::string &type, const framework::VariableNameMap &inputs,
           const framework::VariableNameMap &outputs,
           const framework::AttributeMap &attrs)
37
      : OperatorWithKernel(type, inputs, outputs, attrs) {}
38

39 40 41 42 43 44 45 46 47
  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");
        }
48 49 50 51 52 53 54
      } else if (type == framework::proto::VarType::LOD_TENSOR_ARRAY) {
        if (ctx->IsRuntime()) {
          // The runtime output shape is determined in kernel.
          return;
        } else {
          ctx->SetOutputDim("Out", ctx->GetInputDim("X"));
        }
55 56 57 58 59
      }
    }
  }

 protected:
60 61 62 63 64 65 66 67
  framework::OpKernelType GetKernelTypeForVar(
      const std::string &var_name, const framework::Tensor &tensor,
      const framework::OpKernelType &expected_kernel_type) const override {
    return framework::OpKernelType(expected_kernel_type.data_type_,
                                   expected_kernel_type.place_,
                                   tensor.layout());
  }

68 69
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext &ctx) const override {
L
liym27 已提交
70 71 72 73 74 75 76 77 78 79 80
    const framework::Variable *var = ctx.InputVar("X");
    if (var->IsType<framework::LoDTensorArray>()) {
      auto t_arr = var->Get<framework::LoDTensorArray>();
      // NOTE(liym27): Support an empty tensor array as Input.
      // And set the kernel type is float.
      if (t_arr.size() == 0) {
        return framework::OpKernelType(framework::proto::VarType::FP32,
                                       ctx.device_context());
      }
    }

81 82 83
    return framework::OpKernelType(
        OperatorWithKernel::IndicateVarDataType(ctx, "X"),
        ctx.device_context());
84 85 86
  }
};

87 88 89
class AssignInferVarType : public framework::VarTypeInference {
 public:
  void operator()(framework::InferVarTypeContext *ctx) const override {
90
    ctx->SyncTypeAndDataType("X", "Out");
91 92 93
  }
};

94 95 96 97
class AssignKernel {
 public:
  void operator()(const framework::ExecutionContext &ctx) const {
    auto *x = ctx.InputVar("X");
Y
Yu Yang 已提交
98 99 100
    if (x == nullptr) {
      return;
    }
101 102 103
    PADDLE_ENFORCE_EQ(
        ctx.HasOutput("Out"), true,
        platform::errors::NotFound("Output(Out) of assign_op is not found."));
104
    auto *out = ctx.OutputVar("Out");
Y
Yu Yang 已提交
105
    platform::DeviceContextPool &pool = platform::DeviceContextPool::Instance();
106
    auto &dev_ctx = *pool.Get(ctx.GetPlace());
D
dzhwinter 已提交
107

Y
Yu Yang 已提交
108 109 110 111 112 113
    framework::VisitVarType(*x, AssignFunctor(out, dev_ctx));
  }
};

class AssignOpProtoMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
114
  void Make() override {
Y
Yu Yang 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
    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 已提交
130 131
template <typename T>
class AssignGradMaker : public framework::SingleGradOpMaker<T> {
Y
Yu Yang 已提交
132
 public:
H
hong 已提交
133
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
Y
Yu Yang 已提交
134 135

 protected:
136
  void Apply(GradOpPtr<T> op) const override {
Y
Yu Yang 已提交
137
    op->SetType("assign");
H
hong 已提交
138 139
    op->SetInput("X", this->OutputGrad("Out"));
    op->SetOutput("Out", this->InputGrad("X"));
Y
Yu Yang 已提交
140 141 142
  }
};

143 144
DECLARE_INPLACE_OP_INFERER(AssignOpInplaceInferer, {"X", "Out"});

Y
Yu Yang 已提交
145 146 147 148
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
149
namespace plat = paddle::platform;
H
hong 已提交
150 151 152
REGISTER_OPERATOR(assign, ops::AssignOp,
                  ops::AssignGradMaker<paddle::framework::OpDesc>,
                  ops::AssignGradMaker<paddle::imperative::OpBase>,
153 154
                  ops::AssignOpProtoMaker, ops::AssignOpInplaceInferer,
                  ops::AssignInferVarType);
H
hong 已提交
155

156 157
REGISTER_OP_CPU_KERNEL_FUNCTOR(assign, float, ops::AssignKernel, double,
                               ops::AssignKernel, int, ops::AssignKernel,
158 159 160
                               int64_t, ops::AssignKernel, uint8_t,
                               ops::AssignKernel, bool, ops::AssignKernel,
                               plat::float16, ops::AssignKernel, plat::bfloat16,
G
Guo Sheng 已提交
161
                               ops::AssignKernel);
162

163
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
164 165
REGISTER_OP_CUDA_KERNEL_FUNCTOR(assign, float, ops::AssignKernel, double,
                                ops::AssignKernel, int, ops::AssignKernel,
166 167 168
                                int64_t, ops::AssignKernel, uint8_t,
                                ops::AssignKernel, bool, ops::AssignKernel,
                                plat::float16, ops::AssignKernel);
169
#endif