All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
NnetComputation Struct Reference

#include <nnet-computation.h>

Collaboration diagram for NnetComputation:

Classes

struct  Command
 
struct  MatrixDebugInfo
 
struct  MatrixInfo
 
struct  PrecomputedIndexesInfo
 
struct  SubMatrixInfo
 

Public Member Functions

int32 NewMatrix (int32 num_rows, int32 num_cols, MatrixStrideType stride_type)
 Convenience function used when adding new matrices. More...
 
int32 NewSubMatrix (int32 base_submatrix, int32 row_offset, int32 num_rows, int32 col_offset, int32 num_cols)
 Convenience function used when adding new sub-matrices. More...
 
bool IsWholeMatrix (int32 submatrix_index) const
 
void ComputeCudaIndexes ()
 
void Print (std::ostream &os, const Nnet &nnet) const
 
void Read (std::istream &istream, bool binary)
 
void Write (std::ostream &ostream, bool binary) const
 
void GetSubmatrixStrings (const Nnet &nnet, std::vector< std::string > *submat_strings) const
 
void GetWholeSubmatrices (std::vector< int32 > *whole_submatrices) const
 
void GetCommandStrings (const Nnet &nnet, std::string *preamble, std::vector< std::string > *command_strings) const
 
 ~NnetComputation ()
 
void Clear ()
 
 NnetComputation (const NnetComputation &other)
 
NnetComputationoperator= (const NnetComputation &other)
 
 NnetComputation ()
 

Public Attributes

std::vector< MatrixInfomatrices
 
std::vector< MatrixDebugInfomatrix_debug_info
 
std::vector< SubMatrixInfosubmatrices
 
std::vector
< PrecomputedIndexesInfo
component_precomputed_indexes
 
std::vector< std::vector< int32 > > indexes
 
std::vector< std::vector
< std::pair< int32, int32 > > > 
indexes_multi
 
std::vector< std::vector
< std::pair< int32, int32 > > > 
indexes_ranges
 
std::vector< Commandcommands
 
bool need_model_derivative
 
std::vector< CuArray< int32 > > indexes_cuda
 
std::vector< CuArray< Int32Pair > > indexes_ranges_cuda
 

Detailed Description

Definition at line 264 of file nnet-computation.h.

Constructor & Destructor Documentation

Definition at line 77 of file nnet-computation.cc.

References NnetComputation::component_precomputed_indexes, and rnnlm::i.

77  {
78  // note: component_precomputed_indexes[0].data is the NULL pointer.
79  for (size_t i = 1; i < component_precomputed_indexes.size(); i++)
80  delete component_precomputed_indexes[i].data;
81 }
std::vector< PrecomputedIndexesInfo > component_precomputed_indexes
NnetComputation ( const NnetComputation other)

Definition at line 1097 of file nnet-computation.cc.

References NnetComputation::component_precomputed_indexes, and rnnlm::i.

1097  :
1098  matrices(other.matrices),
1099  matrix_debug_info(other.matrix_debug_info),
1100  submatrices(other.submatrices),
1101  component_precomputed_indexes(other.component_precomputed_indexes),
1102  indexes(other.indexes),
1103  indexes_multi(other.indexes_multi),
1104  indexes_ranges(other.indexes_ranges),
1105  commands(other.commands),
1106  need_model_derivative(other.need_model_derivative),
1107  indexes_cuda(other.indexes_cuda),
1108  indexes_ranges_cuda(other.indexes_ranges_cuda) {
1109  for (size_t i = 1; i < component_precomputed_indexes.size(); i++)
1111  component_precomputed_indexes[i].data->Copy();
1112 }
std::vector< MatrixDebugInfo > matrix_debug_info
std::vector< MatrixInfo > matrices
std::vector< Command > commands
std::vector< CuArray< int32 > > indexes_cuda
std::vector< std::vector< std::pair< int32, int32 > > > indexes_multi
std::vector< SubMatrixInfo > submatrices
std::vector< CuArray< Int32Pair > > indexes_ranges_cuda
std::vector< PrecomputedIndexesInfo > component_precomputed_indexes
std::vector< std::vector< int32 > > indexes
std::vector< std::vector< std::pair< int32, int32 > > > indexes_ranges
NnetComputation ( )
inline

Definition at line 459 of file nnet-computation.h.

Referenced by NnetComputation::Clear().

Member Function Documentation

void Clear ( )
inline
void ComputeCudaIndexes ( )

Definition at line 83 of file nnet-computation.cc.

References rnnlm::i, NnetComputation::indexes, NnetComputation::indexes_cuda, NnetComputation::indexes_ranges, NnetComputation::indexes_ranges_cuda, and KALDI_ASSERT.

Referenced by CachingOptimizingCompiler::CompileNoShortcut(), CachingOptimizingCompiler::CompileViaShortcut(), DecodableNnetSimpleLoopedInfo::Init(), NnetComputation::Read(), kaldi::nnet3::UnitTestNnetCompute(), and kaldi::nnet3::UnitTestNnetOptimizeWithOptions().

83  {
84  indexes_cuda.resize(indexes.size());
85 
86  for (size_t i = 0; i < indexes.size(); i++)
87  indexes_cuda[i].CopyFromVec(indexes[i]);
88 
89  KALDI_ASSERT(sizeof(Int32Pair) == sizeof(std::pair<int32,int32>));
90  indexes_ranges_cuda.resize(indexes_ranges.size());
91  for (int32 i = 0; i < indexes_ranges.size(); i++) {
92  const std::vector<std::pair<int32,int32> > *input = &(indexes_ranges[i]);
93  const std::vector<Int32Pair> *input_cast =
94  reinterpret_cast<const std::vector<Int32Pair> *>(input);
95  // note: the indexes for CUDA use can't very easily use STL types due to
96  // the interface of CUDA being plain C.
97  indexes_ranges_cuda[i].CopyFromVec(*input_cast);
98  }
99 }
std::vector< CuArray< int32 > > indexes_cuda
std::vector< CuArray< Int32Pair > > indexes_ranges_cuda
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::vector< std::vector< int32 > > indexes
std::vector< std::vector< std::pair< int32, int32 > > > indexes_ranges
void GetCommandStrings ( const Nnet nnet,
std::string *  preamble,
std::vector< std::string > *  command_strings 
) const

Definition at line 907 of file nnet-computation.cc.

References NnetComputation::commands, kaldi::nnet3::GetIndexesMultiStrings(), kaldi::nnet3::GetIndexesStrings(), NnetComputation::GetSubmatrixStrings(), kaldi::nnet3::PrintCommand(), and kaldi::nnet3::PrintComputationPreamble().

Referenced by NnetComputer::ExecuteCommand(), and NnetComputer::NnetComputer().

910  {
911  std::vector<std::string> submatrix_strings, indexes_strings,
912  indexes_multi_strings;
913  this->GetSubmatrixStrings(nnet, &submatrix_strings);
914  GetIndexesStrings(nnet, *this, &indexes_strings);
915  GetIndexesMultiStrings(nnet, *this, &indexes_multi_strings);
916  if (preamble != NULL) {
917  std::ostringstream os;
918  PrintComputationPreamble(os, *this, nnet, submatrix_strings,
919  indexes_strings, indexes_multi_strings);
920  *preamble = os.str();
921  }
922  if (command_strings != NULL) {
923  command_strings->resize(commands.size());
924  for (int32 c = 0; c < commands.size(); c++) {
925  std::ostringstream os;
926  PrintCommand(os, nnet, *this, c, submatrix_strings,
927  indexes_strings, indexes_multi_strings);
928  (*command_strings)[c] = os.str();
929  // Remove the final newline.
930  std::string &str = (*command_strings)[c];
931  if (!str.empty())
932  str.resize(str.size() - 1);
933  }
934  }
935 }
static void GetIndexesStrings(const Nnet &nnet, const NnetComputation &computation, std::vector< std::string > *indexes_strings)
std::vector< Command > commands
void GetSubmatrixStrings(const Nnet &nnet, std::vector< std::string > *submat_strings) const
std::string PrintCommand(int32 num_commands, int32 command)
static void PrintComputationPreamble(std::ostream &os, const NnetComputation &c, const Nnet &nnet, const std::vector< std::string > &submatrix_strings, const std::vector< std::string > &indexes_strings, const std::vector< std::string > &indexes_multi_strings)
static void GetIndexesMultiStrings(const Nnet &nnet, const NnetComputation &computation, std::vector< std::string > *indexes_multi_strings)
void GetSubmatrixStrings ( const Nnet nnet,
std::vector< std::string > *  submat_strings 
) const

Definition at line 425 of file nnet-computation.cc.

References NnetComputation::SubMatrixInfo::col_offset, rnnlm::i, NnetComputation::IsWholeMatrix(), KALDI_ASSERT, NnetComputation::SubMatrixInfo::matrix_index, NnetComputation::SubMatrixInfo::num_cols, NnetComputation::SubMatrixInfo::num_rows, NnetComputation::SubMatrixInfo::row_offset, and NnetComputation::submatrices.

Referenced by ComputationExpander::ComputeSubmatrixInfo(), NnetComputation::GetCommandStrings(), NnetComputer::NnetComputer(), NnetComputation::Print(), and kaldi::nnet3::UnitTestNnetAnalyze().

426  {
427  int32 num_submatrices = this->submatrices.size();
428  KALDI_ASSERT(num_submatrices > 0);
429  submat_strings->resize(num_submatrices);
430  (*submat_strings)[0] = "[]"; // the empty matrix
431  for (int32 i = 1; i < num_submatrices; i++) {
432  const NnetComputation::SubMatrixInfo &submat = this->submatrices[i];
433  std::ostringstream os;
434  if (this->IsWholeMatrix(i)) {
435  os << 'm' << submat.matrix_index;
436  } else { // part of a range.
437  os << 'm' << submat.matrix_index << '(' << submat.row_offset << ':'
438  << (submat.row_offset + submat.num_rows - 1) << ", "
439  << submat.col_offset << ':' << (submat.col_offset + submat.num_cols - 1)
440  << ')';
441  }
442  (*submat_strings)[i] = os.str();
443  }
444 }
bool IsWholeMatrix(int32 submatrix_index) const
std::vector< SubMatrixInfo > submatrices
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void GetWholeSubmatrices ( std::vector< int32 > *  whole_submatrices) const

Definition at line 1136 of file nnet-computation.cc.

References NnetComputation::IsWholeMatrix(), KALDI_ASSERT, NnetComputation::matrices, and NnetComputation::submatrices.

Referenced by Compiler::AddCommands(), ComputationLoopedOptimizer::AddMatrixSwapCommands(), ComputationLoopedOptimizer::FindActiveMatrices(), and DerivativeTimeLimiter::LimitDerivTimes().

1137  {
1138  int32 num_matrices = matrices.size(),
1139  num_submatrices = submatrices.size();
1140  whole_submatrices->clear();
1141  whole_submatrices->resize(num_matrices, 0);
1142  for (int32 s = 1; s < num_submatrices; s++) {
1143  if (IsWholeMatrix(s)) {
1144  int32 m = submatrices[s].matrix_index;
1145  (*whole_submatrices)[m] = s;
1146  }
1147  }
1148  for (int32 m = 1; m < num_matrices; m++) {
1149  KALDI_ASSERT((*whole_submatrices)[m] != 0 &&
1150  "Matrix exists with no submatrix that is "
1151  "the whole of it.");
1152  }
1153 }
std::vector< MatrixInfo > matrices
bool IsWholeMatrix(int32 submatrix_index) const
std::vector< SubMatrixInfo > submatrices
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
bool IsWholeMatrix ( int32  submatrix_index) const

Definition at line 938 of file nnet-computation.cc.

References KALDI_ASSERT, NnetComputation::matrices, NnetComputation::MatrixInfo::num_cols, NnetComputation::MatrixInfo::num_rows, and NnetComputation::submatrices.

Referenced by Compiler::AddBackwardStepInput(), Compiler::AddForwardStepInput(), ComputationChecker::CheckComputationIndexes(), kaldi::nnet3::ComputeMatrixAccesses(), ModelUpdateConsolidator::ConsolidateSubmatrices(), NnetComputer::DebugAfterExecute(), NnetComputer::DebugBeforeExecute(), Compiler::DoBackwardComputationDescriptor(), Compiler::DoForwardComputationDescriptor(), ComputationLoopedOptimizer::FindTimeShift(), NnetComputer::GetIoMatrixIndex(), NnetComputation::GetSubmatrixStrings(), NnetComputation::GetWholeSubmatrices(), DerivativeTimeLimiter::LimitMatrices(), VariableMergingOptimizer::MayBeMerged(), and Compiler::OutputDebugInfo().

938  {
939  KALDI_ASSERT(submatrix_index > 0 && submatrix_index < submatrices.size());
940  const SubMatrixInfo &submat_info = submatrices[submatrix_index];
941  const MatrixInfo &mat_info = matrices[submat_info.matrix_index];
942  return submat_info.row_offset == 0 && submat_info.col_offset == 0 &&
943  submat_info.num_rows == mat_info.num_rows &&
944  submat_info.num_cols == mat_info.num_cols;
945 }
std::vector< MatrixInfo > matrices
std::vector< SubMatrixInfo > submatrices
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
int32 NewMatrix ( int32  num_rows,
int32  num_cols,
MatrixStrideType  stride_type 
)

Convenience function used when adding new matrices.

Writes to 'this->matrices' and 'this->submatrices'; and if 'this->matrix_debug_info' is nonempty, also increases its size by one. Returns the *sub-matrix* index corresponding to the newly added matrix.

Definition at line 127 of file nnet-computation.cc.

References KALDI_ASSERT, kaldi::kDefaultStride, NnetComputation::matrices, NnetComputation::matrix_debug_info, and NnetComputation::submatrices.

Referenced by ModelUpdateConsolidator::ConsolidateSubmatrices(), and Compiler::CreateStepInfo().

128  {
129  KALDI_ASSERT(num_rows > 0 && num_cols > 0);
130  if (matrices.empty()) { // Set up the zero matrix; index zero is reserved.
131  matrices.push_back(MatrixInfo(0, 0, kDefaultStride));
132  submatrices.push_back(SubMatrixInfo(0, 0, 0, 0, 0));
133  }
134  int32 matrix_index = matrices.size(),
135  submatrix_index = submatrices.size();
136  matrices.push_back(MatrixInfo(num_rows, num_cols, stride_type));
137  if (!matrix_debug_info.empty())
138  matrix_debug_info.push_back(MatrixDebugInfo());
139  submatrices.push_back(SubMatrixInfo(matrix_index, 0, num_rows, 0, num_cols));
140  return submatrix_index;
141 }
std::vector< MatrixDebugInfo > matrix_debug_info
std::vector< MatrixInfo > matrices
std::vector< SubMatrixInfo > submatrices
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
int32 NewSubMatrix ( int32  base_submatrix,
int32  row_offset,
int32  num_rows,
int32  col_offset,
int32  num_cols 
)

Convenience function used when adding new sub-matrices.

base_submatrix is the submatrix of which we want a column and/or row range. As a convenience, -1 for the 'num_rows' or the 'num_cols' will be interpreted as 'as much as possible'. Returns the new sub-matrix index. Writes to 'this->submatrices'. There is no mechanism to stop duplicates from being created, but calling RenumberComputation() will remove such duplicates.

Definition at line 101 of file nnet-computation.cc.

References KALDI_ASSERT, NnetComputation::matrices, and NnetComputation::submatrices.

Referenced by DerivativeTimeLimiter::ComputeSubmatrixMaps(), ModelUpdateConsolidator::ConsolidateSubmatrices(), Compiler::CreateStepInfo(), DerivativeTimeLimiter::MapSimpleMatrixCommand(), kaldi::nnet3::ReplaceRowWithMatrixOps(), kaldi::nnet3::SnipMultiRowOp(), kaldi::nnet3::SnipRangesRowOp(), and kaldi::nnet3::SnipSingleRowOp().

103  {
104  KALDI_ASSERT(base_submatrix > 0 &&
105  static_cast<size_t>(base_submatrix) < submatrices.size());
106  const SubMatrixInfo &base_info = submatrices[base_submatrix];
107  int32 base_matrix = base_info.matrix_index;
108  KALDI_ASSERT(base_matrix > 0 &&
109  static_cast<size_t>(base_matrix) < matrices.size());
110  if (num_rows == -1) // we interpret this to mean 'as many as possible'.
111  num_rows = base_info.num_rows - row_offset;
112  if (num_cols == -1) // we interpret this to mean 'as many as possible'.
113  num_cols = base_info.num_cols - col_offset;
114  KALDI_ASSERT(row_offset + num_rows <= base_info.num_rows &&
115  col_offset + num_cols <= base_info.num_cols &&
116  row_offset >= 0 && col_offset >= 0 &&
117  num_rows > 0 && num_cols > 0);
118  int32 matrix_row_offset = base_info.row_offset + row_offset,
119  matrix_col_offset = base_info.col_offset + col_offset;
120  int32 ans = submatrices.size();
121  submatrices.push_back(
122  NnetComputation::SubMatrixInfo(base_matrix, matrix_row_offset, num_rows,
123  matrix_col_offset, num_cols));
124  return ans;
125 }
std::vector< MatrixInfo > matrices
std::vector< SubMatrixInfo > submatrices
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
NnetComputation & operator= ( const NnetComputation other)

Definition at line 1114 of file nnet-computation.cc.

References NnetComputation::commands, NnetComputation::component_precomputed_indexes, rnnlm::i, NnetComputation::indexes, NnetComputation::indexes_cuda, NnetComputation::indexes_multi, NnetComputation::indexes_ranges, NnetComputation::indexes_ranges_cuda, NnetComputation::matrices, NnetComputation::matrix_debug_info, NnetComputation::need_model_derivative, and NnetComputation::submatrices.

1114  {
1115  matrices = other.matrices;
1116  matrix_debug_info = other.matrix_debug_info;
1117  submatrices = other.submatrices;
1118  indexes = other.indexes;
1119  indexes_multi = other.indexes_multi;
1120  indexes_ranges = other.indexes_ranges;
1121  commands = other.commands;
1122  need_model_derivative = other.need_model_derivative;
1123  indexes_cuda = other.indexes_cuda;
1124  indexes_ranges_cuda = other.indexes_ranges_cuda;
1125 
1126  for (size_t i = 1; i < component_precomputed_indexes.size(); i++)
1127  delete component_precomputed_indexes[i].data;
1128  component_precomputed_indexes = other.component_precomputed_indexes;
1129  for (size_t i = 1; i < component_precomputed_indexes.size(); i++)
1131  component_precomputed_indexes[i].data->Copy();
1132  return *this;
1133 }
std::vector< MatrixDebugInfo > matrix_debug_info
std::vector< MatrixInfo > matrices
std::vector< Command > commands
std::vector< CuArray< int32 > > indexes_cuda
std::vector< std::vector< std::pair< int32, int32 > > > indexes_multi
std::vector< SubMatrixInfo > submatrices
std::vector< CuArray< Int32Pair > > indexes_ranges_cuda
std::vector< PrecomputedIndexesInfo > component_precomputed_indexes
std::vector< std::vector< int32 > > indexes
std::vector< std::vector< std::pair< int32, int32 > > > indexes_ranges
void Print ( std::ostream &  os,
const Nnet nnet 
) const

Definition at line 680 of file nnet-computation.cc.

References NnetComputation::commands, kaldi::nnet3::GetIndexesMultiStrings(), kaldi::nnet3::GetIndexesStrings(), NnetComputation::GetSubmatrixStrings(), kaldi::nnet3::PrintCommand(), and kaldi::nnet3::PrintComputationPreamble().

Referenced by kaldi::nnet3::CheckComputation(), CachingOptimizingCompiler::CompileNoShortcut(), ComputationExpander::ComputeSubmatrixInfo(), DecodableNnetSimpleLoopedInfo::Init(), NnetComputer::Run(), kaldi::nnet3::UnitTestNnetAnalyze(), kaldi::nnet3::UnitTestNnetCompile(), kaldi::nnet3::UnitTestNnetCompileLooped(), kaldi::nnet3::UnitTestNnetCompute(), kaldi::nnet3::UnitTestNnetModelDerivatives(), and kaldi::nnet3::UnitTestNnetOptimizeWithOptions().

680  {
681  std::vector<std::string> submatrix_strings, indexes_strings,
682  indexes_multi_strings;
683  this->GetSubmatrixStrings(nnet, &submatrix_strings);
684  GetIndexesStrings(nnet, *this, &indexes_strings);
685  GetIndexesMultiStrings(nnet, *this, &indexes_multi_strings);
686  PrintComputationPreamble(os, *this, nnet, submatrix_strings,
687  indexes_strings, indexes_multi_strings);
688  os << "# begin forward commands\n";
689  for (int32 c = 0; c < commands.size(); c++) {
690  PrintCommand(os, nnet, *this, c, submatrix_strings,
691  indexes_strings, indexes_multi_strings);
692  }
693 }
static void GetIndexesStrings(const Nnet &nnet, const NnetComputation &computation, std::vector< std::string > *indexes_strings)
std::vector< Command > commands
void GetSubmatrixStrings(const Nnet &nnet, std::vector< std::string > *submat_strings) const
std::string PrintCommand(int32 num_commands, int32 command)
static void PrintComputationPreamble(std::ostream &os, const NnetComputation &c, const Nnet &nnet, const std::vector< std::string > &submatrix_strings, const std::vector< std::string > &indexes_strings, const std::vector< std::string > &indexes_multi_strings)
static void GetIndexesMultiStrings(const Nnet &nnet, const NnetComputation &computation, std::vector< std::string > *indexes_multi_strings)
void Read ( std::istream &  istream,
bool  binary 
)

Definition at line 695 of file nnet-computation.cc.

References NnetComputation::commands, NnetComputation::component_precomputed_indexes, NnetComputation::ComputeCudaIndexes(), NnetComputation::PrecomputedIndexesInfo::data, kaldi::nnet3::ExpectToken(), rnnlm::i, NnetComputation::indexes, NnetComputation::indexes_multi, NnetComputation::indexes_ranges, NnetComputation::PrecomputedIndexesInfo::input_indexes, KALDI_ASSERT, KALDI_ERR, NnetComputation::matrices, NnetComputation::matrix_debug_info, NnetComputation::need_model_derivative, NnetComputation::PrecomputedIndexesInfo::output_indexes, kaldi::ReadBasicType(), kaldi::nnet3::ReadIndexVector(), kaldi::ReadIntegerPairVector(), kaldi::ReadIntegerVector(), ComponentPrecomputedIndexes::ReadNew(), kaldi::ReadToken(), and NnetComputation::submatrices.

Referenced by CachingOptimizingCompiler::ReadCache(), and kaldi::nnet3::UnitTestNnetComputationIo().

695  {
696  int32 version = 3, // must be in sync with 'version' in Write.
697  version_in = 1; // defaults to 1 if no version specified.
698 
699  ExpectToken(is, binary, "<NnetComputation>");
700  std::string token;
701  ReadToken(is, binary, &token);
702  if (token == "<Version>") {
703  ReadBasicType(is, binary, &version_in);
704  ExpectToken(is, binary, "<NumMatrices>");
705  } else {
706  KALDI_ASSERT(token == "<NumMatrices>");
707  }
708  if (version_in != version) {
709  KALDI_ERR << "Reading NnetComputation failed because version in "
710  << version_in << " != " << version << "... you can "
711  << "ignore this error if the program continues afterward, "
712  << "it would only affect speed.";
713  }
714  size_t num_matrices;
715  ReadBasicType(is, binary, &num_matrices);
716  KALDI_ASSERT(num_matrices >= 0);
717  matrices.resize(num_matrices);
718  ExpectToken(is, binary, "<Matrices>");
719  for (size_t c = 0; c < num_matrices; c++) {
720  matrices[c].Read(is, binary);
721  }
722 
723  size_t num_matrix_debug_info;
724  ExpectToken(is, binary, "<NumMatrixDebugInfo>");
725  ReadBasicType(is, binary, &num_matrix_debug_info);
726  KALDI_ASSERT(num_matrix_debug_info >= 0);
727  matrix_debug_info.resize(num_matrix_debug_info);
728  ExpectToken(is, binary, "<MatrixDebugInfo>");
729  for (size_t c = 0; c < num_matrix_debug_info; c++) {
730  matrix_debug_info[c].Read(is, binary);
731  }
732 
733  size_t num_submatrices;
734  ExpectToken(is, binary, "<NumSubMatrices>");
735  ReadBasicType(is, binary, &num_submatrices);
736  KALDI_ASSERT(num_submatrices >= 0);
737  submatrices.resize(num_submatrices);
738  ExpectToken(is, binary, "<SubMatrices>");
739  for (size_t c = 0; c < num_submatrices; c++) {
740  submatrices[c].Read(is, binary);
741  }
742 
743 
744  // delete any existing pointers in component_precomputed_indexes.
745  // note: component_precomputed_indexes[0] is the NULL pointer.
746  for (size_t i = 1; i < component_precomputed_indexes.size(); i++)
747  delete component_precomputed_indexes[i].data;
749 
750  size_t num_component_precomputed_indexes;
751  ExpectToken(is, binary, "<NumComponentPrecomputedIndexes>");
752  ReadBasicType(is, binary, &num_component_precomputed_indexes);
753  KALDI_ASSERT(num_component_precomputed_indexes >= 0);
754  component_precomputed_indexes.resize(num_component_precomputed_indexes);
755 
756  std::string tok;
757  ReadToken(is, binary, &tok);
758  if (tok == "<ComponentPrecomputedIndexes>") {
759  // Older on-disk format, before that code was extended for shortcut
760  // compilation.
762  component_precomputed_indexes.resize(num_component_precomputed_indexes);
763  for (size_t c = 0; c < num_component_precomputed_indexes; c++) {
764  bool is_null; // a boolean indicating whether the pointer should be NULL.
765  ReadBasicType(is, binary, &is_null);
766  if (!is_null) {
767  ComponentPrecomputedIndexes* p = ComponentPrecomputedIndexes::ReadNew(is, binary);
768  component_precomputed_indexes[c].data = p;
769  }
770  }
771  } else {
772  KALDI_ASSERT(tok == "<PrecomputedIndexesInfo>");
773  for (size_t c = 1; c < num_component_precomputed_indexes; c++) {
774  ComponentPrecomputedIndexes* p = ComponentPrecomputedIndexes::ReadNew(is, binary);
775  KALDI_ASSERT(p != NULL);
776  PrecomputedIndexesInfo &info = component_precomputed_indexes[c];
777  info.data = p;
778  ReadIndexVector(is, binary, &(info.input_indexes));
779  ReadIndexVector(is, binary, &(info.output_indexes));
780  }
781  }
782  size_t num_indexes;
783  ExpectToken(is, binary, "<NumIndexes>");
784  ReadBasicType(is, binary, &num_indexes);
785  KALDI_ASSERT(num_indexes >= 0);
786  indexes.resize(num_indexes);
787  ExpectToken(is, binary, "<Indexes>");
788  for (size_t c = 0; c < num_indexes; c++) {
789  ReadIntegerVector(is, binary, &(indexes[c]));
790  }
791 
792  size_t num_indexes_multi;
793  ExpectToken(is, binary, "<NumIndexesMulti>");
794  ReadBasicType(is, binary, &num_indexes_multi);
795  KALDI_ASSERT(num_indexes_multi >= 0);
796  indexes_multi.resize(num_indexes_multi);
797  ExpectToken(is, binary, "<IndexesMulti>");
798  for (size_t c = 0; c < num_indexes_multi; c++) {
799  ReadIntegerPairVector(is, binary, &(indexes_multi[c]));
800  }
801 
802  size_t num_indexes_ranges;
803  ExpectToken(is, binary, "<NumIndexesRanges>");
804  ReadBasicType(is, binary, &num_indexes_ranges);
805  KALDI_ASSERT(num_indexes_ranges >= 0);
806  indexes_ranges.resize(num_indexes_ranges);
807  ExpectToken(is, binary, "<IndexesRanges>");
808  for (size_t c = 0; c < num_indexes_ranges; c++) {
809  ReadIntegerPairVector(is, binary, &(indexes_ranges[c]));
810  }
811 
812  size_t num_commands;
813  ExpectToken(is, binary, "<NumCommands>");
814  ReadBasicType(is, binary, &num_commands);
815  KALDI_ASSERT(num_commands >= 0);
816  commands.resize(num_commands);
817  ExpectToken(is, binary, "<Commands>");
818  for (size_t c = 0; c < num_commands; c++) {
819  commands[c].Read(is, binary);
820  }
821 
822  ExpectToken(is, binary, "<NeedModelDerivative>");
823  ReadBasicType(is, binary, &need_model_derivative);
824 
826  ExpectToken(is, binary, "</NnetComputation>");
827 }
std::vector< MatrixDebugInfo > matrix_debug_info
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:55
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
Definition: io-funcs.cc:154
std::vector< MatrixInfo > matrices
std::vector< Command > commands
void ReadIntegerPairVector(std::istream &is, bool binary, std::vector< std::pair< T, T > > *v)
Function for reading STL vector of pairs of integer types.
Definition: io-funcs-inl.h:131
void ReadIntegerVector(std::istream &is, bool binary, std::vector< T > *v)
Function for reading STL vector of integer types.
Definition: io-funcs-inl.h:232
static void ExpectToken(const std::string &token, const std::string &what_we_are_parsing, const std::string **next_token)
std::vector< std::vector< std::pair< int32, int32 > > > indexes_multi
std::vector< SubMatrixInfo > submatrices
#define KALDI_ERR
Definition: kaldi-error.h:127
void ReadIndexVector(std::istream &is, bool binary, std::vector< Index > *vec)
Definition: nnet-common.cc:143
static ComponentPrecomputedIndexes * ReadNew(std::istream &is, bool binary)
std::vector< PrecomputedIndexesInfo > component_precomputed_indexes
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::vector< std::vector< int32 > > indexes
std::vector< std::vector< std::pair< int32, int32 > > > indexes_ranges
void Write ( std::ostream &  ostream,
bool  binary 
) const

Definition at line 829 of file nnet-computation.cc.

References NnetComputation::commands, NnetComputation::component_precomputed_indexes, NnetComputation::PrecomputedIndexesInfo::data, NnetComputation::indexes, NnetComputation::indexes_multi, NnetComputation::indexes_ranges, NnetComputation::PrecomputedIndexesInfo::input_indexes, NnetComputation::matrices, NnetComputation::matrix_debug_info, NnetComputation::need_model_derivative, NnetComputation::PrecomputedIndexesInfo::output_indexes, NnetComputation::submatrices, ComponentPrecomputedIndexes::Write(), kaldi::WriteBasicType(), kaldi::nnet3::WriteIndexVector(), kaldi::WriteIntegerPairVector(), kaldi::WriteIntegerVector(), and kaldi::WriteToken().

Referenced by kaldi::nnet3::UnitTestNnetComputationIo().

829  {
830  int32 version = 3; // Must be in sync with version in Read.
831  WriteToken(os, binary, "<NnetComputation>");
832  WriteToken(os, binary, "<Version>");
833  WriteBasicType(os, binary, version);
834  WriteToken(os, binary, "<NumMatrices>");
835  WriteBasicType(os, binary, matrices.size());
836  WriteToken(os, binary, "<Matrices>");
837  for (size_t c = 0; c < matrices.size(); c++) {
838  matrices[c].Write(os, binary);
839  }
840 
841  if (!binary) os << std::endl;
842  WriteToken(os, binary, "<NumMatrixDebugInfo>");
843  WriteBasicType(os, binary, matrix_debug_info.size());
844  WriteToken(os, binary, "<MatrixDebugInfo>");
845  for (size_t c = 0; c < matrix_debug_info.size(); c++) {
846  matrix_debug_info[c].Write(os, binary);
847  }
848 
849  if (!binary) os << std::endl;
850  WriteToken(os, binary, "<NumSubMatrices>");
851  WriteBasicType(os, binary, submatrices.size());
852  WriteToken(os, binary, "<SubMatrices>");
853  for (size_t c = 0; c < submatrices.size(); c++) {
854  submatrices[c].Write(os, binary);
855  }
856 
857  if (!binary) os << std::endl;
858  WriteToken(os, binary, "<NumComponentPrecomputedIndexes>");
859  WriteBasicType(os, binary, component_precomputed_indexes.size());
860  WriteToken(os, binary, "<PrecomputedIndexesInfo>");
861  for (size_t c = 1; c < component_precomputed_indexes.size(); c++) {
862  const PrecomputedIndexesInfo &info = component_precomputed_indexes[c];
863  info.data->Write(os, binary);
864  WriteIndexVector(os, binary, info.input_indexes);
865  WriteIndexVector(os, binary, info.output_indexes);
866  }
867 
868  if (!binary) os << std::endl;
869  WriteToken(os, binary, "<NumIndexes>");
870  WriteBasicType(os, binary, indexes.size());
871  WriteToken(os, binary, "<Indexes>");
872  for (size_t c = 0; c < indexes.size(); c++) {
873  WriteIntegerVector(os, binary, indexes[c]);
874  }
875 
876  if (!binary) os << std::endl;
877  WriteToken(os, binary, "<NumIndexesMulti>");
878  WriteBasicType(os, binary, indexes_multi.size());
879  WriteToken(os, binary, "<IndexesMulti>");
880  for (size_t c = 0; c < indexes_multi.size(); c++) {
881  WriteIntegerPairVector(os, binary, indexes_multi[c]);
882  }
883 
884  if (!binary) os << std::endl;
885  WriteToken(os, binary, "<NumIndexesRanges>");
886  WriteBasicType(os, binary, indexes_ranges.size());
887  WriteToken(os, binary, "<IndexesRanges>");
888  for (size_t c = 0; c < indexes_ranges.size(); c++) {
889  WriteIntegerPairVector(os, binary, indexes_ranges[c]);
890  }
891 
892  if (!binary) os << std::endl;
893  WriteToken(os, binary, "<NumCommands>");
894  WriteBasicType(os, binary, commands.size());
895  WriteToken(os, binary, "<Commands>");
896  for (size_t c = 0; c < commands.size(); c++) {
897  commands[c].Write(os, binary);
898  }
899 
900  if (!binary) os << std::endl;
901  WriteToken(os, binary, "<NeedModelDerivative>");
903  WriteToken(os, binary, "</NnetComputation>");
904  if (!binary) os << std::endl;
905 }
void WriteIndexVector(std::ostream &os, bool binary, const std::vector< Index > &vec)
Definition: nnet-common.cc:126
void WriteIntegerPairVector(std::ostream &os, bool binary, const std::vector< std::pair< T, T > > &v)
Function for writing STL vectors of pairs of integer types.
Definition: io-funcs-inl.h:93
std::vector< MatrixDebugInfo > matrix_debug_info
std::vector< MatrixInfo > matrices
std::vector< Command > commands
std::vector< std::vector< std::pair< int32, int32 > > > indexes_multi
std::vector< SubMatrixInfo > submatrices
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
Definition: io-funcs.cc:134
std::vector< PrecomputedIndexesInfo > component_precomputed_indexes
void WriteIntegerVector(std::ostream &os, bool binary, const std::vector< T > &v)
Function for writing STL vectors of integer types.
Definition: io-funcs-inl.h:198
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:34
std::vector< std::vector< int32 > > indexes
std::vector< std::vector< std::pair< int32, int32 > > > indexes_ranges

Member Data Documentation

std::vector<Command> commands

Definition at line 384 of file nnet-computation.h.

Referenced by Compiler::AddBackwardStepComponent(), Compiler::AddBackwardStepInput(), Compiler::AddCommands(), ModelUpdateConsolidator::AddCommandsToComputation(), Compiler::AddForwardStepComponent(), Compiler::AddForwardStepInput(), ComputationLoopedOptimizer::AddMatrixSwapCommands(), Compiler::AllocateMatrices(), kaldi::nnet3::CheckComputation(), ComputationChecker::CheckComputationIndexes(), kaldi::nnet3::CheckComputationOnline(), NnetComputer::CheckNoPendingIo(), kaldi::nnet3::CompileLoopedInternal(), kaldi::nnet3::ComputeCommandAttributes(), ComputationExpander::ComputeCommands(), kaldi::nnet3::ComputeMatrixAccesses(), ComputationExpander::ComputePrecomputedIndexes(), kaldi::nnet3::ConsolidateIoOperations(), ModelUpdateConsolidator::ConsolidateModelUpdate(), ModelUpdateConsolidator::ConsolidateUpdateForComponent(), kaldi::nnet3::ConvertAdditionToAssignment(), ComputationAnalysis::DataInvalidatedCommand(), Compiler::DeallocateMatrices(), NnetComputer::DebugAfterExecute(), NnetComputer::DebugBeforeExecute(), Compiler::DoBackwardComputation(), Compiler::DoBackwardComputationDescriptor(), Compiler::DoBackwardComputationFromIndexes(), Compiler::DoBackwardComputationFromSubmatLocations(), Compiler::DoForwardComputation(), Compiler::DoForwardComputationDescriptor(), Compiler::DoForwardComputationFromIndexes(), Compiler::DoForwardComputationFromSubmatLocations(), VariableMergingOptimizer::DoMerge(), NnetComputer::ExecuteCommand(), ComputationLoopedOptimizer::FindTimeShift(), ComputationAnalysis::FirstAccess(), ComputationAnalysis::FirstMatrixAccess(), kaldi::nnet3::FixGotoLabel(), ComputationLoopedOptimizer::FormInfiniteLoop(), kaldi::nnet3::GetCommandsOfType(), NnetComputation::GetCommandStrings(), NnetComputer::GetIoMatrixIndex(), kaldi::nnet3::IdentifySubmatrixArgsInComputation(), ComputationAnalysis::LastAccess(), ComputationAnalysis::LastWriteAccess(), VariableMergingOptimizer::MayBeMerged(), VariableMergingOptimizer::MergeVariables(), DerivativeTimeLimiter::ModifyCommands(), kaldi::nnet3::MoveSizingCommands(), NnetComputation::operator=(), NnetComputation::Print(), kaldi::nnet3::PrintCommand(), DerivativeTimeLimiter::PruneMatrices(), NnetComputation::Read(), ComputationRenumberer::RemoveIndexesMultiDuplicates(), kaldi::nnet3::RemoveNoOps(), kaldi::nnet3::RemoveUnnecessaryAllocation(), kaldi::nnet3::RemoveUnnecessaryZeroing(), ComputationRenumberer::RemoveUnusedIndexesMulti(), DerivativeTimeLimiter::RemoveUnusedMemos(), ComputationRenumberer::RenumberIndexes(), ComputationRenumberer::RenumberIndexesRanges(), ComputationRenumberer::RenumberMemos(), kaldi::nnet3::ReplaceRowWithMatrixOps(), NnetComputer::Run(), kaldi::nnet3::SnipMultiRowOp(), kaldi::nnet3::SnipRangesRowOp(), kaldi::nnet3::SnipRowOps(), kaldi::nnet3::SnipSingleRowOp(), kaldi::nnet3::SplitComputationIntoSegments(), kaldi::nnet3::UnitTestNnetAnalyze(), and NnetComputation::Write().

std::vector<MatrixInfo> matrices

Definition at line 340 of file nnet-computation.h.

Referenced by NnetComputer::AcceptInput(), Compiler::AddCommands(), Compiler::AllocateMatrices(), ModelUpdateConsolidator::AppendDebugInfoForSubmatrix(), ComputationChecker::CheckComputationDebugInfo(), ComputationLoopedOptimizer::CheckIdentifiedMatrices(), ComputationExpander::ComputeDebugInfo(), kaldi::nnet3::ComputeMatrixAccesses(), ComputationExpander::ComputeMatrixInfo(), ComputationRenumberer::ComputeMatrixIsUsed(), DerivativeTimeLimiter::ComputeMatrixPruneInfo(), kaldi::nnet3::ComputeMatrixToSubmatrix(), ComputationVariables::ComputeSplitPoints(), ModelUpdateConsolidator::ConsolidateSubmatrices(), ComputationLoopedOptimizer::CreateMatrixPairs(), Compiler::DeallocateMatrices(), VariableMergingOptimizer::DoMerge(), NnetComputer::ExecuteCommand(), ComputationLoopedOptimizer::FindActiveMatrices(), ComputationLoopedOptimizer::FindTimeShift(), ComputationAnalysis::FirstMatrixAccess(), kaldi::nnet3::GetIndexesMultiStrings(), kaldi::nnet3::GetSubMatrixOfSubMatrix(), NnetComputation::GetWholeSubmatrices(), ComputationExpander::InitStrideInfo(), NnetComputation::IsWholeMatrix(), ComputationAnalysis::LastMatrixAccess(), DerivativeTimeLimiter::LimitMatrices(), VariableMergingOptimizer::MayBeMerged(), NnetComputation::NewMatrix(), NnetComputation::NewSubMatrix(), NnetComputer::NnetComputer(), NnetComputation::operator=(), Compiler::OutputDebugInfo(), kaldi::nnet3::PrintComputationPreamble(), DerivativeTimeLimiter::PruneMatrices(), NnetComputation::Read(), kaldi::nnet3::RemoveUnnecessaryAllocation(), ComputationRenumberer::RenumberMatrices(), and NnetComputation::Write().

std::vector<SubMatrixInfo> submatrices

Definition at line 354 of file nnet-computation.h.

Referenced by Compiler::AllocateMatrices(), ModelUpdateConsolidator::AppendDebugInfoForSubmatrix(), ComputationChecker::CheckComputationIndexes(), kaldi::nnet3::ComputeMatrixAccesses(), ComputationRenumberer::ComputeMatrixIsUsed(), kaldi::nnet3::ComputeMatrixToSubmatrix(), ComputationVariables::ComputeSplitPoints(), ComputationExpander::ComputeSubmatrixInfo(), ComputationRenumberer::ComputeSubmatrixIsUsed(), DerivativeTimeLimiter::ComputeSubmatrixMaps(), ComputationVariables::ComputeVariablesForSubmatrix(), ModelUpdateConsolidator::ConsolidateSubmatrices(), ComputationAnalysis::DataInvalidatedCommand(), Compiler::DeallocateMatrices(), Compiler::DoBackwardComputationFromIndexes(), Compiler::DoForwardComputationFromIndexes(), VariableMergingOptimizer::DoMerge(), NnetComputer::ExecuteCommand(), ComputationExpander::ExpandRowRangesCommand(), ComputationExpander::ExpandRowsCommand(), ComputationExpander::ExpandRowsMultiCommand(), ComputationLoopedOptimizer::FindTimeShift(), ComputationAnalysis::FirstAccess(), kaldi::nnet3::GetIndexesMultiStrings(), NnetComputer::GetIoMatrixIndex(), ComputationExpander::GetNewSubmatLocationInfo(), DerivativeTimeLimiter::GetPruneValues(), NnetComputer::GetSubMatrix(), kaldi::nnet3::GetSubMatrixOfSubMatrix(), NnetComputation::GetSubmatrixStrings(), NnetComputation::GetWholeSubmatrices(), kaldi::nnet3::IdentifyMatrixArgsInComputation(), NnetComputation::IsWholeMatrix(), ComputationAnalysis::LastAccess(), ComputationAnalysis::LastWriteAccess(), DerivativeTimeLimiter::LimitMatrices(), DerivativeTimeLimiter::MapAddRowRangesCommand(), DerivativeTimeLimiter::MapIndexesCommand(), DerivativeTimeLimiter::MapIndexesMultiCommand(), DerivativeTimeLimiter::MapSimpleMatrixCommand(), VariableMergingOptimizer::MayBeMerged(), NnetComputation::NewMatrix(), NnetComputation::NewSubMatrix(), NnetComputation::operator=(), Compiler::OutputDebugInfo(), kaldi::nnet3::PrintCommand(), NnetComputation::Read(), kaldi::nnet3::RemoveUnnecessaryAllocation(), ComputationRenumberer::RenumberMatrices(), ComputationRenumberer::RenumberSubmatrices(), DerivativeTimeLimiter::RowIsKept(), ComputationRenumberer::SetUpMappings(), kaldi::nnet3::UnitTestNnetAnalyze(), and NnetComputation::Write().


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