tesseract  5.0.0-alpha-619-ge9db
tesseract::IntSimdMatrix Struct Reference

#include <intsimdmatrix.h>

Public Types

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

Public Member Functions

void Init (const GENERIC_2D_ARRAY< int8_t > &w, std::vector< int8_t > &shaped_w) 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 GenericVector< double > &scales, const int8_t *u, double *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 intSimdMatrixAVX2
 
static const IntSimdMatrix intSimdMatrixSSE
 

Detailed Description

Definition at line 63 of file intsimdmatrix.h.

Member Typedef Documentation

◆ MatrixDotVectorFunction

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

Definition at line 102 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 
) const

Definition at line 29 of file intsimdmatrix.cpp.

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

◆ MatrixDotVector()

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

Definition at line 79 of file intsimdmatrix.cpp.

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

◆ RoundInputs()

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

Definition at line 69 of file intsimdmatrix.h.

69  {
70  return Roundup(size, num_inputs_per_register_);
71  }

◆ RoundOutputs()

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

Definition at line 73 of file intsimdmatrix.h.

73  {
74  return Roundup(size, num_outputs_per_register_);
75  }

◆ 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 116 of file intsimdmatrix.h.

◆ intSimdMatrixAVX2

const IntSimdMatrix tesseract::IntSimdMatrix::intSimdMatrixAVX2
static
Initial value:

Definition at line 117 of file intsimdmatrix.h.

◆ intSimdMatrixSSE

const IntSimdMatrix tesseract::IntSimdMatrix::intSimdMatrixSSE
static
Initial value:
= {
matrixDotVector,
1,
1,
1,
1
}

Definition at line 118 of file intsimdmatrix.h.

◆ matrixDotVectorFunction

MatrixDotVectorFunction tesseract::IntSimdMatrix::matrixDotVectorFunction

Definition at line 103 of file intsimdmatrix.h.

◆ max_output_registers_

int tesseract::IntSimdMatrix::max_output_registers_

Definition at line 108 of file intsimdmatrix.h.

◆ num_inputs_per_group_

int tesseract::IntSimdMatrix::num_inputs_per_group_

Definition at line 112 of file intsimdmatrix.h.

◆ num_inputs_per_register_

int tesseract::IntSimdMatrix::num_inputs_per_register_

Definition at line 110 of file intsimdmatrix.h.

◆ num_outputs_per_register_

int tesseract::IntSimdMatrix::num_outputs_per_register_

Definition at line 106 of file intsimdmatrix.h.


The documentation for this struct was generated from the following files:
tesseract::kNumInputsPerRegister
constexpr int kNumInputsPerRegister
Definition: intsimdmatrixavx2.cpp:37
tesseract::IntSimdMatrix::num_inputs_per_group_
int num_inputs_per_group_
Definition: intsimdmatrix.h:112
tesseract::IntSimdMatrix::RoundOutputs
int RoundOutputs(int size) const
Definition: intsimdmatrix.h:73
tesseract::kNumOutputsPerRegister
constexpr int kNumOutputsPerRegister
Definition: intsimdmatrixavx2.cpp:33
tesseract::kNumInputsPerGroup
constexpr int kNumInputsPerGroup
Definition: intsimdmatrixavx2.cpp:39
tesseract::IntSimdMatrix::max_output_registers_
int max_output_registers_
Definition: intsimdmatrix.h:108
tesseract::IntSimdMatrix::Roundup
static int Roundup(int input, int factor)
Definition: intsimdmatrix.h:87
tesseract::IntSimdMatrix::num_inputs_per_register_
int num_inputs_per_register_
Definition: intsimdmatrix.h:110
GENERIC_2D_ARRAY::dim2
int dim2() const
Definition: matrix.h:206
tesseract::kMaxOutputRegisters
constexpr int kMaxOutputRegisters
Definition: intsimdmatrixavx2.cpp:35
tesseract::IntSimdMatrix::num_outputs_per_register_
int num_outputs_per_register_
Definition: intsimdmatrix.h:106
GENERIC_2D_ARRAY::dim1
int dim1() const
Definition: matrix.h:205