All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ComputationAnalysis Class Reference

This class performs various kinds of specific analysis on top of what class Analyzer gives you immediately. More...

#include <nnet-analyze.h>

Collaboration diagram for ComputationAnalysis:

Public Member Functions

 ComputationAnalysis (const NnetComputation &computation, const Analyzer &analyzer)
 This class stores the const references provided to its constructor -> be careful about changing them or deallocating them during the lifetime of this object. More...
 
int32 FirstNontrivialAccess (int32 s) const
 Returns the first command (read or write) that accesses any part of 's' except for zeroing it (i.e. More...
 
int32 LastAccess (int32 s) const
 Returns the last non-deallocation command that accesses any part of submatrix 's'; if there is no such command it returns -1. More...
 
int32 LastWriteAccess (int32 s) const
 Returns the last command-index that accesses any part of submatrix 's' as a write operation, or -1 if there is no such operation. More...
 
int32 DataInvalidatedCommand (int32 c, int32 s) const
 Returns (the first command-index after 'c' that any part of submatrix 's' is written to); or if there is no such command, then (the command-index of the command that deallocates the matrix underlying s); or if there is no such command, then the total number of commands. More...
 
int32 FirstNontrivialMatrixAccess (int32 m) const
 Returns the first command that is not a zeroing command (kSetConst with alpha=0.0), that accesses any part of 'm' [note: allocation and deallocation do not count a matrix accesses. More...
 
int32 LastMatrixAccess (int32 m) const
 Returns the last non-deallocation command that accesses any part of matrix 'm'; if there is no such command it returns -1. More...
 

Private Attributes

const NnetComputationcomputation_
 
const Analyzeranalyzer_
 

Detailed Description

This class performs various kinds of specific analysis on top of what class Analyzer gives you immediately.

It mostly contains special-purpose things what were needed by class VariableMergingOptimizer (see nnet-optimize.h, and the extended comment above class VariableMergingOptimizer). Be careful about the meaninhg of 'access'- read the comments carefully.

Definition at line 307 of file nnet-analyze.h.

Constructor & Destructor Documentation

ComputationAnalysis ( const NnetComputation computation,
const Analyzer analyzer 
)
inline

This class stores the const references provided to its constructor -> be careful about changing them or deallocating them during the lifetime of this object.

Definition at line 312 of file nnet-analyze.h.

313  : computation_(computation),
314  analyzer_(analyzer) { }
const NnetComputation & computation_
Definition: nnet-analyze.h:355

Member Function Documentation

int32 DataInvalidatedCommand ( int32  c,
int32  s 
) const

Returns (the first command-index after 'c' that any part of submatrix 's' is written to); or if there is no such command, then (the command-index of the command that deallocates the matrix underlying s); or if there is no such command, then the total number of commands.

s must be >0 (i.e. not the empty submatrix).

Definition at line 1181 of file nnet-analyze.cc.

References ComputationAnalysis::analyzer_, ComputationVariables::AppendVariablesForSubmatrix(), NnetComputation::commands, ComputationAnalysis::computation_, KALDI_ASSERT, kaldi::nnet3::kReadAccess, Analyzer::matrix_accesses, NnetComputation::submatrices, Analyzer::variable_accesses, and Analyzer::variables.

Referenced by VariableMergingOptimizer::MayBeMerged(), and kaldi::nnet3::UnitTestNnetAnalyze().

1181  {
1182  KALDI_ASSERT(static_cast<size_t>(c) < computation_.commands.size());
1183  KALDI_ASSERT(static_cast<size_t>(s) < computation_.submatrices.size() && s>0);
1184  int32 matrix_index = computation_.submatrices[s].matrix_index;
1185  int32 ans = analyzer_.matrix_accesses[matrix_index].deallocate_command;
1186  if (ans == -1)
1187  ans = static_cast<int32>(computation_.commands.size());
1188  std::vector<int32> variable_indexes;
1189  analyzer_.variables.AppendVariablesForSubmatrix(s, &variable_indexes);
1190  std::vector<int32>::const_iterator iter = variable_indexes.begin(),
1191  end = variable_indexes.end();
1192  for (; iter != end; ++iter) {
1193  int32 v = *iter;
1194  const std::vector<Access> &accesses = analyzer_.variable_accesses[v];
1195  std::vector<Access>::const_iterator access_iter = accesses.begin(),
1196  access_end = accesses.end();
1197  for (; access_iter != access_end; ++access_iter) {
1198  int32 command_index = access_iter->command_index;
1199  if (command_index > c &&
1200  access_iter->access_type != kReadAccess) {
1201  ans = std::min(ans, command_index);
1202  }
1203  }
1204  }
1205  return ans;
1206 }
void AppendVariablesForSubmatrix(int32 submatrix_index, std::vector< int32 > *variable_indexes) const
std::vector< Command > commands
std::vector< SubMatrixInfo > submatrices
std::vector< std::vector< Access > > variable_accesses
Definition: nnet-analyze.h:296
const NnetComputation & computation_
Definition: nnet-analyze.h:355
std::vector< MatrixAccesses > matrix_accesses
Definition: nnet-analyze.h:297
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
ComputationVariables variables
Definition: nnet-analyze.h:294
int32 FirstNontrivialAccess ( int32  s) const

Returns the first command (read or write) that accesses any part of 's' except for zeroing it (i.e.

kSetConst with zero alpha). [note: kAllocMatrix, kSwapMatrix and kDeallocMatrix do not count as read or write operations]. If there is no such command, it returns num_commands. s must be >0 (i.e. not the empty submatrix).

Definition at line 1057 of file nnet-analyze.cc.

References NnetComputation::Command::alpha, ComputationAnalysis::analyzer_, ComputationVariables::AppendVariablesForSubmatrix(), NnetComputation::Command::command_type, NnetComputation::commands, ComputationAnalysis::computation_, KALDI_ASSERT, kaldi::nnet3::kSetConst, NnetComputation::submatrices, Analyzer::variable_accesses, and Analyzer::variables.

Referenced by kaldi::nnet3::ConvertAdditionToAssignment(), ComputationLoopedOptimizer::FindActiveMatrices(), VariableMergingOptimizer::MayBeMerged(), and kaldi::nnet3::UnitTestNnetAnalyze().

1057  {
1058  KALDI_ASSERT(static_cast<size_t>(s) < computation_.submatrices.size() && s>0);
1059  int32 ans = computation_.commands.size();
1060  std::vector<int32> variable_indexes;
1061  analyzer_.variables.AppendVariablesForSubmatrix(s, &variable_indexes);
1062  std::vector<int32>::const_iterator iter = variable_indexes.begin(),
1063  end = variable_indexes.end();
1064  for (; iter != end; ++iter) {
1065  int32 v = *iter;
1066  const std::vector<Access> &accesses = analyzer_.variable_accesses[v];
1067  std::vector<Access>::const_iterator access_iter = accesses.begin(),
1068  access_end = accesses.end();
1069  for (; access_iter != access_end; ++access_iter) {
1070  int32 command_index = access_iter->command_index;
1071  const NnetComputation::Command &command = computation_.commands[
1072  command_index];
1073  if (!(command.command_type == kSetConst &&
1074  command.alpha == 0.0)) { // if it's not a zeroing command..
1075  ans = std::min(ans, command_index);
1076  break; // break from access_iter loop (an optimization)
1077  }
1078  }
1079  }
1080  return ans;
1081 }
void AppendVariablesForSubmatrix(int32 submatrix_index, std::vector< int32 > *variable_indexes) const
std::vector< Command > commands
std::vector< SubMatrixInfo > submatrices
std::vector< std::vector< Access > > variable_accesses
Definition: nnet-analyze.h:296
const NnetComputation & computation_
Definition: nnet-analyze.h:355
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
ComputationVariables variables
Definition: nnet-analyze.h:294
int32 FirstNontrivialMatrixAccess ( int32  m) const

Returns the first command that is not a zeroing command (kSetConst with alpha=0.0), that accesses any part of 'm' [note: allocation and deallocation do not count a matrix accesses.

] If there is no such command, it returns num_commands. m must be >0 (i.e. not the empty matrix).

Definition at line 1084 of file nnet-analyze.cc.

References NnetComputation::Command::alpha, ComputationAnalysis::analyzer_, NnetComputation::Command::command_type, NnetComputation::commands, ComputationAnalysis::computation_, KALDI_ASSERT, kaldi::nnet3::kSetConst, NnetComputation::matrices, and Analyzer::matrix_accesses.

Referenced by VariableMergingOptimizer::DoMerge().

1084  {
1085  KALDI_ASSERT(static_cast<size_t>(m) < computation_.matrices.size() && m > 0);
1086  int32 ans = computation_.commands.size();
1087  const std::vector<Access> &accesses =
1088  analyzer_.matrix_accesses[m].accesses;
1089  std::vector<Access>::const_iterator access_iter = accesses.begin(),
1090  access_end = accesses.end();
1091  for (; access_iter != access_end; ++access_iter) {
1092  int32 command_index = access_iter->command_index;
1093  const NnetComputation::Command command =
1094  computation_.commands[command_index];
1095  if (!(command.command_type == kSetConst &&
1096  command.alpha == 0.0)) { // except for zeroing commands..
1097  ans = std::min(ans, command_index);
1098  break; // break from access_iter loop (an optimization; note, the
1099  // list 'accesses' is sorted.)
1100  }
1101  }
1102  return ans;
1103 }
std::vector< MatrixInfo > matrices
std::vector< Command > commands
const NnetComputation & computation_
Definition: nnet-analyze.h:355
std::vector< MatrixAccesses > matrix_accesses
Definition: nnet-analyze.h:297
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
int32 LastAccess ( int32  s) const

Returns the last non-deallocation command that accesses any part of submatrix 's'; if there is no such command it returns -1.

s must be >0 (i.e. not the empty submatrix).

Definition at line 1122 of file nnet-analyze.cc.

References ComputationAnalysis::analyzer_, ComputationVariables::AppendVariablesForSubmatrix(), NnetComputation::commands, ComputationAnalysis::computation_, KALDI_ASSERT, kaldi::nnet3::kDeallocMatrix, NnetComputation::submatrices, Analyzer::variable_accesses, and Analyzer::variables.

Referenced by ComputationLoopedOptimizer::FindActiveMatrices(), VariableMergingOptimizer::MayBeMerged(), and kaldi::nnet3::UnitTestNnetAnalyze().

1122  {
1123  KALDI_ASSERT(static_cast<size_t>(s) < computation_.submatrices.size() && s>0);
1124  int32 ans = -1;
1125  std::vector<int32> variable_indexes;
1126  analyzer_.variables.AppendVariablesForSubmatrix(s, &variable_indexes);
1127  std::vector<int32>::const_iterator iter = variable_indexes.begin(),
1128  end = variable_indexes.end();
1129  for (; iter != end; ++iter) {
1130  int32 v = *iter;
1131  const std::vector<Access> &accesses = analyzer_.variable_accesses[v];
1132  // Go through the variable accesses in reverse order (of command index)
1133  std::vector<Access>::const_reverse_iterator access_iter = accesses.rbegin(),
1134  access_end = accesses.rend();
1135  for (; access_iter != access_end; ++access_iter) {
1136  int32 command_index = access_iter->command_index;
1137  CommandType command_type =
1138  computation_.commands[command_index].command_type;
1139  // deallocation command should not be listed here.
1140  KALDI_ASSERT(command_type != kDeallocMatrix);
1141  ans = std::max(ans, command_index);
1142  break; // break from access_iter loop (an optimization)
1143  }
1144  }
1145  return ans;
1146 }
CommandType
CommandType is an enum that describes the category of the command used in the NnetComputation.
void AppendVariablesForSubmatrix(int32 submatrix_index, std::vector< int32 > *variable_indexes) const
std::vector< Command > commands
std::vector< SubMatrixInfo > submatrices
std::vector< std::vector< Access > > variable_accesses
Definition: nnet-analyze.h:296
const NnetComputation & computation_
Definition: nnet-analyze.h:355
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
ComputationVariables variables
Definition: nnet-analyze.h:294
int32 LastMatrixAccess ( int32  m) const

Returns the last non-deallocation command that accesses any part of matrix 'm'; if there is no such command it returns -1.

m must be >0 (i.e. not the empty matrix).

Definition at line 1106 of file nnet-analyze.cc.

References ComputationAnalysis::analyzer_, ComputationAnalysis::computation_, KALDI_ASSERT, NnetComputation::matrices, and Analyzer::matrix_accesses.

1106  {
1107  KALDI_ASSERT(static_cast<size_t>(m) < computation_.matrices.size() && m > 0);
1108  int32 ans = -1;
1109  const std::vector<Access> &accesses =
1110  analyzer_.matrix_accesses[m].accesses;
1111  std::vector<Access>::const_reverse_iterator access_iter = accesses.rbegin(),
1112  access_end = accesses.rend();
1113  for (; access_iter != access_end; ++access_iter) {
1114  int32 command_index = access_iter->command_index;
1115  ans = std::max(ans, command_index);
1116  break; // break from access_iter loop (an optimization)
1117  }
1118  return ans;
1119 }
std::vector< MatrixInfo > matrices
const NnetComputation & computation_
Definition: nnet-analyze.h:355
std::vector< MatrixAccesses > matrix_accesses
Definition: nnet-analyze.h:297
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
int32 LastWriteAccess ( int32  s) const

Returns the last command-index that accesses any part of submatrix 's' as a write operation, or -1 if there is no such operation.

Note: deallocation does not count as a write operation. s must be >0 (i.e. not the empty submatrix).

Definition at line 1149 of file nnet-analyze.cc.

References ComputationAnalysis::analyzer_, ComputationVariables::AppendVariablesForSubmatrix(), NnetComputation::commands, ComputationAnalysis::computation_, KALDI_ASSERT, kaldi::nnet3::kDeallocMatrix, kaldi::nnet3::kReadAccess, Analyzer::matrix_accesses, NnetComputation::submatrices, Analyzer::variable_accesses, and Analyzer::variables.

Referenced by VariableMergingOptimizer::MayBeMerged(), and kaldi::nnet3::UnitTestNnetAnalyze().

1149  {
1150  KALDI_ASSERT(static_cast<size_t>(s) < computation_.submatrices.size() && s>0);
1151  int32 matrix_index = computation_.submatrices[s].matrix_index;
1152  if (analyzer_.matrix_accesses[matrix_index].is_output)
1153  return computation_.commands.size();
1154  int32 ans = -1;
1155  std::vector<int32> variable_indexes;
1156  analyzer_.variables.AppendVariablesForSubmatrix(s, &variable_indexes);
1157  std::vector<int32>::const_iterator iter = variable_indexes.begin(),
1158  end = variable_indexes.end();
1159  for (; iter != end; ++iter) {
1160  int32 v = *iter;
1161  const std::vector<Access> &accesses = analyzer_.variable_accesses[v];
1162  // Go through the variable accesses in reverse order (of command index)
1163  std::vector<Access>::const_reverse_iterator access_iter = accesses.rbegin(),
1164  access_end = accesses.rend();
1165  for (; access_iter != access_end; ++access_iter) {
1166  int32 command_index = access_iter->command_index;
1167  CommandType command_type =
1168  computation_.commands[command_index].command_type;
1169  // deallocation command should not be listed here.
1170  KALDI_ASSERT(command_type != kDeallocMatrix);
1171  if (access_iter->access_type != kReadAccess) {
1172  // If this operation is of type kWriteAccess or kReadWriteAccess
1173  ans = std::max(ans, command_index);
1174  break; // break from access_iter loop (an optimization)
1175  }
1176  }
1177  }
1178  return ans;
1179 }
CommandType
CommandType is an enum that describes the category of the command used in the NnetComputation.
void AppendVariablesForSubmatrix(int32 submatrix_index, std::vector< int32 > *variable_indexes) const
std::vector< Command > commands
std::vector< SubMatrixInfo > submatrices
std::vector< std::vector< Access > > variable_accesses
Definition: nnet-analyze.h:296
const NnetComputation & computation_
Definition: nnet-analyze.h:355
std::vector< MatrixAccesses > matrix_accesses
Definition: nnet-analyze.h:297
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
ComputationVariables variables
Definition: nnet-analyze.h:294

Member Data Documentation


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