Skip to content

More fast math #3337

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 2 commits into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/backend/cuda/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,7 @@ endif()

target_compile_options(afcuda
PRIVATE
$<$<BOOL:${AF_WITH_FAST_MATH}>:$<$<COMPILE_LANGUAGE:CUDA>:-use_fast_math>>
$<$<COMPILE_LANGUAGE:CUDA>:--expt-relaxed-constexpr>
$<$<COMPILE_LANGUAGE:CUDA>:-Xcudafe --diag_suppress=unrecognized_gcc_pragma>
$<$<COMPILE_LANGUAGE:CUDA>: $<$<CXX_COMPILER_ID:MSVC>: -Xcompiler=/wd4251
Expand Down
4 changes: 4 additions & 0 deletions src/backend/cuda/compile_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ Module compileModule(const string &moduleKey, span<const string> sources,
arch.data(),
"--std=c++14",
"--device-as-default-execution-space",
#ifdef AF_WITH_FAST_MATH
"--use_fast_math",
"-DAF_WITH_FAST_MATH",
#endif
#if !(defined(NDEBUG) || defined(__aarch64__) || defined(__LP64__))
"--device-debug",
"--generate-line-info"
Expand Down
5 changes: 5 additions & 0 deletions src/backend/cuda/kernel/jit.cuh
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,13 @@ typedef cuDoubleComplex cdouble;
#define __rem(lhs, rhs) ((lhs) % (rhs))
#define __mod(lhs, rhs) ((lhs) % (rhs))

#ifdef AF_WITH_FAST_MATH
#define __pow(lhs, rhs) \
static_cast<double>(pow(static_cast<double>(lhs), static_cast<double>(rhs)));
#else
#define __pow(lhs, rhs) \
__float2int_rn(pow(__int2float_rn((int)lhs), __int2float_rn((int)rhs)))
#endif
#define __powll(lhs, rhs) \
__double2ll_rn(pow(__ll2double_rn(lhs), __ll2double_rn(rhs)))
#define __powul(lhs, rhs) \
Expand Down
35 changes: 17 additions & 18 deletions src/backend/cuda/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@

namespace cuda {

#ifdef AF_WITH_FAST_MATH
constexpr bool fast_math = true;
#else
constexpr bool fast_math = false;
#endif

template<typename T>
static inline __DH__ T abs(T val) {
return ::abs(val);
Expand Down Expand Up @@ -138,29 +144,22 @@ __DH__ static To scalar(Ti real, Ti imag) {
}

#ifndef __CUDA_ARCH__

template<typename T>
inline T maxval() {
return std::numeric_limits<T>::max();
if constexpr (std::is_floating_point_v<T> && !fast_math) {
return std::numeric_limits<T>::infinity();
} else {
return std::numeric_limits<T>::max();
}
}
template<typename T>
inline T minval() {
return std::numeric_limits<T>::min();
}
template<>
inline float maxval() {
return std::numeric_limits<float>::infinity();
}
template<>
inline double maxval() {
return std::numeric_limits<double>::infinity();
}
template<>
inline float minval() {
return -std::numeric_limits<float>::infinity();
}
template<>
inline double minval() {
return -std::numeric_limits<double>::infinity();
if constexpr (std::is_floating_point_v<T> && !fast_math) {
return -std::numeric_limits<T>::infinity();
} else {
return std::numeric_limits<T>::lowest();
}
}
#else
template<typename T>
Expand Down
6 changes: 6 additions & 0 deletions src/backend/cuda/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ unique_handle<cublasHandle_t> *cublasManager(const int deviceId) {
// call outside of call_once scope.
CUBLAS_CHECK(
cublasSetStream(handles[deviceId], cuda::getStream(deviceId)));
#ifdef AF_WITH_FAST_MATH
CUBLAS_CHECK(
cublasSetMathMode(handles[deviceId], CUBLAS_TF32_TENSOR_OP_MATH));
CUBLAS_CHECK(
cublasSetAtomicsMode(handles[deviceId], CUBLAS_ATOMICS_ALLOWED));
#endif
});

return &handles[deviceId];
Expand Down
4 changes: 4 additions & 0 deletions src/backend/opencl/compile_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ Program buildProgram(span<const string> kernelSources,
ostringstream options;
for (auto &opt : compileOpts) { options << opt; }

#ifdef AF_WITH_FAST_MATH
options << " -cl-fast-relaxed-math -DAF_WITH_FAST_MATH";
#endif

retVal.build({device}, (cl_std + defaults + options.str()).c_str());
} catch (Error &err) {
if (err.err() == CL_BUILD_PROGRAM_FAILURE) {
Expand Down
45 changes: 16 additions & 29 deletions src/backend/opencl/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,40 +106,27 @@ static To scalar(Ti real, Ti imag) {
return cval;
}

#ifdef AF_WITH_FAST_MATH
constexpr bool fast_math = true;
#else
constexpr bool fast_math = false;
#endif

template<typename T>
inline T maxval() {
return std::numeric_limits<T>::max();
if constexpr (std::is_floating_point_v<T> && !fast_math) {
return std::numeric_limits<T>::infinity();
} else {
return std::numeric_limits<T>::max();
}
}
template<typename T>
inline T minval() {
return std::numeric_limits<T>::min();
}
template<>
inline float maxval() {
return std::numeric_limits<float>::infinity();
}
template<>
inline double maxval() {
return std::numeric_limits<double>::infinity();
}

template<>
inline common::half maxval() {
return std::numeric_limits<common::half>::infinity();
}

template<>
inline float minval() {
return -std::numeric_limits<float>::infinity();
}

template<>
inline double minval() {
return -std::numeric_limits<double>::infinity();
}
template<>
inline common::half minval() {
return -std::numeric_limits<common::half>::infinity();
if constexpr (std::is_floating_point_v<T> && !fast_math) {
return -std::numeric_limits<T>::infinity();
} else {
return std::numeric_limits<T>::lowest();
}
}

static inline double real(cdouble in) { return in.s[0]; }
Expand Down
1 change: 1 addition & 0 deletions test/reduce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2296,6 +2296,7 @@ TEST(Reduce, Test_Sum_Global_Array_nanval) {
}

TEST(Reduce, nanval_issue_3255) {
SKIP_IF_FAST_MATH_ENABLED();
char *info_str;
af_array ikeys, ivals, okeys, ovals;
dim_t dims[1] = {8};
Expand Down