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 FirstAccess (int32 s) const
 Returns the first command (read or write) that accesses any part of 's', including possibly zeroing it. 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 308 of file nnet-analyze.h.

Constructor & Destructor Documentation

◆ ComputationAnalysis()

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 313 of file nnet-analyze.h.

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

Member Function Documentation

◆ DataInvalidatedCommand()

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 1323 of file nnet-analyze.cc.

References NnetComputation::commands, ComputationChecker::computation_, KALDI_ASSERT, kaldi::nnet3::kReadAccess, and NnetComputation::submatrices.

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

1323  {
1324  KALDI_ASSERT(static_cast<size_t>(c) < computation_.commands.size());
1325  KALDI_ASSERT(static_cast<size_t>(s) < computation_.submatrices.size() && s>0);
1326  int32 matrix_index = computation_.submatrices[s].matrix_index;
1327  int32 ans = analyzer_.matrix_accesses[matrix_index].deallocate_command;
1328  if (ans == -1)
1329  ans = static_cast<int32>(computation_.commands.size());
1330  std::vector<int32> variable_indexes;
1331  analyzer_.variables.AppendVariablesForSubmatrix(s, &variable_indexes);
1332  std::vector<int32>::const_iterator iter = variable_indexes.begin(),
1333  end = variable_indexes.end();
1334  for (; iter != end; ++iter) {
1335  int32 v = *iter;
1336  const std::vector<Access> &accesses = analyzer_.variable_accesses[v];
1337  std::vector<Access>::const_iterator access_iter = accesses.begin(),
1338  access_end = accesses.end();
1339  for (; access_iter != access_end; ++access_iter) {
1340  int32 command_index = access_iter->command_index;
1341  if (command_index > c &&
1342  access_iter->access_type != kReadAccess) {
1343  ans = std::min(ans, command_index);
1344  }
1345  }
1346  }
1347  return ans;
1348 }
kaldi::int32 int32
std::vector< Command > commands
std::vector< SubMatrixInfo > submatrices
std::vector< std::vector< Access > > variable_accesses
Definition: nnet-analyze.h:297
const NnetComputation & computation_
Definition: nnet-analyze.h:363
std::vector< MatrixAccesses > matrix_accesses
Definition: nnet-analyze.h:298
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void AppendVariablesForSubmatrix(int32 submatrix_index, std::vector< int32 > *variable_indexes) const
ComputationVariables variables
Definition: nnet-analyze.h:295

◆ FirstAccess()

int32 FirstAccess ( int32  s) const

Returns the first command (read or write) that accesses any part of 's', including possibly zeroing it.

[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 1209 of file nnet-analyze.cc.

References NnetComputation::commands, ComputationChecker::computation_, KALDI_ASSERT, and NnetComputation::submatrices.

1209  {
1210  KALDI_ASSERT(static_cast<size_t>(s) < computation_.submatrices.size() && s>0);
1211  int32 ans = computation_.commands.size();
1212  std::vector<int32> variable_indexes;
1213  analyzer_.variables.AppendVariablesForSubmatrix(s, &variable_indexes);
1214  std::vector<int32>::const_iterator iter = variable_indexes.begin(),
1215  end = variable_indexes.end();
1216  for (; iter != end; ++iter) {
1217  int32 v = *iter;
1218  const std::vector<Access> &accesses = analyzer_.variable_accesses[v];
1219  if (!accesses.empty())
1220  ans = std::min(ans, accesses[0].command_index);
1221  }
1222  return ans;
1223 }
kaldi::int32 int32
std::vector< Command > commands
std::vector< SubMatrixInfo > submatrices
std::vector< std::vector< Access > > variable_accesses
Definition: nnet-analyze.h:297
const NnetComputation & computation_
Definition: nnet-analyze.h:363
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void AppendVariablesForSubmatrix(int32 submatrix_index, std::vector< int32 > *variable_indexes) const
ComputationVariables variables
Definition: nnet-analyze.h:295

◆ FirstNontrivialAccess()

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 1182 of file nnet-analyze.cc.

References NnetComputation::Command::alpha, NnetComputation::Command::command_type, NnetComputation::commands, ComputationChecker::computation_, KALDI_ASSERT, kaldi::nnet3::kSetConst, and NnetComputation::submatrices.

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

1182  {
1183  KALDI_ASSERT(static_cast<size_t>(s) < computation_.submatrices.size() && s>0);
1184  int32 ans = computation_.commands.size();
1185  std::vector<int32> variable_indexes;
1186  analyzer_.variables.AppendVariablesForSubmatrix(s, &variable_indexes);
1187  std::vector<int32>::const_iterator iter = variable_indexes.begin(),
1188  end = variable_indexes.end();
1189  for (; iter != end; ++iter) {
1190  int32 v = *iter;
1191  const std::vector<Access> &accesses = analyzer_.variable_accesses[v];
1192  std::vector<Access>::const_iterator access_iter = accesses.begin(),
1193  access_end = accesses.end();
1194  for (; access_iter != access_end; ++access_iter) {
1195  int32 command_index = access_iter->command_index;
1196  const NnetComputation::Command &command = computation_.commands[
1197  command_index];
1198  if (!(command.command_type == kSetConst &&
1199  command.alpha == 0.0)) { // if it's not a zeroing command..
1200  ans = std::min(ans, command_index);
1201  break; // break from access_iter loop (an optimization)
1202  }
1203  }
1204  }
1205  return ans;
1206 }
kaldi::int32 int32
std::vector< Command > commands
std::vector< SubMatrixInfo > submatrices
std::vector< std::vector< Access > > variable_accesses
Definition: nnet-analyze.h:297
const NnetComputation & computation_
Definition: nnet-analyze.h:363
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void AppendVariablesForSubmatrix(int32 submatrix_index, std::vector< int32 > *variable_indexes) const
ComputationVariables variables
Definition: nnet-analyze.h:295

◆ FirstNontrivialMatrixAccess()

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 1226 of file nnet-analyze.cc.

References NnetComputation::Command::alpha, NnetComputation::Command::command_type, NnetComputation::commands, ComputationChecker::computation_, KALDI_ASSERT, kaldi::nnet3::kSetConst, and NnetComputation::matrices.

Referenced by VariableMergingOptimizer::DoMerge().

1226  {
1227  KALDI_ASSERT(static_cast<size_t>(m) < computation_.matrices.size() && m > 0);
1228  int32 ans = computation_.commands.size();
1229  const std::vector<Access> &accesses =
1230  analyzer_.matrix_accesses[m].accesses;
1231  std::vector<Access>::const_iterator access_iter = accesses.begin(),
1232  access_end = accesses.end();
1233  for (; access_iter != access_end; ++access_iter) {
1234  int32 command_index = access_iter->command_index;
1235  const NnetComputation::Command command =
1236  computation_.commands[command_index];
1237  if (!(command.command_type == kSetConst &&
1238  command.alpha == 0.0)) { // except for zeroing commands..
1239  ans = std::min(ans, command_index);
1240  break; // break from access_iter loop (an optimization; note, the
1241  // list 'accesses' is sorted.)
1242  }
1243  }
1244  return ans;
1245 }
kaldi::int32 int32
std::vector< MatrixInfo > matrices
std::vector< Command > commands
const NnetComputation & computation_
Definition: nnet-analyze.h:363
std::vector< MatrixAccesses > matrix_accesses
Definition: nnet-analyze.h:298
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ LastAccess()

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 1264 of file nnet-analyze.cc.

References NnetComputation::commands, ComputationChecker::computation_, KALDI_ASSERT, kaldi::nnet3::kDeallocMatrix, and NnetComputation::submatrices.

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

1264  {
1265  KALDI_ASSERT(static_cast<size_t>(s) < computation_.submatrices.size() && s>0);
1266  int32 ans = -1;
1267  std::vector<int32> variable_indexes;
1268  analyzer_.variables.AppendVariablesForSubmatrix(s, &variable_indexes);
1269  std::vector<int32>::const_iterator iter = variable_indexes.begin(),
1270  end = variable_indexes.end();
1271  for (; iter != end; ++iter) {
1272  int32 v = *iter;
1273  const std::vector<Access> &accesses = analyzer_.variable_accesses[v];
1274  // Go through the variable accesses in reverse order (of command index)
1275  std::vector<Access>::const_reverse_iterator access_iter = accesses.rbegin(),
1276  access_end = accesses.rend();
1277  for (; access_iter != access_end; ++access_iter) {
1278  int32 command_index = access_iter->command_index;
1279  CommandType command_type =
1280  computation_.commands[command_index].command_type;
1281  // deallocation command should not be listed here.
1282  KALDI_ASSERT(command_type != kDeallocMatrix);
1283  ans = std::max(ans, command_index);
1284  break; // break from access_iter loop (an optimization)
1285  }
1286  }
1287  return ans;
1288 }
CommandType
CommandType is an enum that describes the category of the command used in the NnetComputation.
kaldi::int32 int32
std::vector< Command > commands
std::vector< SubMatrixInfo > submatrices
std::vector< std::vector< Access > > variable_accesses
Definition: nnet-analyze.h:297
const NnetComputation & computation_
Definition: nnet-analyze.h:363
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void AppendVariablesForSubmatrix(int32 submatrix_index, std::vector< int32 > *variable_indexes) const
ComputationVariables variables
Definition: nnet-analyze.h:295

◆ LastMatrixAccess()

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 1248 of file nnet-analyze.cc.

References ComputationChecker::computation_, KALDI_ASSERT, and NnetComputation::matrices.

1248  {
1249  KALDI_ASSERT(static_cast<size_t>(m) < computation_.matrices.size() && m > 0);
1250  int32 ans = -1;
1251  const std::vector<Access> &accesses =
1252  analyzer_.matrix_accesses[m].accesses;
1253  std::vector<Access>::const_reverse_iterator access_iter = accesses.rbegin(),
1254  access_end = accesses.rend();
1255  for (; access_iter != access_end; ++access_iter) {
1256  int32 command_index = access_iter->command_index;
1257  ans = std::max(ans, command_index);
1258  break; // break from access_iter loop (an optimization)
1259  }
1260  return ans;
1261 }
kaldi::int32 int32
std::vector< MatrixInfo > matrices
const NnetComputation & computation_
Definition: nnet-analyze.h:363
std::vector< MatrixAccesses > matrix_accesses
Definition: nnet-analyze.h:298
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ LastWriteAccess()

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 1291 of file nnet-analyze.cc.

References NnetComputation::commands, ComputationChecker::computation_, KALDI_ASSERT, kaldi::nnet3::kDeallocMatrix, kaldi::nnet3::kReadAccess, and NnetComputation::submatrices.

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

1291  {
1292  KALDI_ASSERT(static_cast<size_t>(s) < computation_.submatrices.size() && s>0);
1293  int32 matrix_index = computation_.submatrices[s].matrix_index;
1294  if (analyzer_.matrix_accesses[matrix_index].is_output)
1295  return computation_.commands.size();
1296  int32 ans = -1;
1297  std::vector<int32> variable_indexes;
1298  analyzer_.variables.AppendVariablesForSubmatrix(s, &variable_indexes);
1299  std::vector<int32>::const_iterator iter = variable_indexes.begin(),
1300  end = variable_indexes.end();
1301  for (; iter != end; ++iter) {
1302  int32 v = *iter;
1303  const std::vector<Access> &accesses = analyzer_.variable_accesses[v];
1304  // Go through the variable accesses in reverse order (of command index)
1305  std::vector<Access>::const_reverse_iterator access_iter = accesses.rbegin(),
1306  access_end = accesses.rend();
1307  for (; access_iter != access_end; ++access_iter) {
1308  int32 command_index = access_iter->command_index;
1309  CommandType command_type =
1310  computation_.commands[command_index].command_type;
1311  // deallocation command should not be listed here.
1312  KALDI_ASSERT(command_type != kDeallocMatrix);
1313  if (access_iter->access_type != kReadAccess) {
1314  // If this operation is of type kWriteAccess or kReadWriteAccess
1315  ans = std::max(ans, command_index);
1316  break; // break from access_iter loop (an optimization)
1317  }
1318  }
1319  }
1320  return ans;
1321 }
CommandType
CommandType is an enum that describes the category of the command used in the NnetComputation.
kaldi::int32 int32
std::vector< Command > commands
std::vector< SubMatrixInfo > submatrices
std::vector< std::vector< Access > > variable_accesses
Definition: nnet-analyze.h:297
const NnetComputation & computation_
Definition: nnet-analyze.h:363
std::vector< MatrixAccesses > matrix_accesses
Definition: nnet-analyze.h:298
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void AppendVariablesForSubmatrix(int32 submatrix_index, std::vector< int32 > *variable_indexes) const
ComputationVariables variables
Definition: nnet-analyze.h:295

Member Data Documentation

◆ analyzer_

const Analyzer& analyzer_
private

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

◆ computation_

const NnetComputation& computation_
private

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


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