// 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 "fastdeploy/function/isfinite.h" #include "fastdeploy/core/float16.h" #include #include namespace fastdeploy { namespace function { template struct IsNanFunctor { OutT operator()(const T& a) const { return static_cast(std::isnan(a)); } }; template struct IsNanFunctor::value>::type> { OutT operator()(const T& a) const { return static_cast(false); } }; template struct IsNanFunctor { OutT operator()(const fastdeploy::float16& a) const { return static_cast(fastdeploy::isnan(a)); } }; template struct IsInfFunctor { OutT operator()(const T& a) const { return static_cast(std::isinf(a)); } }; template struct IsInfFunctor::value>::type> { OutT operator()(const T& a) const { return static_cast(false); } }; template struct IsInfFunctor { OutT operator()(const fastdeploy::float16& a) const { return static_cast(fastdeploy::isinf(a)); } }; template struct IsFiniteFunctor { OutT operator()(const T& a) const { return static_cast(std::isfinite(a)); } }; template struct IsFiniteFunctor< T, OutT, typename std::enable_if::value>::type> { OutT operator()(const T& a) const { return static_cast(true); } }; template struct IsFiniteFunctor { OutT operator()(const fastdeploy::float16& a) const { return static_cast(fastdeploy::isfinite(a)); } }; #define DEFINE_ISFINITE_KERNEL(isfinite_kernel, functor) \ template \ void isfinite_kernel(const FDTensor& x, FDTensor* out, FDDataType dtype) { \ FD_VISIT_ALL_TYPES(dtype, #isfinite_kernel, ([&] { \ out->Allocate(x.Shape(), dtype); \ functor unary_func; \ data_t* out_ptr = \ reinterpret_cast(out->Data()); \ const T* input_ptr = \ reinterpret_cast(x.Data()); \ std::transform(input_ptr, input_ptr + x.Numel(), \ out_ptr, unary_func); \ })); \ } DEFINE_ISFINITE_KERNEL(IsNanKernel, IsNanFunctor) DEFINE_ISFINITE_KERNEL(IsInfKernel, IsInfFunctor) DEFINE_ISFINITE_KERNEL(IsFiniteKernel, IsFiniteFunctor) #undef DEFINE_ISFINITE_KERNEL void IsNan(const FDTensor& x, FDTensor* out, FDDataType dtype) { FD_VISIT_FLOAT_TYPES(x.dtype, "IsNanKernel", ([&] { IsNanKernel(x, out, dtype); })); } void IsInf(const FDTensor& x, FDTensor* out, FDDataType dtype) { FD_VISIT_FLOAT_TYPES(x.dtype, "IsInfKernel", ([&] { IsInfKernel(x, out, dtype); })); } void IsFinite(const FDTensor& x, FDTensor* out, FDDataType dtype) { FD_VISIT_FLOAT_TYPES(x.dtype, "IsFiniteKernel", ([&] { IsFiniteKernel(x, out, dtype); })); } } // namespace function } // namespace fastdeploy