All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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< bool > submatrix_is_used_
 
std::vector< bool > submatrix_is_kept_
 
std::vector< bool > matrix_is_used_
 
NnetComputationcomputation_
 
int32 num_matrices_new_
 
int32 num_submatrices_new_
 
std::vector< int32 > old_to_new_matrix_
 
std::vector< int32 > old_to_new_submatrix_
 

Detailed Description

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

Constructor & Destructor Documentation

ComputationRenumberer ( NnetComputation computation)
inline

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

152  :
153  computation_(computation) { }

Member Function Documentation

void ComputeMatrixIsUsed ( )
private

Definition at line 387 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::Renumber().

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

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

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

Referenced by ComputationRenumberer::Renumber().

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

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

Referenced by ComputationRenumberer::RemoveUnusedIndexesMulti(), and ComputationRenumberer::SetUpMappings().

276  {
277  KALDI_ASSERT(IsSortedAndUniq(to_remove) && old_num_elements > 0);
278  renumbering->clear();
279  renumbering->resize(old_num_elements, 0);
280  int32 num_remove = to_remove.size();
281  for (int32 r = 0; r < num_remove; r++) {
282  int32 this_remove = to_remove[r];
283  // the "> 0" would be ">= 0" in a more generic context, but zero is
284  // not valid in this particular application.
285  KALDI_ASSERT(this_remove > 0 && this_remove < old_num_elements);
286  (*renumbering)[this_remove] = -1;
287  }
288  int32 cur_number = 0;
289  for (int32 i = 0; i < old_num_elements; i++) {
290  if ((*renumbering)[i] != -1)
291  (*renumbering)[i] = cur_number++;
292  }
293  KALDI_ASSERT(cur_number == old_num_elements -
294  static_cast<int32>(to_remove.size()));
295 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
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
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 258 of file nnet-optimize-utils.cc.

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

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

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

Referenced by ComputationRenumberer::Renumber().

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

Definition at line 504 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::Renumber().

504  {
505  int32 num_indexes_multi = computation_->indexes_multi.size();
506  if (num_indexes_multi == 0)
507  return; // Nothing to do. An optimization.
508  std::vector<bool> indexes_multi_used(num_indexes_multi, false);
509  std::vector<int32*> indexes_multi_args;
510  IdentifyIndexesMultiArgs(&(computation_->commands), &indexes_multi_args);
511  std::vector<int32*>::iterator iter = indexes_multi_args.begin(),
512  end = indexes_multi_args.end();
513  for (; iter != end; ++iter) {
514  int32 indexes_multi_index = **iter;
515  KALDI_ASSERT(indexes_multi_index >= 0 &&
516  indexes_multi_index < num_indexes_multi);
517  indexes_multi_used[indexes_multi_index] = 1;
518  }
519  // old->new mapping for the indexes_multi arrays. will remain -1 for
520  // ones that are unused.
521  std::vector<int32> old_to_new(num_indexes_multi, -1);
522  int32 new_num_indexes_multi = CreateRenumbering(indexes_multi_used,
523  &old_to_new);
524  if (new_num_indexes_multi == num_indexes_multi)
525  return; // Nothing to do. An optimization.
526  std::vector<std::vector<std::pair<int32, int32> > >
527  new_indexes_multi(new_num_indexes_multi);
528  for (int32 i = 0; i < num_indexes_multi; i++) {
529  if (old_to_new[i] != -1)
530  new_indexes_multi[old_to_new[i]].swap(computation_->indexes_multi[i]);
531  }
532  computation_->indexes_multi.swap(new_indexes_multi);
533  // renumber within the commands.
534  for (iter = indexes_multi_args.begin(); iter != end; ++iter)
535  **iter = old_to_new[**iter];
536 }
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.
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's ind...
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void Renumber ( )

Definition at line 491 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 kaldi::nnet3::RenumberComputation().

491  {
495  SetUpMappings();
499  RenumberIndexes();
501  RenumberMemos();
502 }
void RenumberIndexes ( )
private

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

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

Referenced by ComputationRenumberer::Renumber().

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

Definition at line 637 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::Renumber().

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

Definition at line 456 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::Renumber().

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

Definition at line 298 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::Renumber().

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

Definition at line 430 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::Renumber().

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

Definition at line 403 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::Renumber().

403  {
405 
406  unordered_map<NnetComputation::SubMatrixInfo, int32,
407  SubMatrixHasher> submat_map;
408  int32 cur_index = 1, num_submatrices_orig =
409  computation_->submatrices.size();
410  // the old_to_new_submatrix_ map will remove duplicates.
411  // -1's will appear wherever a particular submatrix was never used.
413  old_to_new_submatrix_.resize(num_submatrices_orig, -1);
414  old_to_new_submatrix_[0] = 0;
415  for (int32 s = 1; s < num_submatrices_orig; s++) {
416  if (submatrix_is_used_[s]) {
417  const NnetComputation::SubMatrixInfo &info =
419  if (submat_map.count(info) > 0) { // a duplicate...
420  old_to_new_submatrix_[s] = submat_map[info];
421  submatrix_is_kept_[s] = false;
422  } else {
423  old_to_new_submatrix_[s] = (submat_map[info] = cur_index++);
424  }
425  }
426  }
427  num_submatrices_new_ = cur_index;
428 }
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.
std::vector< SubMatrixInfo > submatrices

Member Data Documentation

int32 num_matrices_new_
private

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

Referenced by ComputationRenumberer::SetUpMappings().

int32 num_submatrices_new_
private

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

Referenced by ComputationRenumberer::SetUpMappings().

std::vector<int32> old_to_new_matrix_
private
std::vector<int32> old_to_new_submatrix_
private
std::vector<bool> submatrix_is_kept_
private

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