This class relates the matrices and sub-matrices in the computation to imaginary "variables", such that we can think of the operations as operating on sets of individual variables, and we can then do analysis that lets us do optimization. More...
#include <nnet-analyze.h>
Public Member Functions | |
void | Init (const NnetComputation &computation) |
void | RecordAccessForSubmatrix (int32 submatrix_index, AccessType access_type, CommandAttributes *ca) const |
void | AppendVariablesForMatrix (int32 matrix_index, std::vector< int32 > *variable_indexes) const |
Appends to variables_indexes the sorted list of variables corresponding to a matrix index. More... | |
void | AppendVariablesForSubmatrix (int32 submatrix_index, std::vector< int32 > *variable_indexes) const |
int32 | NumVariables () const |
int32 | GetMatrixForVariable (int32 variable) const |
std::string | DescribeVariable (int32 variable) const |
NnetComputation::SubMatrixInfo | VariableInfo (int32 variable) const |
Private Member Functions | |
void | ComputeSplitPoints (const NnetComputation &computation) |
void | ComputeVariablesForSubmatrix (const NnetComputation &computation) |
void | ComputeVariableToMatrix () |
Static Private Member Functions | |
static int32 | FindIndexOf (const std::vector< int32 > &sorted_vec, int32 i) |
Private Attributes | |
std::vector< std::vector< int32 > > | column_split_points_ |
std::vector< std::vector< int32 > > | row_split_points_ |
std::vector< int32 > | matrix_to_variable_index_ |
std::vector< int32 > | submatrix_to_matrix_ |
std::vector< bool > | submatrix_is_whole_matrix_ |
std::vector< int32 > | variable_to_matrix_ |
int32 | num_variables_ |
std::vector< std::vector< int32 > > | variables_for_submatrix_ |
This class relates the matrices and sub-matrices in the computation to imaginary "variables", such that we can think of the operations as operating on sets of individual variables, and we can then do analysis that lets us do optimization.
In principle it might make sense to have those variables correspond to the elements of the matrices, but that would be very inefficient. On the other hand we could do a coarse-grained analysis making the variables correspond to the matrices, but that would cause the resulting analysis to be inaccurate.
What we do instead, which is accurate enough in the cases we envisage, is to make the variables correspond to the most specific row and column ranges in the matrices that we ever access. We do this as follows: for each matrix in the computation we get a list of all the "split points" at which the row and column ranges respectively ever start and end, and define a split_point_index as the index into the array. The variable could be defined as the triple (matrix_index, row_split_point_index, column_split_point_index), but we map it to a single integer index called variable_index. This is a zero-based index formed by listing all the existing variables iterating first over the matrix index, then the row split-point-index, then the column split-point-index. In the end, if we know the matrix-index, the row-split-point-index and the column-split-point-index, we can compute the variable-index using the expression variable-index = matrix_to_variable_index_[matrix-index] + row-split-point-index * num-column-variables-for-this-matrix + column-split-point-index where in code, num-column-variables-for-this-matrix equals column_split_points_[matrix-index].size()-1. The array matrix_to_variable_index_ is a precomputed array telling us at which variable index the variables for any given matrix begin.
Each sub-matrix in the computation will now correspond to a list of variables, and because these lists are always a contiguous range we can just store the row and column split-points corresponding to the start and end of the submatrix. In addition we note, for each submatrix, whether it spans the entirety of the underlying matrix. The reason we need to know this is that a write operation to just part of a matrix would have to be classed as a read-write operation on the underlying matrix because the final contents after the operation would in that case depend on the original contents.
Definition at line 121 of file nnet-analyze.h.
Appends to variables_indexes the sorted list of variables corresponding to a matrix index.
Definition at line 156 of file nnet-analyze.cc.
References KALDI_ASSERT, and ComputationVariables::matrix_to_variable_index_.
Referenced by kaldi::nnet3::RemoveUnnecessaryZeroing().
void AppendVariablesForSubmatrix | ( | int32 | submatrix_index, |
std::vector< int32 > * | variable_indexes | ||
) | const |
Definition at line 146 of file nnet-analyze.cc.
References KALDI_ASSERT, and ComputationVariables::variables_for_submatrix_.
Referenced by DerivativeTimeLimiter::CanLimitMatrix(), VariableMergingOptimizer::MarkAsDirty(), VariableMergingOptimizer::MayBeMerged(), and ComputationVariables::RecordAccessForSubmatrix().
|
private |
Definition at line 25 of file nnet-analyze.cc.
References NnetComputation::SubMatrixInfo::col_offset, ComputationVariables::column_split_points_, KALDI_ASSERT, NnetComputation::matrices, NnetComputation::SubMatrixInfo::matrix_index, ComputationVariables::matrix_to_variable_index_, NnetComputation::SubMatrixInfo::num_cols, NnetComputation::SubMatrixInfo::num_rows, ComputationVariables::num_variables_, NnetComputation::SubMatrixInfo::row_offset, ComputationVariables::row_split_points_, kaldi::SortAndUniq(), and NnetComputation::submatrices.
Referenced by ComputationVariables::Init().
|
private |
Definition at line 81 of file nnet-analyze.cc.
References NnetComputation::SubMatrixInfo::col_offset, ComputationVariables::column_split_points_, ComputationVariables::FindIndexOf(), KALDI_ASSERT, NnetComputation::SubMatrixInfo::matrix_index, ComputationVariables::matrix_to_variable_index_, NnetComputation::SubMatrixInfo::num_cols, NnetComputation::SubMatrixInfo::num_rows, NnetComputation::SubMatrixInfo::row_offset, ComputationVariables::row_split_points_, NnetComputation::submatrices, ComputationVariables::submatrix_is_whole_matrix_, ComputationVariables::submatrix_to_matrix_, and ComputationVariables::variables_for_submatrix_.
Referenced by ComputationVariables::Init().
|
private |
Definition at line 121 of file nnet-analyze.cc.
References rnnlm::i, ComputationVariables::matrix_to_variable_index_, ComputationVariables::NumVariables(), and ComputationVariables::variable_to_matrix_.
Referenced by ComputationVariables::Init().
std::string DescribeVariable | ( | int32 | variable | ) | const |
Definition at line 208 of file nnet-analyze.cc.
References ComputationVariables::column_split_points_, KALDI_ASSERT, ComputationVariables::matrix_to_variable_index_, ComputationVariables::num_variables_, ComputationVariables::row_split_points_, and ComputationVariables::variable_to_matrix_.
Referenced by ComputationChecker::CheckComputationRewrite(), and ComputationChecker::CheckComputationUndefined().
Definition at line 73 of file nnet-analyze.cc.
References rnnlm::i, and KALDI_ASSERT.
Referenced by ComputationVariables::ComputeVariablesForSubmatrix().
Definition at line 141 of file nnet-analyze.cc.
References KALDI_ASSERT, and ComputationVariables::variable_to_matrix_.
void Init | ( | const NnetComputation & | computation | ) |
Definition at line 133 of file nnet-analyze.cc.
References ComputationVariables::ComputeSplitPoints(), ComputationVariables::ComputeVariablesForSubmatrix(), ComputationVariables::ComputeVariableToMatrix(), KALDI_ASSERT, and ComputationVariables::row_split_points_.
Referenced by NnetComputer::Init(), and kaldi::nnet3::MoveSizingCommands().
|
inline |
Definition at line 155 of file nnet-analyze.h.
References rnnlm::i.
Referenced by kaldi::nnet3::ComputeVariableAccesses(), ComputationVariables::ComputeVariableToMatrix(), and VariableMergingOptimizer::VariableMergingOptimizer().
void RecordAccessForSubmatrix | ( | int32 | submatrix_index, |
AccessType | access_type, | ||
CommandAttributes * | ca | ||
) | const |
Definition at line 168 of file nnet-analyze.cc.
References ComputationVariables::AppendVariablesForSubmatrix(), KALDI_ASSERT, kaldi::nnet3::kReadAccess, kaldi::nnet3::kReadWriteAccess, kaldi::nnet3::kWriteAccess, CommandAttributes::matrices_read, CommandAttributes::matrices_written, CommandAttributes::submatrices_read, CommandAttributes::submatrices_written, ComputationVariables::submatrix_is_whole_matrix_, ComputationVariables::submatrix_to_matrix_, CommandAttributes::variables_read, and CommandAttributes::variables_written.
Referenced by kaldi::nnet3::ComputeCommandAttributes().
NnetComputation::SubMatrixInfo VariableInfo | ( | int32 | variable | ) | const |
Definition at line 241 of file nnet-analyze.cc.
References ComputationVariables::column_split_points_, KALDI_ASSERT, ComputationVariables::matrix_to_variable_index_, ComputationVariables::num_variables_, ComputationVariables::row_split_points_, and ComputationVariables::variable_to_matrix_.
Referenced by ComputationChecker::CheckComputationUndefined().
|
private |
Definition at line 185 of file nnet-analyze.h.
Referenced by ComputationVariables::ComputeSplitPoints(), ComputationVariables::ComputeVariablesForSubmatrix(), ComputationVariables::DescribeVariable(), and ComputationVariables::VariableInfo().
|
private |
Definition at line 197 of file nnet-analyze.h.
Referenced by ComputationVariables::AppendVariablesForMatrix(), ComputationVariables::ComputeSplitPoints(), ComputationVariables::ComputeVariablesForSubmatrix(), ComputationVariables::ComputeVariableToMatrix(), ComputationVariables::DescribeVariable(), and ComputationVariables::VariableInfo().
|
private |
Definition at line 209 of file nnet-analyze.h.
Referenced by ComputationVariables::ComputeSplitPoints(), ComputationVariables::DescribeVariable(), and ComputationVariables::VariableInfo().
|
private |
Definition at line 188 of file nnet-analyze.h.
Referenced by ComputationVariables::ComputeSplitPoints(), ComputationVariables::ComputeVariablesForSubmatrix(), ComputationVariables::DescribeVariable(), ComputationVariables::Init(), and ComputationVariables::VariableInfo().
|
private |
Definition at line 203 of file nnet-analyze.h.
Referenced by ComputationVariables::ComputeVariablesForSubmatrix(), and ComputationVariables::RecordAccessForSubmatrix().
|
private |
Definition at line 199 of file nnet-analyze.h.
Referenced by ComputationVariables::ComputeVariablesForSubmatrix(), and ComputationVariables::RecordAccessForSubmatrix().
|
private |
Definition at line 207 of file nnet-analyze.h.
Referenced by ComputationVariables::ComputeVariableToMatrix(), ComputationVariables::DescribeVariable(), ComputationVariables::GetMatrixForVariable(), and ComputationVariables::VariableInfo().
|
private |
Definition at line 213 of file nnet-analyze.h.
Referenced by ComputationVariables::AppendVariablesForSubmatrix(), and ComputationVariables::ComputeVariablesForSubmatrix().