|
1 |
| -Accuracy level |
2 |
| -=== |
| 1 | +# Accuracy Level |
3 | 2 |
|
4 |
| -Many of the functions in Kernel Float take an additional `Accuracy` option as a template parameter. |
5 |
| -This option can be used to increase the performance of certain operations, at the cost of lower accuracy. |
| 3 | +For certain operations, there might be alternative versions available that provide better performance at the cost of lower accuracy. |
| 4 | +In other words, they are faster but also have a small error. |
6 | 5 |
|
7 |
| -There are four possible values for this parameter: |
| 6 | +## Fast Math |
8 | 7 |
|
9 |
| -* `accurate_policy`: Use the most accurate version of the function available. |
10 |
| -* `fast_policy`: Use the "fast math" version (for example, `__sinf` for sin on CUDA devices). Falls back to `accurate_policy` if such a version is not available. |
11 |
| -* `approx_policy<N>`: Rough approximation using a polynomial of degree `N`. Falls back to `fast_policy` if no such polynomial exists. |
12 |
| -* `default_policy`: Use a global default policy (see the next section). |
| 8 | +For several operations in single precision (float32), there are "fast math" versions. These functions are faster since they are hardware accelerated, but are not IEEE compliant for all inputs. |
13 | 9 |
|
| 10 | +To use this functionality, use the `fast_*` functions from Kernel Float. |
14 | 11 |
|
15 |
| -For example, consider this code: |
| 12 | +```cpp |
| 13 | +kf::vec<float, 4> x = {1.0f, 2.0f, 3.0f, 4.0f}; |
| 14 | + |
| 15 | +// Sine |
| 16 | +kf::vec<float, 4> a = kf::fast_sin(x); |
| 17 | + |
| 18 | +// Square root |
| 19 | +kf::vec<float, 4> b = kf::fast_sqrt(x); |
| 20 | + |
| 21 | +// Reciprocal `1/x` |
| 22 | +kf::vec<float, 4> c = kf::fast_rcp(x); |
| 23 | + |
| 24 | +// Division `a/b` |
| 25 | +kf::vec<float, 4> d = kf::fast_div(a, b); |
| 26 | +``` |
| 27 | +
|
| 28 | +These functions are only functional for 32-bit and 16-bit floats. |
| 29 | +For other input types, the operation falls back to the regular version. |
| 30 | +
|
| 31 | +## Approximate Math |
| 32 | +
|
| 33 | +For 16-bit floats, several approximate functions are provided. |
| 34 | +These use approximations (typically low-degree polynomials) to calculate rough estimates of the functions. |
| 35 | +This can be very fast but also less accurate. |
| 36 | +
|
| 37 | +
|
| 38 | +To use this functionality, use the `approx_*` functions from Kernel Float. For other input types, the operation falls back to the `fast_*` variant. |
| 39 | +
|
| 40 | +```cpp |
| 41 | +kf::vec<half, 4> x = {1.0, 2.0, 3.0, 4.0}; |
| 42 | +
|
| 43 | +// Sine |
| 44 | +kf::vec<half, 4> a = kf::approx_sin(x); |
16 | 45 |
|
17 |
| -```C++ |
| 46 | +// Square root |
| 47 | +kf::vec<half, 4> b = kf::approx_sqrt(x); |
18 | 48 |
|
19 |
| -#include "kernel_float.h" |
20 |
| -namespace kf = kernel_float; |
| 49 | +// Reciprocal `1/x` |
| 50 | +kf::vec<half, 4> c = kf::approx_rcp(x); |
21 | 51 |
|
| 52 | +// Division `a/b` |
| 53 | +kf::vec<half, 4> d = kf::approx_div(a, b); |
| 54 | +``` |
| 55 | + |
| 56 | +You can adjust the degree of approximation by supplying an integer template parameter: |
| 57 | + |
| 58 | + |
| 59 | +```cpp |
| 60 | +// Sine approximation with polynomial of degree 1 |
| 61 | +kf::vec<half, 4> a = kf::approx_sin<1>(x); |
| 62 | + |
| 63 | +// Polynomial of degree 2 |
| 64 | +kf::vec<half, 4> a = kf::approx_sin<2>(x); |
| 65 | + |
| 66 | +// Polynomial of degree 3 |
| 67 | +kf::vec<half, 4> a = kf::approx_sin<3>(x); |
| 68 | +``` |
| 69 | + |
| 70 | +## Tuning Accuracy Level |
| 71 | + |
| 72 | +Many functions in Kernel Float accept an additional Accuracy option as a template parameter. |
| 73 | +This allows you to tune the accuracy level without changing the function name. |
| 74 | + |
| 75 | +There are four possible values for this parameter: |
| 76 | + |
| 77 | +- `kf::accurate_policy`: Use the most accurate version of the function available. |
| 78 | +- `kf::fast_policy`: Use the "fast math" version. |
| 79 | +- `kf::approx_policy<N>`: Use the approximate version with degree `N`. |
| 80 | +- `kf::default_policy`: Use a global default policy (see the next section). |
| 81 | + |
| 82 | +For example, consider this code: |
22 | 83 |
|
23 |
| -int main() { |
24 |
| - kf::vec<float, 2> input = {1.0f, 2.0f}; |
| 84 | +```cpp |
| 85 | +kf::vec<float, 2> input = {1.0f, 2.0f}; |
25 | 86 |
|
26 |
| - // Use the default policy |
27 |
| - kf::vec<float, 2> A = kf::cos(input); |
| 87 | +// Use the default policy |
| 88 | +kf::vec<float, 2> a = kf::cos(input); |
28 | 89 |
|
29 |
| - // Use the most accuracy policy |
30 |
| - kf::vec<float, 2> B = kf::cos<kf::accurate_policy>(input); |
| 90 | +// Use the default policy |
| 91 | +kf::vec<float, 2> b = kf::cos<kf::default_policy>(input); |
31 | 92 |
|
32 |
| - // Use the fastest policy |
33 |
| - kf::vec<float, 2> C = kf::cos<kf::fast_policy>(input); |
| 93 | +// Use the most accurate policy |
| 94 | +kf::vec<float, 2> c = kf::cos<kf::accurate_policy>(input); |
34 | 95 |
|
35 |
| - printf("A = %f, %f", A[0], A[1]); |
36 |
| - printf("B = %f, %f", B[0], B[1]); |
37 |
| - printf("C = %f, %f", C[0], C[1]); |
| 96 | +// Use the fastest policy |
| 97 | +kf::vec<float, 2> d = kf::cos<kf::fast_policy>(input); |
38 | 98 |
|
39 |
| - return EXIT_SUCCESS; |
40 |
| -} |
| 99 | +// Use the approximate policy |
| 100 | +kf::vec<float, 2> e = kf::cos<kf::approx_policy<3>>(input); |
41 | 101 |
|
| 102 | +// You can use aliases to define your own policy |
| 103 | +using my_own_policy = kf::fast_policy; |
| 104 | +kf::vec<float, 2> f = kf::cos<my_own_policy>(input); |
42 | 105 | ```
|
43 | 106 |
|
| 107 | +## Setting `default_policy` |
44 | 108 |
|
45 |
| -Setting `default_policy` |
46 |
| ---- |
47 |
| -By default, the value for `default_policy` is `accurate_policy`. |
| 109 | +By default, `kf::default_policy` is set to `kf::accurate_policy`. |
48 | 110 |
|
49 |
| -Set the preprocessor option `KERNEL_FLOAT_FAST_MATH=1` to change the default policy to `fast_policy`. |
| 111 | +Set the preprocessor option `KERNEL_FLOAT_FAST_MATH=1` to change the default policy to `kf::fast_policy`. |
| 112 | +This will use fast math for all functions and data types that support it. |
0 commit comments