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 FirstAccess (int32 s) const
 Returns the first command (read or write) that is not a kAlloc* command, that accesses any part of 's' [note: deallocation does not count as a read or write operation]. 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 FirstMatrixAccess (int32 m) const
 Returns the first command (read or write or accept-input) that is not an kAllocate* command, that accesses any part of 'm' [note: deallocation does not count as a read or write operation]. 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 306 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 311 of file nnet-analyze.h.

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

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 1177 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().

1177  {
1178  KALDI_ASSERT(static_cast<size_t>(c) < computation_.commands.size());
1179  KALDI_ASSERT(static_cast<size_t>(s) < computation_.submatrices.size() && s>0);
1180  int32 matrix_index = computation_.submatrices[s].matrix_index;
1181  int32 ans = analyzer_.matrix_accesses[matrix_index].deallocate_command;
1182  if (ans == -1)
1183  ans = static_cast<int32>(computation_.commands.size());
1184  std::vector<int32> variable_indexes;
1185  analyzer_.variables.AppendVariablesForSubmatrix(s, &variable_indexes);
1186  std::vector<int32>::const_iterator iter = variable_indexes.begin(),
1187  end = variable_indexes.end();
1188  for (; iter != end; ++iter) {
1189  int32 v = *iter;
1190  const std::vector<Access> &accesses = analyzer_.variable_accesses[v];
1191  std::vector<Access>::const_iterator access_iter = accesses.begin(),
1192  access_end = accesses.end();
1193  for (; access_iter != access_end; ++access_iter) {
1194  int32 command_index = access_iter->command_index;
1195  if (command_index > c &&
1196  access_iter->access_type != kReadAccess) {
1197  ans = std::min(ans, command_index);
1198  }
1199  }
1200  }
1201  return ans;
1202 }
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:295
const NnetComputation & computation_
Definition: nnet-analyze.h:352
std::vector< MatrixAccesses > matrix_accesses
Definition: nnet-analyze.h:296
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
ComputationVariables variables
Definition: nnet-analyze.h:293
int32 FirstAccess ( int32  s) const

Returns the first command (read or write) that is not a kAlloc* command, that accesses any part of 's' [note: deallocation does not count as a read or write operation].

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

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

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

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

1048  {
1049  KALDI_ASSERT(static_cast<size_t>(s) < computation_.submatrices.size() && s>0);
1050  int32 ans = computation_.commands.size();
1051  std::vector<int32> variable_indexes;
1052  analyzer_.variables.AppendVariablesForSubmatrix(s, &variable_indexes);
1053  std::vector<int32>::const_iterator iter = variable_indexes.begin(),
1054  end = variable_indexes.end();
1055  for (; iter != end; ++iter) {
1056  int32 v = *iter;
1057  const std::vector<Access> &accesses = analyzer_.variable_accesses[v];
1058  std::vector<Access>::const_iterator access_iter = accesses.begin(),
1059  access_end = accesses.end();
1060  for (; access_iter != access_end; ++access_iter) {
1061  int32 command_index = access_iter->command_index;
1062  CommandType command_type =
1063  computation_.commands[command_index].command_type;
1064  // The following two command types are not considered writes or reads,
1065  // so they should not even appear in this list.
1066  KALDI_ASSERT(command_type != kAllocMatrixUndefined &&
1067  command_type != kAllocMatrixFromOther);
1068  if (command_type != kAllocMatrixZeroed &&
1069  command_type != kAllocMatrixFromOtherZeroed) {
1070  ans = std::min(ans, command_index);
1071  break; // break from access_iter loop (an optimization)
1072  }
1073  }
1074  }
1075  return ans;
1076 }
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:295
const NnetComputation & computation_
Definition: nnet-analyze.h:352
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
ComputationVariables variables
Definition: nnet-analyze.h:293
int32 FirstMatrixAccess ( int32  m) const

Returns the first command (read or write or accept-input) that is not an kAllocate* command, that accesses any part of 'm' [note: deallocation does not count as a read or write operation].

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

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

References ComputationAnalysis::analyzer_, NnetComputation::commands, ComputationAnalysis::computation_, KALDI_ASSERT, kaldi::nnet3::kAllocMatrixFromOther, kaldi::nnet3::kAllocMatrixFromOtherZeroed, kaldi::nnet3::kAllocMatrixUndefined, kaldi::nnet3::kAllocMatrixZeroed, NnetComputation::matrices, and Analyzer::matrix_accesses.

Referenced by VariableMergingOptimizer::DoMerge().

1079  {
1080  KALDI_ASSERT(static_cast<size_t>(m) < computation_.matrices.size() && m > 0);
1081  int32 ans = computation_.commands.size();
1082  const std::vector<Access> &accesses =
1083  analyzer_.matrix_accesses[m].accesses;
1084  std::vector<Access>::const_iterator access_iter = accesses.begin(),
1085  access_end = accesses.end();
1086  for (; access_iter != access_end; ++access_iter) {
1087  int32 command_index = access_iter->command_index;
1088  CommandType command_type =
1089  computation_.commands[command_index].command_type;
1090  if (command_type != kAllocMatrixUndefined &&
1091  command_type != kAllocMatrixZeroed &&
1092  command_type != kAllocMatrixFromOther &&
1093  command_type != kAllocMatrixFromOtherZeroed) {
1094  ans = std::min(ans, command_index);
1095  break; // break from access_iter loop (an optimization)
1096  }
1097  }
1098  return ans;
1099 }
CommandType
CommandType is an enum that describes the category of the command used in the NnetComputation.
std::vector< MatrixInfo > matrices
std::vector< Command > commands
const NnetComputation & computation_
Definition: nnet-analyze.h:352
std::vector< MatrixAccesses > matrix_accesses
Definition: nnet-analyze.h:296
#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 1118 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().

1118  {
1119  KALDI_ASSERT(static_cast<size_t>(s) < computation_.submatrices.size() && s>0);
1120  int32 ans = -1;
1121  std::vector<int32> variable_indexes;
1122  analyzer_.variables.AppendVariablesForSubmatrix(s, &variable_indexes);
1123  std::vector<int32>::const_iterator iter = variable_indexes.begin(),
1124  end = variable_indexes.end();
1125  for (; iter != end; ++iter) {
1126  int32 v = *iter;
1127  const std::vector<Access> &accesses = analyzer_.variable_accesses[v];
1128  // Go through the variable accesses in reverse order (of command index)
1129  std::vector<Access>::const_reverse_iterator access_iter = accesses.rbegin(),
1130  access_end = accesses.rend();
1131  for (; access_iter != access_end; ++access_iter) {
1132  int32 command_index = access_iter->command_index;
1133  CommandType command_type =
1134  computation_.commands[command_index].command_type;
1135  // deallocation command should not be listed here.
1136  KALDI_ASSERT(command_type != kDeallocMatrix);
1137  ans = std::max(ans, command_index);
1138  break; // break from access_iter loop (an optimization)
1139  }
1140  }
1141  return ans;
1142 }
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:295
const NnetComputation & computation_
Definition: nnet-analyze.h:352
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
ComputationVariables variables
Definition: nnet-analyze.h:293
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 1102 of file nnet-analyze.cc.

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

1102  {
1103  KALDI_ASSERT(static_cast<size_t>(m) < computation_.matrices.size() && m > 0);
1104  int32 ans = -1;
1105  const std::vector<Access> &accesses =
1106  analyzer_.matrix_accesses[m].accesses;
1107  std::vector<Access>::const_reverse_iterator access_iter = accesses.rbegin(),
1108  access_end = accesses.rend();
1109  for (; access_iter != access_end; ++access_iter) {
1110  int32 command_index = access_iter->command_index;
1111  ans = std::max(ans, command_index);
1112  break; // break from access_iter loop (an optimization)
1113  }
1114  return ans;
1115 }
std::vector< MatrixInfo > matrices
const NnetComputation & computation_
Definition: nnet-analyze.h:352
std::vector< MatrixAccesses > matrix_accesses
Definition: nnet-analyze.h:296
#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 1145 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().

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

Member Data Documentation


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