tesseract  4.00.00dev
parallel.cpp
Go to the documentation of this file.
1 // File: parallel.cpp
3 // Description: Runs networks in parallel on the same input.
4 // Author: Ray Smith
5 // Created: Thu May 02 08:06:06 PST 2013
6 //
7 // (C) Copyright 2013, Google Inc.
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
18 
19 #include "parallel.h"
20 
21 #ifdef _OPENMP
22 #include <omp.h>
23 #endif
24 
25 #include "functions.h" // For conditional undef of _OPENMP.
26 #include "networkscratch.h"
27 
28 namespace tesseract {
29 
30 // ni_ and no_ will be set by AddToStack.
31 Parallel::Parallel(const STRING& name, NetworkType type) : Plumbing(name) {
32  type_ = type;
33 }
34 
36 }
37 
38 // Returns the shape output from the network given an input shape (which may
39 // be partially unknown ie zero).
40 StaticShape Parallel::OutputShape(const StaticShape& input_shape) const {
41  StaticShape result = stack_[0]->OutputShape(input_shape);
42  int stack_size = stack_.size();
43  for (int i = 1; i < stack_size; ++i) {
44  StaticShape shape = stack_[i]->OutputShape(input_shape);
45  result.set_depth(result.depth() + shape.depth());
46  }
47  return result;
48 }
49 
50 // Runs forward propagation of activations on the input line.
51 // See NetworkCpp for a detailed discussion of the arguments.
52 void Parallel::Forward(bool debug, const NetworkIO& input,
53  const TransposedArray* input_transpose,
54  NetworkScratch* scratch, NetworkIO* output) {
55  bool parallel_debug = false;
56  // If this parallel is a replicator of convolvers, or holds a 1-d LSTM pair,
57  // or a 2-d LSTM quad, do debug locally, and don't pass the flag on.
58  if (debug && type_ != NT_PARALLEL) {
59  parallel_debug = true;
60  debug = false;
61  }
62  int stack_size = stack_.size();
63  if (type_ == NT_PAR_2D_LSTM) {
64  // Special case, run parallel in parallel.
66  results.init_to_size(stack_size, NetworkScratch::IO());
67  for (int i = 0; i < stack_size; ++i) {
68  results[i].Resize(input, stack_[i]->NumOutputs(), scratch);
69  }
70 #ifdef _OPENMP
71 #pragma omp parallel for num_threads(stack_size)
72 #endif
73  for (int i = 0; i < stack_size; ++i) {
74  stack_[i]->Forward(debug, input, NULL, scratch, results[i]);
75  }
76  // Now pack all the results (serially) into the output.
77  int out_offset = 0;
78  output->Resize(*results[0], NumOutputs());
79  for (int i = 0; i < stack_size; ++i) {
80  out_offset = output->CopyPacking(*results[i], out_offset);
81  }
82  } else {
83  // Revolving intermediate result.
84  NetworkScratch::IO result(input, scratch);
85  // Source for divided replicated.
86  NetworkScratch::IO source_part;
87  TransposedArray* src_transpose = NULL;
88  if (IsTraining() && type_ == NT_REPLICATED) {
89  // Make a transposed copy of the input.
90  input.Transpose(&transposed_input_);
91  src_transpose = &transposed_input_;
92  }
93  // Run each network, putting the outputs into result.
94  int out_offset = 0;
95  for (int i = 0; i < stack_size; ++i) {
96  stack_[i]->Forward(debug, input, src_transpose, scratch, result);
97  // All networks must have the same output width
98  if (i == 0) {
99  output->Resize(*result, NumOutputs());
100  } else {
101  ASSERT_HOST(result->Width() == output->Width());
102  }
103  out_offset = output->CopyPacking(*result, out_offset);
104  }
105  }
106  if (parallel_debug) {
107  DisplayForward(*output);
108  }
109 }
110 
111 // Runs backward propagation of errors on the deltas line.
112 // See NetworkCpp for a detailed discussion of the arguments.
113 bool Parallel::Backward(bool debug, const NetworkIO& fwd_deltas,
114  NetworkScratch* scratch,
115  NetworkIO* back_deltas) {
116  // If this parallel is a replicator of convolvers, or holds a 1-d LSTM pair,
117  // or a 2-d LSTM quad, do debug locally, and don't pass the flag on.
118  if (debug && type_ != NT_PARALLEL) {
119  DisplayBackward(fwd_deltas);
120  debug = false;
121  }
122  int stack_size = stack_.size();
123  if (type_ == NT_PAR_2D_LSTM) {
124  // Special case, run parallel in parallel.
125  GenericVector<NetworkScratch::IO> in_deltas, out_deltas;
126  in_deltas.init_to_size(stack_size, NetworkScratch::IO());
127  out_deltas.init_to_size(stack_size, NetworkScratch::IO());
128  // Split the forward deltas for each stack element.
129  int feature_offset = 0;
130  for (int i = 0; i < stack_.size(); ++i) {
131  int num_features = stack_[i]->NumOutputs();
132  in_deltas[i].Resize(fwd_deltas, num_features, scratch);
133  out_deltas[i].Resize(fwd_deltas, stack_[i]->NumInputs(), scratch);
134  in_deltas[i]->CopyUnpacking(fwd_deltas, feature_offset, num_features);
135  feature_offset += num_features;
136  }
137 #ifdef _OPENMP
138 #pragma omp parallel for num_threads(stack_size)
139 #endif
140  for (int i = 0; i < stack_size; ++i) {
141  stack_[i]->Backward(debug, *in_deltas[i], scratch,
142  i == 0 ? back_deltas : out_deltas[i]);
143  }
144  if (needs_to_backprop_) {
145  for (int i = 1; i < stack_size; ++i) {
146  back_deltas->AddAllToFloat(*out_deltas[i]);
147  }
148  }
149  } else {
150  // Revolving partial deltas.
151  NetworkScratch::IO in_deltas(fwd_deltas, scratch);
152  // The sum of deltas from different sources, which will eventually go into
153  // back_deltas.
154  NetworkScratch::IO out_deltas;
155  int feature_offset = 0;
156  for (int i = 0; i < stack_.size(); ++i) {
157  int num_features = stack_[i]->NumOutputs();
158  in_deltas->CopyUnpacking(fwd_deltas, feature_offset, num_features);
159  feature_offset += num_features;
160  if (stack_[i]->Backward(debug, *in_deltas, scratch, back_deltas)) {
161  if (i == 0) {
162  out_deltas.ResizeFloat(*back_deltas, back_deltas->NumFeatures(),
163  scratch);
164  out_deltas->CopyAll(*back_deltas);
165  } else if (back_deltas->NumFeatures() == out_deltas->NumFeatures()) {
166  // Widths are allowed to be different going back, as we may have
167  // input nets, so only accumulate the deltas if the widths are the
168  // same.
169  out_deltas->AddAllToFloat(*back_deltas);
170  }
171  }
172  }
173  if (needs_to_backprop_) back_deltas->CopyAll(*out_deltas);
174  }
175  if (needs_to_backprop_) back_deltas->ScaleFloatBy(1.0f / stack_size);
176  return needs_to_backprop_;
177 }
178 
179 } // namespace tesseract.
void CopyUnpacking(const NetworkIO &src, int feature_offset, int num_features)
Definition: networkio.cpp:945
bool needs_to_backprop_
Definition: network.h:287
int Width() const
Definition: networkio.h:107
void init_to_size(int size, T t)
void DisplayForward(const NetworkIO &matrix)
Definition: network.cpp:285
Parallel(const STRING &name, NetworkType type)
Definition: parallel.cpp:31
NetworkType type() const
Definition: network.h:112
void Resize(const NetworkIO &src, int num_features)
Definition: networkio.h:45
bool IsTraining() const
Definition: network.h:115
int CopyPacking(const NetworkIO &src, int feature_offset)
Definition: networkio.cpp:917
#define ASSERT_HOST(x)
Definition: errcode.h:84
void AddAllToFloat(const NetworkIO &src)
Definition: networkio.cpp:817
void ScaleFloatBy(float factor)
Definition: networkio.h:234
void ResizeFloat(const NetworkIO &src, int num_features, NetworkScratch *scratch)
PointerVector< Network > stack_
Definition: plumbing.h:133
Definition: strngs.h:45
void CopyAll(const NetworkIO &src)
Definition: networkio.cpp:811
virtual ~Parallel()
Definition: parallel.cpp:35
virtual void Forward(bool debug, const NetworkIO &input, const TransposedArray *input_transpose, NetworkScratch *scratch, NetworkIO *output)
Definition: parallel.cpp:52
NetworkType
Definition: network.h:43
NetworkType type_
Definition: network.h:285
int NumInputs() const
Definition: network.h:120
int NumFeatures() const
Definition: networkio.h:111
virtual StaticShape OutputShape(const StaticShape &input_shape) const
Definition: parallel.cpp:40
void Transpose(TransposedArray *dest) const
Definition: networkio.cpp:964
int NumOutputs() const
Definition: network.h:123
virtual bool Backward(bool debug, const NetworkIO &fwd_deltas, NetworkScratch *scratch, NetworkIO *back_deltas)
Definition: parallel.cpp:113
void DisplayBackward(const NetworkIO &matrix)
Definition: network.cpp:296
void set_depth(int value)
Definition: static_shape.h:47