Commit d7960fdd for tesseract
commit d7960fdddd86098988a3d371ea85caa8062568f7
Author: Stefan Weil <sw@weilnetz.de>
Date: Sat Nov 14 09:42:19 2026 +0100
lstm: calculate several dot products at once with NEON
The products of a float weight matrix with a vector (forward and
backward) and the sum of the outer products for the gradients were
calculated as single dot products, one function call for each row or
each element of the result. Profiling of lstmtraining on ARM64 shows
that about two thirds of the time are spent there.
Add the function pointers RowDotProducts and CrossDotProducts for
these products. They follow the selected dot product function:
the generic implementations call DotProduct for each result, so
nothing changes for the other architectures, and the results are
still the same as before for dotproduct=generic and the other choices.
The new NEON implementations for ARM64 with FAST_FLOAT handle four
rows of the matrix per pass (matrix times vector) or a block of
4 x 4 dot products (sum of outer products), so the vectors are loaded
less often and more independent sums are calculated at the same time.
Training of the default network of tesstrain
([1,36,0,1 Ct3,3,16 Mp3,3 Lfys48 Lfx96 Lrx96 Lfx192 O1c111]) with a
single thread is 1.4 times faster on a Cortex-X925 (NVIDIA DGX Spark,
g++ 13: 14.4 s -> 10.2 s for 1000 lines) and on an Apple M1 Pro
(22.2 s -> 15.9 s). Recognition with float models uses the same
forward product.
The order of the additions is different, so the results on ARM64
differ slightly from those of the old code (like those of different
dot product functions do), but they are still deterministic.
Add a unit test which compares the three products of WeightMatrix
with a calculation with doubles.
Assisted-by: OpenCode / Claude Fable 5.2 (Anthropic)
Signed-off-by: Stefan Weil <sw@weilnetz.de>
diff --git a/Makefile.am b/Makefile.am
index 391d2e75..5b429b05 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1259,6 +1259,7 @@ check_PROGRAMS += validator_test
endif # ENABLE_TRAINING
check_PROGRAMS += weightmatrix_deserialize_test
check_PROGRAMS += weightmatrix_intdim_test
+check_PROGRAMS += weightmatrix_product_test
check_PROGRAMS += weightmatrix_range_test
check_PROGRAMS += weightmatrix_scales_test
@@ -1651,9 +1652,15 @@ weightmatrix_intdim_test_SOURCES = unittest/weightmatrix_intdim_test.cc
weightmatrix_intdim_test_CPPFLAGS = $(unittest_CPPFLAGS)
weightmatrix_intdim_test_LDADD = $(TESS_LIBS)
+<<<<<<< HEAD
weightmatrix_range_test_SOURCES = unittest/weightmatrix_range_test.cc
weightmatrix_range_test_CPPFLAGS = $(unittest_CPPFLAGS)
weightmatrix_range_test_LDADD = $(TESS_LIBS)
+=======
+weightmatrix_product_test_SOURCES = unittest/weightmatrix_product_test.cc
+weightmatrix_product_test_CPPFLAGS = $(unittest_CPPFLAGS)
+weightmatrix_product_test_LDADD = $(TESS_LIBS)
+>>>>>>> 48aa9b075 (lstm: calculate several dot products at once with NEON)
weightmatrix_scales_test_SOURCES = unittest/weightmatrix_scales_test.cc
weightmatrix_scales_test_CPPFLAGS = $(unittest_CPPFLAGS)
diff --git a/src/arch/dotproduct.h b/src/arch/dotproduct.h
index 47a2d939..79d7bc0b 100644
--- a/src/arch/dotproduct.h
+++ b/src/arch/dotproduct.h
@@ -39,6 +39,18 @@ TFloat DotProductSSE(const TFloat *u, const TFloat *v, int n);
// Use NEON intrinsics.
TFloat DotProductNEON(const TFloat *u, const TFloat *v, int n);
+#if defined(FAST_FLOAT) && defined(__ARM_ARCH_ISA_A64)
+// Computes v[i] = w[i] . u for num_rows rows of w with n used elements each.
+// Row i of w starts at w + i * w_stride.
+void RowDotProductsNEON(const float *w, int w_stride, int num_rows,
+ const float *u, int n, float *v);
+
+// Computes out[i * out_stride + j] = u[i] . v[j] for the num_u rows of u and
+// the num_v rows of v, which all have n elements and are stored without gaps.
+void CrossDotProductsNEON(const float *u, int num_u, const float *v, int num_v,
+ int n, float *out, int out_stride);
+#endif
+
} // namespace tesseract.
#endif // TESSERACT_ARCH_DOTPRODUCT_H_
diff --git a/src/arch/dotproductneon.cpp b/src/arch/dotproductneon.cpp
index af68b12d..c1e1c62f 100644
--- a/src/arch/dotproductneon.cpp
+++ b/src/arch/dotproductneon.cpp
@@ -17,6 +17,7 @@
#if defined(__ARM_NEON)
#include <arm_neon.h>
+#include <cstddef> // for size_t
#include "dotproduct.h"
namespace tesseract {
@@ -50,6 +51,153 @@ float DotProductNEON(const float *u, const float *v, int n) {
return total;
}
+// Computes v[i] = w[i] . u for num_rows rows of w with n used elements each.
+// Four rows are handled per pass, so u is loaded only once for them and
+// eight independent sums are calculated at the same time.
+void RowDotProductsNEON(const float *w, int w_stride, int num_rows,
+ const float *u, int n, float *v) {
+ int i = 0;
+ for (; i + 4 <= num_rows; i += 4) {
+ const float *w0 = w + static_cast<size_t>(i) * w_stride;
+ const float *w1 = w0 + w_stride;
+ const float *w2 = w1 + w_stride;
+ const float *w3 = w2 + w_stride;
+ float32x4_t a0 = vdupq_n_f32(0.0f);
+ float32x4_t a1 = a0, a2 = a0, a3 = a0;
+ float32x4_t b0 = a0, b1 = a0, b2 = a0, b3 = a0;
+ int j = 0;
+ for (; j + 8 <= n; j += 8) {
+ float32x4_t x = vld1q_f32(u + j);
+ float32x4_t y = vld1q_f32(u + j + 4);
+ a0 = vfmaq_f32(a0, vld1q_f32(w0 + j), x);
+ b0 = vfmaq_f32(b0, vld1q_f32(w0 + j + 4), y);
+ a1 = vfmaq_f32(a1, vld1q_f32(w1 + j), x);
+ b1 = vfmaq_f32(b1, vld1q_f32(w1 + j + 4), y);
+ a2 = vfmaq_f32(a2, vld1q_f32(w2 + j), x);
+ b2 = vfmaq_f32(b2, vld1q_f32(w2 + j + 4), y);
+ a3 = vfmaq_f32(a3, vld1q_f32(w3 + j), x);
+ b3 = vfmaq_f32(b3, vld1q_f32(w3 + j + 4), y);
+ }
+ if (j + 4 <= n) {
+ float32x4_t x = vld1q_f32(u + j);
+ a0 = vfmaq_f32(a0, vld1q_f32(w0 + j), x);
+ a1 = vfmaq_f32(a1, vld1q_f32(w1 + j), x);
+ a2 = vfmaq_f32(a2, vld1q_f32(w2 + j), x);
+ a3 = vfmaq_f32(a3, vld1q_f32(w3 + j), x);
+ j += 4;
+ }
+ float total0 = vaddvq_f32(vaddq_f32(a0, b0));
+ float total1 = vaddvq_f32(vaddq_f32(a1, b1));
+ float total2 = vaddvq_f32(vaddq_f32(a2, b2));
+ float total3 = vaddvq_f32(vaddq_f32(a3, b3));
+ for (; j < n; j++) {
+ total0 += w0[j] * u[j];
+ total1 += w1[j] * u[j];
+ total2 += w2[j] * u[j];
+ total3 += w3[j] * u[j];
+ }
+ v[i] = total0;
+ v[i + 1] = total1;
+ v[i + 2] = total2;
+ v[i + 3] = total3;
+ }
+ for (; i < num_rows; i++) {
+ v[i] = DotProductNEON(w + static_cast<size_t>(i) * w_stride, u, n);
+ }
+}
+
+// Computes out[i * out_stride + j] = u[i] . v[j] for the num_u rows of u and
+// the num_v rows of v, which all have n elements and are stored without gaps.
+// Four rows of u and four rows of v are handled per pass, so each of them is
+// loaded only once for 16 sums which are calculated at the same time.
+// The accumulators are named variables instead of an array, because some
+// compilers do not keep an array of vectors in registers.
+void CrossDotProductsNEON(const float *u, int num_u, const float *v, int num_v,
+ int n, float *out, int out_stride) {
+ int i = 0;
+ for (; i + 4 <= num_u; i += 4) {
+ const float *u0 = u + static_cast<size_t>(i) * n;
+ const float *u1 = u0 + n;
+ const float *u2 = u1 + n;
+ const float *u3 = u2 + n;
+ float *out0 = out + static_cast<size_t>(i) * out_stride;
+ float *out1 = out0 + out_stride;
+ float *out2 = out1 + out_stride;
+ float *out3 = out2 + out_stride;
+ int j = 0;
+ for (; j + 4 <= num_v; j += 4) {
+ const float *v0 = v + static_cast<size_t>(j) * n;
+ const float *v1 = v0 + n;
+ const float *v2 = v1 + n;
+ const float *v3 = v2 + n;
+ float32x4_t a0 = vdupq_n_f32(0.0f);
+ float32x4_t a1 = a0, a2 = a0, a3 = a0;
+ float32x4_t b0 = a0, b1 = a0, b2 = a0, b3 = a0;
+ float32x4_t c0 = a0, c1 = a0, c2 = a0, c3 = a0;
+ float32x4_t d0 = a0, d1 = a0, d2 = a0, d3 = a0;
+ int k = 0;
+ for (; k + 4 <= n; k += 4) {
+ float32x4_t y0 = vld1q_f32(v0 + k);
+ float32x4_t y1 = vld1q_f32(v1 + k);
+ float32x4_t y2 = vld1q_f32(v2 + k);
+ float32x4_t y3 = vld1q_f32(v3 + k);
+ float32x4_t x = vld1q_f32(u0 + k);
+ a0 = vfmaq_f32(a0, x, y0);
+ a1 = vfmaq_f32(a1, x, y1);
+ a2 = vfmaq_f32(a2, x, y2);
+ a3 = vfmaq_f32(a3, x, y3);
+ x = vld1q_f32(u1 + k);
+ b0 = vfmaq_f32(b0, x, y0);
+ b1 = vfmaq_f32(b1, x, y1);
+ b2 = vfmaq_f32(b2, x, y2);
+ b3 = vfmaq_f32(b3, x, y3);
+ x = vld1q_f32(u2 + k);
+ c0 = vfmaq_f32(c0, x, y0);
+ c1 = vfmaq_f32(c1, x, y1);
+ c2 = vfmaq_f32(c2, x, y2);
+ c3 = vfmaq_f32(c3, x, y3);
+ x = vld1q_f32(u3 + k);
+ d0 = vfmaq_f32(d0, x, y0);
+ d1 = vfmaq_f32(d1, x, y1);
+ d2 = vfmaq_f32(d2, x, y2);
+ d3 = vfmaq_f32(d3, x, y3);
+ }
+ float total[4][4] = {
+ {vaddvq_f32(a0), vaddvq_f32(a1), vaddvq_f32(a2), vaddvq_f32(a3)},
+ {vaddvq_f32(b0), vaddvq_f32(b1), vaddvq_f32(b2), vaddvq_f32(b3)},
+ {vaddvq_f32(c0), vaddvq_f32(c1), vaddvq_f32(c2), vaddvq_f32(c3)},
+ {vaddvq_f32(d0), vaddvq_f32(d1), vaddvq_f32(d2), vaddvq_f32(d3)}};
+ for (; k < n; k++) {
+ const float x[4] = {u0[k], u1[k], u2[k], u3[k]};
+ const float y[4] = {v0[k], v1[k], v2[k], v3[k]};
+ for (int r = 0; r < 4; r++) {
+ for (int c = 0; c < 4; c++) {
+ total[r][c] += x[r] * y[c];
+ }
+ }
+ }
+ for (int c = 0; c < 4; c++) {
+ out0[j + c] = total[0][c];
+ out1[j + c] = total[1][c];
+ out2[j + c] = total[2][c];
+ out3[j + c] = total[3][c];
+ }
+ }
+ for (; j < num_v; j++) {
+ const float *vj = v + static_cast<size_t>(j) * n;
+ out0[j] = DotProductNEON(u0, vj, n);
+ out1[j] = DotProductNEON(u1, vj, n);
+ out2[j] = DotProductNEON(u2, vj, n);
+ out3[j] = DotProductNEON(u3, vj, n);
+ }
+ }
+ // The remaining rows of u are single vectors for the rows of v.
+ for (; i < num_u; i++) {
+ RowDotProductsNEON(v, n, num_v, u + static_cast<size_t>(i) * n, n,
+ out + static_cast<size_t>(i) * out_stride);
+ }
+}
+
#else
// Computes and returns the dot product of the two n-vectors u and v.
diff --git a/src/arch/simddetect.cpp b/src/arch/simddetect.cpp
index f188d67e..33c41e4a 100644
--- a/src/arch/simddetect.cpp
+++ b/src/arch/simddetect.cpp
@@ -87,6 +87,14 @@ namespace tesseract {
// in AVX registers.
DotProductFunction DotProduct;
+// The functions for several dot products at once always match DotProduct.
+// The generic implementations use DotProduct for each of the results, so they
+// give exactly the same results as separate calls of DotProduct. Optimized
+// implementations can give slightly different results for the same reason
+// as different dot product functions do.
+RowDotProductsFunction RowDotProducts;
+CrossDotProductsFunction CrossDotProducts;
+
static STRING_VAR(dotproduct, "auto", "Function used for calculation of dot product");
const SIMDDetect &SIMDDetect::GetDetector() {
@@ -161,8 +169,39 @@ static TFloat DotProductStdInnerProduct(const TFloat *u, const TFloat *v, int n)
return std::inner_product(u, u + n, v, static_cast<TFloat>(0));
}
+// Computes v[i] = w[i] . u for num_rows rows of w using DotProduct.
+static void RowDotProductsGeneric(const TFloat *w, int w_stride, int num_rows,
+ const TFloat *u, int n, TFloat *v) {
+ for (int i = 0; i < num_rows; ++i) {
+ v[i] = DotProduct(w + static_cast<size_t>(i) * w_stride, u, n);
+ }
+}
+
+// Computes out[i * out_stride + j] = u[i] . v[j] using DotProduct.
+static void CrossDotProductsGeneric(const TFloat *u, int num_u, const TFloat *v,
+ int num_v, int n, TFloat *out,
+ int out_stride) {
+ for (int i = 0; i < num_u; ++i) {
+ const TFloat *ui = u + static_cast<size_t>(i) * n;
+ TFloat *out_i = out + static_cast<size_t>(i) * out_stride;
+ for (int j = 0; j < num_v; ++j) {
+ out_i[j] = DotProduct(ui, v + static_cast<size_t>(j) * n, n);
+ }
+ }
+}
+
static void SetDotProduct(DotProductFunction f, const IntSimdMatrix *m = nullptr) {
+ RowDotProductsFunction rows = RowDotProductsGeneric;
+ CrossDotProductsFunction cross = CrossDotProductsGeneric;
+#if defined(FAST_FLOAT) && defined(__ARM_ARCH_ISA_A64)
+ if (f == DotProductNEON) {
+ rows = RowDotProductsNEON;
+ cross = CrossDotProductsNEON;
+ }
+#endif
DotProduct = f;
+ RowDotProducts = rows;
+ CrossDotProducts = cross;
IntSimdMatrix::intSimdMatrix = m;
}
diff --git a/src/arch/simddetect.h b/src/arch/simddetect.h
index f3f4b6fa..8e903607 100644
--- a/src/arch/simddetect.h
+++ b/src/arch/simddetect.h
@@ -26,6 +26,24 @@ namespace tesseract {
using DotProductFunction = TFloat (*)(const TFloat *, const TFloat *, int);
extern DotProductFunction DotProduct;
+// Function pointer for best calculation of the dot products of the rows of a
+// matrix with a vector: v[i] = w[i] . u for the num_rows rows of w, where row
+// i starts at w + i * w_stride and u and the used part of each row have n
+// elements.
+using RowDotProductsFunction = void (*)(const TFloat *w, int w_stride,
+ int num_rows, const TFloat *u, int n,
+ TFloat *v);
+extern RowDotProductsFunction RowDotProducts;
+
+// Function pointer for best calculation of the dot products of all rows of
+// one matrix with all rows of another one: out[i * out_stride + j] =
+// u[i] . v[j] for the num_u rows of u and the num_v rows of v. All rows have
+// n elements and are stored without gaps.
+using CrossDotProductsFunction = void (*)(const TFloat *u, int num_u,
+ const TFloat *v, int num_v, int n,
+ TFloat *out, int out_stride);
+extern CrossDotProductsFunction CrossDotProducts;
+
// Architecture detector. Add code here to detect any other architectures for
// SIMD-based faster dot product functions. Intended to be a single static
// object, but it does no real harm to have more than one.
diff --git a/src/lstm/weightmatrix.cpp b/src/lstm/weightmatrix.cpp
index 1a0babf7..74647215 100644
--- a/src/lstm/weightmatrix.cpp
+++ b/src/lstm/weightmatrix.cpp
@@ -17,11 +17,12 @@
#include "weightmatrix.h"
-#include <cassert> // for assert
-#include <cfloat> // for FLT_MAX
-#include <cstring> // for std::memcpy
+#include <algorithm> // for std::min
+#include <cassert> // for assert
+#include <cfloat> // for FLT_MAX
+#include <cstring> // for std::memcpy
#include "intsimdmatrix.h"
-#include "simddetect.h" // for DotProduct
+#include "simddetect.h" // for RowDotProducts, CrossDotProducts
#include "statistc.h"
#include "tprintf.h" // forTFloat
@@ -123,13 +124,14 @@ static inline void MatrixDotVectorInternal(const GENERIC_2D_ARRAY<TFloat> &w, bo
bool skip_bias_back, const TFloat *u, TFloat *v) {
int num_results = w.dim1() - skip_bias_back;
int extent = w.dim2() - add_bias_fwd;
- for (int i = 0; i < num_results; ++i) {
- const TFloat *wi = w[i];
- TFloat total = DotProduct(wi, u, extent);
- if (add_bias_fwd) {
- total += wi[extent]; // The bias value.
+ if (num_results <= 0) {
+ return;
+ }
+ RowDotProducts(w[0], w.dim2(), num_results, u, extent, v);
+ if (add_bias_fwd) {
+ for (int i = 0; i < num_results; ++i) {
+ v[i] += w[i][extent]; // The bias value.
}
- v[i] = total;
}
}
@@ -520,21 +522,26 @@ void WeightMatrix::SumOuterTransposed(const TransposedArray &u, const Transposed
int num_samples = u.dim2();
// v is missing the last element in dim1.
assert(v.dim1() == num_inputs);
+ // The rows of u are handled in blocks, so that an optimized function can
+ // calculate the dot products for several rows of u at the same time.
+ const int kBlockSize = 4;
+ const TFloat *v0 = num_inputs > 0 ? v[0] : nullptr;
#ifdef _OPENMP
# pragma omp parallel for num_threads(4) if (in_parallel)
#endif
- for (int i = 0; i < num_outputs; ++i) {
- TFloat *dwi = dw_[i];
- const TFloat *ui = u[i];
- for (int j = 0; j < num_inputs; ++j) {
- dwi[j] = DotProduct(ui, v[j], num_samples);
- }
- // The last element of v is missing, presumed 1.0f.
- TFloat total = 0;
- for (int k = 0; k < num_samples; ++k) {
- total += ui[k];
+ for (int block = 0; block < num_outputs; block += kBlockSize) {
+ int block_end = std::min(block + kBlockSize, num_outputs);
+ CrossDotProducts(u[block], block_end - block, v0, num_inputs, num_samples,
+ dw_[block], dw_.dim2());
+ for (int i = block; i < block_end; ++i) {
+ // The last element of v is missing, presumed 1.0f.
+ const TFloat *ui = u[i];
+ TFloat total = 0;
+ for (int k = 0; k < num_samples; ++k) {
+ total += ui[k];
+ }
+ dw_[i][num_inputs] = total;
}
- dwi[num_inputs] = total;
}
}
diff --git a/unittest/weightmatrix_product_test.cc b/unittest/weightmatrix_product_test.cc
new file mode 100644
index 00000000..ecbfc264
--- /dev/null
+++ b/unittest/weightmatrix_product_test.cc
@@ -0,0 +1,176 @@
+///////////////////////////////////////////////////////////////////////
+// File: weightmatrix_product_test.cc
+// Description: Tests the matrix products of a float-mode WeightMatrix
+// (MatrixDotVector, VectorDotMatrix and SumOuterTransposed)
+// against a straightforward calculation with doubles.
+// The sizes are chosen to cover all the blocks and the
+// remainders of the SIMD implementations.
+//
+// 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
+//
+///////////////////////////////////////////////////////////////////////
+
+#include "include_gunit.h"
+
+#include "helpers.h" // for TRand
+#include "weightmatrix.h" // for WeightMatrix, TransposedArray
+
+#include <cmath>
+#include <vector>
+
+namespace tesseract {
+namespace {
+
+// The results are sums of n products of floats. Allow an error relative to
+// the sum of the absolute values of the products, which is the bound for
+// the rounding errors of any order of summation.
+const double kRelativeTolerance = 1e-5;
+const double kAbsoluteTolerance = 1e-6;
+
+class WeightMatrixProductTest : public ::testing::Test {
+protected:
+ void SetUp() override {
+ randomizer_.set_seed(42);
+ }
+
+ std::vector<TFloat> RandomVector(int size) {
+ std::vector<TFloat> v(size);
+ for (auto &value : v) {
+ value = static_cast<TFloat>(randomizer_.SignedRand(1.0));
+ }
+ return v;
+ }
+
+ void FillRandom(int size1, int size2, TransposedArray *array) {
+ array->ResizeNoInit(size1, size2);
+ for (int i = 0; i < size1; ++i) {
+ for (int j = 0; j < size2; ++j) {
+ array->put(i, j, static_cast<TFloat>(randomizer_.SignedRand(1.0)));
+ }
+ }
+ }
+
+ static void ExpectClose(double expected, double sum_abs, TFloat actual,
+ const char *what, int no, int ni, int index) {
+ EXPECT_NEAR(expected, actual,
+ kRelativeTolerance * sum_abs + kAbsoluteTolerance)
+ << what << " for no=" << no << ", ni=" << ni << ", index=" << index;
+ }
+
+ // Tests the products with vectors for no outputs and ni inputs.
+ void TestVectorProducts(int no, int ni) {
+ WeightMatrix matrix;
+ matrix.InitWeightsFloat(no, ni + 1, false, 1.0f, &randomizer_);
+
+ // Forward: v = W u with the bias in the last column of W.
+ // The extra elements detect writes beyond the end of the result.
+ const TFloat kGuard = 12345;
+ std::vector<TFloat> u = RandomVector(ni);
+ std::vector<TFloat> v(no + 1, kGuard);
+ matrix.MatrixDotVector(u.data(), v.data());
+ for (int i = 0; i < no; ++i) {
+ const TFloat *w = matrix.GetWeights(i);
+ double expected = w[ni];
+ double sum_abs = std::fabs(w[ni]);
+ for (int j = 0; j < ni; ++j) {
+ double product = static_cast<double>(w[j]) * u[j];
+ expected += product;
+ sum_abs += std::fabs(product);
+ }
+ ExpectClose(expected, sum_abs, v[i], "MatrixDotVector", no, ni, i);
+ }
+ EXPECT_EQ(v[no], kGuard);
+
+ // Backward: v = u W without the bias column.
+ u = RandomVector(no);
+ v.assign(ni + 1, kGuard);
+ matrix.VectorDotMatrix(u.data(), v.data());
+ for (int j = 0; j < ni; ++j) {
+ double expected = 0;
+ double sum_abs = 0;
+ for (int i = 0; i < no; ++i) {
+ double product = static_cast<double>(matrix.GetWeights(i)[j]) * u[i];
+ expected += product;
+ sum_abs += std::fabs(product);
+ }
+ ExpectClose(expected, sum_abs, v[j], "VectorDotMatrix", no, ni, j);
+ }
+ EXPECT_EQ(v[ni], kGuard);
+ }
+
+ // Tests the sum of the outer products for no outputs, ni inputs and
+ // the given number of samples (timesteps).
+ void TestSumOuter(int no, int ni, int num_samples) {
+ WeightMatrix matrix;
+ matrix.InitWeightsFloat(no, ni + 1, false, 1.0f, &randomizer_);
+ TransposedArray u;
+ TransposedArray v;
+ FillRandom(no, num_samples, &u);
+ FillRandom(ni, num_samples, &v);
+ matrix.SumOuterTransposed(u, v, false);
+ std::vector<TFloat> serial_result;
+ for (int i = 0; i < no; ++i) {
+ // The last result is the sum for the bias with an input of 1.
+ for (int j = 0; j <= ni; ++j) {
+ double expected = 0;
+ double sum_abs = 0;
+ for (int k = 0; k < num_samples; ++k) {
+ double product =
+ static_cast<double>(u(i, k)) * (j < ni ? v(j, k) : 1);
+ expected += product;
+ sum_abs += std::fabs(product);
+ }
+ TFloat actual = matrix.GetDW(i, j);
+ EXPECT_NEAR(expected, actual,
+ kRelativeTolerance * sum_abs + kAbsoluteTolerance)
+ << "SumOuterTransposed for no=" << no << ", ni=" << ni
+ << ", num_samples=" << num_samples << ", i=" << i << ", j=" << j;
+ serial_result.push_back(actual);
+ }
+ }
+ // The parallel calculation must give exactly the same results.
+ matrix.SumOuterTransposed(u, v, true);
+ size_t index = 0;
+ for (int i = 0; i < no; ++i) {
+ for (int j = 0; j <= ni; ++j) {
+ EXPECT_EQ(serial_result[index++], matrix.GetDW(i, j))
+ << "parallel SumOuterTransposed for no=" << no << ", ni=" << ni
+ << ", num_samples=" << num_samples << ", i=" << i << ", j=" << j;
+ }
+ }
+ }
+
+ TRand randomizer_;
+};
+
+// Sizes around the block sizes of the SIMD code (4 rows, 4 or 8 elements)
+// and some sizes of real networks.
+const int kSizes[] = {1, 2, 3, 4, 5, 7, 8, 9, 11, 12, 13, 16, 17, 31, 48, 111};
+
+TEST_F(WeightMatrixProductTest, VectorProducts) {
+ for (int no : kSizes) {
+ for (int ni : kSizes) {
+ TestVectorProducts(no, ni);
+ }
+ }
+ TestVectorProducts(192, 288);
+}
+
+TEST_F(WeightMatrixProductTest, SumOuterTransposed) {
+ const int kOutputs[] = {1, 2, 3, 4, 5, 7, 8, 9, 13, 48};
+ const int kInputs[] = {1, 2, 3, 4, 5, 7, 8, 9, 13, 64};
+ const int kSamples[] = {1, 2, 3, 4, 5, 7, 8, 9, 15, 16, 17, 100};
+ for (int no : kOutputs) {
+ for (int ni : kInputs) {
+ for (int num_samples : kSamples) {
+ TestSumOuter(no, ni, num_samples);
+ }
+ }
+ }
+}
+
+} // namespace
+} // namespace tesseract