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< PrecomputedIndexesInfocomponent_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 303 of file nnet-computation.h.

Constructor & Destructor Documentation

◆ ~NnetComputation()

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

References rnnlm::i.

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

◆ NnetComputation() [1/2]

NnetComputation ( const NnetComputation other)

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

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

1134  :
1135  matrices(other.matrices),
1136  matrix_debug_info(other.matrix_debug_info),
1137  submatrices(other.submatrices),
1138  component_precomputed_indexes(other.component_precomputed_indexes),
1139  indexes(other.indexes),
1140  indexes_multi(other.indexes_multi),
1141  indexes_ranges(other.indexes_ranges),
1142  commands(other.commands),
1143  need_model_derivative(other.need_model_derivative),
1144  indexes_cuda(other.indexes_cuda),
1145  indexes_ranges_cuda(other.indexes_ranges_cuda) {
1146  for (size_t i = 1; i < component_precomputed_indexes.size(); i++)
1148  component_precomputed_indexes[i].data->Copy();
1149 }
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() [2/2]

NnetComputation ( )
inline

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

Member Function Documentation

◆ Clear()

void Clear ( )
inline

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

References kaldi::nnet2::NnetComputation().

Referenced by Compiler::CreateComputation().

◆ ComputeCudaIndexes()

void ComputeCudaIndexes ( )

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

References rnnlm::i, and KALDI_ASSERT.

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

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

◆ GetCommandStrings()

void GetCommandStrings ( const Nnet nnet,
std::string *  preamble,
std::vector< std::string > *  command_strings 
) const

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

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

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

947  {
948  std::vector<std::string> submatrix_strings, indexes_strings,
949  indexes_multi_strings;
950  this->GetSubmatrixStrings(nnet, &submatrix_strings);
951  GetIndexesStrings(nnet, *this, &indexes_strings);
952  GetIndexesMultiStrings(nnet, *this, &indexes_multi_strings);
953  if (preamble != NULL) {
954  std::ostringstream os;
955  PrintComputationPreamble(os, *this, nnet, submatrix_strings,
956  indexes_strings, indexes_multi_strings);
957  *preamble = os.str();
958  }
959  if (command_strings != NULL) {
960  command_strings->resize(commands.size());
961  for (int32 c = 0; c < commands.size(); c++) {
962  std::ostringstream os;
963  PrintCommand(os, nnet, *this, c, submatrix_strings,
964  indexes_strings, indexes_multi_strings);
965  (*command_strings)[c] = os.str();
966  // Remove the final newline.
967  std::string &str = (*command_strings)[c];
968  if (!str.empty())
969  str.resize(str.size() - 1);
970  }
971  }
972 }
static void GetIndexesStrings(const Nnet &nnet, const NnetComputation &computation, std::vector< std::string > *indexes_strings)
kaldi::int32 int32
std::vector< Command > commands
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

◆ GetSubmatrixStrings()

void GetSubmatrixStrings ( const Nnet nnet,
std::vector< std::string > *  submat_strings 
) const

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

References NnetComputation::SubMatrixInfo::col_offset, rnnlm::i, KALDI_ASSERT, NnetComputation::SubMatrixInfo::matrix_index, NnetComputation::SubMatrixInfo::num_cols, NnetComputation::SubMatrixInfo::num_rows, and NnetComputation::SubMatrixInfo::row_offset.

Referenced by ComputationExpander::ComputeSubmatrixInfo(), NnetComputer::Init(), and kaldi::nnet3::UnitTestNnetAnalyze().

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

◆ GetWholeSubmatrices()

void GetWholeSubmatrices ( std::vector< int32 > *  whole_submatrices) const

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

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

Referenced by Compiler::AddCommands(), ComputationLoopedOptimizer::AddMatrixSwapCommands(), ComputationLoopedOptimizer::FindActiveMatrices(), MatrixExtender::FixComputation(), DerivativeTimeLimiter::LimitDerivTimes(), and MemoryCompressionOptimizer::ModifyComputation().

1174  {
1175  int32 num_matrices = matrices.size(),
1176  num_submatrices = submatrices.size();
1177  whole_submatrices->clear();
1178  whole_submatrices->resize(num_matrices, 0);
1179  for (int32 s = 1; s < num_submatrices; s++) {
1180  if (IsWholeMatrix(s)) {
1181  int32 m = submatrices[s].matrix_index;
1182  (*whole_submatrices)[m] = s;
1183  }
1184  }
1185  for (int32 m = 1; m < num_matrices; m++) {
1186  KALDI_ASSERT((*whole_submatrices)[m] != 0 &&
1187  "Matrix exists with no submatrix that is "
1188  "the whole of it.");
1189  }
1190 }
kaldi::int32 int32
std::vector< MatrixInfo > matrices
std::vector< SubMatrixInfo > submatrices
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
bool IsWholeMatrix(int32 submatrix_index) const

◆ IsWholeMatrix()

bool IsWholeMatrix ( int32  submatrix_index) const

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

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

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

975  {
976  KALDI_ASSERT(submatrix_index > 0 && submatrix_index < submatrices.size());
977  const SubMatrixInfo &submat_info = submatrices[submatrix_index];
978  const MatrixInfo &mat_info = matrices[submat_info.matrix_index];
979  return submat_info.row_offset == 0 && submat_info.col_offset == 0 &&
980  submat_info.num_rows == mat_info.num_rows &&
981  submat_info.num_cols == mat_info.num_cols;
982 }
std::vector< MatrixInfo > matrices
std::vector< SubMatrixInfo > submatrices
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ NewMatrix()

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 128 of file nnet-computation.cc.

References KALDI_ASSERT, and kaldi::kDefaultStride.

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

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

◆ NewSubMatrix()

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 102 of file nnet-computation.cc.

References KALDI_ASSERT, and NnetComputation::SubMatrixInfo::matrix_index.

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

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

◆ operator=()

NnetComputation & operator= ( const NnetComputation other)

Definition at line 1151 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.

1151  {
1152  matrices = other.matrices;
1153  matrix_debug_info = other.matrix_debug_info;
1154  submatrices = other.submatrices;
1155  indexes = other.indexes;
1156  indexes_multi = other.indexes_multi;
1157  indexes_ranges = other.indexes_ranges;
1158  commands = other.commands;
1159  need_model_derivative = other.need_model_derivative;
1160  indexes_cuda = other.indexes_cuda;
1161  indexes_ranges_cuda = other.indexes_ranges_cuda;
1162 
1163  for (size_t i = 1; i < component_precomputed_indexes.size(); i++)
1164  delete component_precomputed_indexes[i].data;
1165  component_precomputed_indexes = other.component_precomputed_indexes;
1166  for (size_t i = 1; i < component_precomputed_indexes.size(); i++)
1168  component_precomputed_indexes[i].data->Copy();
1169  return *this;
1170 }
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

◆ Print()

void Print ( std::ostream &  os,
const Nnet nnet 
) const

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

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

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

717  {
718  std::vector<std::string> submatrix_strings, indexes_strings,
719  indexes_multi_strings;
720  this->GetSubmatrixStrings(nnet, &submatrix_strings);
721  GetIndexesStrings(nnet, *this, &indexes_strings);
722  GetIndexesMultiStrings(nnet, *this, &indexes_multi_strings);
723  PrintComputationPreamble(os, *this, nnet, submatrix_strings,
724  indexes_strings, indexes_multi_strings);
725  os << "# begin forward commands\n";
726  for (int32 c = 0; c < commands.size(); c++) {
727  PrintCommand(os, nnet, *this, c, submatrix_strings,
728  indexes_strings, indexes_multi_strings);
729  }
730 }
static void GetIndexesStrings(const Nnet &nnet, const NnetComputation &computation, std::vector< std::string > *indexes_strings)
kaldi::int32 int32
std::vector< Command > commands
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

◆ Read()

void Read ( std::istream &  istream,
bool  binary 
)

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

References NnetComputation::PrecomputedIndexesInfo::data, kaldi::nnet3::ExpectToken(), rnnlm::i, NnetComputation::PrecomputedIndexesInfo::input_indexes, KALDI_ASSERT, KALDI_ERR, ComputationRequest::need_model_derivative, NnetComputation::PrecomputedIndexesInfo::output_indexes, kaldi::ReadBasicType(), kaldi::nnet3::ReadIndexVector(), kaldi::ReadIntegerPairVector(), kaldi::ReadIntegerVector(), ComponentPrecomputedIndexes::ReadNew(), and kaldi::ReadToken().

Referenced by ComputationCache::Read(), and kaldi::nnet3::UnitTestNnetComputationIo().

732  {
733  int32 version = 5, // must be in sync with 'version' in Write.
734  version_in = 1; // defaults to 1 if no version specified.
735 
736  ExpectToken(is, binary, "<NnetComputation>");
737  std::string token;
738  ReadToken(is, binary, &token);
739  if (token == "<Version>") {
740  ReadBasicType(is, binary, &version_in);
741  ExpectToken(is, binary, "<NumMatrices>");
742  } else {
743  KALDI_ASSERT(token == "<NumMatrices>");
744  }
745  if (version_in != version) {
746  KALDI_ERR << "Reading NnetComputation failed because version in "
747  << version_in << " != " << version << "... you can "
748  << "ignore this error if the program continues afterward, "
749  << "it would only affect speed.";
750  }
751  size_t num_matrices;
752  ReadBasicType(is, binary, &num_matrices);
753  KALDI_ASSERT(num_matrices >= 0);
754  matrices.resize(num_matrices);
755  ExpectToken(is, binary, "<Matrices>");
756  for (size_t c = 0; c < num_matrices; c++) {
757  matrices[c].Read(is, binary);
758  }
759 
760  size_t num_matrix_debug_info;
761  ExpectToken(is, binary, "<NumMatrixDebugInfo>");
762  ReadBasicType(is, binary, &num_matrix_debug_info);
763  KALDI_ASSERT(num_matrix_debug_info >= 0);
764  matrix_debug_info.resize(num_matrix_debug_info);
765  ExpectToken(is, binary, "<MatrixDebugInfo>");
766  for (size_t c = 0; c < num_matrix_debug_info; c++) {
767  matrix_debug_info[c].Read(is, binary);
768  }
769 
770  size_t num_submatrices;
771  ExpectToken(is, binary, "<NumSubMatrices>");
772  ReadBasicType(is, binary, &num_submatrices);
773  KALDI_ASSERT(num_submatrices >= 0);
774  submatrices.resize(num_submatrices);
775  ExpectToken(is, binary, "<SubMatrices>");
776  for (size_t c = 0; c < num_submatrices; c++) {
777  submatrices[c].Read(is, binary);
778  }
779 
780 
781  // delete any existing pointers in component_precomputed_indexes.
782  // note: component_precomputed_indexes[0] is the NULL pointer.
783  for (size_t i = 1; i < component_precomputed_indexes.size(); i++)
784  delete component_precomputed_indexes[i].data;
786 
787  size_t num_component_precomputed_indexes;
788  ExpectToken(is, binary, "<NumComponentPrecomputedIndexes>");
789  ReadBasicType(is, binary, &num_component_precomputed_indexes);
790  KALDI_ASSERT(num_component_precomputed_indexes >= 0);
791  component_precomputed_indexes.resize(num_component_precomputed_indexes);
792 
793  std::string tok;
794  ReadToken(is, binary, &tok);
795  if (tok == "<ComponentPrecomputedIndexes>") {
796  // Older on-disk format, before that code was extended for shortcut
797  // compilation.
799  component_precomputed_indexes.resize(num_component_precomputed_indexes);
800  for (size_t c = 0; c < num_component_precomputed_indexes; c++) {
801  bool is_null; // a boolean indicating whether the pointer should be NULL.
802  ReadBasicType(is, binary, &is_null);
803  if (!is_null) {
804  ComponentPrecomputedIndexes* p = ComponentPrecomputedIndexes::ReadNew(is, binary);
805  component_precomputed_indexes[c].data = p;
806  }
807  }
808  } else {
809  KALDI_ASSERT(tok == "<PrecomputedIndexesInfo>");
810  for (size_t c = 1; c < num_component_precomputed_indexes; c++) {
811  ComponentPrecomputedIndexes* p = ComponentPrecomputedIndexes::ReadNew(is, binary);
812  KALDI_ASSERT(p != NULL);
813  PrecomputedIndexesInfo &info = component_precomputed_indexes[c];
814  info.data = p;
815  ReadIndexVector(is, binary, &(info.input_indexes));
816  ReadIndexVector(is, binary, &(info.output_indexes));
817  }
818  }
819  size_t num_indexes;
820  ExpectToken(is, binary, "<NumIndexes>");
821  ReadBasicType(is, binary, &num_indexes);
822  KALDI_ASSERT(num_indexes >= 0);
823  indexes.resize(num_indexes);
824  ExpectToken(is, binary, "<Indexes>");
825  for (size_t c = 0; c < num_indexes; c++) {
826  ReadIntegerVector(is, binary, &(indexes[c]));
827  }
828 
829  size_t num_indexes_multi;
830  ExpectToken(is, binary, "<NumIndexesMulti>");
831  ReadBasicType(is, binary, &num_indexes_multi);
832  KALDI_ASSERT(num_indexes_multi >= 0);
833  indexes_multi.resize(num_indexes_multi);
834  ExpectToken(is, binary, "<IndexesMulti>");
835  for (size_t c = 0; c < num_indexes_multi; c++) {
836  ReadIntegerPairVector(is, binary, &(indexes_multi[c]));
837  }
838 
839  size_t num_indexes_ranges;
840  ExpectToken(is, binary, "<NumIndexesRanges>");
841  ReadBasicType(is, binary, &num_indexes_ranges);
842  KALDI_ASSERT(num_indexes_ranges >= 0);
843  indexes_ranges.resize(num_indexes_ranges);
844  ExpectToken(is, binary, "<IndexesRanges>");
845  for (size_t c = 0; c < num_indexes_ranges; c++) {
846  ReadIntegerPairVector(is, binary, &(indexes_ranges[c]));
847  }
848 
849  size_t num_commands;
850  ExpectToken(is, binary, "<NumCommands>");
851  ReadBasicType(is, binary, &num_commands);
852  KALDI_ASSERT(num_commands >= 0);
853  commands.resize(num_commands);
854  ExpectToken(is, binary, "<Commands>");
855  for (size_t c = 0; c < num_commands; c++) {
856  commands[c].Read(is, binary);
857  }
858 
859  ExpectToken(is, binary, "<NeedModelDerivative>");
860  ReadBasicType(is, binary, &need_model_derivative);
861 
863  ExpectToken(is, binary, "</NnetComputation>");
864 }
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
kaldi::int32 int32
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:147
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:185
std::vector< std::vector< int32 > > indexes
std::vector< std::vector< std::pair< int32, int32 > > > indexes_ranges

◆ Write()

void Write ( std::ostream &  ostream,
bool  binary 
) const

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

References NnetComputation::PrecomputedIndexesInfo::data, NnetComputation::PrecomputedIndexesInfo::input_indexes, ComputationRequest::need_model_derivative, NnetComputation::PrecomputedIndexesInfo::output_indexes, ComponentPrecomputedIndexes::Write(), kaldi::WriteBasicType(), kaldi::nnet3::WriteIndexVector(), kaldi::WriteIntegerPairVector(), kaldi::WriteIntegerVector(), and kaldi::WriteToken().

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

866  {
867  int32 version = 5; // Must be in sync with version in Read.
868  WriteToken(os, binary, "<NnetComputation>");
869  WriteToken(os, binary, "<Version>");
870  WriteBasicType(os, binary, version);
871  WriteToken(os, binary, "<NumMatrices>");
872  WriteBasicType(os, binary, matrices.size());
873  WriteToken(os, binary, "<Matrices>");
874  for (size_t c = 0; c < matrices.size(); c++) {
875  matrices[c].Write(os, binary);
876  }
877 
878  if (!binary) os << std::endl;
879  WriteToken(os, binary, "<NumMatrixDebugInfo>");
880  WriteBasicType(os, binary, matrix_debug_info.size());
881  WriteToken(os, binary, "<MatrixDebugInfo>");
882  for (size_t c = 0; c < matrix_debug_info.size(); c++) {
883  matrix_debug_info[c].Write(os, binary);
884  }
885 
886  if (!binary) os << std::endl;
887  WriteToken(os, binary, "<NumSubMatrices>");
888  WriteBasicType(os, binary, submatrices.size());
889  WriteToken(os, binary, "<SubMatrices>");
890  for (size_t c = 0; c < submatrices.size(); c++) {
891  submatrices[c].Write(os, binary);
892  }
893 
894  if (!binary) os << std::endl;
895  WriteToken(os, binary, "<NumComponentPrecomputedIndexes>");
896  WriteBasicType(os, binary, component_precomputed_indexes.size());
897  WriteToken(os, binary, "<PrecomputedIndexesInfo>");
898  for (size_t c = 1; c < component_precomputed_indexes.size(); c++) {
899  const PrecomputedIndexesInfo &info = component_precomputed_indexes[c];
900  info.data->Write(os, binary);
901  WriteIndexVector(os, binary, info.input_indexes);
902  WriteIndexVector(os, binary, info.output_indexes);
903  }
904 
905  if (!binary) os << std::endl;
906  WriteToken(os, binary, "<NumIndexes>");
907  WriteBasicType(os, binary, indexes.size());
908  WriteToken(os, binary, "<Indexes>");
909  for (size_t c = 0; c < indexes.size(); c++) {
910  WriteIntegerVector(os, binary, indexes[c]);
911  }
912 
913  if (!binary) os << std::endl;
914  WriteToken(os, binary, "<NumIndexesMulti>");
915  WriteBasicType(os, binary, indexes_multi.size());
916  WriteToken(os, binary, "<IndexesMulti>");
917  for (size_t c = 0; c < indexes_multi.size(); c++) {
918  WriteIntegerPairVector(os, binary, indexes_multi[c]);
919  }
920 
921  if (!binary) os << std::endl;
922  WriteToken(os, binary, "<NumIndexesRanges>");
923  WriteBasicType(os, binary, indexes_ranges.size());
924  WriteToken(os, binary, "<IndexesRanges>");
925  for (size_t c = 0; c < indexes_ranges.size(); c++) {
926  WriteIntegerPairVector(os, binary, indexes_ranges[c]);
927  }
928 
929  if (!binary) os << std::endl;
930  WriteToken(os, binary, "<NumCommands>");
931  WriteBasicType(os, binary, commands.size());
932  WriteToken(os, binary, "<Commands>");
933  for (size_t c = 0; c < commands.size(); c++) {
934  commands[c].Write(os, binary);
935  }
936 
937  if (!binary) os << std::endl;
938  WriteToken(os, binary, "<NeedModelDerivative>");
940  WriteToken(os, binary, "</NnetComputation>");
941  if (!binary) os << std::endl;
942 }
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
kaldi::int32 int32
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

◆ commands

std::vector<Command> commands

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

Referenced by Compiler::AddBackwardStepComponent(), Compiler::AddBackwardStepInput(), Compiler::AddCommands(), ModelUpdateConsolidator::AddCommandsToComputation(), Compiler::AddForwardStepComponent(), Compiler::AddForwardStepInput(), ComputationLoopedOptimizer::AddMatrixSwapCommands(), Compiler::AllocateMatrices(), DerivativeTimeLimiter::CanLimitMatrix(), kaldi::nnet3::CheckComputation(), ComputationChecker::CheckComputationCompression(), ComputationChecker::CheckComputationIndexes(), ComputationChecker::CheckComputationMatrixAccesses(), kaldi::nnet3::CheckComputationOnline(), ComputationChecker::CheckComputationUndefined(), NnetComputer::CheckNoPendingIo(), Compiler::CompileBackward(), Compiler::CompileBackwardDescriptor(), Compiler::CompileBackwardFromIndexes(), Compiler::CompileBackwardFromSubmatLocations(), Compiler::CompileForward(), Compiler::CompileForwardDescriptor(), Compiler::CompileForwardFromIndexes(), Compiler::CompileForwardFromSubmatLocations(), Compiler::CompileForwardSumDescriptor(), 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(), VariableMergingOptimizer::DoMerge(), NnetComputer::ExecuteCommand(), MatrixExtender::ExtendMatrices(), ComputationLoopedOptimizer::FindTimeShift(), ComputationAnalysis::FirstAccess(), ComputationAnalysis::FirstNontrivialAccess(), ComputationAnalysis::FirstNontrivialMatrixAccess(), MatrixExtender::FixComputation(), kaldi::nnet3::FixGotoLabel(), ComputationLoopedOptimizer::FormInfiniteLoop(), kaldi::nnet3::GetCommandsOfType(), NnetComputer::GetIoMatrixIndex(), kaldi::nnet3::GetMaxMemoryUse(), kaldi::nnet3::IdentifySubmatrixArgsInComputation(), kaldi::nnet3::InsertCommands(), ComputationAnalysis::LastAccess(), ComputationAnalysis::LastWriteAccess(), MatrixExtender::MatrixExtender(), kaldi::nnet3::MatrixIsUnused(), VariableMergingOptimizer::MayBeMerged(), VariableMergingOptimizer::MergeVariables(), DerivativeTimeLimiter::ModifyCommands(), kaldi::nnet3::MoveSizingCommands(), NnetComputation::operator=(), kaldi::nnet3::OptimizeMemoryCompression(), kaldi::nnet3::PrintCommand(), MemoryCompressionOptimizer::ProcessMatrix(), kaldi::nnet3::RemoveCommandsForUnusedMatrix(), 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(), RowOpsSplitter::SplitCommand(), RowOpsSplitter::SplitCommands(), kaldi::nnet3::SplitComputationIntoSegments(), and kaldi::nnet3::UnitTestNnetAnalyze().

◆ component_precomputed_indexes

◆ indexes

◆ indexes_cuda

std::vector<CuArray<int32> > indexes_cuda

◆ indexes_multi

◆ indexes_ranges

◆ indexes_ranges_cuda

std::vector<CuArray<Int32Pair> > indexes_ranges_cuda

◆ matrices

std::vector<MatrixInfo> matrices

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

Referenced by NnetComputer::AcceptInput(), Compiler::AddCommands(), Compiler::AllocateMatrices(), ModelUpdateConsolidator::AppendDebugInfoForSubmatrix(), MatrixExtender::CanBeExtended(), ComputationChecker::CheckComputationDebugInfo(), ComputationChecker::CheckComputationUndefined(), 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(), MatrixExtender::Extend(), ComputationLoopedOptimizer::FindActiveMatrices(), ComputationLoopedOptimizer::FindTimeShift(), ComputationAnalysis::FirstNontrivialMatrixAccess(), MatrixExtender::FixComputation(), MatrixExtender::FixDebugInfo(), kaldi::nnet3::GetIndexesMultiStrings(), kaldi::nnet3::GetSubMatrixOfSubMatrix(), NnetComputation::GetWholeSubmatrices(), NnetComputer::Init(), ComputationExpander::InitStrideInfo(), ComputationAnalysis::LastMatrixAccess(), DerivativeTimeLimiter::LimitMatrices(), MatrixExtender::MatrixExtender(), VariableMergingOptimizer::MayBeMerged(), NnetComputation::operator=(), MemoryCompressionOptimizer::Optimize(), Compiler::OutputDebugInfo(), kaldi::nnet3::PrintComputationPreamble(), DerivativeTimeLimiter::PruneMatrices(), kaldi::nnet3::RemoveUnnecessaryAllocation(), and ComputationRenumberer::RenumberMatrices().

◆ matrix_debug_info

◆ need_model_derivative

◆ submatrices

std::vector<SubMatrixInfo> submatrices

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

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


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