parent
58db07b7bb
commit
f74fb79036
@ -0,0 +1,82 @@
|
||||
/* 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 CompareOpProtoMaker : public framework::OpProtoAndCheckerMaker {
|
||||
public:
|
||||
CompareOpProtoMaker(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 CompareOpInferShape : public framework::InferShapeBase {
|
||||
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);
|
||||
auto dim_x = context->GetInputDim("X");
|
||||
auto dim_y = context->GetInputDim("Y");
|
||||
PADDLE_ENFORCE_EQ(framework::product(dim_x), framework::product(dim_y),
|
||||
"The number of elements in X and Y should be same");
|
||||
|
||||
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::CompareOpProtoMaker<_##op_type##Comment>, \
|
||||
::paddle::operators::CompareOpInferShape<_##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);
|
@ -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);
|
@ -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 CompareOpKernel
|
||||
: 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::CompareOpKernel<::paddle::platform::dev##Place, \
|
||||
functor<int>>, \
|
||||
::paddle::operators::CompareOpKernel<::paddle::platform::dev##Place, \
|
||||
functor<int64_t>>, \
|
||||
::paddle::operators::CompareOpKernel<::paddle::platform::dev##Place, \
|
||||
functor<float>>, \
|
||||
::paddle::operators::CompareOpKernel<::paddle::platform::dev##Place, \
|
||||
functor<double>>);
|
@ -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()
|
Loading…
Reference in new issue