Repository navigation
Flush denormal floats to zero for ONNX inference and compute threads #1619
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
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| /* Floating-Point Mode | ||
| * | ||
| * From: https://github.com/PokemonAutomation/ | ||
| * | ||
| * Turn on flush-to-zero for denormal floats on the current thread. | ||
| * | ||
| * Denormal (subnormal) floats are nonzero values too small for the normal float | ||
| * format: below about 1.2e-38 for float. Intel CPUs handle many operations on one | ||
| * in microcode, which is many times slower than normal float math. AMD and Apple | ||
| * CPUs pay much less. The large PaddleOCR model for Chinese and Japanese produces | ||
| * them: on the Mac Intel CI runner (an i7-8700B), each of those OCR tests took | ||
| * 35-185 s, and under 5 s with flush-to-zero. | ||
| * | ||
| * With flush-to-zero, the CPU treats denormal inputs as zero and rounds denormal | ||
| * results to zero. Model inference doesn't need values that small. | ||
| * | ||
| * This program turns it on only while ONNX Runtime runs a model: | ||
| * - on ONNX Runtime's own thread pool, with `SetGlobalDenormalAsZero()` in | ||
| * ML/Models/ML_OrtEnv.cpp, and | ||
| * - on the thread that calls `Run()`, for the length of the call, with | ||
| * `FlushDenormalsToZeroScope`. | ||
| * All other code keeps the default floating-point behavior. | ||
| * | ||
| * The mode belongs to each thread: | ||
| * - x86-64: the FTZ and DAZ bits of MXCSR, which control SSE and AVX math. | ||
| * - ARM64 (GCC and Clang): the FZ bit of FPCR, which controls scalar and NEON math. | ||
| * - Anywhere else, these functions do nothing. | ||
| * | ||
| */ | ||
|
|
||
| #ifndef PokemonAutomation_Hardware_FloatingPointMode_H | ||
| #define PokemonAutomation_Hardware_FloatingPointMode_H | ||
|
|
||
| #include <stdint.h> | ||
| #if defined(__x86_64__) || defined(_M_X64) | ||
| #include <xmmintrin.h> | ||
| #endif | ||
|
|
||
| namespace PokemonAutomation{ | ||
|
|
||
|
|
||
| // Return the current thread's floating-point control register, so it can be | ||
| // restored later with `set_floating_point_control()`. | ||
| inline uint64_t get_floating_point_control(){ | ||
| #if defined(__x86_64__) || defined(_M_X64) | ||
| return _mm_getcsr(); | ||
| #elif defined(__aarch64__) | ||
| uint64_t fpcr; | ||
| __asm__ __volatile__("mrs %0, fpcr" : "=r"(fpcr)); | ||
| return fpcr; | ||
| #else | ||
| return 0; | ||
| #endif | ||
| } | ||
|
|
||
| // Restore a value returned by `get_floating_point_control()`. | ||
| inline void set_floating_point_control(uint64_t value){ | ||
| #if defined(__x86_64__) || defined(_M_X64) | ||
| _mm_setcsr((unsigned int)value); | ||
| #elif defined(__aarch64__) | ||
| __asm__ __volatile__("msr fpcr, %0" : : "r"(value)); | ||
| #else | ||
| (void)value; | ||
| #endif | ||
| } | ||
|
|
||
| // Turn on flush-to-zero for denormal floats on the current thread, until the | ||
| // thread ends or the mode is changed again. Prefer `FlushDenormalsToZeroScope`, | ||
| // which restores the previous mode. | ||
| inline void enable_flush_denormals_to_zero(){ | ||
| #if defined(__x86_64__) || defined(_M_X64) | ||
| // 0x8000: FTZ, flush denormal results to zero. | ||
| // 0x0040: DAZ, treat denormal inputs as zero. | ||
| _mm_setcsr(_mm_getcsr() | 0x8040); | ||
| #elif defined(__aarch64__) | ||
| // Bit 24: FZ, flush denormal inputs and results to zero. | ||
| set_floating_point_control(get_floating_point_control() | ((uint64_t)1 << 24)); | ||
| #endif | ||
| } | ||
|
|
||
|
|
||
| // Turn on flush-to-zero on the current thread for this object's lifetime, then | ||
| // restore the previous mode. Wrap each ONNX Runtime `Run()` call in one: ONNX | ||
| // Runtime runs part of each inference on the calling thread, which can be any | ||
| // thread in this program, and its own thread pool doesn't cover that part. | ||
| class FlushDenormalsToZeroScope{ | ||
| public: | ||
| FlushDenormalsToZeroScope(const FlushDenormalsToZeroScope&) = delete; | ||
| void operator=(const FlushDenormalsToZeroScope&) = delete; | ||
|
|
||
| FlushDenormalsToZeroScope() | ||
| : m_saved(get_floating_point_control()) | ||
| { | ||
| enable_flush_denormals_to_zero(); | ||
| } | ||
| ~FlushDenormalsToZeroScope(){ | ||
| set_floating_point_control(m_saved); | ||
| } | ||
|
|
||
| private: | ||
| uint64_t m_saved; | ||
| }; | ||
|
|
||
|
|
||
| } | ||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.