tesseract  4.0.0-1-g2a2b
tesseract::Parallel Class Reference

#include <parallel.h>

Inheritance diagram for tesseract::Parallel:
tesseract::Plumbing tesseract::Network

Public Member Functions

 Parallel (const STRING &name, NetworkType type)
 
virtual ~Parallel ()=default
 
StaticShape OutputShape (const StaticShape &input_shape) const override
 
STRING spec () const override
 
void Forward (bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output) override
 
bool Backward (bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
 
- Public Member Functions inherited from tesseract::Plumbing
 Plumbing (const STRING &name)
 
virtual ~Plumbing ()=default
 
StaticShape InputShape () const override
 
STRING spec () const override
 
bool IsPlumbingType () const override
 
void SetEnableTraining (TrainingState state) override
 
void SetNetworkFlags (uint32_t flags) override
 
int InitWeights (float range, TRand *randomizer) override
 
int RemapOutputs (int old_no, const std::vector< int > &code_map) override
 
void ConvertToInt () override
 
void SetRandomizer (TRand *randomizer) override
 
virtual void AddToStack (Network *network)
 
bool SetupNeedsBackprop (bool needs_backprop) override
 
int XScaleFactor () const override
 
void CacheXScaleFactor (int factor) override
 
void DebugWeights () override
 
const PointerVector< Network > & stack () const
 
void EnumerateLayers (const STRING *prefix, GenericVector< STRING > *layers) const
 
NetworkGetLayer (const char *id) const
 
float LayerLearningRate (const char *id) const
 
void ScaleLayerLearningRate (const char *id, double factor)
 
float * LayerLearningRatePtr (const char *id) const
 
bool Serialize (TFile *fp) const override
 
bool DeSerialize (TFile *fp) override
 
void Update (float learning_rate, float momentum, float adam_beta, int num_samples) override
 
void CountAlternators (const Network &other, double *same, double *changed) const override
 
- Public Member Functions inherited from tesseract::Network
 Network ()
 
 Network (NetworkType type, const STRING &name, int ni, int no)
 
virtual ~Network ()=default
 
NetworkType type () const
 
bool IsTraining () const
 
bool needs_to_backprop () const
 
int num_weights () const
 
int NumInputs () const
 
int NumOutputs () const
 
const STRINGname () const
 
bool TestFlag (NetworkFlags flag) const
 
void DisplayForward (const NetworkIO &matrix)
 
void DisplayBackward (const NetworkIO &matrix)
 

Additional Inherited Members

- Static Public Member Functions inherited from tesseract::Network
static NetworkCreateFromFile (TFile *fp)
 
static void ClearWindow (bool tess_coords, const char *window_name, int width, int height, ScrollView **window)
 
static int DisplayImage (Pix *pix, ScrollView *window)
 
- Protected Member Functions inherited from tesseract::Network
double Random (double range)
 
- Protected Attributes inherited from tesseract::Plumbing
PointerVector< Networkstack_
 
GenericVector< float > learning_rates_
 
- Protected Attributes inherited from tesseract::Network
NetworkType type_
 
TrainingState training_
 
bool needs_to_backprop_
 
int32_t network_flags_
 
int32_t ni_
 
int32_t no_
 
int32_t num_weights_
 
STRING name_
 
ScrollViewforward_win_
 
ScrollViewbackward_win_
 
TRandrandomizer_
 
- Static Protected Attributes inherited from tesseract::Network
static char const *const kTypeNames [NT_COUNT]
 

Detailed Description

Definition at line 27 of file parallel.h.

Constructor & Destructor Documentation

◆ Parallel()

tesseract::Parallel::Parallel ( const STRING name,
NetworkType  type 
)

Definition at line 31 of file parallel.cpp.

31  : Plumbing(name) {
32  type_ = type;
33 }
Plumbing(const STRING &name)
Definition: plumbing.cpp:25
NetworkType type_
Definition: network.h:299
const STRING & name() const
Definition: network.h:138
NetworkType type() const
Definition: network.h:112

◆ ~Parallel()

virtual tesseract::Parallel::~Parallel ( )
virtualdefault

Member Function Documentation

◆ Backward()

bool tesseract::Parallel::Backward ( bool  debug,
const NetworkIO fwd_deltas,
NetworkScratch scratch,
NetworkIO back_deltas 
)
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 110 of file parallel.cpp.

112  {
113  // If this parallel is a replicator of convolvers, or holds a 1-d LSTM pair,
114  // or a 2-d LSTM quad, do debug locally, and don't pass the flag on.
115  if (debug && type_ != NT_PARALLEL) {
116  DisplayBackward(fwd_deltas);
117  debug = false;
118  }
119  int stack_size = stack_.size();
120  if (type_ == NT_PAR_2D_LSTM) {
121  // Special case, run parallel in parallel.
122  GenericVector<NetworkScratch::IO> in_deltas, out_deltas;
123  in_deltas.init_to_size(stack_size, NetworkScratch::IO());
124  out_deltas.init_to_size(stack_size, NetworkScratch::IO());
125  // Split the forward deltas for each stack element.
126  int feature_offset = 0;
127  for (int i = 0; i < stack_.size(); ++i) {
128  int num_features = stack_[i]->NumOutputs();
129  in_deltas[i].Resize(fwd_deltas, num_features, scratch);
130  out_deltas[i].Resize(fwd_deltas, stack_[i]->NumInputs(), scratch);
131  in_deltas[i]->CopyUnpacking(fwd_deltas, feature_offset, num_features);
132  feature_offset += num_features;
133  }
134 #ifdef _OPENMP
135 #pragma omp parallel for num_threads(stack_size)
136 #endif
137  for (int i = 0; i < stack_size; ++i) {
138  stack_[i]->Backward(debug, *in_deltas[i], scratch,
139  i == 0 ? back_deltas : out_deltas[i]);
140  }
141  if (needs_to_backprop_) {
142  for (int i = 1; i < stack_size; ++i) {
143  back_deltas->AddAllToFloat(*out_deltas[i]);
144  }
145  }
146  } else {
147  // Revolving partial deltas.
148  NetworkScratch::IO in_deltas(fwd_deltas, scratch);
149  // The sum of deltas from different sources, which will eventually go into
150  // back_deltas.
151  NetworkScratch::IO out_deltas;
152  int feature_offset = 0;
153  for (int i = 0; i < stack_.size(); ++i) {
154  int num_features = stack_[i]->NumOutputs();
155  in_deltas->CopyUnpacking(fwd_deltas, feature_offset, num_features);
156  feature_offset += num_features;
157  if (stack_[i]->Backward(debug, *in_deltas, scratch, back_deltas)) {
158  if (i == 0) {
159  out_deltas.ResizeFloat(*back_deltas, back_deltas->NumFeatures(),
160  scratch);
161  out_deltas->CopyAll(*back_deltas);
162  } else if (back_deltas->NumFeatures() == out_deltas->NumFeatures()) {
163  // Widths are allowed to be different going back, as we may have
164  // input nets, so only accumulate the deltas if the widths are the
165  // same.
166  out_deltas->AddAllToFloat(*back_deltas);
167  }
168  }
169  }
170  if (needs_to_backprop_) back_deltas->CopyAll(*out_deltas);
171  }
172  if (needs_to_backprop_) back_deltas->ScaleFloatBy(1.0f / stack_size);
173  return needs_to_backprop_;
174 }
bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas) override
Definition: parallel.cpp:110
void DisplayBackward(const NetworkIO &matrix)
Definition: network.cpp:293
NetworkType type_
Definition: network.h:299
PointerVector< Network > stack_
Definition: plumbing.h:136
int NumInputs() const
Definition: network.h:120
void init_to_size(int size, const T &t)
bool needs_to_backprop_
Definition: network.h:301

◆ Forward()

void tesseract::Parallel::Forward ( bool  debug,
const NetworkIO input,
const TransposedArray input_transpose,
NetworkScratch scratch,
NetworkIO output 
)
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 49 of file parallel.cpp.

51  {
52  bool parallel_debug = false;
53  // If this parallel is a replicator of convolvers, or holds a 1-d LSTM pair,
54  // or a 2-d LSTM quad, do debug locally, and don't pass the flag on.
55  if (debug && type_ != NT_PARALLEL) {
56  parallel_debug = true;
57  debug = false;
58  }
59  int stack_size = stack_.size();
60  if (type_ == NT_PAR_2D_LSTM) {
61  // Special case, run parallel in parallel.
63  results.init_to_size(stack_size, NetworkScratch::IO());
64  for (int i = 0; i < stack_size; ++i) {
65  results[i].Resize(input, stack_[i]->NumOutputs(), scratch);
66  }
67 #ifdef _OPENMP
68 #pragma omp parallel for num_threads(stack_size)
69 #endif
70  for (int i = 0; i < stack_size; ++i) {
71  stack_[i]->Forward(debug, input, nullptr, scratch, results[i]);
72  }
73  // Now pack all the results (serially) into the output.
74  int out_offset = 0;
75  output->Resize(*results[0], NumOutputs());
76  for (int i = 0; i < stack_size; ++i) {
77  out_offset = output->CopyPacking(*results[i], out_offset);
78  }
79  } else {
80  // Revolving intermediate result.
81  NetworkScratch::IO result(input, scratch);
82  // Source for divided replicated.
83  NetworkScratch::IO source_part;
84  TransposedArray* src_transpose = nullptr;
85  if (IsTraining() && type_ == NT_REPLICATED) {
86  // Make a transposed copy of the input.
87  input.Transpose(&transposed_input_);
88  src_transpose = &transposed_input_;
89  }
90  // Run each network, putting the outputs into result.
91  int out_offset = 0;
92  for (int i = 0; i < stack_size; ++i) {
93  stack_[i]->Forward(debug, input, src_transpose, scratch, result);
94  // All networks must have the same output width
95  if (i == 0) {
96  output->Resize(*result, NumOutputs());
97  } else {
98  ASSERT_HOST(result->Width() == output->Width());
99  }
100  out_offset = output->CopyPacking(*result, out_offset);
101  }
102  }
103  if (parallel_debug) {
104  DisplayForward(*output);
105  }
106 }
int NumOutputs() const
Definition: network.h:123
NetworkType type_
Definition: network.h:299
PointerVector< Network > stack_
Definition: plumbing.h:136
void init_to_size(int size, const T &t)
void DisplayForward(const NetworkIO &matrix)
Definition: network.cpp:282
bool IsTraining() const
Definition: network.h:115
#define ASSERT_HOST(x)
Definition: errcode.h:84

◆ OutputShape()

StaticShape tesseract::Parallel::OutputShape ( const StaticShape input_shape) const
overridevirtual

Reimplemented from tesseract::Network.

Definition at line 37 of file parallel.cpp.

37  {
38  StaticShape result = stack_[0]->OutputShape(input_shape);
39  int stack_size = stack_.size();
40  for (int i = 1; i < stack_size; ++i) {
41  StaticShape shape = stack_[i]->OutputShape(input_shape);
42  result.set_depth(result.depth() + shape.depth());
43  }
44  return result;
45 }
PointerVector< Network > stack_
Definition: plumbing.h:136

◆ spec()

STRING tesseract::Parallel::spec ( ) const
inlineoverridevirtual

Reimplemented from tesseract::Network.

Definition at line 37 of file parallel.h.

37  {
38  STRING spec;
39  if (type_ == NT_PAR_2D_LSTM) {
40  // We have 4 LSTMs operating in parallel here, so the size of each is
41  // the number of outputs/4.
42  spec.add_str_int("L2xy", no_ / 4);
43  } else if (type_ == NT_PAR_RL_LSTM) {
44  // We have 2 LSTMs operating in parallel here, so the size of each is
45  // the number of outputs/2.
46  if (stack_[0]->type() == NT_LSTM_SUMMARY)
47  spec.add_str_int("Lbxs", no_ / 2);
48  else
49  spec.add_str_int("Lbx", no_ / 2);
50  } else {
51  if (type_ == NT_REPLICATED) {
52  spec.add_str_int("R", stack_.size());
53  spec += "(";
54  spec += stack_[0]->spec();
55  } else {
56  spec = "(";
57  for (int i = 0; i < stack_.size(); ++i) spec += stack_[i]->spec();
58  }
59  spec += ")";
60  }
61  return spec;
62  }
STRING spec() const override
Definition: parallel.h:37
NetworkType type_
Definition: network.h:299
PointerVector< Network > stack_
Definition: plumbing.h:136
NetworkType type() const
Definition: network.h:112
void add_str_int(const char *str, int number)
Definition: strngs.cpp:379
Definition: strngs.h:45

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