|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + you may not use this file except in compliance with the License. |
| 5 | + You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | + Unless required by applicable law or agreed to in writing, software |
| 10 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + See the License for the specific language governing permissions and |
| 13 | + limitations under the License. */ |
| 14 | + |
| 15 | +#include "paddle/framework/data_type.h" |
| 16 | +#include "paddle/framework/op_registry.h" |
| 17 | +#include "paddle/framework/var_type.h" |
| 18 | + |
| 19 | +namespace paddle { |
| 20 | +namespace operators { |
| 21 | +class AssignFunctor { |
| 22 | + public: |
| 23 | + AssignFunctor(framework::Variable *out, |
| 24 | + const platform::DeviceContext &dev_ctx) |
| 25 | + : out_(out), dev_ctx_(dev_ctx) {} |
| 26 | + |
| 27 | + void operator()(const framework::LoDTensor &lod_tensor) const { |
| 28 | + auto &out_tensor = *out_->GetMutable<framework::LoDTensor>(); |
| 29 | + copy_tensor(lod_tensor, &out_tensor); |
| 30 | + } |
| 31 | + |
| 32 | + void operator()(const framework::LoDTensorArray &array) const { |
| 33 | + auto &out_array = *out_->GetMutable<framework::LoDTensorArray>(); |
| 34 | + out_array.resize(array.size()); |
| 35 | + for (size_t i = 0; i < array.size(); ++i) { |
| 36 | + copy_tensor(array[i], &out_array[i]); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + void operator()(const framework::SelectedRows &rows) const { |
| 41 | + framework::SelectedRows &out_rows = |
| 42 | + *out_->GetMutable<framework::SelectedRows>(); |
| 43 | + out_rows.set_rows(rows.rows()); |
| 44 | + out_rows.set_height(rows.height()); |
| 45 | + auto &t = rows.value(); |
| 46 | + out_rows.mutable_value()->CopyFrom(t, t.place(), dev_ctx_); |
| 47 | + } |
| 48 | + |
| 49 | + template <typename T> |
| 50 | + void operator()(const T &v) const { |
| 51 | + PADDLE_THROW("Not support type for assign op %s", typeid(T).name()); |
| 52 | + } |
| 53 | + |
| 54 | + private: |
| 55 | + void copy_tensor(const framework::LoDTensor &lod_tensor, |
| 56 | + framework::LoDTensor *out) const { |
| 57 | + auto &out_tensor = *out; |
| 58 | + out_tensor.CopyFrom(lod_tensor, lod_tensor.place(), dev_ctx_); |
| 59 | + out_tensor.set_lod(lod_tensor.lod()); |
| 60 | + } |
| 61 | + |
| 62 | + framework::Variable *out_; |
| 63 | + const platform::DeviceContext &dev_ctx_; |
| 64 | +}; |
| 65 | + |
| 66 | +class AssignOp : public framework::OperatorBase { |
| 67 | + public: |
| 68 | + AssignOp(const std::string &type, const framework::VariableNameMap &inputs, |
| 69 | + const framework::VariableNameMap &outputs, |
| 70 | + const framework::AttributeMap &attrs) |
| 71 | + : OperatorBase(type, inputs, outputs, attrs) {} |
| 72 | + void Run(const framework::Scope &scope, |
| 73 | + const platform::DeviceContext &dev_ctx) const override { |
| 74 | + auto *x = scope.FindVar(Input("X")); |
| 75 | + if (x == nullptr) { |
| 76 | + return; |
| 77 | + } |
| 78 | + auto *out = scope.FindVar(Output("Out")); |
| 79 | + PADDLE_ENFORCE( |
| 80 | + out != nullptr, |
| 81 | + "The Output(Out) should not be null if the Input(X) is set."); |
| 82 | + framework::VisitVarType(*x, AssignFunctor(out, dev_ctx)); |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | +class AssignOpProtoMaker : public framework::OpProtoAndCheckerMaker { |
| 87 | + public: |
| 88 | + AssignOpProtoMaker(framework::OpProto *proto, |
| 89 | + framework::OpAttrChecker *op_checker) |
| 90 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 91 | + AddInput("X", |
| 92 | + "(LoDTensor, SelectedRows or LoDTensorArray) The input variable " |
| 93 | + "could be LoDTensor, SelectedRows or LoDTensorArray.") |
| 94 | + .AsDispensable(); |
| 95 | + AddOutput("Out", |
| 96 | + "(LoDTensor, SelectedRows or LoDTensorArray) The type of output " |
| 97 | + "is the same as input X."); |
| 98 | + AddComment(R"DOC(Assign Operator |
| 99 | +
|
| 100 | +Out = X, when type in [LoDTensor/SelectedRows/LoDTensorArray] |
| 101 | +raise error if the type is not listed above. |
| 102 | +)DOC"); |
| 103 | + } |
| 104 | +}; |
| 105 | + |
| 106 | +class AssignInferShape : public framework::InferShapeBase { |
| 107 | + public: |
| 108 | + void operator()(framework::InferShapeContext *context) const override { |
| 109 | + if (context->HasInput("X")) { |
| 110 | + auto type = context->GetInputsVarType("X")[0]; |
| 111 | + if (type == framework::VarDesc_VarType_SELECTED_ROWS || |
| 112 | + type == framework::VarDesc_VarType_LOD_TENSOR) { |
| 113 | + context->SetOutputDim("Out", context->GetInputDim("X")); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | +}; |
| 118 | + |
| 119 | +class AssignGradMaker : public framework::SingleGradOpDescMaker { |
| 120 | + public: |
| 121 | + using framework::SingleGradOpDescMaker::SingleGradOpDescMaker; |
| 122 | + |
| 123 | + protected: |
| 124 | + std::unique_ptr<framework::OpDescBind> Apply() const override { |
| 125 | + auto *op = new framework::OpDescBind(); |
| 126 | + op->SetType("assign"); |
| 127 | + op->SetInput("X", OutputGrad("Out")); |
| 128 | + op->SetOutput("Out", InputGrad("X")); |
| 129 | + return std::unique_ptr<framework::OpDescBind>(op); |
| 130 | + } |
| 131 | +}; |
| 132 | + |
| 133 | +} // namespace operators |
| 134 | +} // namespace paddle |
| 135 | + |
| 136 | +namespace ops = paddle::operators; |
| 137 | +REGISTER_OPERATOR(assign, ops::AssignOp, ops::AssignGradMaker, |
| 138 | + ops::AssignInferShape, ops::AssignOpProtoMaker); |
0 commit comments