未验证 提交 3c536f2e 编写于 作者: W WJJ1995 提交者: GitHub

[phi] move isnan_v2、isfinite_v2、isinf_v2 to phi (#40076)

* support isfinite for phi

* mark v2

* fixed bugs

* fixed include bugs

* deal with comments

* decoupling selected_rows

* rm bfloat16

* fixed infermeta

* fixed code style

* rm useless code

* replace pt by pd
上级 f876320a
......@@ -12,11 +12,13 @@
// 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 "paddle/fluid/framework/infershape_utils.h"
#include "paddle/fluid/framework/op_registry.h"
#include "paddle/fluid/operators/common_infer_shape_functions.h"
#include "paddle/phi/core/infermeta_utils.h"
#include "paddle/phi/infermeta/unary.h"
namespace paddle {
namespace framework {
......@@ -49,11 +51,6 @@ class OverflowV2Op : public framework::OperatorWithKernel {
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(
......@@ -104,6 +101,14 @@ element of X as a tensor.
} // namespace paddle
namespace ops = paddle::operators;
DECLARE_INFER_SHAPE_FUNCTOR(isinf_v2, IsinfInferShapeFunctor,
PD_INFER_META(phi::IsfiniteInferMeta));
DECLARE_INFER_SHAPE_FUNCTOR(isnan_v2, IsnanInferShapeFunctor,
PD_INFER_META(phi::IsfiniteInferMeta));
DECLARE_INFER_SHAPE_FUNCTOR(isfinite_v2, IsfiniteInferShapeFunctor,
PD_INFER_META(phi::IsfiniteInferMeta));
#define REGISTER_V2OP_MAKER(op_type, comment) \
namespace paddle { \
......@@ -124,50 +129,17 @@ REGISTER_V2OP_MAKER(isfinite_v2, "isfinitev2(X)");
REGISTER_OPERATOR(
isinf_v2, ops::OverflowV2Op, ops::_isinf_v2OverflowV2OpMaker,
paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>);
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
IsinfInferShapeFunctor);
REGISTER_OPERATOR(
isnan_v2, ops::OverflowV2Op, ops::_isnan_v2OverflowV2OpMaker,
paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>);
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
IsnanInferShapeFunctor);
REGISTER_OPERATOR(
isfinite_v2, ops::OverflowV2Op, ops::_isfinite_v2OverflowV2OpMaker,
paddle::framework::EmptyGradOpMaker<paddle::framework::OpDesc>,
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>);
REGISTER_OP_CPU_KERNEL(isnan_v2,
ops::OverflowKernel<paddle::platform::CPUDeviceContext,
int, ops::NANV2Functor>,
ops::OverflowKernel<paddle::platform::CPUDeviceContext,
int64_t, ops::NANV2Functor>,
ops::OverflowKernel<paddle::platform::CPUDeviceContext,
float, ops::NANV2Functor>,
ops::OverflowKernel<paddle::platform::CPUDeviceContext,
double, ops::NANV2Functor>,
ops::OverflowKernel<paddle::platform::CPUDeviceContext,
plat::float16, ops::NANV2Functor>);
REGISTER_OP_CPU_KERNEL(
isinf_v2, ops::OverflowKernel<paddle::platform::CPUDeviceContext, int,
ops::InfinityV2Functor>,
ops::OverflowKernel<paddle::platform::CPUDeviceContext, int64_t,
ops::InfinityV2Functor>,
ops::OverflowKernel<paddle::platform::CPUDeviceContext, float,
ops::InfinityV2Functor>,
ops::OverflowKernel<paddle::platform::CPUDeviceContext, double,
ops::InfinityV2Functor>,
ops::OverflowKernel<paddle::platform::CPUDeviceContext, plat::float16,
ops::InfinityV2Functor>);
REGISTER_OP_CPU_KERNEL(
isfinite_v2, ops::OverflowKernel<paddle::platform::CPUDeviceContext, int,
ops::IsfiniteV2Functor>,
ops::OverflowKernel<paddle::platform::CPUDeviceContext, int64_t,
ops::IsfiniteV2Functor>,
ops::OverflowKernel<paddle::platform::CPUDeviceContext, float,
ops::IsfiniteV2Functor>,
ops::OverflowKernel<paddle::platform::CPUDeviceContext, double,
ops::IsfiniteV2Functor>,
ops::OverflowKernel<paddle::platform::CPUDeviceContext, plat::float16,
ops::IsfiniteV2Functor>);
paddle::framework::EmptyGradOpMaker<paddle::imperative::OpBase>,
IsfiniteInferShapeFunctor);
// 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;
REGISTER_OP_CUDA_KERNEL(isnan_v2,
ops::OverflowKernel<paddle::platform::CUDADeviceContext,
int, ops::NANV2Functor>,
ops::OverflowKernel<paddle::platform::CUDADeviceContext,
int64_t, ops::NANV2Functor>,
ops::OverflowKernel<paddle::platform::CUDADeviceContext,
float, ops::NANV2Functor>,
ops::OverflowKernel<paddle::platform::CUDADeviceContext,
double, ops::NANV2Functor>,
ops::OverflowKernel<paddle::platform::CUDADeviceContext,
plat::float16, ops::NANV2Functor>);
REGISTER_OP_CUDA_KERNEL(
isinf_v2, ops::OverflowKernel<paddle::platform::CUDADeviceContext, int,
ops::InfinityV2Functor>,
ops::OverflowKernel<paddle::platform::CUDADeviceContext, int64_t,
ops::InfinityV2Functor>,
ops::OverflowKernel<paddle::platform::CUDADeviceContext, float,
ops::InfinityV2Functor>,
ops::OverflowKernel<paddle::platform::CUDADeviceContext, double,
ops::InfinityV2Functor>,
ops::OverflowKernel<paddle::platform::CUDADeviceContext, plat::float16,
ops::InfinityV2Functor>);
REGISTER_OP_CUDA_KERNEL(
isfinite_v2, ops::OverflowKernel<paddle::platform::CUDADeviceContext, int,
ops::IsfiniteV2Functor>,
ops::OverflowKernel<paddle::platform::CUDADeviceContext, int64_t,
ops::IsfiniteV2Functor>,
ops::OverflowKernel<paddle::platform::CUDADeviceContext, float,
ops::IsfiniteV2Functor>,
ops::OverflowKernel<paddle::platform::CUDADeviceContext, double,
ops::IsfiniteV2Functor>,
ops::OverflowKernel<paddle::platform::CUDADeviceContext, plat::float16,
ops::IsfiniteV2Functor>);
......@@ -40,6 +40,9 @@ const std::unordered_set<std::string> standard_kernel_suffixs({
const std::unordered_set<std::string> deprecated_op_names({"diag",
"flatten",
"flatten_grad",
"isinf",
"isnan",
"isfinite",
"matmul",
"matmul_grad",
"matmul_grad_grad",
......
......@@ -1007,6 +1007,11 @@ void SizeInferMeta(const MetaTensor& input, MetaTensor* out) {
out->set_dims({1});
}
void IsfiniteInferMeta(const MetaTensor& x, MetaTensor* out) {
out->set_dims(x.dims());
out->set_dtype(DataType::BOOL);
}
void PixelShuffleInferMeta(const MetaTensor& x,
int upscale_factor,
const std::string& data_format,
......
......@@ -157,6 +157,8 @@ void PixelShuffleInferMeta(const MetaTensor& x,
const std::string& data_format,
MetaTensor* out);
void IsfiniteInferMeta(const MetaTensor& input, MetaTensor* out);
void TransposeInferMeta(const MetaTensor& x,
const std::vector<int>& axis,
MetaTensor* out);
......
// Copyright (c) 2022 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/phi/kernels/isfinite_kernel.h"
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/isfinite_kernel_impl.h"
namespace phi {
template <typename T, typename Context, typename Functor>
inline void IsfiniteKernelImpl(const Context& dev_ctx,
const DenseTensor& x,
DenseTensor* out) {
dev_ctx.template Alloc<T>(out);
Functor functor;
functor(x, out);
}
} // namespace phi
PD_REGISTER_KERNEL(isinf,
CPU,
ALL_LAYOUT,
phi::IsinfKernel,
float,
double,
phi::dtype::float16,
int,
int64_t) {}
PD_REGISTER_KERNEL(isnan,
CPU,
ALL_LAYOUT,
phi::IsnanKernel,
float,
double,
phi::dtype::float16,
int,
int64_t) {}
PD_REGISTER_KERNEL(isfinite,
CPU,
ALL_LAYOUT,
phi::IsfiniteKernel,
float,
double,
phi::dtype::float16,
int,
int64_t) {}
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
// Copyright (c) 2022 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.
......@@ -14,39 +14,32 @@
#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"
#include "paddle/phi/common/scalar.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/infermeta/unary.h"
namespace phi {
class DenseTensor;
} // namespace phi
namespace paddle {
namespace operators {
namespace funcs {
struct InfinityV2Functor {
void operator()(const framework::Tensor& tensor, framework::Tensor* out) {
framework::TensorContainsInfV2(tensor, out);
void operator()(const DenseTensor& tensor, DenseTensor* out) {
paddle::framework::TensorContainsInfV2(tensor, out);
}
};
struct NANV2Functor {
void operator()(const framework::Tensor& tensor, framework::Tensor* out) {
framework::TensorContainsNANV2(tensor, out);
void operator()(const DenseTensor& tensor, DenseTensor* out) {
paddle::framework::TensorContainsNANV2(tensor, out);
}
};
struct IsfiniteV2Functor {
void operator()(const framework::Tensor& tensor, framework::Tensor* out) {
framework::TensorIsfiniteV2(tensor, out);
void operator()(const DenseTensor& tensor, DenseTensor* out) {
paddle::framework::TensorIsfiniteV2(tensor, out);
}
};
} // namespace operators
} // namespace paddle
} // namespace funcs
} // namespace phi
// Copyright (c) 2022 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/phi/backends/gpu/gpu_context.h"
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/impl/isfinite_kernel_impl.h"
#include "paddle/phi/kernels/isfinite_kernel.h"
namespace phi {
template <typename T, typename Context, typename Functor>
inline void IsfiniteKernelImpl(const Context& dev_ctx,
const DenseTensor& x,
DenseTensor* out) {
dev_ctx.template Alloc<T>(out);
Functor functor;
functor(x, out);
}
} // namespace phi
PD_REGISTER_KERNEL(isinf,
GPU,
ALL_LAYOUT,
phi::IsinfKernel,
float,
double,
phi::dtype::float16,
int,
int64_t) {}
PD_REGISTER_KERNEL(isnan,
GPU,
ALL_LAYOUT,
phi::IsnanKernel,
float,
double,
phi::dtype::float16,
int,
int64_t) {}
PD_REGISTER_KERNEL(isfinite,
GPU,
ALL_LAYOUT,
phi::IsfiniteKernel,
float,
double,
phi::dtype::float16,
int,
int64_t) {}
// Copyright (c) 2022 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 "paddle/phi/kernels/funcs/isfinite_functor.h"
#include "paddle/phi/kernels/isfinite_kernel.h"
namespace phi {
template <typename T, typename Context, typename Functor>
inline void IsfiniteKernelImpl(const Context& ctx,
const DenseTensor& x,
DenseTensor* out);
#define DEFINE_ISFINITE_KERNEL(isfinite_kernel, functor) \
template <typename T, typename Context> \
void isfinite_kernel( \
const Context& ctx, const DenseTensor& x, DenseTensor* out) { \
IsfiniteKernelImpl<T, Context, functor>(ctx, x, out); \
}
DEFINE_ISFINITE_KERNEL(IsinfKernel, funcs::InfinityV2Functor)
DEFINE_ISFINITE_KERNEL(IsnanKernel, funcs::NANV2Functor)
DEFINE_ISFINITE_KERNEL(IsfiniteKernel, funcs::IsfiniteV2Functor)
#undef DEFINE_ISFINITE_KERNEL
} // namespace phi
/* Copyright (c) 2021 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 "paddle/phi/core/dense_tensor.h"
namespace phi {
#define DEFINE_ISFINITE_KERNEL(isfinite_kernel) \
template <typename T, typename Context> \
void isfinite_kernel( \
const Context& ctx, const DenseTensor& x, DenseTensor* out);
DEFINE_ISFINITE_KERNEL(IsinfKernel)
DEFINE_ISFINITE_KERNEL(IsnanKernel)
DEFINE_ISFINITE_KERNEL(IsfiniteKernel)
#undef DEFINE_ISFINITE_KERNEL
} // namespace phi
// Copyright (c) 2022 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/phi/kernels/selected_rows/isfinite_kernel.h"
#include "paddle/phi/backends/cpu/cpu_context.h"
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
#include "paddle/phi/backends/gpu/gpu_context.h"
#endif
#include "paddle/phi/core/kernel_registry.h"
#include "paddle/phi/kernels/selected_rows/isfinite_kernel_impl.h"
namespace phi {
template <typename T, typename Context, typename Functor>
inline void IsfiniteSRImpl(const Context& dev_ctx,
const SelectedRows& x,
SelectedRows* out) {
dev_ctx.template Alloc<T>(out);
Functor functor;
functor(x.value(), out->mutable_value());
}
} // namespace phi
PD_REGISTER_KERNEL(isinf_sr,
CPU,
ALL_LAYOUT,
phi::IsinfSR,
float,
double,
phi::dtype::float16,
int,
int64_t) {}
PD_REGISTER_KERNEL(isnan_sr,
CPU,
ALL_LAYOUT,
phi::IsnanSR,
float,
double,
phi::dtype::float16,
int,
int64_t) {}
PD_REGISTER_KERNEL(isfinite_sr,
CPU,
ALL_LAYOUT,
phi::IsfiniteSR,
float,
double,
phi::dtype::float16,
int,
int64_t) {}
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
PD_REGISTER_KERNEL(isinf_sr,
GPU,
ALL_LAYOUT,
phi::IsinfSR,
float,
double,
phi::dtype::float16,
int,
int64_t) {}
PD_REGISTER_KERNEL(isnan_sr,
GPU,
ALL_LAYOUT,
phi::IsnanSR,
float,
double,
phi::dtype::float16,
int,
int64_t) {}
PD_REGISTER_KERNEL(isfinite_sr,
GPU,
ALL_LAYOUT,
phi::IsfiniteSR,
float,
double,
phi::dtype::float16,
int,
int64_t) {}
#endif
/* Copyright (c) 2021 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 "paddle/phi/core/selected_rows.h"
namespace phi {
#define DEFINE_ISFINITE_SR(isfinite_sr) \
template <typename T, typename Context> \
void isfinite_sr( \
const Context& ctx, const SelectedRows& x, SelectedRows* out);
DEFINE_ISFINITE_SR(IsinfSR)
DEFINE_ISFINITE_SR(IsnanSR)
DEFINE_ISFINITE_SR(IsfiniteSR)
#undef DEFINE_ISFINITE_SR
} // namespace phi
// Copyright (c) 2022 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 "paddle/phi/kernels/funcs/isfinite_functor.h"
#include "paddle/phi/kernels/selected_rows/isfinite_kernel.h"
namespace phi {
template <typename T, typename Context, typename Functor>
inline void IsfiniteSRImpl(const Context& ctx,
const SelectedRows& x,
SelectedRows* out);
#define DEFINE_ISFINITE_SR(isfinite_sr, functor) \
template <typename T, typename Context> \
void isfinite_sr( \
const Context& ctx, const SelectedRows& x, SelectedRows* out) { \
IsfiniteSRImpl<T, Context, functor>(ctx, x, out); \
}
DEFINE_ISFINITE_SR(IsinfSR, funcs::InfinityV2Functor)
DEFINE_ISFINITE_SR(IsnanSR, funcs::NANV2Functor)
DEFINE_ISFINITE_SR(IsfiniteSR, funcs::IsfiniteV2Functor)
#undef DEFINE_ISFINITE_SR
} // namespace phi
// Copyright (c) 2022 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/phi/core/compat/op_utils.h"
PD_REGISTER_BASE_KERNEL_NAME(isinf_v2, isinf);
PD_REGISTER_BASE_KERNEL_NAME(isnan_v2, isnan);
PD_REGISTER_BASE_KERNEL_NAME(isfinite_v2, isfinite);
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册