tesseract v5.3.3.20231005
intsimdmatrix_test.cc
Go to the documentation of this file.
1
2// File: intsimdmatrix_test.cc
3// Author: rays@google.com (Ray Smith)
4//
5// Copyright 2017 Google Inc. All Rights Reserved.
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9// http://www.apache.org/licenses/LICENSE-2.0
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
16
17#include "intsimdmatrix.h"
18#include <gtest/gtest.h>
20#include <memory>
21#include <vector>
22#include "include_gunit.h"
23#include "matrix.h"
24#include "simddetect.h"
25#include "tprintf.h"
26
27namespace tesseract {
28
29class IntSimdMatrixTest : public ::testing::Test {
30protected:
31 void SetUp() override {
32 std::locale::global(std::locale(""));
33 }
34
35 // Makes a random weights matrix of the given size.
37 GENERIC_2D_ARRAY<int8_t> a(no, ni, 0);
38 for (int i = 0; i < no; ++i) {
39 for (int j = 0; j < ni; ++j) {
40 a(i, j) = static_cast<int8_t>(random_.SignedRand(INT8_MAX));
41 }
42 }
43 return a;
44 }
45 // Makes a random input vector of the given size, with rounding up.
46 std::vector<int8_t> RandomVector(int size, const IntSimdMatrix &matrix) {
47 int rounded_size = matrix.RoundInputs(size);
48 std::vector<int8_t> v(rounded_size, 0);
49 for (int i = 0; i < size; ++i) {
50 v[i] = static_cast<int8_t>(random_.SignedRand(INT8_MAX));
51 }
52 return v;
53 }
54 // Makes a random scales vector of the given size.
55 std::vector<TFloat> RandomScales(int size) {
56 std::vector<TFloat> v(size);
57 for (int i = 0; i < size; ++i) {
58 v[i] = (1.0 + random_.SignedRand(1.0)) / INT8_MAX;
59 }
60 return v;
61 }
62 // Tests a range of sizes and compares the results against the generic version.
63 void ExpectEqualResults(const IntSimdMatrix &matrix) {
64 TFloat total = 0.0;
65 for (int num_out = 1; num_out < 130; ++num_out) {
66 for (int num_in = 1; num_in < 130; ++num_in) {
67 GENERIC_2D_ARRAY<int8_t> w = InitRandom(num_out, num_in + 1);
68 std::vector<int8_t> u = RandomVector(num_in, matrix);
69 std::vector<TFloat> scales = RandomScales(num_out);
70 int ro = num_out;
73 }
74 std::vector<TFloat> base_result(num_out);
75 IntSimdMatrix::MatrixDotVector(w, scales, u.data(), base_result.data());
76 std::vector<TFloat> test_result(ro);
77 std::vector<int8_t> shaped_wi;
78 int32_t rounded_num_out;
79 matrix.Init(w, shaped_wi, rounded_num_out);
80 scales.resize(rounded_num_out);
81 if (matrix.matrixDotVectorFunction) {
82 matrix.matrixDotVectorFunction(w.dim1(), w.dim2(), &shaped_wi[0], &scales[0], &u[0],
83 &test_result[0]);
84 } else {
85 IntSimdMatrix::MatrixDotVector(w, scales, u.data(), test_result.data());
86 }
87 for (int i = 0; i < num_out; ++i) {
88 EXPECT_FLOAT_EQ(base_result[i], test_result[i]) << "i=" << i;
89 total += base_result[i];
90 }
91 }
92 }
93 // Compare sum of all results with expected value.
94#ifdef FAST_FLOAT
95 EXPECT_FLOAT_EQ(total, 337852.16f);
96#else
97 EXPECT_FLOAT_EQ(total, 337849.39354684710);
98#endif
99 }
100
102};
103
104// Test the C++ implementation without SIMD.
105TEST_F(IntSimdMatrixTest, C) {
106 static const IntSimdMatrix matrix = {nullptr, 1, 1, 1, 1};
107 ExpectEqualResults(matrix);
108}
109
110// Tests that the SSE implementation gets the same result as the vanilla.
111TEST_F(IntSimdMatrixTest, SSE) {
112#if defined(HAVE_SSE4_1)
114 GTEST_LOG_(INFO) << "No SSE found! Not tested!";
115 GTEST_SKIP();
116 }
117 ExpectEqualResults(IntSimdMatrix::intSimdMatrixSSE);
118#else
119 GTEST_LOG_(INFO) << "SSE unsupported! Not tested!";
120 GTEST_SKIP();
121#endif
122}
123
124// Tests that the AVX2 implementation gets the same result as the vanilla.
125TEST_F(IntSimdMatrixTest, AVX2) {
126#if defined(HAVE_AVX2)
128 GTEST_LOG_(INFO) << "No AVX2 found! Not tested!";
129 GTEST_SKIP();
130 }
131 ExpectEqualResults(IntSimdMatrix::intSimdMatrixAVX2);
132#else
133 GTEST_LOG_(INFO) << "AVX2 unsupported! Not tested!";
134 GTEST_SKIP();
135#endif
136}
137
138} // namespace tesseract
TRand random_
@ INFO
Definition: log.h:28
#define GTEST_SKIP()
Definition: gtest.h:1889
#define EXPECT_FLOAT_EQ(val1, val2)
Definition: gtest.h:2144
#define GTEST_LOG_(severity)
Definition: gtest-port.h:984
double TFloat
Definition: tesstypes.h:39
TEST_F(EuroText, FastLatinOCR)
static const IntSimdMatrix intSimdMatrixAVX2
int RoundOutputs(int size) const
Definition: intsimdmatrix.h:74
static void MatrixDotVector(const GENERIC_2D_ARRAY< int8_t > &w, const std::vector< TFloat > &scales, const int8_t *u, TFloat *v)
int RoundInputs(int size) const
Definition: intsimdmatrix.h:70
MatrixDotVectorFunction matrixDotVectorFunction
static const IntSimdMatrix * intSimdMatrix
static const IntSimdMatrix intSimdMatrixSSE
void Init(const GENERIC_2D_ARRAY< int8_t > &w, std::vector< int8_t > &shaped_w, int32_t &rounded_num_out) const
static bool IsSSEAvailable()
Definition: simddetect.h:59
static bool IsAVX2Available()
Definition: simddetect.h:39
std::vector< int8_t > RandomVector(int size, const IntSimdMatrix &matrix)
std::vector< TFloat > RandomScales(int size)
void ExpectEqualResults(const IntSimdMatrix &matrix)
GENERIC_2D_ARRAY< int8_t > InitRandom(int no, int ni)