ComputationRenumberer Class Reference
Collaboration diagram for ComputationRenumberer:

Classes

struct  PointerCompare
 
struct  SubMatrixHasher
 

Public Member Functions

 ComputationRenumberer (NnetComputation *computation)
 
void Renumber ()
 

Private Member Functions

void RemoveUnusedIndexesMulti ()
 
void ComputeSubmatrixIsUsed ()
 
void ComputeMatrixIsUsed ()
 
void SetUpMappings ()
 
void RenumberSubmatrices ()
 
void RenumberMatrices ()
 
void RemoveIndexesMultiDuplicates ()
 
void RenumberIndexes ()
 
void RenumberIndexesRanges ()
 
void RenumberMemos ()
 

Static Private Member Functions

static void CreateRenumbering (int32 old_num_elements, const std::vector< int32 > &to_remove, std::vector< int32 > *renumbering)
 creates a renumbering that removes the elements in "to_remove", e.g. More...
 
static int32 CreateRenumbering (const std::vector< bool > &used, std::vector< int32 > *renumbering)
 creates a renumbering from old to new index that removes the unused elements, e.g. More...
 

Private Attributes

std::vector< boolsubmatrix_is_used_
 
std::vector< boolsubmatrix_is_kept_
 
std::vector< boolmatrix_is_used_
 
NnetComputationcomputation_
 
int32 num_matrices_new_
 
int32 num_submatrices_new_
 
std::vector< int32old_to_new_matrix_
 
std::vector< int32old_to_new_submatrix_
 

Detailed Description

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

Constructor & Destructor Documentation

◆ ComputationRenumberer()

Member Function Documentation

◆ ComputeMatrixIsUsed()

void ComputeMatrixIsUsed ( )
private

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

References ComputationRenumberer::computation_, NnetComputation::matrices, ComputationRenumberer::matrix_is_used_, NnetComputation::submatrices, and ComputationRenumberer::submatrix_is_used_.

Referenced by ComputationRenumberer::ComputationRenumberer(), and ComputationRenumberer::Renumber().

385  {
386  matrix_is_used_.clear();
387  matrix_is_used_.resize(computation_->matrices.size(), false);
388  matrix_is_used_[0] = true;
389  // We also need to take into account when matrices are used indirectly via
390  // submatrices (which is actually the main way they are accessed).
391  int32 num_submatrices = computation_->submatrices.size();
392  for (int32 s = 1; s < num_submatrices; s++) {
393  int32 matrix_index = computation_->submatrices[s].matrix_index;
394  if (submatrix_is_used_[s])
395  matrix_is_used_[matrix_index] = true;
396  }
397 }
kaldi::int32 int32
std::vector< MatrixInfo > matrices
std::vector< SubMatrixInfo > submatrices

◆ ComputeSubmatrixIsUsed()

void ComputeSubmatrixIsUsed ( )
private

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

References ComputationRenumberer::computation_, kaldi::nnet3::IdentifySubmatrixArgsInComputation(), KALDI_ASSERT, NnetComputation::submatrices, and ComputationRenumberer::submatrix_is_used_.

Referenced by ComputationRenumberer::ComputationRenumberer(), and ComputationRenumberer::Renumber().

361  {
362  int32 num_submatrices = computation_->submatrices.size();
363  submatrix_is_used_.clear();
364  submatrix_is_used_.resize(num_submatrices, false);
365  submatrix_is_used_[0] = true;
366  // the zeroth element of the array is 'special', it refers to the
367  // zero submatrix, and we don't want to renumber it.
368  std::vector<int32*> submatrix_args;
370  std::vector<int32*>::iterator iter = submatrix_args.begin(),
371  end = submatrix_args.end();
372  int32 cur_submatrix_index = -1; // an optimization to avoid too many
373  // indexings of the bool vector
374  // submatrix_is_used_.
375  for (; iter != end; ++iter) {
376  int32 submatrix_index = **iter;
377  if (submatrix_index > 0 && submatrix_index != cur_submatrix_index) {
378  cur_submatrix_index = submatrix_index;
379  KALDI_ASSERT(submatrix_index < num_submatrices);
380  submatrix_is_used_[submatrix_index] = true;
381  }
382  }
383 }
void IdentifySubmatrixArgsInComputation(NnetComputation *computation, std::vector< int32 *> *submatrix_args)
This function outputs to "submatrix_args" the addresses of integers in &#39;computation&#39; that correspond ...
kaldi::int32 int32
std::vector< SubMatrixInfo > submatrices
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ CreateRenumbering() [1/2]

void CreateRenumbering ( int32  old_num_elements,
const std::vector< int32 > &  to_remove,
std::vector< int32 > *  renumbering 
)
staticprivate

creates a renumbering that removes the elements in "to_remove", e.g.

if old_num_elements = 3 and to_remove = [1], would output the vector [ 0, -1, 1 ].

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

References rnnlm::i, kaldi::IsSortedAndUniq(), and KALDI_ASSERT.

Referenced by ComputationRenumberer::PointerCompare< T >::operator()(), ComputationRenumberer::RemoveUnusedIndexesMulti(), and ComputationRenumberer::SetUpMappings().

274  {
275  KALDI_ASSERT(IsSortedAndUniq(to_remove) && old_num_elements > 0);
276  renumbering->clear();
277  renumbering->resize(old_num_elements, 0);
278  int32 num_remove = to_remove.size();
279  for (int32 r = 0; r < num_remove; r++) {
280  int32 this_remove = to_remove[r];
281  // the "> 0" would be ">= 0" in a more generic context, but zero is
282  // not valid in this particular application.
283  KALDI_ASSERT(this_remove > 0 && this_remove < old_num_elements);
284  (*renumbering)[this_remove] = -1;
285  }
286  int32 cur_number = 0;
287  for (int32 i = 0; i < old_num_elements; i++) {
288  if ((*renumbering)[i] != -1)
289  (*renumbering)[i] = cur_number++;
290  }
291  KALDI_ASSERT(cur_number == old_num_elements -
292  static_cast<int32>(to_remove.size()));
293 }
kaldi::int32 int32
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
bool IsSortedAndUniq(const std::vector< T > &vec)
Returns true if the vector is sorted and contains each element only once.
Definition: stl-utils.h:63

◆ CreateRenumbering() [2/2]

int32 CreateRenumbering ( const std::vector< bool > &  used,
std::vector< int32 > *  renumbering 
)
staticprivate

creates a renumbering from old to new index that removes the unused elements, e.g.

if used == [ true, false, true, true], would output the vector [ 0, -1, 1, 2 ]. Returns number of new elements, i.e. the number of elements of 'used' that were true.

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

258  {
259  renumbering->clear();
260  renumbering->reserve(used.size());
261  std::vector<bool>::const_iterator iter = used.begin(), end = used.end();
262  int32 cur_index = 0;
263  for (; iter != end; ++iter) {
264  if (*iter) renumbering->push_back(cur_index++);
265  else renumbering->push_back(-1);
266  }
267  return cur_index;
268 }
kaldi::int32 int32

◆ RemoveIndexesMultiDuplicates()

void RemoveIndexesMultiDuplicates ( )
private

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

References NnetComputation::commands, ComputationRenumberer::computation_, rnnlm::i, kaldi::nnet3::IdentifyIndexesMultiArgs(), and NnetComputation::indexes_multi.

Referenced by ComputationRenumberer::ComputationRenumberer(), and ComputationRenumberer::Renumber().

538  {
539  int32 cur_index = 0,
540  old_indexes_multi_size = computation_->indexes_multi.size();
541  if (old_indexes_multi_size == 0)
542  return;
543  // create index mapping from old to new. the use of map is generally not that
544  // efficient, but the idea here is that we can do most of the comparisons just
545  // based on the size of the vectors, and avoid even visiting most of their
546  // contents.
547  std::vector<int32> indexes_multi_old_to_new(old_indexes_multi_size);
548  typedef std::vector<std::pair<int32,int32> > PairVectorType;
549  typedef std::map<const PairVectorType*, int32,
550  PointerCompare<std::pair<int32,int32> > > MapType;
551  MapType indexes_multi_map;
552  for (int32 i = 0; i < computation_->indexes_multi.size(); i++) {
553  std::pair<MapType::iterator, bool> p =
554  indexes_multi_map.insert(std::pair<const PairVectorType*, int32>(
555  &(computation_->indexes_multi[i]), cur_index));
556  if (p.second) { // was inserted-- was not there already.
557  indexes_multi_old_to_new[i] = cur_index++;
558  } else {
559  int32 index_from_map = p.first->second;
560  indexes_multi_old_to_new[i] = index_from_map;
561  }
562  }
563  if (cur_index == old_indexes_multi_size)
564  return; // An optimization. No duplicates were found.
565  std::vector<PairVectorType> new_indexes_multi(cur_index);
566  for (int32 i = 0; i < old_indexes_multi_size; i++) {
567  int32 new_index = indexes_multi_old_to_new[i];
568  computation_->indexes_multi[i].swap(new_indexes_multi[new_index]);
569  }
570  computation_->indexes_multi.swap(new_indexes_multi);
571 
572  std::vector<int32*> indexes_multi_args;
573  IdentifyIndexesMultiArgs(&(computation_->commands), &indexes_multi_args);
574  std::vector<int32*>::const_iterator iter = indexes_multi_args.begin(),
575  end = indexes_multi_args.end();
576  for (; iter != end; ++iter)
577  **iter = indexes_multi_old_to_new[**iter];
578 }
kaldi::int32 int32
std::vector< Command > commands
std::vector< std::vector< std::pair< int32, int32 > > > indexes_multi
void IdentifyIndexesMultiArgs(std::vector< NnetComputation::Command > *commands, std::vector< int32 *> *indexes_multi_args)
Identifies in the vector of commands, arguments that correspond to indexes into the computation&#39;s ind...

◆ RemoveUnusedIndexesMulti()

void RemoveUnusedIndexesMulti ( )
private

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

References NnetComputation::commands, ComputationRenumberer::computation_, ComputationRenumberer::CreateRenumbering(), rnnlm::i, kaldi::nnet3::IdentifyIndexesMultiArgs(), NnetComputation::indexes_multi, and KALDI_ASSERT.

Referenced by ComputationRenumberer::ComputationRenumberer(), and ComputationRenumberer::Renumber().

502  {
503  int32 num_indexes_multi = computation_->indexes_multi.size();
504  if (num_indexes_multi == 0)
505  return; // Nothing to do. An optimization.
506  std::vector<bool> indexes_multi_used(num_indexes_multi, false);
507  std::vector<int32*> indexes_multi_args;
508  IdentifyIndexesMultiArgs(&(computation_->commands), &indexes_multi_args);
509  std::vector<int32*>::iterator iter = indexes_multi_args.begin(),
510  end = indexes_multi_args.end();
511  for (; iter != end; ++iter) {
512  int32 indexes_multi_index = **iter;
513  KALDI_ASSERT(indexes_multi_index >= 0 &&
514  indexes_multi_index < num_indexes_multi);
515  indexes_multi_used[indexes_multi_index] = 1;
516  }
517  // old->new mapping for the indexes_multi arrays. will remain -1 for
518  // ones that are unused.
519  std::vector<int32> old_to_new(num_indexes_multi, -1);
520  int32 new_num_indexes_multi = CreateRenumbering(indexes_multi_used,
521  &old_to_new);
522  if (new_num_indexes_multi == num_indexes_multi)
523  return; // Nothing to do. An optimization.
524  std::vector<std::vector<std::pair<int32, int32> > >
525  new_indexes_multi(new_num_indexes_multi);
526  for (int32 i = 0; i < num_indexes_multi; i++) {
527  if (old_to_new[i] != -1)
528  new_indexes_multi[old_to_new[i]].swap(computation_->indexes_multi[i]);
529  }
530  computation_->indexes_multi.swap(new_indexes_multi);
531  // renumber within the commands.
532  for (iter = indexes_multi_args.begin(); iter != end; ++iter)
533  **iter = old_to_new[**iter];
534 }
static void CreateRenumbering(int32 old_num_elements, const std::vector< int32 > &to_remove, std::vector< int32 > *renumbering)
creates a renumbering that removes the elements in "to_remove", e.g.
kaldi::int32 int32
std::vector< Command > commands
std::vector< std::vector< std::pair< int32, int32 > > > indexes_multi
void IdentifyIndexesMultiArgs(std::vector< NnetComputation::Command > *commands, std::vector< int32 *> *indexes_multi_args)
Identifies in the vector of commands, arguments that correspond to indexes into the computation&#39;s ind...
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Renumber()

void Renumber ( )

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

References ComputationRenumberer::ComputeMatrixIsUsed(), ComputationRenumberer::ComputeSubmatrixIsUsed(), ComputationRenumberer::RemoveIndexesMultiDuplicates(), ComputationRenumberer::RemoveUnusedIndexesMulti(), ComputationRenumberer::RenumberIndexes(), ComputationRenumberer::RenumberIndexesRanges(), ComputationRenumberer::RenumberMatrices(), ComputationRenumberer::RenumberMemos(), ComputationRenumberer::RenumberSubmatrices(), and ComputationRenumberer::SetUpMappings().

Referenced by ComputationRenumberer::ComputationRenumberer(), and kaldi::nnet3::RenumberComputation().

489  {
493  SetUpMappings();
497  RenumberIndexes();
499  RenumberMemos();
500 }

◆ RenumberIndexes()

void RenumberIndexes ( )
private

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

References NnetComputation::commands, ComputationRenumberer::computation_, rnnlm::i, kaldi::nnet3::IdentifyIndexesArgs(), NnetComputation::indexes, and KALDI_ASSERT.

Referenced by ComputationRenumberer::ComputationRenumberer(), and ComputationRenumberer::Renumber().

581  {
582  int32 old_num_indexes = computation_->indexes.size();
583  if (old_num_indexes == 0)
584  return;
585  std::vector<int32*> indexes_args;
586  IdentifyIndexesArgs(&(computation_->commands), &indexes_args);
587 
588  std::vector<bool> indexes_seen(old_num_indexes, false);
589  std::vector<int32*>::const_iterator iter = indexes_args.begin(),
590  end = indexes_args.end();
591  for (; iter != end; ++iter)
592  indexes_seen[**iter] = true;
593 
594  std::vector<int32> old_to_new_index(old_num_indexes);
595  typedef std::map<const std::vector<int32>*, int32,
596  PointerCompare<int32> > MapType;
597  MapType indexes_map;
598 
599  int32 cur_index = 0;
600  for (int32 i = 0; i < old_num_indexes; i++) {
601  if (!indexes_seen[i]) {
602  old_to_new_index[i] = -1;
603  } else {
604  std::pair<MapType::iterator, bool> p =
605  indexes_map.insert(std::pair<const std::vector<int32>*, int32>(
606  &(computation_->indexes[i]), cur_index));
607  if (p.second) { // was inserted-- was not there already.
608  old_to_new_index[i] = cur_index++;
609  } else {
610  int32 index_from_map = p.first->second;
611  old_to_new_index[i] = index_from_map;
612  }
613  }
614  }
615  if (cur_index == old_num_indexes)
616  return; // An optimization. No changes to the numbering are made.
617  std::vector<std::vector<int32> > new_indexes(cur_index);
618  for (int32 i = 0; i < old_num_indexes; i++) {
619  int32 new_index = old_to_new_index[i];
620  if (new_index != -1)
621  computation_->indexes[i].swap(new_indexes[new_index]);
622  }
623  computation_->indexes.swap(new_indexes);
624 
625  // renumber the indexes inside the commmands.
626  for (iter = indexes_args.begin(); iter != end; ++iter) {
627  int32 old_index = **iter;
628  KALDI_ASSERT(old_index >= 0 && old_index < old_num_indexes);
629  int32 new_index = old_to_new_index[old_index];
630  KALDI_ASSERT(new_index >= 0);
631  **iter = new_index;
632  }
633 }
kaldi::int32 int32
std::vector< Command > commands
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::vector< std::vector< int32 > > indexes
void IdentifyIndexesArgs(std::vector< NnetComputation::Command > *commands, std::vector< int32 *> *indexes_args)
Identifies in the vector of commands, arguments that correspond to indexes into the computation&#39;s &#39;in...

◆ RenumberIndexesRanges()

void RenumberIndexesRanges ( )
private

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

References NnetComputation::commands, ComputationRenumberer::computation_, rnnlm::i, kaldi::nnet3::IdentifyIndexesRangesArgs(), NnetComputation::indexes_ranges, and KALDI_ASSERT.

Referenced by ComputationRenumberer::ComputationRenumberer(), and ComputationRenumberer::Renumber().

635  {
636  int32 old_num_indexes_ranges = computation_->indexes_ranges.size();
637  if (old_num_indexes_ranges == 0)
638  return;
639  std::vector<int32*> indexes_ranges_args;
640  IdentifyIndexesRangesArgs(&(computation_->commands), &indexes_ranges_args);
641 
642  std::vector<bool> is_seen(old_num_indexes_ranges, false);
643  std::vector<int32*>::const_iterator iter = indexes_ranges_args.begin(),
644  end = indexes_ranges_args.end();
645  for (; iter != end; ++iter)
646  is_seen[**iter] = true;
647 
648  std::vector<int32> old_to_new_index(old_num_indexes_ranges);
649  typedef std::map<const std::vector<std::pair<int32, int32> >*, int32,
650  PointerCompare<std::pair<int32, int32> > > MapType;
651  MapType indexes_map;
652  int32 cur_index = 0;
653  for (int32 i = 0; i < old_num_indexes_ranges; i++) {
654  if (!is_seen[i]) {
655  old_to_new_index[i] = -1;
656  } else {
657  std::pair<MapType::iterator, bool> p =
658  indexes_map.insert(
659  std::pair<const std::vector<std::pair<int32, int32> >*, int32>(
660  &(computation_->indexes_ranges[i]), cur_index));
661  if (p.second) { // was inserted-- was not there already.
662  old_to_new_index[i] = cur_index++;
663  } else {
664  int32 index_from_map = p.first->second;
665  old_to_new_index[i] = index_from_map;
666  }
667  }
668  }
669  if (cur_index == old_num_indexes_ranges)
670  return; // An optimization. No changes to the numbering are made.
671  std::vector<std::vector<std::pair<int32, int32> > > new_indexes_ranges(
672  cur_index);
673  for (int32 i = 0; i < old_num_indexes_ranges; i++) {
674  int32 new_index = old_to_new_index[i];
675  if (new_index != -1)
676  computation_->indexes_ranges[i].swap(new_indexes_ranges[new_index]);
677  }
678  computation_->indexes_ranges.swap(new_indexes_ranges);
679 
680  // renumber the indexes inside the commmands.
681  for (iter = indexes_ranges_args.begin(); iter != end; ++iter) {
682  int32 old_index = **iter;
683  KALDI_ASSERT(old_index >= 0 && old_index < old_num_indexes_ranges);
684  int32 new_index = old_to_new_index[old_index];
685  KALDI_ASSERT(new_index >= 0);
686  **iter = new_index;
687  }
688 }
kaldi::int32 int32
std::vector< Command > commands
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void IdentifyIndexesRangesArgs(std::vector< NnetComputation::Command > *commands, std::vector< int32 *> *indexes_ranges_args)
Identifies in the vector of commands, arguments that correspond to indexes into the computation&#39;s &#39;in...
std::vector< std::vector< std::pair< int32, int32 > > > indexes_ranges

◆ RenumberMatrices()

void RenumberMatrices ( )
private

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

References ComputationRenumberer::computation_, KALDI_ASSERT, NnetComputation::matrices, NnetComputation::matrix_debug_info, ComputationRenumberer::matrix_is_used_, ComputationRenumberer::old_to_new_matrix_, and NnetComputation::submatrices.

Referenced by ComputationRenumberer::ComputationRenumberer(), and ComputationRenumberer::Renumber().

454  {
455  std::vector<int32*> matrix_args;
456  int32 num_submatrices = computation_->submatrices.size();
457  for (int32 s = 1; s < num_submatrices; s++) {
458  int32 *matrix_index = &(computation_->submatrices[s].matrix_index);
459  // old_to_new_matrix_[s] for s > 0 is only <= 0 (actually, -1) for
460  // submatrices that are never accessed, and these should never appear
461  // in this list. (presumably because we renumber the submatrices first).
462  int32 new_matrix_index = old_to_new_matrix_[*matrix_index];
463  KALDI_ASSERT(new_matrix_index > 0);
464  *matrix_index = new_matrix_index;
465  }
466 
467  std::vector<NnetComputation::MatrixInfo> new_matrices;
468  int32 num_matrices_old = computation_->matrices.size();
469  new_matrices.reserve(num_matrices_old);
470  for (int32 m = 0; m < num_matrices_old; m++)
471  if (matrix_is_used_[m])
472  new_matrices.push_back(computation_->matrices[m]);
473  computation_->matrices.swap(new_matrices);
474 
475  std::vector<NnetComputation::MatrixDebugInfo> new_debug_info;
476  int32 debug_info_size = computation_->matrix_debug_info.size();
477  KALDI_ASSERT(debug_info_size == 0 || debug_info_size == num_matrices_old);
478  new_debug_info.reserve(debug_info_size);
479  for (int32 m = 0; m < debug_info_size; m++) {
480  if (matrix_is_used_[m]) {
481  new_debug_info.push_back(NnetComputation::MatrixDebugInfo());
482  new_debug_info.back().Swap(&(computation_->matrix_debug_info[m]));
483  }
484  }
485  computation_->matrix_debug_info.swap(new_debug_info);
486 }
std::vector< MatrixDebugInfo > matrix_debug_info
kaldi::int32 int32
std::vector< MatrixInfo > matrices
std::vector< SubMatrixInfo > submatrices
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ RenumberMemos()

void RenumberMemos ( )
private

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

References NnetComputation::Command::arg5, NnetComputation::Command::arg7, NnetComputation::Command::command_type, NnetComputation::commands, ComputationRenumberer::computation_, KALDI_ASSERT, kaldi::nnet3::kBackprop, and kaldi::nnet3::kPropagate.

Referenced by ComputationRenumberer::ComputationRenumberer(), and ComputationRenumberer::Renumber().

296  {
297  // this is indexed by memo-index, and maps to the
298  // (propagate, backprop) commands that use that memo-index, or
299  // (-1, -1) if there are no such commands.
300  std::vector<std::pair<int32, int32> > memo_to_commands;
301  std::vector<int32> memo_indexes_used;
302  std::pair<int32, int32> blank(-1, -1);
303  int32 num_commands = computation_->commands.size();
304  for (int32 c = 0; c < num_commands; c++) {
305  NnetComputation::Command &command = computation_->commands[c];
306  if (command.command_type == kPropagate) {
307  int32 memo_index = command.arg5;
308  if (memo_index > 0) {
309  if (memo_to_commands.size() <= static_cast<size_t>(memo_index))
310  memo_to_commands.resize(memo_index + 1, blank);
311  KALDI_ASSERT(memo_to_commands[memo_index].first == -1);
312  memo_to_commands[memo_index].first = c;
313  memo_indexes_used.push_back(memo_index);
314  }
315  } else if (command.command_type == kBackprop) {
316  int32 memo_index = command.arg7;
317  if (memo_index > 0) {
318  if (memo_to_commands.size() <= static_cast<size_t>(memo_index))
319  memo_to_commands.resize(memo_index + 1, blank);
320  KALDI_ASSERT(memo_to_commands[memo_index].first > 0 &&
321  memo_to_commands[memo_index].second == -1);
322  memo_to_commands[memo_index].second = c;
323  }
324  }
325  }
326  int32 new_memo_index = 1;
327  for (std::vector<int32>::iterator iter = memo_indexes_used.begin();
328  iter != memo_indexes_used.end(); ++iter) {
329  int32 memo_index = *iter;
330  int32 propagate_command = memo_to_commands[memo_index].first,
331  backprop_command = memo_to_commands[memo_index].second;
332  KALDI_ASSERT(backprop_command > 0 &&
333  "Propagate generates memo but backprop doesn't use it.");
334  computation_->commands[propagate_command].arg5 = new_memo_index;
335  computation_->commands[backprop_command].arg7 = new_memo_index;
336  new_memo_index++;
337  }
338 }
kaldi::int32 int32
std::vector< Command > commands
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ RenumberSubmatrices()

void RenumberSubmatrices ( )
private

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

References ComputationRenumberer::computation_, kaldi::nnet3::IdentifySubmatrixArgsInComputation(), KALDI_ASSERT, ComputationRenumberer::old_to_new_submatrix_, NnetComputation::submatrices, and ComputationRenumberer::submatrix_is_kept_.

Referenced by ComputationRenumberer::ComputationRenumberer(), and ComputationRenumberer::Renumber().

428  {
429  std::vector<int32*> submatrix_args;
431  std::vector<int32*>::iterator iter = submatrix_args.begin(),
432  end = submatrix_args.end();
433  for (; iter != end; ++iter) {
434  if (**iter > 0) {
435  int32 new_submatrix_index = old_to_new_submatrix_[**iter];
436  // old_to_new_submatrix_[s] for s > 0 is only <= 0 (actually, -1) for
437  // submatrices that are never accessed, and these should never appear
438  // in this list.
439  KALDI_ASSERT(new_submatrix_index > 0);
440  **iter = new_submatrix_index;
441  }
442  }
443  std::vector<NnetComputation::SubMatrixInfo> new_submatrices;
444  int32 num_submatrices_old = computation_->submatrices.size();
445  new_submatrices.reserve(num_submatrices_old);
446  for (int32 s = 0; s < num_submatrices_old; s++)
447  if (submatrix_is_kept_[s])
448  new_submatrices.push_back(computation_->submatrices[s]);
449  computation_->submatrices.swap(new_submatrices);
450  // We'll map the matrix indexes inside computation_->submatrices
451  // when we call RenumberMatrices().
452 }
void IdentifySubmatrixArgsInComputation(NnetComputation *computation, std::vector< int32 *> *submatrix_args)
This function outputs to "submatrix_args" the addresses of integers in &#39;computation&#39; that correspond ...
kaldi::int32 int32
std::vector< SubMatrixInfo > submatrices
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ SetUpMappings()

void SetUpMappings ( )
private

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

References ComputationRenumberer::computation_, ComputationRenumberer::CreateRenumbering(), ComputationRenumberer::matrix_is_used_, ComputationRenumberer::num_matrices_new_, ComputationRenumberer::num_submatrices_new_, ComputationRenumberer::old_to_new_matrix_, ComputationRenumberer::old_to_new_submatrix_, NnetComputation::submatrices, ComputationRenumberer::submatrix_is_kept_, and ComputationRenumberer::submatrix_is_used_.

Referenced by ComputationRenumberer::ComputationRenumberer(), and ComputationRenumberer::Renumber().

401  {
403 
404  unordered_map<NnetComputation::SubMatrixInfo, int32,
405  SubMatrixHasher> submat_map;
406  int32 cur_index = 1, num_submatrices_orig =
407  computation_->submatrices.size();
408  // the old_to_new_submatrix_ map will remove duplicates.
409  // -1's will appear wherever a particular submatrix was never used.
411  old_to_new_submatrix_.resize(num_submatrices_orig, -1);
412  old_to_new_submatrix_[0] = 0;
413  for (int32 s = 1; s < num_submatrices_orig; s++) {
414  if (submatrix_is_used_[s]) {
415  const NnetComputation::SubMatrixInfo &info =
417  if (submat_map.count(info) > 0) { // a duplicate...
418  old_to_new_submatrix_[s] = submat_map[info];
419  submatrix_is_kept_[s] = false;
420  } else {
421  old_to_new_submatrix_[s] = (submat_map[info] = cur_index++);
422  }
423  }
424  }
425  num_submatrices_new_ = cur_index;
426 }
static void CreateRenumbering(int32 old_num_elements, const std::vector< int32 > &to_remove, std::vector< int32 > *renumbering)
creates a renumbering that removes the elements in "to_remove", e.g.
kaldi::int32 int32
std::vector< SubMatrixInfo > submatrices

Member Data Documentation

◆ computation_

◆ matrix_is_used_

◆ num_matrices_new_

int32 num_matrices_new_
private

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

Referenced by ComputationRenumberer::SetUpMappings().

◆ num_submatrices_new_

int32 num_submatrices_new_
private

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

Referenced by ComputationRenumberer::SetUpMappings().

◆ old_to_new_matrix_

std::vector<int32> old_to_new_matrix_
private

◆ old_to_new_submatrix_

std::vector<int32> old_to_new_submatrix_
private

◆ submatrix_is_kept_

std::vector<bool> submatrix_is_kept_
private

◆ submatrix_is_used_


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