-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Add isfinite v2 op #26344
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
Merged
Merged
Add isfinite v2 op #26344
Changes from 15 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
5757aa1
add isfinitev2 op
joey12300 0255d58
finish all visitor
joey12300 d47d19d
modify cuda kernel of bothfalse visitor
joey12300 2f45e46
Merge branch 'develop' of github.com:PaddlePaddle/Paddle into add_isf…
joey12300 a973644
add isnan, isinf, isfinite api
joey12300 ecac473
add comment for isfinite, isnan, isinf
joey12300 c57659e
add alias for isnan, isinf, isfinite
joey12300 1963a09
add int64 support
joey12300 cc1f486
fix typo
joey12300 22b6693
fix isfinite_v2 op can't evaluate tensor with dim larger than 1
joey12300 c36a80b
add unittest and register int64_t, float16 op kernel
joey12300 831f14d
add error testcase
joey12300 848baf6
update comment of test_isfinite_v2_op.py
joey12300 856ca1e
remove @skipIf
joey12300 b4d0ded
add specified type of error
joey12300 4b6ac65
remove alias comment
joey12300 1255817
data->mutable_data, use GetMaxPhysicalThreadCount to calculate MAX_GR…
joey12300 59043f9
rolback
joey12300 aaee7e3
to_variable->to_tensor
joey12300 423455c
add cpu only test
joey12300 f47ebe7
Merge branch 'develop' of github.com:PaddlePaddle/Paddle into add_isf…
joey12300 15777f6
fix isfinite test case failed
joey12300 73fef26
fix conflict in paddle.tensor.math
joey12300 6caf966
1. remove useless macro
joey12300 d812983
Merge branch 'develop' of github.com:PaddlePaddle/Paddle into add_isf…
joey12300 a84e213
add alias of isnan, isinf, isfinite
joey12300 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 hidden or 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 hidden or 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 hidden or 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,119 @@ | ||
| // Copyright (c) 2018 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/fluid/operators/isfinite_v2_op.h" | ||
| #include <string> | ||
| #include <vector> | ||
| #include "paddle/fluid/operators/common_infer_shape_functions.h" | ||
| #include "paddle/fluid/platform/float16.h" | ||
|
|
||
| namespace plat = paddle::platform; | ||
|
|
||
| namespace paddle { | ||
| namespace operators { | ||
|
|
||
| class OverflowV2Op : public framework::OperatorWithKernel { | ||
| public: | ||
| OverflowV2Op(const std::string &type, | ||
| const framework::VariableNameMap &inputs, | ||
| const framework::VariableNameMap &outputs, | ||
| const framework::AttributeMap &attrs) | ||
| : OperatorWithKernel(type, inputs, outputs, attrs) {} | ||
| void InferShape(framework::InferShapeContext *ctx) const override { | ||
| OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "isfinitev2"); | ||
| OP_INOUT_CHECK(ctx->HasOutput("Out"), "Output", "Out", "isfinitev2"); | ||
| UnaryOpUnchangedInferShape(ctx); | ||
| } | ||
|
|
||
| protected: | ||
| framework::OpKernelType GetExpectedKernelType( | ||
| const framework::ExecutionContext &ctx) const override { | ||
| int dtype = -1; | ||
| auto *x_var = ctx.InputVar("X"); | ||
| if (x_var->IsType<framework::LoDTensor>()) { | ||
| dtype = x_var->Get<framework::LoDTensor>().type(); | ||
| } else if (x_var->IsType<framework::SelectedRows>()) { | ||
| dtype = x_var->Get<framework::SelectedRows>().value().type(); | ||
| } else { | ||
| PADDLE_THROW(plat::errors::InvalidArgument( | ||
| "Cannot find the input data type by all input data")); | ||
| } | ||
| return framework::OpKernelType(framework::proto::VarType::Type(dtype), | ||
| ctx.GetPlace()); | ||
| } | ||
| }; | ||
|
|
||
| class OverflowV2OpMaker : public framework::OpProtoAndCheckerMaker { | ||
| public: | ||
| void Make() override { | ||
| AddInput("X", "(Tensor) The input tensors of overflowv2 operator."); | ||
| AddOutput("Out", | ||
| "(Tensor) The output tensor of overflowv2 operator. " | ||
| "Same size compare to input tensor"); | ||
| AddComment(string::Sprintf(R"DOC( | ||
| Overflow %s operator. | ||
|
|
||
| $$Out = %s(X)$$ | ||
|
|
||
| Check whether each element of X is Inf or Nan, return the bool result of each | ||
| element of X as a tensor. | ||
|
|
||
| %s | ||
| )DOC", | ||
| GetName(), GetComments())); | ||
| } | ||
|
|
||
| protected: | ||
| virtual std::string GetName() const = 0; | ||
| virtual std::string GetComments() const = 0; | ||
| }; | ||
|
|
||
| } // namespace operators | ||
| } // namespace paddle | ||
|
|
||
| namespace ops = paddle::operators; | ||
|
|
||
| #define REGISTER_V2OP_MAKER(op_type, comment) \ | ||
| namespace paddle { \ | ||
| namespace operators { \ | ||
| class _##op_type##OverflowV2OpMaker \ | ||
| : public ::paddle::operators::OverflowV2OpMaker { \ | ||
| protected: \ | ||
| std::string GetName() const { return #op_type; } \ | ||
| std::string GetComments() const { return comment; } \ | ||
| }; \ | ||
| } \ | ||
| } \ | ||
| REGISTER_OPERATOR( \ | ||
| op_type, ops::OverflowV2Op, ops::_##op_type##OverflowV2OpMaker, \ | ||
| paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>, \ | ||
| paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>) | ||
|
|
||
| #define REGISTER_OVERFLOW_CPU_KERNEL(op_type, functor) \ | ||
| REGISTER_OP_CPU_KERNEL( \ | ||
| op_type, ops::OverflowKernel<paddle::platform::CPUDeviceContext, int, \ | ||
| ops::functor>, \ | ||
| ops::OverflowKernel<paddle::platform::CPUDeviceContext, int64_t, \ | ||
| ops::functor>, \ | ||
| ops::OverflowKernel<paddle::platform::CPUDeviceContext, float, \ | ||
| ops::functor>, \ | ||
| ops::OverflowKernel<paddle::platform::CPUDeviceContext, double, \ | ||
| ops::functor>, \ | ||
| ops::OverflowKernel<paddle::platform::CPUDeviceContext, plat::float16, \ | ||
| ops::functor>); | ||
|
|
||
| REGISTER_V2OP_MAKER(isinf_v2, "isinfv2(X)"); | ||
| REGISTER_V2OP_MAKER(isnan_v2, "isnanv2(X)"); | ||
| REGISTER_V2OP_MAKER(isfinite_v2, "isfinitev2(X)"); | ||
| FOR_EACH_KERNEL_V2FUNCTOR(REGISTER_OVERFLOW_CPU_KERNEL); |
This file contains hidden or 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,34 @@ | ||
| // Copyright (c) 2020 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/fluid/operators/isfinite_v2_op.h" | ||
| #include "paddle/fluid/platform/float16.h" | ||
|
|
||
| namespace ops = paddle::operators; | ||
| namespace plat = paddle::platform; | ||
|
|
||
| #define REGISTER_OVERFLOW_CUDA_KERNEL(op_type, functor) \ | ||
| REGISTER_OP_CUDA_KERNEL( \ | ||
| op_type, ops::OverflowKernel<paddle::platform::CUDADeviceContext, int, \ | ||
| ops::functor>, \ | ||
| ops::OverflowKernel<paddle::platform::CUDADeviceContext, int64_t, \ | ||
| ops::functor>, \ | ||
| ops::OverflowKernel<paddle::platform::CUDADeviceContext, float, \ | ||
| ops::functor>, \ | ||
| ops::OverflowKernel<paddle::platform::CUDADeviceContext, double, \ | ||
| ops::functor>, \ | ||
| ops::OverflowKernel<paddle::platform::CUDADeviceContext, plat::float16, \ | ||
| ops::functor>); | ||
|
|
||
| FOR_EACH_KERNEL_V2FUNCTOR(REGISTER_OVERFLOW_CUDA_KERNEL); |
This file contains hidden or 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,52 @@ | ||
| // Copyright (c) 2018 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 <vector> | ||
| #include "paddle/fluid/framework/eigen.h" | ||
| #include "paddle/fluid/framework/op_registry.h" | ||
| #include "paddle/fluid/framework/tensor_util.h" | ||
| #include "paddle/fluid/operators/isfinite_op.h" | ||
| #include "paddle/fluid/platform/float16.h" | ||
| #include "paddle/fluid/platform/transform.h" | ||
|
|
||
| namespace paddle { | ||
| namespace operators { | ||
|
|
||
| struct InfinityV2Functor { | ||
| void operator()(const framework::Tensor& tensor, framework::Tensor* out) { | ||
| framework::TensorContainsInfV2(tensor, out); | ||
| } | ||
| }; | ||
|
|
||
| struct NANV2Functor { | ||
| void operator()(const framework::Tensor& tensor, framework::Tensor* out) { | ||
| framework::TensorContainsNANV2(tensor, out); | ||
| } | ||
| }; | ||
|
|
||
| struct IsfiniteV2Functor { | ||
| void operator()(const framework::Tensor& tensor, framework::Tensor* out) { | ||
| framework::TensorIsfiniteV2(tensor, out); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace operators | ||
| } // namespace paddle | ||
|
|
||
| #define FOR_EACH_KERNEL_V2FUNCTOR(__macro) \ | ||
|
||
| __macro(isinf_v2, InfinityV2Functor); \ | ||
| __macro(isnan_v2, NANV2Functor); \ | ||
| __macro(isfinite_v2, IsfiniteV2Functor); | ||
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.