Merge pull request #2740 from stweil/master

Use pre-calculated lookup tables for all C++ compilers
This commit is contained in:
Egor Pugin 2019-11-01 01:13:42 +03:00 committed by GitHub
commit 0d96ff3617
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 8224 additions and 77 deletions

File diff suppressed because it is too large Load Diff

View File

@ -35,40 +35,9 @@ constexpr int kTableSize = 4096;
// Scale factor for float arg to int index.
constexpr double kScaleFactor = 256.0;
#if __cplusplus < 201402 || defined(__clang__) || defined(__INTEL_COMPILER) // C++11
extern double TanhTable[];
extern double LogisticTable[];
#else // C++14 or newer
typedef double (*LUT_FUNCTION)(int i);
constexpr double LUTFuncTanh(int i) {
return std::tanh(i / kScaleFactor);
}
constexpr double LUTFuncLog(int i) {
return 1 / (1 + std::exp(-i / kScaleFactor));
}
template<int n, LUT_FUNCTION f>
struct LUTTempl {
constexpr LUTTempl() : table_() {
for (auto i = 0; i < n; ++i) {
table_[i] = f(i);
}
}
const double& operator[](size_t i) const {
return table_[i];
}
double table_[n];
};
extern const LUTTempl<kTableSize, LUTFuncTanh> TanhTable;
extern const LUTTempl<kTableSize, LUTFuncLog> LogisticTable;
#endif
// Generated lookup tables.
extern const double TanhTable[];
extern const double LogisticTable[];
// Non-linearity (sigmoid) functions with cache tables and clipping.
inline double Tanh(double x) {

25
src/lstm/generate_lut.py Executable file
View File

@ -0,0 +1,25 @@
#!/usr/bin/env python3
# Create C/C++ code for two lookup tables.
import math
# Size of static tables.
kTableSize = 4096
# Scale factor for float arg to int index.
kScaleFactor = 256.0
print("// Generated code with lookup tables")
print('#include "functions.h"')
print("namespace tesseract {")
print("const double TanhTable[] = {")
for i in range(kTableSize):
print(" %a," % math.tanh(i / kScaleFactor))
print("};")
print("const double LogisticTable[] = {")
for i in range(kTableSize):
print(" %a," % (1 / (1 + math.exp(-i / kScaleFactor))))
print("};")
print("} // namespace tesseract.")