-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Compare Operator #5325
Merged
Merged
Compare Operator #5325
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. | ||
|
||
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 | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
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. */ | ||
|
||
#include "paddle/operators/compare_op.h" | ||
#include "paddle/framework/op_registry.h" | ||
namespace paddle { | ||
namespace operators { | ||
template <typename OpComment> | ||
class LogicalOpProtoMaker : public framework::OpProtoAndCheckerMaker { | ||
public: | ||
LogicalOpProtoMaker(framework::OpProto *proto, | ||
framework::OpAttrChecker *op_checker) | ||
: OpProtoAndCheckerMaker(proto, op_checker) { | ||
OpComment comment; | ||
AddInput("X", | ||
string::Sprintf("(LoDTensor) the left hand operand of %s operator", | ||
comment.type)); | ||
AddInput("Y", string::Sprintf( | ||
"(LoDTensor) the right hand operand of %s operator", | ||
comment.type)); | ||
AddOutput("Out", string::Sprintf( | ||
"(LoDTensor) n-dim bool tensor. Each element is %s", | ||
comment.equation)); | ||
AddComment(string::Sprintf(R"DOC(%s Operator | ||
|
||
It operates element-wise on X and Y, and returns the Out. Each of them is a | ||
N-dim tensor. X and Y could be any type. The each element of the Out tensor is | ||
calculated by %s | ||
)DOC", | ||
comment.type, comment.equation)); | ||
} | ||
}; | ||
|
||
template <typename OpComment> | ||
class LogicalOpInferShape : public framework::InferShapeBase { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. BinaryLogicalOpInferShape |
||
public: | ||
void operator()(framework::InferShapeContext *context) const override { | ||
OpComment comment; | ||
PADDLE_ENFORCE(context->HasInput("X"), "%s operator must has input X", | ||
comment.type); | ||
PADDLE_ENFORCE(context->HasInput("Y"), "%s operator must has input Y", | ||
comment.type); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. enforce X and Y to have the same shape. |
||
context->SetOutputDim("Out", context->GetInputDim("X")); | ||
context->ShareLoD("X", "Out"); | ||
} | ||
}; | ||
|
||
} // namespace operators | ||
} // namespace paddle | ||
|
||
#define REGISTER_LOGICAL_OP(op_type, _equation) \ | ||
struct _##op_type##Comment { \ | ||
static char type[]; \ | ||
static char equation[]; \ | ||
}; \ | ||
char _##op_type##Comment::type[]{#op_type}; \ | ||
char _##op_type##Comment::equation[]{_equation}; \ | ||
REGISTER_OP_WITH_KERNEL( \ | ||
op_type, ::paddle::operators::LogicalOpProtoMaker<_##op_type##Comment>, \ | ||
::paddle::operators::LogicalOpInferShape<_##op_type##Comment>, \ | ||
::paddle::framework::EmptyGradOpMaker); | ||
|
||
REGISTER_LOGICAL_OP(less_than, "Out = X < Y"); | ||
REGISTER_LOGICAL_KERNEL(less_than, CPU, paddle::operators::LessThanFunctor); | ||
REGISTER_LOGICAL_OP(equal, "Out = X == Y"); | ||
REGISTER_LOGICAL_KERNEL(equal, CPU, paddle::operators::EqualFunctor); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. | ||
|
||
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 | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
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. */ | ||
|
||
#include "paddle/operators/compare_op.h" | ||
|
||
REGISTER_LOGICAL_KERNEL(less_than, GPU, paddle::operators::LessThanFunctor); | ||
REGISTER_LOGICAL_KERNEL(equal, GPU, paddle::operators::EqualFunctor); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. | ||
|
||
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 | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
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. */ | ||
|
||
#pragma once | ||
#include <math.h> | ||
#include <type_traits> | ||
#include "paddle/framework/op_registry.h" | ||
#include "paddle/platform/transform.h" | ||
|
||
namespace paddle { | ||
namespace operators { | ||
|
||
template <typename T> | ||
struct LessThanFunctor { | ||
using ELEM_TYPE = T; | ||
HOSTDEVICE bool operator()(const T& a, const T& b) const { return a < b; } | ||
}; | ||
|
||
template <typename T> | ||
struct EqualFunctor { | ||
using ELEM_TYPE = T; | ||
HOSTDEVICE bool operator()(const T& a, const T& b) const { | ||
if (std::is_floating_point<T>::value) { | ||
// This branch will be optimized while compiling if T is integer. It is | ||
// safe to cast a and b to double. | ||
return fabs(static_cast<double>(a - b)) < 1e-8; | ||
} else { | ||
return (a == b); | ||
} | ||
} | ||
}; | ||
|
||
template <typename Place, typename Functor> | ||
class LogicalOpKernel | ||
: public framework::OpKernel<typename Functor::ELEM_TYPE> { | ||
public: | ||
void Compute(const framework::ExecutionContext& context) const override { | ||
using T = typename Functor::ELEM_TYPE; | ||
auto* x = context.Input<framework::Tensor>("X"); | ||
auto* y = context.Input<framework::Tensor>("Y"); | ||
auto* out = context.Output<framework::Tensor>("Out"); | ||
Functor binary_func; | ||
platform::Transform<Place> trans; | ||
trans(context.device_context(), x->data<T>(), x->data<T>() + x->numel(), | ||
y->data<T>(), out->mutable_data<bool>(context.GetPlace()), | ||
binary_func); | ||
} | ||
}; | ||
|
||
} // namespace operators | ||
} // namespace paddle | ||
|
||
#define REGISTER_LOGICAL_KERNEL(op_type, dev, functor) \ | ||
REGISTER_OP_##dev##_KERNEL( \ | ||
op_type, \ | ||
::paddle::operators::LogicalOpKernel<::paddle::platform::dev##Place, \ | ||
functor<int>>, \ | ||
::paddle::operators::LogicalOpKernel<::paddle::platform::dev##Place, \ | ||
functor<int64_t>>, \ | ||
::paddle::operators::LogicalOpKernel<::paddle::platform::dev##Place, \ | ||
functor<float>>, \ | ||
::paddle::operators::LogicalOpKernel<::paddle::platform::dev##Place, \ | ||
functor<double>>); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import op_test | ||
import unittest | ||
import numpy | ||
|
||
|
||
def create_test_class(op_type, typename, callback): | ||
class Cls(op_test.OpTest): | ||
def setUp(self): | ||
a = numpy.random.random(size=(10, 7)).astype(typename) | ||
b = numpy.random.random(size=(10, 7)).astype(typename) | ||
c = callback(a, b) | ||
self.inputs = {'X': a, 'Y': b} | ||
self.outputs = {'Out': c} | ||
self.op_type = op_type | ||
|
||
def test_output(self): | ||
self.check_output() | ||
|
||
cls_name = "{0}_{1}".format(op_type, typename) | ||
Cls.__name__ = cls_name | ||
globals()[cls_name] = Cls | ||
|
||
|
||
for _type_name in {'float32', 'float64', 'int32', 'int64'}: | ||
create_test_class('less_than', _type_name, lambda _a, _b: _a < _b) | ||
create_test_class('equal', _type_name, lambda _a, _b: _a == _b) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe BinaryLogicalOpProtoMaker is more accurate.