Aussie AI
FTZ and DAZ CPU Modes
-
Book Excerpt from "Generative AI in C++"
-
by David Spuler, Ph.D.
FTZ and DAZ CPU Modes
In many CPUs, the need to handle overflow, underflow and denormalized values is a cause of inefficiency. The CPU can do floating-point computations faster if it can ignore those situations. This would be in violation of the IEEE 754 standard, but sometimes you have to sacrifice greatness for speed.
There are two commonly used modifications to CPUs that speed up floating-point arithmetic, by ignoring underflow and tiny numbers:
Flush-To-Zero (FTZ). This mode means that when the results are “subnormal” they are “flushed” to zero instead of calculating the correct “denormalized” result. Since these denormalized numbers are tiny, this isn't a concern in AI engines.
Denormalized-Are-Zero (DAZ). This is similar to FTZ, but allows treating inputs that are some type of denormalized floating-point as a zero input.
Both these modes, FTZ and DAZ, are only relevant to very tiny numbers, well below the resolution that AI engines
need to worry about, so you can totally enable them,
provided we can figure out how to do so.
CPUs with support for the FTZ and DAZ modes include x86 CPUs and ARM Cortex cores, and likely other processors.
Google TPU doesn't support FTZ/DAZ because it operates on bfloat16
floating-point numbers.
Enabling FTZ and DAZ.
Finding details on how to enable FTZ and DAZ is quite hard!
(If only there was an AI engine to help me search the internet.
Oh, wait, nevermind!)
For command-line options, it seems to be “-ftz
” on Linux/Mac or “/Qftz
” on Windows.
To control these modes dynamically in C++ code,
you need to modify the MXCSR x86-64 CPU control register at runtime to set (or clear)
the bits corresponding to FTZ and DAZ.
Some of the primitives available to do so via GCC intrinsics include:
__builtin_ia32_ldmxcsr
__builtin_ia32_stmxcsr
_mm_getcsr
_mm_setcsr
In MSVS, there are preprocessor macros for FTZ in <xmmintrin.h>
and for DAZ in <pmmintrin.h>
header files.
These control the FTZ and DAZ bits in the MXCSR,
which is a CPU register with flags to control the CPU and the FPU.
The C++ snippet to enable these modes looks like:
#include <xmmintrin.h> #include <pmmintrin.h> void aussie_float_enable_FTZ_DAZ(bool ftz, bool daz) { if (ftz) { // FTZ mode _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); } else { _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_OFF); } if (daz) { // DAZ mode _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON); } else { _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_OFF); } }
These intrinsics for FTZ and DAZ are dynamic C++ calls. You can also disable these modes in C++, or switch back-and-forth between them dynamically. The MXCSR values are per-thread, so these modes must be set at the start of every new thread.
• Next: • Up: Table of Contents |
The new AI programming book by Aussie AI co-founders:
Get your copy from Amazon: Generative AI in C++ |