nnet-compute.h
Go to the documentation of this file.
1 // nnet3/nnet-compute.h
2 
3 // Copyright 2012-2015 Johns Hopkins University (author: Daniel Povey)
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 #ifndef KALDI_NNET3_NNET_COMPUTE_H_
21 #define KALDI_NNET3_NNET_COMPUTE_H_
22 
23 #include "nnet3/nnet-common.h"
24 #include "nnet3/nnet-nnet.h"
25 #include "nnet3/nnet-computation.h"
26 #include "nnet3/nnet-analyze.h"
27 #include "nnet3/nnet-example.h"
28 
29 #include <iostream>
30 #include <sstream>
31 #include <vector>
32 #include <map>
33 
34 
35 namespace kaldi {
36 namespace nnet3 {
37 
38 
40  bool debug;
41  NnetComputeOptions(): debug(false) { }
42  void Register(OptionsItf *opts) {
43  opts->Register("debug", &debug, "If true, turn on "
44  "debug for the neural net computation (very verbose!) "
45  "Will be turned on regardless if --verbose >= 5");
46  }
47 
48 };
49 
50 
59 class NnetComputer {
60  public:
68  NnetComputer(const NnetComputeOptions &options,
69  const NnetComputation &computation,
70  const Nnet &nnet,
71  Nnet *nnet_to_update);
72 
77  NnetComputer(const NnetComputeOptions &options,
78  const NnetComputation &computation,
79  Nnet *nnet,
80  Nnet *nnet_to_update);
81 
82 
88  NnetComputer(const NnetComputer &other);
89 
97  void AcceptInput(const std::string &node_name,
98  CuMatrix<BaseFloat> *input);
99 
103  void AcceptInputs(const Nnet &nnet,
104  const std::vector<NnetIo> &io);
105 
106 
113  void Run();
114 
115  // e.g. GetOutput("output"). This function can also be used to get
116  // derivatives w.r.t. inputs. It's non-const because it may only
117  // be called once and it keeps track of that.
118  const CuMatrixBase<BaseFloat> &GetOutput(const std::string &node_name);
119 
120  // Version of GetOutput that calls Swap(), destroying the output stored inside
121  // this object. You should probably not use this if you plan to call
122  // Backward() on the same NnetComputer object, or it's a recurrent
123  // computation-- it may lead to a crash.
124  void GetOutputDestructive(const std::string &output_name,
125  CuMatrix<BaseFloat> *output);
126 
127 
128  ~NnetComputer();
129  private:
130  void Init(); // called from constructors.
131 
134  const Nnet &nnet_;
135 
136  int32 program_counter_; // command index to execute next.
137  // To deal with inputs and outputs that are not provided/taken by the user in
138  // the same order as listed in the computation, pending_commands_ contains a
139  // list of program commands that were skipped over but are in the queue to be
140  // executed.
141  std::vector<int32> pending_commands_;
142 
143  // A pointer to the copy of the nnet which we'll be using for stats
144  // accumulation (the StoreStats() function). May be NULL or the same
145  // as nnet_ or nnet_to_update_.
147  // A pointer to the copy of the nnet which we'll be updating the parameters
148  // of (nnet_to_update in the backprop function). May be NULL and usually
149  // will not be the same as nnet_.
151  bool debug_;
152  // command_attributes_ is only used if debug_=true.
153  std::vector<CommandAttributes> command_attributes_;
154  // submatrix_strings_ is only used if debug_=true.
155  std::vector<std::string> submatrix_strings_;
156  // command_strings_ is only used if debug_=true, or in case of error.
157  std::vector<std::string> command_strings_;
158 
159  // The matrices used in the computation.
160  std::vector<CuMatrix<BaseFloat> > matrices_;
161 
162  // Memos returned by Propagate() that must be passed to the corresponding
163  // Backprop() routines, indexed by memo-index (zeroth element always
164  // NULL).
165  std::vector<void*> memos_;
166 
167  // This is only used when commands kCompressMatrix and kDecompressMatrix are
168  // invoked. It will be (the first time we compress a matrix) resized to be
169  // the same size as 'matrices_' (i.e., indexed by matrix index). When we
170  // compress a matrix m we set compressed_matrices_[m] to a non-NULL value and
171  // resize matrices_[m] to empty; and when we uncompress it, the reverse
172  // happens.
173  std::vector<CuCompressedMatrixBase*> compressed_matrices_;
174 
175 
176  // executes the command in computation_.commands[program_counter_].
177  void ExecuteCommand();
178 
179  // Returns the matrix index where the input (if is_output==false) or output
180  // matrix index for "node_name" is stored. This looks at the next command (at
181  // program_counter_) and in pending_commands_, and sees whether we were
182  // expecting any input or output for this node, and if there is a match,
183  // returns it and "consumes" the command by either advancing program_counter_
184  // or consuming something from pending_commands_.
185  // If there is not a match (i.e. we were not expecting this type of I/O
186  // at this point in the computation), it prints an error and dies.
187  int32 GetIoMatrixIndex(const std::string &node_name, bool is_output);
188 
189 
190  // This function, called from Run(), checks that there is no pending I/O
191  // that we were waiting for, that would block the running of the
192  // computation; it crashes if there was pending input, and ignores and
193  // skips over any pending output.
194  void CheckNoPendingIo();
195 
196  CuSubMatrix<BaseFloat> GetSubMatrix(int32 submatrix_index);
197 
198  void GetPointers(int32 indexes_multi_index,
199  int32 num_cols,
200  CuArray<BaseFloat*> *pointers);
201  void GetPointers(int32 indexes_multi_index,
202  int32 num_cols,
203  CuArray<const BaseFloat*> *pointers);
204 
206  // Uncentered standard deviations of elements of all matrices that this
207  // command writes. Dimension is the same as
208  // command_attributes_[c].matrices_written
209  std::vector<BaseFloat> matrices_written_stddevs;
210  // Uncentered standard deviations of elements of all submatrices that this
211  // command writes (if they are not whole matrices). Dimension is the same
212  // as command_attributes_[c].submatrices_written
213  std::vector<BaseFloat> submatrices_written_stddevs;
214 
215  // Uncentered standard deviation of parameters of the component (if any)
216  // that is updated by this command.
218  };
219  // Used in debugging code
220  static BaseFloat MatrixStddev(const CuMatrixBase<BaseFloat> &m);
221  // Used in debugging code
222  static BaseFloat ParameterStddev(const Component &c);
223 
224  // only non-const because of the way GetSubMatrix works.
225  void DebugBeforeExecute(int32 command,
226  CommandDebugInfo *info);
227  // only non-const because of the way GetSubMatrix works.
228  void DebugAfterExecute(int32 command,
229  const CommandDebugInfo &info,
230  double command_execution_time);
231 
232  // simple helper function used in executing Propagate().
233  // saves 'memo' at memo-index 'memo_index'; if memo
234  // is non-NULL and memo_index is 0, it is an error.
235  inline void SaveMemo(int32 memo_index, const Component &c, void *memo);
236 
237  // simple helper function used in executing Backprop().
238  // Retrieves memo from 'memo_index' (or returns NULL if
239  // memo_index = 0), and sets that value to NULL as
240  // memos are not reusable.
241  inline void *GetMemo(int32 memo_index);
242 
243  NnetComputer &operator = (const NnetComputer &other); // Disallow.
244 };
245 
246 
247 
248 } // namespace nnet3
249 } // namespace kaldi
250 
251 #endif
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
std::vector< BaseFloat > matrices_written_stddevs
Definition: nnet-compute.h:209
Abstract base-class for neural-net components.
void GetOutput(OnlineFeatureInterface *a, Matrix< BaseFloat > *output)
const NnetComputeOptions & options_
Definition: nnet-compute.h:132
std::vector< CommandAttributes > command_attributes_
Definition: nnet-compute.h:153
This file contains utilities for analyzing and checking computations, which are used in the optimizat...
kaldi::int32 int32
This class represents a matrix that&#39;s stored on the GPU if we have one, and in memory if not...
Definition: matrix-common.h:71
virtual void Register(const std::string &name, bool *ptr, const std::string &doc)=0
std::vector< BaseFloat > submatrices_written_stddevs
Definition: nnet-compute.h:213
The two main classes defined in this header are struct ComputationRequest, which basically defines a ...
std::vector< int32 > pending_commands_
Definition: nnet-compute.h:141
std::vector< CuCompressedMatrixBase * > compressed_matrices_
Definition: nnet-compute.h:173
This class is used for a piece of a CuMatrix.
Definition: matrix-common.h:70
const NnetComputation & computation_
Definition: nnet-compute.h:133
std::vector< CuMatrix< BaseFloat > > matrices_
Definition: nnet-compute.h:160
void Register(OptionsItf *opts)
Definition: nnet-compute.h:42
Matrix for CUDA computing.
Definition: matrix-common.h:69
std::vector< std::string > submatrix_strings_
Definition: nnet-compute.h:155
class NnetComputer is responsible for executing the computation described in the "computation" object...
Definition: nnet-compute.h:59
Class CuArray represents a vector of an integer or struct of type T.
Definition: cu-array.h:32
std::vector< std::string > command_strings_
Definition: nnet-compute.h:157
std::vector< void * > memos_
Definition: nnet-compute.h:165