MatrixExtender Class Reference
Collaboration diagram for MatrixExtender:

Public Types

typedef NnetComputation::SubMatrixInfo SubMatrixInfo
 
typedef NnetComputation::MatrixInfo MatrixInfo
 

Public Member Functions

 MatrixExtender (NnetComputation *computation)
 
void ExtendMatrices ()
 

Private Member Functions

bool CanBeExtended (int32 dest_submatrix_index, int32 src_submatrix_index)
 
void Extend (int32 *dest_submatrix_index, int32 *src_submatrix_index)
 
void FixComputation ()
 
void FixDebugInfo ()
 

Private Attributes

BaseFloat min_proportion_
 
NnetComputationcomputation_
 
std::vector< int32orig_num_rows_
 
std::vector< boolis_input_or_output_
 

Detailed Description

Definition at line 1019 of file nnet-optimize-utils.cc.

Member Typedef Documentation

◆ MatrixInfo

◆ SubMatrixInfo

Constructor & Destructor Documentation

◆ MatrixExtender()

MatrixExtender ( NnetComputation computation)

Definition at line 1067 of file nnet-optimize-utils.cc.

References NnetComputation::Command::arg1, NnetComputation::Command::command_type, NnetComputation::commands, MatrixExtender::computation_, MatrixExtender::is_input_or_output_, kaldi::nnet3::kAcceptInput, KALDI_ASSERT, kaldi::nnet3::kProvideOutput, kaldi::nnet3::kSwapMatrix, NnetComputation::matrices, MatrixExtender::orig_num_rows_, and NnetComputation::submatrices.

1067  :
1068  min_proportion_(0.8),
1069  computation_(computation) {
1070  int32 num_matrices = computation_->matrices.size();
1071 
1072  { // set up orig_num_rows_.
1073  orig_num_rows_.resize(num_matrices);
1074  // matrix 0 is not a real matrix so skip that index.
1075  for (int32 m = 1; m < num_matrices; m++)
1076  orig_num_rows_[m] = computation_->matrices[m].num_rows;
1077  }
1078  { // set up is_input_or_output_.
1079  is_input_or_output_.resize(num_matrices, false);
1080  std::vector<NnetComputation::Command>::iterator
1081  command_iter = computation_->commands.begin(),
1082  command_end = computation_->commands.end();
1083  for (; command_iter != command_end; ++command_iter) {
1084  const NnetComputation::Command &command = *command_iter;
1085  // make sure there are no kSwapMatrix commands; they should not be present
1086  // at this stage of optimization.
1087  KALDI_ASSERT(command.command_type != kSwapMatrix);
1088  if (command.command_type == kProvideOutput ||
1089  command.command_type == kAcceptInput) {
1090  int32 s = command.arg1,
1091  m = computation_->submatrices[s].matrix_index;
1092  is_input_or_output_[m] = true;
1093  }
1094  }
1095  }
1096 }
kaldi::int32 int32
std::vector< MatrixInfo > matrices
std::vector< Command > commands
std::vector< bool > is_input_or_output_
std::vector< SubMatrixInfo > submatrices
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

Member Function Documentation

◆ CanBeExtended()

bool CanBeExtended ( int32  dest_submatrix_index,
int32  src_submatrix_index 
)
private

Definition at line 1099 of file nnet-optimize-utils.cc.

References NnetComputation::SubMatrixInfo::col_offset, MatrixExtender::computation_, MatrixExtender::is_input_or_output_, NnetComputation::matrices, NnetComputation::SubMatrixInfo::matrix_index, MatrixExtender::min_proportion_, NnetComputation::MatrixInfo::num_cols, NnetComputation::SubMatrixInfo::num_cols, NnetComputation::MatrixInfo::num_rows, NnetComputation::SubMatrixInfo::num_rows, MatrixExtender::orig_num_rows_, NnetComputation::SubMatrixInfo::row_offset, and NnetComputation::submatrices.

Referenced by MatrixExtender::ExtendMatrices().

1100  {
1101  const SubMatrixInfo
1102  &src_submatrix = computation_->submatrices[src_submatrix_index],
1103  &dest_submatrix = computation_->submatrices[dest_submatrix_index];
1104  if (src_submatrix.matrix_index == dest_submatrix.matrix_index)
1105  return false;
1106 
1107  // we can't resize the destination matrix if it's involved in input or output.
1108  if (is_input_or_output_[dest_submatrix.matrix_index])
1109  return false;
1110 
1111  const MatrixInfo
1112  &src_matrix = computation_->matrices[src_submatrix.matrix_index];
1113 
1114  int32 dest_matrix_orig_num_rows = orig_num_rows_[dest_submatrix.matrix_index],
1115  src_matrix_orig_num_rows = orig_num_rows_[src_submatrix.matrix_index];
1116 
1117  if (src_submatrix.num_rows < min_proportion_ * src_matrix_orig_num_rows)
1118  return false;
1119 
1120  // The following checks that the source submatrix covers be all of the
1121  // source matrix except a few final rows, and the destination submatrix goes
1122  // to the final row of its matrix.
1123  return (src_submatrix.col_offset == 0 &&
1124  src_submatrix.num_cols == src_matrix.num_cols &&
1125  src_submatrix.row_offset == 0 &&
1126  src_submatrix.num_rows < src_matrix.num_rows &&
1127  dest_submatrix.row_offset + dest_submatrix.num_rows ==
1128  dest_matrix_orig_num_rows);
1129 }
kaldi::int32 int32
std::vector< MatrixInfo > matrices
std::vector< bool > is_input_or_output_
std::vector< SubMatrixInfo > submatrices
NnetComputation::SubMatrixInfo SubMatrixInfo
NnetComputation::MatrixInfo MatrixInfo

◆ Extend()

void Extend ( int32 dest_submatrix_index,
int32 src_submatrix_index 
)
private

Definition at line 1132 of file nnet-optimize-utils.cc.

References MatrixExtender::computation_, NnetComputation::matrices, NnetComputation::SubMatrixInfo::matrix_index, NnetComputation::MatrixInfo::num_cols, NnetComputation::MatrixInfo::num_rows, and NnetComputation::submatrices.

Referenced by MatrixExtender::ExtendMatrices().

1133  {
1134  // copy the SubMatrixInfo to avoid iterator invalidation.
1136  src_submatrix = computation_->submatrices[*src_submatrix_index],
1137  dest_submatrix = computation_->submatrices[*dest_submatrix_index];
1138 
1139  MatrixInfo &src_matrix = computation_->matrices[src_submatrix.matrix_index],
1140  &dest_matrix = computation_->matrices[dest_submatrix.matrix_index];
1141 
1142  int32 new_dest_num_rows = dest_submatrix.row_offset + src_matrix.num_rows;
1143 
1144  // extend the destination matrix so it has enough rows to fit the entire
1145  // source matrix. Note: doing this will break certain invariances in the
1146  // computation, principally with allocation and deallocation commands, which
1147  // we'll later fix up by calling FixComputation().
1148  if (new_dest_num_rows > dest_matrix.num_rows) {
1149  dest_matrix.num_rows = new_dest_num_rows;
1150  // make sure there's a submatrix index covering the whole of the dest matrix.
1151  computation_->submatrices.push_back(
1152  SubMatrixInfo(dest_submatrix.matrix_index, 0, new_dest_num_rows,
1153  0, dest_matrix.num_cols));
1154  }
1155 
1156  // The following 3 statements create a new submatrix that will be
1157  // the destination submatrix; it's the same as the original destination
1158  // submatrix, but with a few extra rows.
1159  *dest_submatrix_index = computation_->submatrices.size();
1160  dest_submatrix.num_rows = src_matrix.num_rows;
1161  computation_->submatrices.push_back(
1162  SubMatrixInfo(dest_submatrix));
1163 
1164  // The following 3 statements create a new submatrix that will be
1165  // the source submatrix; it's the same as the original source
1166  // submatrix, but with a few extra rows, and actually will cover
1167  // the entire source matrix.
1168  *src_submatrix_index = computation_->submatrices.size();
1169  computation_->submatrices.push_back(
1170  SubMatrixInfo(src_submatrix.matrix_index, 0, src_matrix.num_rows,
1171  0, src_matrix.num_cols));
1172 }
kaldi::int32 int32
std::vector< MatrixInfo > matrices
std::vector< SubMatrixInfo > submatrices
NnetComputation::SubMatrixInfo SubMatrixInfo
NnetComputation::MatrixInfo MatrixInfo

◆ ExtendMatrices()

void ExtendMatrices ( )

Definition at line 1174 of file nnet-optimize-utils.cc.

References NnetComputation::Command::alpha, NnetComputation::Command::arg1, NnetComputation::Command::arg2, MatrixExtender::CanBeExtended(), NnetComputation::Command::command_type, NnetComputation::commands, MatrixExtender::computation_, MatrixExtender::Extend(), MatrixExtender::FixComputation(), and kaldi::nnet3::kMatrixCopy.

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

1174  {
1175  std::vector<NnetComputation::Command>::iterator
1176  command_iter = computation_->commands.begin(),
1177  command_end = computation_->commands.end();
1178  bool changed = false;
1179  for (; command_iter != command_end; ++command_iter) {
1180  NnetComputation::Command &command = *command_iter;
1181  if (command.command_type == kMatrixCopy &&
1182  command.alpha == 1.0) {
1183  int32 dest_submatrix_index = command.arg1,
1184  src_submatrix_index = command.arg2;
1185  if (CanBeExtended(dest_submatrix_index, src_submatrix_index)) {
1186  Extend(&command.arg1, &command.arg2);
1187  changed = true;
1188  }
1189  }
1190  }
1191  if (changed)
1192  FixComputation();
1193 }
void Extend(int32 *dest_submatrix_index, int32 *src_submatrix_index)
kaldi::int32 int32
std::vector< Command > commands
bool CanBeExtended(int32 dest_submatrix_index, int32 src_submatrix_index)

◆ FixComputation()

void FixComputation ( )
private

Definition at line 1195 of file nnet-optimize-utils.cc.

References NnetComputation::Command::alpha, NnetComputation::Command::arg1, NnetComputation::SubMatrixInfo::col_offset, NnetComputation::Command::command_type, NnetComputation::commands, MatrixExtender::computation_, MatrixExtender::FixDebugInfo(), NnetComputation::GetWholeSubmatrices(), KALDI_ASSERT, kaldi::nnet3::kAllocMatrix, kaldi::nnet3::kDeallocMatrix, kaldi::nnet3::kSetConst, NnetComputation::matrices, NnetComputation::matrix_debug_info, NnetComputation::SubMatrixInfo::matrix_index, NnetComputation::MatrixInfo::num_cols, NnetComputation::SubMatrixInfo::num_cols, NnetComputation::SubMatrixInfo::num_rows, MatrixExtender::orig_num_rows_, kaldi::nnet3::RenumberComputation(), NnetComputation::SubMatrixInfo::row_offset, and NnetComputation::submatrices.

Referenced by MatrixExtender::ExtendMatrices().

1195  {
1196  // make sure that allocation and deallocation commands
1197  // operate on whole matrix.
1198  std::vector<NnetComputation::Command>::iterator
1199  command_iter = computation_->commands.begin(),
1200  command_end = computation_->commands.end();
1201  std::vector<int32> whole_submatrices;
1202  computation_->GetWholeSubmatrices(&whole_submatrices);
1203  for (; command_iter != command_end; ++command_iter) {
1204  NnetComputation::Command &command = *command_iter;
1205  if (command.command_type == kAllocMatrix ||
1206  command.command_type == kDeallocMatrix) {
1207  int32 s = command.arg1,
1208  m = computation_->submatrices[s].matrix_index,
1209  new_s = whole_submatrices[m];
1210  if (new_s != s) {
1211  KALDI_ASSERT(
1213  orig_num_rows_[m] != computation_->matrices[m].num_rows);
1214  command.arg1 = new_s;
1215  }
1216  }
1217  if (command.command_type == kSetConst && command.alpha == 0.0) {
1218  int32 s = command.arg1,
1219  m = computation_->submatrices[s].matrix_index,
1220  new_s = whole_submatrices[m];
1221  if (new_s != s) {
1222  {
1223  const NnetComputation::SubMatrixInfo &info = computation_->submatrices[
1224  command.arg1];
1225  const NnetComputation::MatrixInfo &mat_info = computation_->matrices[
1226  info.matrix_index];
1227  // If this command wasn't zeroing the the entirety of a matrix,
1228  // (before we extended the matrix), we don't need to extend it.
1229  if (!(info.row_offset == 0 && info.col_offset == 0 &&
1230  info.num_cols == mat_info.num_cols &&
1231  info.num_rows == orig_num_rows_[info.matrix_index]))
1232  continue;
1233  // I know doing this via 'continue' is odd, but it's done this way to
1234  // avoid invalid iterators still being in scope; I think some runtimes
1235  // check for it.
1236  }
1237  command.arg1 = new_s;
1238  }
1239  }
1240  }
1241  if (!computation_->matrix_debug_info.empty())
1242  FixDebugInfo();
1244 }
std::vector< MatrixDebugInfo > matrix_debug_info
void RenumberComputation(NnetComputation *computation)
This function detects submatrices and matrices that are never used (e.g.
kaldi::int32 int32
std::vector< MatrixInfo > matrices
std::vector< Command > commands
void GetWholeSubmatrices(std::vector< int32 > *whole_submatrices) const
std::vector< SubMatrixInfo > submatrices
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ FixDebugInfo()

void FixDebugInfo ( )
private

Definition at line 1246 of file nnet-optimize-utils.cc.

References NnetComputation::MatrixDebugInfo::cindexes, MatrixExtender::computation_, KALDI_ASSERT, kaldi::nnet3::kNoTime, NnetComputation::matrices, and NnetComputation::matrix_debug_info.

Referenced by MatrixExtender::FixComputation().

1246  {
1247  int32 num_matrices = computation_->matrices.size();
1248  // matrix zero is not a 'real' matrix.
1249  for (int32 m = 1; m < num_matrices; m++) {
1250  NnetComputation::MatrixDebugInfo &debug_info =
1252  int32 new_num_rows = computation_->matrices[m].num_rows,
1253  old_num_rows = debug_info.cindexes.size();
1254  if (new_num_rows != old_num_rows) {
1255  debug_info.cindexes.resize(new_num_rows);
1256  int32 num_extra_rows = new_num_rows - old_num_rows;
1257  // the following should be true because min_proportion_ > 0.5.
1258  KALDI_ASSERT(num_extra_rows <= old_num_rows);
1259  for (int32 r = old_num_rows; r < new_num_rows; r++) {
1260  Cindex cindex = debug_info.cindexes[r - num_extra_rows];
1261  // set the 't' value to kNoTime which indicates that it's not a 'real'
1262  // time step, and may avoid errors in checking code.
1263  cindex.second.t = kNoTime;
1264  debug_info.cindexes[r] = cindex;
1265  }
1266  }
1267  }
1268 }
std::vector< MatrixDebugInfo > matrix_debug_info
kaldi::int32 int32
std::vector< MatrixInfo > matrices
std::pair< int32, Index > Cindex
Definition: nnet-common.h:115
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
const int kNoTime
Definition: nnet-common.cc:573

Member Data Documentation

◆ computation_

◆ is_input_or_output_

std::vector<bool> is_input_or_output_
private

◆ min_proportion_

BaseFloat min_proportion_
private

Definition at line 1050 of file nnet-optimize-utils.cc.

Referenced by MatrixExtender::CanBeExtended().

◆ orig_num_rows_

std::vector<int32> orig_num_rows_
private

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