2017-09-08 22:06:19 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// File: intsimdmatrix_test.cc
|
|
|
|
// Author: rays@google.com (Ray Smith)
|
|
|
|
//
|
|
|
|
// Copyright 2017 Google Inc. All Rights Reserved.
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "intsimdmatrix.h"
|
|
|
|
#include <memory>
|
2019-11-29 00:36:07 +08:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <gtest/internal/gtest-port.h>
|
2019-10-30 01:01:18 +08:00
|
|
|
#include <tesseract/genericvector.h>
|
2017-09-08 22:06:19 +08:00
|
|
|
#include "include_gunit.h"
|
2018-06-24 22:14:28 +08:00
|
|
|
#include "matrix.h"
|
2017-09-08 22:06:19 +08:00
|
|
|
#include "simddetect.h"
|
|
|
|
#include "tprintf.h"
|
|
|
|
|
|
|
|
namespace tesseract {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class IntSimdMatrixTest : public ::testing::Test {
|
|
|
|
protected:
|
2019-05-17 00:12:06 +08:00
|
|
|
void SetUp() {
|
|
|
|
std::locale::global(std::locale(""));
|
|
|
|
}
|
|
|
|
|
2017-09-08 22:06:19 +08:00
|
|
|
// Makes a random weights matrix of the given size.
|
|
|
|
GENERIC_2D_ARRAY<int8_t> InitRandom(int no, int ni) {
|
|
|
|
GENERIC_2D_ARRAY<int8_t> a(no, ni, 0);
|
|
|
|
for (int i = 0; i < no; ++i) {
|
|
|
|
for (int j = 0; j < ni; ++j) {
|
Use POSIX data types and macros (#878)
* api: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccmain: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccstruct: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* classify: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* cutil: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* dict: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* textord: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* training: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* wordrec: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccutil: Replace Tesseract data types by POSIX data types
Now all Tesseract data types which are no longer needed can be removed
from ccutil/host.h.
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccmain: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccstruct: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* classify: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* dict: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* lstm: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* textord: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* wordrec: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccutil: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Remove the macros which are now unused from ccutil/host.h.
Remove also the obsolete history comments.
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* Fix build error caused by ambiguous ClipToRange
Error message vom Appveyor CI:
C:\projects\tesseract\ccstruct\coutln.cpp(818): error C2672: 'ClipToRange': no matching overloaded function found [C:\projects\tesseract\build\libtesseract.vcxproj]
C:\projects\tesseract\ccstruct\coutln.cpp(818): error C2782: 'T ClipToRange(const T &,const T &,const T &)': template parameter 'T' is ambiguous [C:\projects\tesseract\build\libtesseract.vcxproj]
c:\projects\tesseract\ccutil\helpers.h(122): note: see declaration of 'ClipToRange'
C:\projects\tesseract\ccstruct\coutln.cpp(818): note: could be 'char'
C:\projects\tesseract\ccstruct\coutln.cpp(818): note: or 'int'
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* unittest: Replace Tesseract's MAX_INT8 by POSIX INT8_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* arch: Replace Tesseract's MAX_INT8 by POSIX INT8_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
2018-03-14 04:36:30 +08:00
|
|
|
a(i, j) = static_cast<int8_t>(random_.SignedRand(INT8_MAX));
|
2017-09-08 22:06:19 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return a;
|
|
|
|
}
|
|
|
|
// Makes a random input vector of the given size, with rounding up.
|
|
|
|
std::vector<int8_t> RandomVector(int size, const IntSimdMatrix& matrix) {
|
|
|
|
int rounded_size = matrix.RoundInputs(size);
|
|
|
|
std::vector<int8_t> v(rounded_size, 0);
|
|
|
|
for (int i = 0; i < size; ++i) {
|
Use POSIX data types and macros (#878)
* api: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccmain: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccstruct: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* classify: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* cutil: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* dict: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* textord: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* training: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* wordrec: Replace Tesseract data types by POSIX data types
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccutil: Replace Tesseract data types by POSIX data types
Now all Tesseract data types which are no longer needed can be removed
from ccutil/host.h.
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccmain: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccstruct: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* classify: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* dict: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* lstm: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* textord: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* wordrec: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* ccutil: Replace Tesseract's MIN_*INT, MAX_*INT* by POSIX *INT*_MIN, *INT*_MAX
Remove the macros which are now unused from ccutil/host.h.
Remove also the obsolete history comments.
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* Fix build error caused by ambiguous ClipToRange
Error message vom Appveyor CI:
C:\projects\tesseract\ccstruct\coutln.cpp(818): error C2672: 'ClipToRange': no matching overloaded function found [C:\projects\tesseract\build\libtesseract.vcxproj]
C:\projects\tesseract\ccstruct\coutln.cpp(818): error C2782: 'T ClipToRange(const T &,const T &,const T &)': template parameter 'T' is ambiguous [C:\projects\tesseract\build\libtesseract.vcxproj]
c:\projects\tesseract\ccutil\helpers.h(122): note: see declaration of 'ClipToRange'
C:\projects\tesseract\ccstruct\coutln.cpp(818): note: could be 'char'
C:\projects\tesseract\ccstruct\coutln.cpp(818): note: or 'int'
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* unittest: Replace Tesseract's MAX_INT8 by POSIX INT8_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
* arch: Replace Tesseract's MAX_INT8 by POSIX INT8_MAX
Signed-off-by: Stefan Weil <sw@weilnetz.de>
2018-03-14 04:36:30 +08:00
|
|
|
v[i] = static_cast<int8_t>(random_.SignedRand(INT8_MAX));
|
2017-09-08 22:06:19 +08:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
// Makes a random scales vector of the given size.
|
|
|
|
GenericVector<double> RandomScales(int size) {
|
|
|
|
GenericVector<double> v(size, 0.0);
|
|
|
|
for (int i = 0; i < size; ++i) {
|
2020-07-06 23:10:49 +08:00
|
|
|
v[i] = (1.0 + random_.SignedRand(1.0)) / INT8_MAX;
|
2017-09-08 22:06:19 +08:00
|
|
|
}
|
|
|
|
return v;
|
|
|
|
}
|
2019-01-12 16:44:08 +08:00
|
|
|
// Tests a range of sizes and compares the results against the generic version.
|
|
|
|
void ExpectEqualResults(const IntSimdMatrix& matrix) {
|
2019-01-13 19:50:50 +08:00
|
|
|
double total = 0.0;
|
2017-09-08 22:06:19 +08:00
|
|
|
for (int num_out = 1; num_out < 130; ++num_out) {
|
|
|
|
for (int num_in = 1; num_in < 130; ++num_in) {
|
|
|
|
GENERIC_2D_ARRAY<int8_t> w = InitRandom(num_out, num_in + 1);
|
2019-01-12 16:44:08 +08:00
|
|
|
std::vector<int8_t> u = RandomVector(num_in, matrix);
|
2017-09-08 22:06:19 +08:00
|
|
|
GenericVector<double> scales = RandomScales(num_out);
|
2020-05-18 22:36:42 +08:00
|
|
|
int ro = num_out;
|
|
|
|
if (IntSimdMatrix::intSimdMatrix)
|
|
|
|
ro = IntSimdMatrix::intSimdMatrix->RoundOutputs(ro);
|
|
|
|
std::vector<double> base_result(ro);
|
|
|
|
base_result.resize(num_out);
|
2019-01-13 06:00:31 +08:00
|
|
|
IntSimdMatrix::MatrixDotVector(w, scales, u.data(), base_result.data());
|
2020-05-18 22:36:42 +08:00
|
|
|
std::vector<double> test_result(ro);
|
|
|
|
test_result.resize(num_out);
|
2019-01-12 14:35:41 +08:00
|
|
|
std::vector<int8_t> shaped_wi;
|
2020-10-29 03:35:24 +08:00
|
|
|
int32_t rounded_num_out;
|
|
|
|
matrix.Init(w, shaped_wi, rounded_num_out);
|
|
|
|
scales.resize_no_init(rounded_num_out);
|
2019-01-13 06:00:31 +08:00
|
|
|
if (matrix.matrixDotVectorFunction) {
|
|
|
|
matrix.matrixDotVectorFunction(w.dim1(), w.dim2(), &shaped_wi[0],
|
|
|
|
&scales[0], &u[0], &test_result[0]);
|
|
|
|
} else {
|
|
|
|
IntSimdMatrix::MatrixDotVector(w, scales, u.data(), test_result.data());
|
|
|
|
}
|
2019-01-13 19:50:50 +08:00
|
|
|
for (int i = 0; i < num_out; ++i) {
|
2017-09-08 22:06:19 +08:00
|
|
|
EXPECT_FLOAT_EQ(base_result[i], test_result[i]) << "i=" << i;
|
2019-01-13 19:50:50 +08:00
|
|
|
total += base_result[i];
|
|
|
|
}
|
2017-09-08 22:06:19 +08:00
|
|
|
}
|
|
|
|
}
|
2019-01-13 19:50:50 +08:00
|
|
|
// Compare sum of all results with expected value.
|
|
|
|
EXPECT_FLOAT_EQ(total, -423243.392011);
|
2017-09-08 22:06:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TRand random_;
|
|
|
|
};
|
|
|
|
|
2019-01-13 19:50:50 +08:00
|
|
|
// Test the C++ implementation without SIMD.
|
|
|
|
TEST_F(IntSimdMatrixTest, C) {
|
2019-01-14 03:25:58 +08:00
|
|
|
static const IntSimdMatrix matrix = {nullptr, 1, 1, 1, 1};
|
2019-01-12 16:44:08 +08:00
|
|
|
ExpectEqualResults(matrix);
|
2019-01-13 19:50:50 +08:00
|
|
|
}
|
|
|
|
|
2017-09-08 22:06:19 +08:00
|
|
|
// Tests that the SSE implementation gets the same result as the vanilla.
|
|
|
|
TEST_F(IntSimdMatrixTest, SSE) {
|
Replace AVX_OPT, ..., AVX macros by HAVE_AVX, ... and clean related code
- Replace AVX_OPT, AVX2_OPT, FMA_OPT, SSE41_OPT
- Replace AVX, AVX2, FMA, SSE4_1
- Write new HAVE_AVX, HAVE_AVX2, HAVE_FMA, HAVE_SSE4_1 into config_auto.h
- Put related conditionals in Makefile.am in one place
This makes the code clearer and fixes a log message in
IntSimdMatrixTest.AVX2.
Signed-off-by: Stefan Weil <sw@weilnetz.de>
2019-11-29 00:28:06 +08:00
|
|
|
#if defined(HAVE_SSE4_1)
|
2019-11-29 00:36:07 +08:00
|
|
|
if (!SIMDDetect::IsSSEAvailable()) {
|
|
|
|
GTEST_LOG_(INFO) << "No SSE found! Not tested!";
|
|
|
|
GTEST_SKIP();
|
2017-09-08 22:06:19 +08:00
|
|
|
}
|
2019-01-13 06:00:31 +08:00
|
|
|
ExpectEqualResults(IntSimdMatrix::intSimdMatrixSSE);
|
2019-01-13 04:30:45 +08:00
|
|
|
#else
|
2019-11-29 00:36:07 +08:00
|
|
|
GTEST_LOG_(INFO) << "SSE unsupported! Not tested!";
|
|
|
|
GTEST_SKIP();
|
2019-01-13 04:30:45 +08:00
|
|
|
#endif
|
2017-09-08 22:06:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that the AVX2 implementation gets the same result as the vanilla.
|
|
|
|
TEST_F(IntSimdMatrixTest, AVX2) {
|
Replace AVX_OPT, ..., AVX macros by HAVE_AVX, ... and clean related code
- Replace AVX_OPT, AVX2_OPT, FMA_OPT, SSE41_OPT
- Replace AVX, AVX2, FMA, SSE4_1
- Write new HAVE_AVX, HAVE_AVX2, HAVE_FMA, HAVE_SSE4_1 into config_auto.h
- Put related conditionals in Makefile.am in one place
This makes the code clearer and fixes a log message in
IntSimdMatrixTest.AVX2.
Signed-off-by: Stefan Weil <sw@weilnetz.de>
2019-11-29 00:28:06 +08:00
|
|
|
#if defined(HAVE_AVX2)
|
2019-11-29 00:36:07 +08:00
|
|
|
if (!SIMDDetect::IsAVX2Available()) {
|
|
|
|
GTEST_LOG_(INFO) << "No AVX2 found! Not tested!";
|
|
|
|
GTEST_SKIP();
|
2017-09-08 22:06:19 +08:00
|
|
|
}
|
2019-01-13 06:00:31 +08:00
|
|
|
ExpectEqualResults(IntSimdMatrix::intSimdMatrixAVX2);
|
2019-01-13 04:30:45 +08:00
|
|
|
#else
|
2019-11-29 00:36:07 +08:00
|
|
|
GTEST_LOG_(INFO) << "AVX2 unsupported! Not tested!";
|
|
|
|
GTEST_SKIP();
|
2019-01-13 04:30:45 +08:00
|
|
|
#endif
|
2017-09-08 22:06:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
} // namespace tesseract
|