|
| 1 | +/* Copyright (c) 2021 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/fluid/operators/share_data_op.h" |
| 16 | +#include "paddle/fluid/framework/op_registry.h" |
| 17 | + |
| 18 | +namespace paddle { |
| 19 | +namespace operators { |
| 20 | + |
| 21 | +class ShareDataOp : public framework::OperatorWithKernel { |
| 22 | + public: |
| 23 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 24 | + |
| 25 | + void InferShape(framework::InferShapeContext *ctx) const override { |
| 26 | + OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "ShareData"); |
| 27 | + OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "ShareData"); |
| 28 | + auto in_type = ctx->GetInputsVarType("X")[0]; |
| 29 | + auto out_type = ctx->GetOutputsVarType("Out")[0]; |
| 30 | + |
| 31 | + PADDLE_ENFORCE_EQ( |
| 32 | + in_type == framework::proto::VarType::LOD_TENSOR || |
| 33 | + in_type == framework::proto::VarType::SELECTED_ROWS, |
| 34 | + true, platform::errors::InvalidArgument( |
| 35 | + "Type of Variable[X] must be LoDTensor or SelectedRows!")); |
| 36 | + PADDLE_ENFORCE_EQ( |
| 37 | + in_type, out_type, |
| 38 | + platform::errors::InvalidArgument( |
| 39 | + "The type of input (X) and output (Out) are inconsistent.")); |
| 40 | + |
| 41 | + ctx->ShareDim("X", "Out"); |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +class ShareDataOpMaker : public framework::OpProtoAndCheckerMaker { |
| 46 | + public: |
| 47 | + void Make() override { |
| 48 | + AddInput("X", "(Tensor), The input tensor of share_data op"); |
| 49 | + AddOutput("Out", "(Tensor), The output tensor of share_data op"); |
| 50 | + AddComment(R"DOC( |
| 51 | +ShareData Operator. |
| 52 | +
|
| 53 | +Return a tensor $Out$ that shares data with the input tensor $X$ and without tensor copy. |
| 54 | +)DOC"); |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | +} // namespace operators |
| 59 | +} // namespace paddle |
| 60 | + |
| 61 | +namespace ops = paddle::operators; |
| 62 | +REGISTER_OPERATOR( |
| 63 | + share_data, ops::ShareDataOp, ops::ShareDataOpMaker, |
| 64 | + paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>, |
| 65 | + paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>); |
| 66 | +REGISTER_OP_CPU_KERNEL(share_data, ops::ShareDataKernel<bool>, |
| 67 | + ops::ShareDataKernel<int>, ops::ShareDataKernel<int8_t>, |
| 68 | + ops::ShareDataKernel<uint8_t>, |
| 69 | + ops::ShareDataKernel<paddle::platform::float16>, |
| 70 | + ops::ShareDataKernel<int64_t>, |
| 71 | + ops::ShareDataKernel<float>, |
| 72 | + ops::ShareDataKernel<double>) |
0 commit comments