|
| 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/operators/logical_op.h" |
| 16 | +#include "paddle/framework/op_registry.h" |
| 17 | + |
| 18 | +namespace paddle { |
| 19 | +namespace operators { |
| 20 | +template <typename OpComment> |
| 21 | +class BinaryLogicalOpProtoMaker : public framework::OpProtoAndCheckerMaker { |
| 22 | + public: |
| 23 | + BinaryLogicalOpProtoMaker(framework::OpProto *proto, |
| 24 | + framework::OpAttrChecker *op_checker) |
| 25 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 26 | + OpComment comment; |
| 27 | + AddInput("X", |
| 28 | + string::Sprintf("(LoDTensor) Left hand operand of %s operator", |
| 29 | + comment.type)); |
| 30 | + AddInput("Y", |
| 31 | + string::Sprintf("(LoDTensor) Right hand operand of %s operator", |
| 32 | + comment.type)); |
| 33 | + AddOutput("Out", string::Sprintf( |
| 34 | + "(LoDTensor) n-dim bool tensor. Each element is %s", |
| 35 | + comment.equation)); |
| 36 | + AddComment(string::Sprintf(R"DOC(%s Operator |
| 37 | +
|
| 38 | +It operates element-wise on X and Y, and returns the Out. X, Y and Out are N-dim boolean tensors. |
| 39 | +Each element of Out is calculated by %s |
| 40 | +)DOC", |
| 41 | + comment.type, comment.equation)); |
| 42 | + } |
| 43 | +}; |
| 44 | + |
| 45 | +template <typename OpComment> |
| 46 | +class UnaryLogicalOpProtoMaker : public framework::OpProtoAndCheckerMaker { |
| 47 | + public: |
| 48 | + UnaryLogicalOpProtoMaker(framework::OpProto *proto, |
| 49 | + framework::OpAttrChecker *op_checker) |
| 50 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 51 | + OpComment comment; |
| 52 | + AddInput("X", string::Sprintf("(LoDTensor) Operand of %s operator", |
| 53 | + comment.type)); |
| 54 | + AddOutput("Out", string::Sprintf( |
| 55 | + "(LoDTensor) n-dim bool tensor. Each element is %s", |
| 56 | + comment.equation)); |
| 57 | + AddComment(string::Sprintf(R"DOC(%s Operator |
| 58 | +
|
| 59 | +It operates element-wise on X, and returns the Out. X and Out are N-dim boolean tensors. |
| 60 | +Each element of Out is calculated by %s |
| 61 | +)DOC", |
| 62 | + comment.type, comment.equation)); |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +template <typename OpComment> |
| 67 | +class BinaryLogicalOpInferShape : public framework::InferShapeBase { |
| 68 | + public: |
| 69 | + void operator()(framework::InferShapeContext *context) const override { |
| 70 | + OpComment comment; |
| 71 | + PADDLE_ENFORCE(context->HasInput("X"), |
| 72 | + "Input(X) of %s operator must not be null", comment.type); |
| 73 | + PADDLE_ENFORCE(context->HasInput("Y"), |
| 74 | + "Input(Y) of %s operator must not be null", comment.type); |
| 75 | + auto dim_x = context->GetInputDim("X"); |
| 76 | + auto dim_y = context->GetInputDim("Y"); |
| 77 | + PADDLE_ENFORCE_EQ(framework::product(dim_x), framework::product(dim_y), |
| 78 | + "The number of elements in X and Y should be same"); |
| 79 | + |
| 80 | + context->SetOutputDim("Out", context->GetInputDim("X")); |
| 81 | + context->ShareLoD("X", "Out"); |
| 82 | + } |
| 83 | +}; |
| 84 | + |
| 85 | +template <typename OpComment> |
| 86 | +class UnaryLogicalOpInferShape : public framework::InferShapeBase { |
| 87 | + public: |
| 88 | + void operator()(framework::InferShapeContext *context) const override { |
| 89 | + OpComment comment; |
| 90 | + PADDLE_ENFORCE(context->HasInput("X"), |
| 91 | + "Input(X) of %s operator must not be null", comment.type); |
| 92 | + auto dim_x = context->GetInputDim("X"); |
| 93 | + |
| 94 | + context->SetOutputDim("Out", context->GetInputDim("X")); |
| 95 | + context->ShareLoD("X", "Out"); |
| 96 | + } |
| 97 | +}; |
| 98 | + |
| 99 | +class LogicalOp : public framework::OperatorWithKernel { |
| 100 | + public: |
| 101 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 102 | + |
| 103 | + protected: |
| 104 | + framework::OpKernelType GetKernelType( |
| 105 | + const framework::ExecutionContext &ctx) const override { |
| 106 | + framework::OpKernelType kt = OperatorWithKernel::GetKernelType(ctx); |
| 107 | + // LogicalOp kernel's device type is decided by input tensor place |
| 108 | + kt.place_ = ctx.Input<framework::LoDTensor>("X")->place(); |
| 109 | + return kt; |
| 110 | + } |
| 111 | +}; |
| 112 | + |
| 113 | +} // namespace operators |
| 114 | +} // namespace paddle |
| 115 | + |
| 116 | +#define REGISTER_BINARY_LOGICAL_OP(op_type, _equation) \ |
| 117 | + struct _##op_type##Comment { \ |
| 118 | + static char type[]; \ |
| 119 | + static char equation[]; \ |
| 120 | + }; \ |
| 121 | + char _##op_type##Comment::type[]{#op_type}; \ |
| 122 | + char _##op_type##Comment::equation[]{_equation}; \ |
| 123 | + REGISTER_OPERATOR( \ |
| 124 | + op_type, ::paddle::operators::LogicalOp, \ |
| 125 | + ::paddle::operators::BinaryLogicalOpProtoMaker<_##op_type##Comment>, \ |
| 126 | + ::paddle::operators::BinaryLogicalOpInferShape<_##op_type##Comment>, \ |
| 127 | + ::paddle::framework::EmptyGradOpMaker); |
| 128 | + |
| 129 | +#define REGISTER_UNARY_LOGICAL_OP(op_type, _equation) \ |
| 130 | + struct _##op_type##Comment { \ |
| 131 | + static char type[]; \ |
| 132 | + static char equation[]; \ |
| 133 | + }; \ |
| 134 | + char _##op_type##Comment::type[]{#op_type}; \ |
| 135 | + char _##op_type##Comment::equation[]{_equation}; \ |
| 136 | + REGISTER_OPERATOR( \ |
| 137 | + op_type, ::paddle::operators::LogicalOp, \ |
| 138 | + ::paddle::operators::UnaryLogicalOpProtoMaker<_##op_type##Comment>, \ |
| 139 | + ::paddle::operators::UnaryLogicalOpInferShape<_##op_type##Comment>, \ |
| 140 | + ::paddle::framework::EmptyGradOpMaker); |
| 141 | + |
| 142 | +REGISTER_BINARY_LOGICAL_OP(logical_and, "Out = X && Y"); |
| 143 | +REGISTER_BINARY_LOGICAL_KERNEL(logical_and, CPU, |
| 144 | + paddle::operators::LogicalAndFunctor); |
| 145 | +REGISTER_BINARY_LOGICAL_OP(logical_or, "Out = X && Y"); |
| 146 | +REGISTER_BINARY_LOGICAL_KERNEL(logical_or, CPU, |
| 147 | + paddle::operators::LogicalOrFunctor); |
| 148 | +REGISTER_UNARY_LOGICAL_OP(logical_not, "Out = !X"); |
| 149 | +REGISTER_UNARY_LOGICAL_KERNEL(logical_not, CPU, |
| 150 | + paddle::operators::LogicalNotFunctor); |
| 151 | +REGISTER_BINARY_LOGICAL_OP(logical_xor, "Out = (X || Y) && !(X && Y)"); |
| 152 | +REGISTER_BINARY_LOGICAL_KERNEL(logical_xor, CPU, |
| 153 | + paddle::operators::LogicalXorFunctor); |
0 commit comments