tesseract v5.3.3.20231005
dotproduct_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 <memory>
19#include <gtest/gtest.h>
21#include <tesseract/genericvector.h>
22#include "include_gunit.h"
23#include "matrix.h"
24#include "simddetect.h"
25#include "tprintf.h"
26
27namespace tesseract {
28namespace {
29
30class IntSimdMatrixTest : public ::testing::Test {
31 protected:
32 void SetUp() {
33 std::locale::global(std::locale(""));
34 }
35
36 // Makes a random weights matrix of the given size.
37 GENERIC_2D_ARRAY<int8_t> InitRandom(int no, int ni) {
38 GENERIC_2D_ARRAY<int8_t> a(no, ni, 0);
39 for (int i = 0; i < no; ++i) {
40 for (int j = 0; j < ni; ++j) {
41 a(i, j) = static_cast<int8_t>(random_.SignedRand(INT8_MAX));
42 }
43 }
44 return a;
45 }
46 // Makes a random input vector of the given size, with rounding up.
47 std::vector<int8_t> RandomVector(int size, const IntSimdMatrix& matrix) {
48 int rounded_size = matrix.RoundInputs(size);
49 std::vector<int8_t> v(rounded_size, 0);
50 for (int i = 0; i < size; ++i) {
51 v[i] = static_cast<int8_t>(random_.SignedRand(INT8_MAX));
52 }
53 return v;
54 }
55 // Makes a random scales vector of the given size.
56 GenericVector<double> RandomScales(int size) {
57 GenericVector<double> v(size, 0.0);
58 for (int i = 0; i < size; ++i) {
59 v[i] = 1.0 + random_.SignedRand(1.0);
60 }
61 return v;
62 }
63 // Tests a range of sizes and compares the results against the generic version.
64 void ExpectEqualResults(const IntSimdMatrix& matrix) {
65 double total = 0.0;
66 for (int num_out = 1; num_out < 130; ++num_out) {
67 for (int num_in = 1; num_in < 130; ++num_in) {
68 GENERIC_2D_ARRAY<int8_t> w = InitRandom(num_out, num_in + 1);
69 std::vector<int8_t> u = RandomVector(num_in, matrix);
70 GenericVector<double> scales = RandomScales(num_out);
71 std::vector<double> base_result(num_out);
72 IntSimdMatrix::MatrixDotVector(w, scales, u.data(), base_result.data());
73 std::vector<double> test_result(num_out);
74 std::vector<int8_t> shaped_wi;
75 matrix.Init(w, shaped_wi);
76 if (matrix.matrixDotVectorFunction) {
77 matrix.matrixDotVectorFunction(w.dim1(), w.dim2(), &shaped_wi[0],
78 &scales[0], &u[0], &test_result[0]);
79 } else {
80 IntSimdMatrix::MatrixDotVector(w, scales, u.data(), test_result.data());
81 }
82 for (int i = 0; i < num_out; ++i) {
83 EXPECT_FLOAT_EQ(base_result[i], test_result[i]) << "i=" << i;
84 total += base_result[i];
85 }
86 }
87 }
88 // Compare sum of all results with expected value.
89 EXPECT_FLOAT_EQ(total, -423243.392011);
90 }
91
92 TRand random_;
93};
94
95// Test the C++ implementation without SIMD.
96TEST_F(IntSimdMatrixTest, C) {
97 static const IntSimdMatrix matrix = {nullptr, 1, 1, 1, 1};
98 ExpectEqualResults(matrix);
99}
100
101// Tests that the SSE implementation gets the same result as the vanilla.
102TEST_F(IntSimdMatrixTest, SSE) {
103#if defined(HAVE_SSE4_1)
105 GTEST_LOG_(INFO) << "No SSE found! Not tested!";
106 GTEST_SKIP();
107 }
108 ExpectEqualResults(IntSimdMatrix::intSimdMatrixSSE);
109#else
110 GTEST_LOG_(INFO) << "SSE unsupported! Not tested!";
111 GTEST_SKIP();
112#endif
113}
114
115// Tests that the AVX2 implementation gets the same result as the vanilla.
116TEST_F(IntSimdMatrixTest, AVX2) {
117#if defined(HAVE_AVX2)
119 GTEST_LOG_(INFO) << "No AVX2 found! Not tested!";
120 GTEST_SKIP();
121 }
122 ExpectEqualResults(IntSimdMatrix::intSimdMatrixAVX2);
123#else
124 GTEST_LOG_(INFO) << "AVX2 unsupported! Not tested!";
125 GTEST_SKIP();
126#endif
127}
128
129} // namespace
130} // 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
TEST_F(EuroText, FastLatinOCR)
static const IntSimdMatrix intSimdMatrixAVX2
static void MatrixDotVector(const GENERIC_2D_ARRAY< int8_t > &w, const std::vector< TFloat > &scales, const int8_t *u, TFloat *v)
static const IntSimdMatrix intSimdMatrixSSE
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)