tesseract v5.3.3.20231005
tesseract::IntSimdMatrix Struct Reference

#include <intsimdmatrix.h>

Public Types

using MatrixDotVectorFunction = void(*)(int, int, const int8_t *, const TFloat *, const int8_t *, TFloat *)
 

Public Member Functions

void Init (const GENERIC_2D_ARRAY< int8_t > &w, std::vector< int8_t > &shaped_w, int32_t &rounded_num_out) const
 
int RoundInputs (int size) const
 
int RoundOutputs (int size) const
 

Static Public Member Functions

static void MatrixDotVector (const GENERIC_2D_ARRAY< int8_t > &w, const std::vector< TFloat > &scales, const int8_t *u, TFloat *v)
 
static int Roundup (int input, int factor)
 

Public Attributes

MatrixDotVectorFunction matrixDotVectorFunction
 
int num_outputs_per_register_
 
int max_output_registers_
 
int num_inputs_per_register_
 
int num_inputs_per_group_
 

Static Public Attributes

static const IntSimdMatrixintSimdMatrix = nullptr
 
static const IntSimdMatrix intSimdMatrixNEON
 
static const IntSimdMatrix intSimdMatrixAVX2
 
static const IntSimdMatrix intSimdMatrixSSE
 

Detailed Description

Definition at line 64 of file intsimdmatrix.h.

Member Typedef Documentation

◆ MatrixDotVectorFunction

using tesseract::IntSimdMatrix::MatrixDotVectorFunction = void (*)(int, int, const int8_t *, const TFloat *, const int8_t *, TFloat *)

Definition at line 100 of file intsimdmatrix.h.

Member Function Documentation

◆ Init()

void tesseract::IntSimdMatrix::Init ( const GENERIC_2D_ARRAY< int8_t > &  w,
std::vector< int8_t > &  shaped_w,
int32_t &  rounded_num_out 
) const

Definition at line 27 of file intsimdmatrix.cpp.

28 {
29 const int num_out = w.dim1();
30 const int num_in = w.dim2() - 1;
31 // The rounded-up sizes of the reshaped weight matrix, excluding biases.
32 int rounded_num_in = Roundup(num_in, num_inputs_per_group_);
33 rounded_num_out = RoundOutputs(num_out);
34 // Add the bias and compute the required size.
35 shaped_w.resize((rounded_num_in + 1) * rounded_num_out, 0);
36 int shaped_index = 0;
37 int output = 0;
38 // Each number of registers needs a different format! Iterates over the
39 // different numbers of registers (each a power of 2).
40 for (int num_registers = max_output_registers_; num_registers >= 1; num_registers /= 2) {
41 // The number of outputs that we will generate with this many registers.
42 int num_outputs_per_register_set = num_registers * num_outputs_per_register_;
43 // Use the max number of registers until we have to go fewer.
44 while (output + num_outputs_per_register_set <= rounded_num_out) {
45 // Accumulating outputs in registers saves iterating over the inputs, so
46 // we only have to do it once per output register set.
47 for (int input = 0; input < num_in; input += num_inputs_per_group_) {
48 // Iterate over the number of outputs in a register set.
49 for (int j = 0; j < num_outputs_per_register_set; ++j) {
50 // Inner-most loop corresponds to the number of inputs in an input
51 // group.
52 for (int i = 0; i < num_inputs_per_group_; ++i) {
53 int8_t weight = 0;
54 if (output + j < num_out && input + i < num_in) {
55 weight = w(output + j, input + i);
56 }
57 shaped_w[shaped_index++] = weight;
58 }
59 }
60 }
61 // Append the bias weights for the register set.
62 for (int j = 0; j < num_outputs_per_register_set; ++j) {
63 int8_t weight = 0;
64 if (output + j < num_out) {
65 weight = w(output + j, num_in);
66 }
67 shaped_w[shaped_index++] = weight;
68 }
69 output += num_outputs_per_register_set;
70 }
71 }
72}
int RoundOutputs(int size) const
Definition: intsimdmatrix.h:74
static int Roundup(int input, int factor)
Definition: intsimdmatrix.h:87

◆ MatrixDotVector()

void tesseract::IntSimdMatrix::MatrixDotVector ( const GENERIC_2D_ARRAY< int8_t > &  w,
const std::vector< TFloat > &  scales,
const int8_t *  u,
TFloat v 
)
static

Definition at line 78 of file intsimdmatrix.cpp.

79 {
80 int num_out = w.dim1();
81 int num_in = w.dim2() - 1;
82 // Base implementation.
83 for (int i = 0; i < num_out; ++i) {
84 const int8_t *wi = w[i];
85 int total = 0;
86 for (int j = 0; j < num_in; ++j) {
87 total += wi[j] * u[j];
88 }
89 // Add in the bias and correct for integer values.
90 v[i] = (total + wi[num_in] * INT8_MAX) * scales[i];
91 }
92}

◆ RoundInputs()

int tesseract::IntSimdMatrix::RoundInputs ( int  size) const
inline

Definition at line 70 of file intsimdmatrix.h.

70 {
72 }

◆ RoundOutputs()

int tesseract::IntSimdMatrix::RoundOutputs ( int  size) const
inline

Definition at line 74 of file intsimdmatrix.h.

74 {
76 }

◆ Roundup()

static int tesseract::IntSimdMatrix::Roundup ( int  input,
int  factor 
)
inlinestatic

Definition at line 87 of file intsimdmatrix.h.

87 {
88 return (input + factor - 1) / factor * factor;
89 }

Member Data Documentation

◆ intSimdMatrix

const IntSimdMatrix * tesseract::IntSimdMatrix::intSimdMatrix = nullptr
static

Definition at line 115 of file intsimdmatrix.h.

◆ intSimdMatrixAVX2

const IntSimdMatrix tesseract::IntSimdMatrix::intSimdMatrixAVX2
static

Definition at line 119 of file intsimdmatrix.h.

◆ intSimdMatrixNEON

const IntSimdMatrix tesseract::IntSimdMatrix::intSimdMatrixNEON
static

Definition at line 117 of file intsimdmatrix.h.

◆ intSimdMatrixSSE

const IntSimdMatrix tesseract::IntSimdMatrix::intSimdMatrixSSE
static

Definition at line 120 of file intsimdmatrix.h.

◆ matrixDotVectorFunction

MatrixDotVectorFunction tesseract::IntSimdMatrix::matrixDotVectorFunction

Definition at line 102 of file intsimdmatrix.h.

◆ max_output_registers_

int tesseract::IntSimdMatrix::max_output_registers_

Definition at line 107 of file intsimdmatrix.h.

◆ num_inputs_per_group_

int tesseract::IntSimdMatrix::num_inputs_per_group_

Definition at line 111 of file intsimdmatrix.h.

◆ num_inputs_per_register_

int tesseract::IntSimdMatrix::num_inputs_per_register_

Definition at line 109 of file intsimdmatrix.h.

◆ num_outputs_per_register_

int tesseract::IntSimdMatrix::num_outputs_per_register_

Definition at line 105 of file intsimdmatrix.h.


The documentation for this struct was generated from the following files: