31 Nnet *nnet_to_update):
32 options_(options), computation_(computation), nnet_(nnet),
33 program_counter_(0), nnet_to_store_stats_(nnet_to_update),
34 nnet_to_update_(nnet_to_update) {
41 Nnet *nnet_to_update):
51 "You must call NnetComputation::ComputeCudaIndexes() before " 52 "executing the computation.");
78 "Attempting to get parameter stddev of non-updatable component");
85 const std::vector<int32> &matrices_written =
87 size_t size = matrices_written.size();
89 for (
size_t i = 0;
i < size;
i++) {
90 int32 m = matrices_written[
i];
95 const std::vector<int32> &submatrices_written =
97 size_t size = submatrices_written.size();
99 for (
size_t i = 0;
i < size;
i++) {
100 int32 s = submatrices_written[
i];
118 double command_exec_time) {
119 std::ostringstream os;
122 const std::vector<int32> &matrices_written =
124 size_t size = matrices_written.size();
126 for (
size_t i = 0;
i < size;
i++) {
127 int32 m = matrices_written[
i];
130 os <<
'm' << m <<
": " << old_stddev <<
"->" << stddev <<
" ";
134 const std::vector<int32> &submatrices_written =
136 size_t size = submatrices_written.size();
138 for (
size_t i = 0;
i < size;
i++) {
139 int32 s = submatrices_written[
i];
158 os <<
"\t|\t time: " << command_exec_time <<
" secs";
165 if (memo_index <= 0) {
170 if (
memos_.size() <=
static_cast<size_t>(memo_index))
171 memos_.resize(memo_index + 1, NULL);
172 memos_[memo_index] = memo;
177 if (memo_index == 0) {
180 if (static_cast<size_t>(memo_index) >=
memos_.size())
181 KALDI_ERR <<
"Memo requested that was not generated.";
182 void *ans =
memos_[memo_index];
183 memos_[memo_index] = NULL;
205 KALDI_ERR <<
"You cannot use the copy constructor of NnetComputer if " 238 NVTX_RANGE(
"NnetComputer::ExecuteCommand::kPropagate");
244 void *memo = component->
Propagate(indexes, input, &output);
248 bool was_in_place = (c.
arg3 == c.
arg4);
253 stats_component->
StoreStats(maybe_input, output, memo);
260 NVTX_RANGE(
"NnetComputer::ExecuteCommand::kBackpropNoModelUpdate");
261 std::ostringstream debug_str;
268 Nnet *nnet_to_update;
288 component->
Backprop(debug_str.str(), indexes,
289 in_value, out_value, out_deriv,
291 c.
arg6 == 0 ? NULL : &in_deriv);
324 if (alpha == 0.0)
break;
325 dest.
Scale(1.0 / alpha);
339 if (alpha == 0.0)
break;
340 dest.
Scale(1.0 / alpha);
378 if (alpha == 0.0)
break;
379 dest.
Scale(1.0 / alpha);
390 if (CuDevice::Instantiate().Enabled()) {
397 bool truncate = (c.
arg3 != 0);
399 static_cast<CuCompressedMatrixType>(c.
arg2),
408 if (CuDevice::Instantiate().Enabled()) {
419 delete compressed_matrix;
432 KALDI_ERR <<
"Invalid command in computation";
436 std::string preamble;
438 KALDI_WARN <<
"Printing some background info since error was detected";
456 mat, info.row_offset, info.num_rows, info.col_offset, info.num_cols);
464 const std::vector<std::pair<int32,int32> > &pairs =
466 int32 size = pairs.size();
467 std::vector<BaseFloat*> vec(size);
471 unordered_map<int32, std::pair<BaseFloat*, int32> > lookup;
474 int32 submatrix_index = pairs[
i].first, row = pairs[
i].second;
475 if (submatrix_index != -1) {
476 unordered_map<int32, std::pair<BaseFloat*, int32> >::iterator
477 iter = lookup.find(submatrix_index);
478 if (iter == lookup.end()) {
480 lookup[submatrix_index] = std::pair<BaseFloat*, int32>(m.
Data(),
482 iter = lookup.find(submatrix_index);
485 int32 stride = iter->second.second;
486 vec[
i] = data + (row * stride);
492 #ifdef KALDI_PARANOID 495 int32 submatrix_index = pairs[
i].first, row = pairs[
i].second;
496 if (submatrix_index != -1) {
515 int32 num_commands = c.size();
519 KALDI_ERR <<
"Running computation that has finished: program-counter=" 526 double total_elapsed_previous = 0.0;
539 double total_elapsed_now = timer.
Elapsed();
541 total_elapsed_now - total_elapsed_previous);
542 total_elapsed_previous = total_elapsed_now;
549 bool is_output =
false;
555 KALDI_ERR <<
"Num-rows mismatch for input '" << node_name
557 <<
" in computation-request, " << input->
NumRows()
561 KALDI_ERR <<
"Num-cols mismatch for input '" << node_name
563 <<
" in computation-request, " << input->
NumCols()
573 matrices_[matrix_index].CopyFromMat(*input);
579 const std::string &node_name) {
580 bool is_output =
true;
589 bool is_output =
true;
612 int32 node = c[command].arg2;
613 KALDI_ERR <<
"Cannot run computation-- we did not get input for node '" 623 if (node_index == -1)
624 KALDI_ERR <<
"No node named '" << node_name <<
"'in network.";
637 bool this_command_is_output =
639 int32 this_submatrix_index = c[command].arg1,
640 this_node_index = c[command].arg2;
641 if (this_command_is_output == is_output && node_index == this_node_index) {
648 KALDI_ERR <<
"Getting input or output that is not a whole matrix " 649 <<
"(probably some optimization code needs to be changed)";
656 << (is_output ?
"provide output " :
"accept input ")
657 <<
"for network node " << node_name
658 <<
" (it is not expected at this point in the computation)";
664 const std::vector<NnetIo> &io_vec) {
665 for (
size_t i = 0;
i < io_vec.size();
i++) {
668 if (node_index == -1)
void GetPointers(int32 indexes_multi_index, int32 num_cols, CuArray< BaseFloat *> *pointers)
void Init(const NnetComputation &computation)
void CopyFromMat(const MatrixBase< OtherReal > &src, MatrixTransposeType trans=kNoTrans)
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
MatrixIndexT Stride() const
virtual void * Propagate(const ComponentPrecomputedIndexes *indexes, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const =0
Propagate function.
void CopyFromVec(const std::vector< T > &src)
This function resizes if needed.
std::vector< BaseFloat > matrices_written_stddevs
const std::string & GetNodeName(int32 node_index) const
returns individual node name.
Abstract base-class for neural-net components.
void AddRows(Real alpha, const CuMatrixBase< Real > &src, const CuArrayBase< MatrixIndexT > &indexes)
Does for each row r, this.Row(r) += alpha * src.row(indexes[r]).
void AddRowRanges(const CuMatrixBase< Real > &src, const CuArrayBase< Int32Pair > &indexes)
For each row r of this and for each column c, do (*this)(r, c) += src(j, c), where j ranges from ind...
int32 GetVerboseLevel()
Get verbosity level, usually set via command line '–verbose=' switch.
const NnetComputeOptions & options_
virtual int32 NumParameters() const
The following new virtual function returns the total dimension of the parameters in this class...
void Print(std::ostream &os, const Nnet &nnet) const
std::vector< CommandAttributes > command_attributes_
void * GetMemo(int32 memo_index)
bool IsInputNode(int32 node) const
Returns true if this is an output node, meaning that it is of type kInput.
GeneralMatrix features
The features or labels.
void AddMat(Real alpha, const CuMatrixBase< Real > &A, MatrixTransposeType trans=kNoTrans)
*this += alpha * A
std::vector< MatrixInfo > matrices
This class represents a matrix that's stored on the GPU if we have one, and in memory if not...
NnetComputer(const NnetComputeOptions &options, const NnetComputation &computation, const Nnet &nnet, Nnet *nnet_to_update)
Constructor.
virtual void DeleteMemo(void *memo) const
This virtual function only needs to be overwritten by Components that return a non-NULL memo from the...
void ComputeCommandAttributes(const Nnet &nnet, const NnetComputation &computation, const ComputationVariables &vars, std::vector< CommandAttributes > *attributes)
std::vector< Command > commands
MatrixStrideType stride_type
std::vector< BaseFloat > submatrices_written_stddevs
virtual void Backprop(const std::string &debug_info, const ComponentPrecomputedIndexes *indexes, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, void *memo, Component *to_update, CuMatrixBase< BaseFloat > *in_deriv) const =0
Backprop function; depending on which of the arguments 'to_update' and 'in_deriv' are non-NULL...
void AcceptInput(const std::string &node_name, CuMatrix< BaseFloat > *input)
e.g.
MatrixIndexT NumCols() const
std::vector< int32 > pending_commands_
std::vector< CuCompressedMatrixBase * > compressed_matrices_
void GetCommandStrings(const Nnet &nnet, std::string *preamble, std::vector< std::string > *command_strings) const
std::vector< CuArray< int32 > > indexes_cuda
This class relates the matrices and sub-matrices in the computation to imaginary "variables", such that we can think of the operations as operating on sets of individual variables, and we can then do analysis that lets us do optimization.
bool need_model_derivative
const CuMatrixBase< BaseFloat > & GetOutput(const std::string &node_name)
void DebugBeforeExecute(int32 command, CommandDebugInfo *info)
std::vector< std::vector< std::pair< int32, int32 > > > indexes_multi
Nnet * nnet_to_store_stats_
virtual void StoreStats(const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, void *memo)
This function may store stats on average activation values, and for some component types...
virtual BaseFloat DotProduct(const UpdatableComponent &other) const =0
Computes dot-product between parameters of two instances of a Component.
virtual int32 Properties() const =0
Return bitmask of the component's properties.
void AddToRows(Real alpha, const CuArrayBase< MatrixIndexT > &indexes, CuMatrixBase< Real > *dst) const
For each row i of *this, adds this->Row(i) to dst->Row(indexes(i)) if indexes(i) >= 0...
void SetZero()
Math operations, some calling kernels.
void AcceptInputs(const Nnet &nnet, const std::vector< NnetIo > &io)
This convenience function calls AcceptInput() in turn on all the inputs in the training example...
static BaseFloat ParameterStddev(const Component &c)
BaseFloat components_parameter_stddev
std::vector< SubMatrixInfo > submatrices
void CopyRows(const CuMatrixBase< Real > &src, const CuArrayBase< MatrixIndexT > &indexes)
Copies row r from row indexes[r] of src.
#define KALDI_PARANOID_ASSERT(cond)
int32 GetIoMatrixIndex(const std::string &node_name, bool is_output)
Real TraceMatMat(const MatrixBase< Real > &A, const MatrixBase< Real > &B, MatrixTransposeType trans)
We need to declare this here as it will be a friend function.
std::vector< CuArray< Int32Pair > > indexes_ranges_cuda
const std::string & GetComponentName(int32 component_index) const
returns individual component name.
This class is used for a piece of a CuMatrix.
const NnetComputation & computation_
Component * GetComponent(int32 c)
Return component indexed c. Not a copy; not owned by caller.
Class UpdatableComponent is a Component which has trainable parameters; it extends the interface of C...
std::vector< CuMatrix< BaseFloat > > matrices_
void CopyFromGeneralMat(const GeneralMatrix &src, MatrixTransposeType trans=kNoTrans)
std::vector< PrecomputedIndexesInfo > component_precomputed_indexes
const Real * Data() const
Return data pointer (const).
void SaveMemo(int32 memo_index, const Component &c, void *memo)
Matrix for CUDA computing.
MatrixIndexT NumCols() const
std::vector< std::string > submatrix_strings_
class NnetComputer is responsible for executing the computation described in the "computation" object...
#define KALDI_ASSERT(cond)
MatrixIndexT NumRows() const
std::string name
the name of the input in the neural net; in simple setups it will just be "input".
std::vector< std::string > command_strings_
static BaseFloat MatrixStddev(const CuMatrixBase< BaseFloat > &m)
std::vector< std::vector< int32 > > indexes
void CopyToRows(const CuArrayBase< Real *> &dst) const
For each row r of this matrix, copies it to the array of floats at the location given by dst[r]...
void GetOutputDestructive(const std::string &output_name, CuMatrix< BaseFloat > *output)
int32 GetNodeIndex(const std::string &node_name) const
returns index associated with this node name, or -1 if no such index.
MatrixIndexT NumRows() const
Dimensions.
Class CuCompressedMatrixBase is an abstract base class that allows you to compress a matrix of type C...
double Elapsed() const
Returns time in seconds.
CuSubMatrix< BaseFloat > GetSubMatrix(int32 submatrix_index)
virtual int32 NumRows() const =0
bool IsWholeMatrix(int32 submatrix_index) const
CuCompressedMatrixBase * NewCuCompressedMatrix(CuCompressedMatrixType t, BaseFloat range, bool truncat)
This function allocates a new CuCompressedMatrix with type determined by t, and with the 'range' and ...
void Resize(MatrixIndexT rows, MatrixIndexT cols, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Allocate the memory.
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
std::vector< void * > memos_
virtual int32 NumCols() const =0
virtual void CopyToMat(CuMatrixBase< BaseFloat > *mat) const =0
Copies the contents of *this to 'mat', which should be correctly sized beforehand.
void GetSubmatrixStrings(const Nnet &nnet, std::vector< std::string > *submat_strings) const
void DebugAfterExecute(int32 command, const CommandDebugInfo &info, double command_execution_time)
void Run()
This does either the forward or backward computation, depending when it is called (in a typical compu...
std::vector< std::vector< std::pair< int32, int32 > > > indexes_ranges