imate
C++/CUDA Reference
Loading...
Searching...
No Matches
_cu_epsilon.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: Copyright 2021, Siavash Ameli <sameli@berkeley.edu>
3 * SPDX-License-Identifier: BSD-3-Clause
4 * SPDX-FileType: SOURCE
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the license found in the LICENSE.txt file in the root
8 * directory of this source tree.
9 */
10
11#ifndef _CU_ARITHMETICS_CU_EPSILON_H_
12#define _CU_ARITHMETICS_CU_EPSILON_H_
13
14// =======
15// Headers
16// =======
17
18#include "../_cu_definitions/cu_types.h" // __nv_fp8_e5m2, __nv_fp8_e4m3,
19 // __half, __nv_bfloat16
20
21
22// =============
23// cu arithmetic
24// =============
25
38
39namespace cu_arithmetics
40{
41 // =======
42 // epsilon
43 // =======
44
55
56 template <typename DataType>
58
59
60 // =======
61 // epsilon (__nv_fp8_e5m2)
62 // =======
63
72
73 #if defined(USE_CUDA_FP8_E5M2) && (USE_CUDA_FP8_E5M2 == 1)
74 template<>
76 {
77 // This is 2^{-10}, as __nv_fp8_e5m2 type has 2 digits for mantissa.
78 return __nv_fp8_e5m2(0.25f);
79 }
80 #endif
81
82
83 // =======
84 // epsilon (__nv_fp8_e4m3)
85 // =======
86
95
96 #if defined(USE_CUDA_FP8_E4M3) && (USE_CUDA_FP8_E4M3 == 1)
97 template<>
99 {
100 // This is 2^{-10}, as __nv_fp8_e4m3 type has 3 digits for mantissa.
101 return __nv_fp8_e4m3(0.125f);
102 }
103 #endif
104
105
106 // =======
107 // epsilon (__half)
108 // =======
109
117
118 #if defined(USE_CUDA_FP16) && (USE_CUDA_FP16 == 1)
119 template<>
121 {
122 // This is 2^{-10}, as __half type has 10 digits for mantissa.
123 return __float2half(0.00097656f);
124 }
125 #endif
126
127
128 // =======
129 // epsilon (__nv_bfloat16)
130 // =======
131
140
141#if defined(USE_CUDA_BF16) && (USE_CUDA_BF16 == 1)
142 template<>
144 {
145 // This is 2^{-7}, as __half type has 10 digits for mantissa.
146 return __float2bfloat16(0.0078125f);
147 }
148 #endif
149
150
151 // =======
152 // epsilon (float)
153 // =======
154
162
163 #if defined(USE_CUDA_FP32) && (USE_CUDA_FP32 == 1)
164 template<>
166 {
167 // Instead of the hard-coded number below, one may use
168 // the std::numeric_limits::epsilon(), but, then this function cannot
169 // be called as a __device__ code. Hence, the value of 2^(-23) is hard
170 // coded below.
171 return 1.1920929e-7f;
172 }
173 #endif
174
175
176 // =======
177 // epsilon (float)
178 // =======
179
187
188 #if defined(USE_CUDA_FP64) && (USE_CUDA_FP64 == 1)
189 template<>
191 {
192 // Instead of the hard-coded number below, one may use
193 // the std::numeric_limits::epsilon(), but, then this function cannot
194 // be called as a __device__ code. Hence, the value of 2^(-52) is hard
195 // coded below.
196 return 2.220446049250313e-16;
197 }
198 #endif
199
200} // namespace cu_arithmetics
201
202#endif // _CU_ARITHMETICS_CU_EPSILON_H_
Cast from float to __half and __nv_bfloat16 types and vice-versa, and float to double and vice-versa.
Definition _cu_abs.h:43
__host__ __device__ DataType abs(const DataType x)
Absolute value of a floating point number.
__host__ __device__ DataType epsilon()
epsilon for various floating point precisions.
__host__ __device__ double epsilon< double >()
epsilon for float type, which is equal to since float has 52 bits for fraction (mantissa).
__host__ __device__ float epsilon< float >()
epsilon for __nv_fp8_e5m2 type, which is equal to since __nv_fp8_e5m2 has 2 bits for fraction (manti...