All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ComputationExpander Class Reference
Collaboration diagram for ComputationExpander:

Public Member Functions

 ComputationExpander (const Nnet &nnet, const MiscComputationInfo &misc_info, const NnetComputation &computation, bool need_debug_info, int32 num_n_values, NnetComputation *expanded_computation)
 
void Expand ()
 

Private Member Functions

void InitStrideInfo ()
 
void ComputeMatrixInfo ()
 
void ComputeDebugInfo ()
 
void ComputeSubmatrixInfo ()
 
void ExpandRowsCommand (const NnetComputation::Command &c_in, NnetComputation::Command *c_out)
 
void ExpandRowsMultiCommand (const NnetComputation::Command &c_in, NnetComputation::Command *c_out)
 
void ExpandRowRangesCommand (const NnetComputation::Command &c_in, NnetComputation::Command *c_out)
 
void ComputePrecomputedIndexes ()
 
void ComputeCommands ()
 
void EnsureDebugInfoExists (int32 submatrix_index)
 
bool GetNewSubmatLocationInfo (int32 submat_index, int32 old_row_index, int32 *new_row_index, int32 *n_stride) const
 
int32 GetNewMatrixLocationInfo (int32 old_matrix_index, int32 old_row_index) const
 This function is used in mapping row-indexes into matrices, from the old to the new computation. More...
 
void ExpandIndexes (const std::vector< Index > &indexes, std::vector< Index > *indexes_expanded) const
 

Private Attributes

std::vector< int32 > n_stride_
 
const Nnetnnet_
 
const MiscComputationInfomisc_info_
 
const NnetComputationcomputation_
 
bool need_debug_info_
 
int32 num_n_values_
 
NnetComputationexpanded_computation_
 

Detailed Description

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

Constructor & Destructor Documentation

ComputationExpander ( const Nnet nnet,
const MiscComputationInfo misc_info,
const NnetComputation computation,
bool  need_debug_info,
int32  num_n_values,
NnetComputation expanded_computation 
)
inline

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

References KALDI_ASSERT.

3148  :
3149  nnet_(nnet), misc_info_(misc_info),
3150  computation_(computation),
3151  need_debug_info_(need_debug_info),
3152  num_n_values_(num_n_values),
3153  expanded_computation_(expanded_computation) {
3154  KALDI_ASSERT(num_n_values > 2);
3155  }
const MiscComputationInfo & misc_info_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169

Member Function Documentation

void ComputeCommands ( )
private

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

References NnetComputation::Command::command_type, NnetComputation::commands, ComputationExpander::computation_, ComputationExpander::expanded_computation_, ComputationExpander::ExpandRowRangesCommand(), ComputationExpander::ExpandRowsCommand(), ComputationExpander::ExpandRowsMultiCommand(), kaldi::nnet3::kAcceptInput, kaldi::nnet3::kAddRowRanges, kaldi::nnet3::kAddRows, kaldi::nnet3::kAddRowsMulti, kaldi::nnet3::kAddToRowsMulti, KALDI_ERR, kaldi::nnet3::kAllocMatrix, kaldi::nnet3::kBackprop, kaldi::nnet3::kBackpropNoModelUpdate, kaldi::nnet3::kCompressMatrix, kaldi::nnet3::kCopyRows, kaldi::nnet3::kCopyRowsMulti, kaldi::nnet3::kCopyToRowsMulti, kaldi::nnet3::kDeallocMatrix, kaldi::nnet3::kDecompressMatrix, kaldi::nnet3::kGotoLabel, kaldi::nnet3::kMatrixAdd, kaldi::nnet3::kMatrixCopy, kaldi::nnet3::kNoOperation, kaldi::nnet3::kNoOperationLabel, kaldi::nnet3::kNoOperationMarker, kaldi::nnet3::kNoOperationPermanent, kaldi::nnet3::kPropagate, kaldi::nnet3::kProvideOutput, kaldi::nnet3::kSetConst, and kaldi::nnet3::kSwapMatrix.

Referenced by ComputationExpander::Expand().

3504  {
3505  int32 num_commands = computation_.commands.size();
3506  expanded_computation_->commands.resize(num_commands);
3507  for (int32 command_index = 0; command_index < num_commands;
3508  command_index++) {
3509  const NnetComputation::Command &c = computation_.commands[command_index];
3510  NnetComputation::Command &c_out =
3511  expanded_computation_->commands[command_index];
3512  c_out = c;
3513  // Commands that only operate on submatrices, components and
3514  // precomputed-indexes do not have to be changed because we'll take care of
3515  // the expansion by suitably redefining the matrices and submatrices, and
3516  // recreating the precomputed-indexes.
3517  // However, commands that require, 'indexes', 'indexes_multi' or
3518  // 'indexes_ranges' do need to be modified.
3519  switch (c.command_type) {
3520  case kAllocMatrix:
3521  case kDeallocMatrix:
3522  case kSetConst:
3523  case kSwapMatrix:
3524  case kPropagate: case kBackprop:
3526  break;
3527  case kCopyRows: case kAddRows:
3528  ExpandRowsCommand(c, &c_out);
3529  break;
3530  case kCopyRowsMulti: case kAddRowsMulti:
3531  case kCopyToRowsMulti: case kAddToRowsMulti:
3532  ExpandRowsMultiCommand(c, &c_out);
3533  break;
3534  case kAddRowRanges:
3535  ExpandRowRangesCommand(c, &c_out);
3536  break;
3538  case kAcceptInput: case kProvideOutput: case kNoOperation:
3540  case kNoOperationLabel: case kGotoLabel:
3541  break;
3542  default:
3543  KALDI_ERR << "Un-handled command type";
3544  }
3545  }
3546 }
void ExpandRowsMultiCommand(const NnetComputation::Command &c_in, NnetComputation::Command *c_out)
void ExpandRowRangesCommand(const NnetComputation::Command &c_in, NnetComputation::Command *c_out)
std::vector< Command > commands
#define KALDI_ERR
Definition: kaldi-error.h:127
void ExpandRowsCommand(const NnetComputation::Command &c_in, NnetComputation::Command *c_out)
void ComputeDebugInfo ( )
private

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

References NnetComputation::MatrixDebugInfo::cindexes, ComputationExpander::computation_, ComputationExpander::expanded_computation_, ComputationExpander::GetNewMatrixLocationInfo(), NnetComputation::MatrixDebugInfo::is_deriv, KALDI_ASSERT, NnetComputation::matrices, NnetComputation::matrix_debug_info, rnnlm::n, ComputationExpander::n_stride_, and ComputationExpander::num_n_values_.

Referenced by ComputationExpander::Expand().

3606  {
3607  int32 num_matrices = computation_.matrices.size();
3608  KALDI_ASSERT(computation_.matrix_debug_info.size() == num_matrices);
3609  expanded_computation_->matrix_debug_info.resize(num_matrices);
3610  // Matrix zero is a special case; it's the empty matrix.
3613  int32 num_n_values = num_n_values_;
3614  for (int32 m = 1; m < num_matrices; m++) {
3615  const NnetComputation::MatrixDebugInfo &info_in =
3617  NnetComputation::MatrixDebugInfo &info_out =
3619  info_out.is_deriv = info_in.is_deriv;
3620  int32 num_rows_in = computation_.matrices[m].num_rows,
3621  num_rows_out = expanded_computation_->matrices[m].num_rows;
3622  KALDI_ASSERT(num_rows_in == info_in.cindexes.size());
3623  info_out.cindexes.resize(num_rows_out);
3624  const Cindex *cindexes_in = &(info_in.cindexes[0]);
3625  Cindex *cindexes_out = &(info_out.cindexes[0]);
3626  for (int32 r = 0; r < num_rows_in; r++) {
3627  if (info_in.cindexes[r].second.n == 0) {
3628  int32 new_r = GetNewMatrixLocationInfo(m, r),
3629  n_stride = n_stride_[m];
3630  for (int32 n = 0; n < num_n_values; n++) {
3631  int32 r_out = new_r + n * n_stride;
3632  cindexes_out[r_out] = cindexes_in[r];
3633  cindexes_out[r_out].second.n = n;
3634  }
3635  }
3636  }
3637  }
3638 }
std::vector< MatrixDebugInfo > matrix_debug_info
std::vector< MatrixInfo > matrices
std::pair< int32, Index > Cindex
Definition: nnet-common.h:115
struct rnnlm::@11::@12 n
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
int32 GetNewMatrixLocationInfo(int32 old_matrix_index, int32 old_row_index) const
This function is used in mapping row-indexes into matrices, from the old to the new computation...
void ComputeMatrixInfo ( )
private

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

References ComputationExpander::computation_, ComputationExpander::expanded_computation_, NnetComputation::matrices, and ComputationExpander::num_n_values_.

Referenced by ComputationExpander::Expand().

3591  {
3592  int32 num_matrices = computation_.matrices.size();
3593  expanded_computation_->matrices.resize(num_matrices);
3594  // Matrix zero is a special case; it's the empty matrix.
3596  int32 old_num_n_values = 2,
3597  new_num_n_values = num_n_values_;
3598  for (int32 m = 1; m < num_matrices; m++) {
3600  expanded_computation_->matrices[m].num_rows =
3601  (computation_.matrices[m].num_rows / old_num_n_values) *
3602  new_num_n_values;
3603  }
3604 }
std::vector< MatrixInfo > matrices
void ComputePrecomputedIndexes ( )
private

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

References NnetComputation::Command::arg1, NnetComputation::Command::arg2, NnetComputation::Command::command_type, NnetComputation::commands, NnetComputation::component_precomputed_indexes, ComputationExpander::computation_, NnetComputation::PrecomputedIndexesInfo::data, ComputationExpander::expanded_computation_, ComputationExpander::ExpandIndexes(), Nnet::GetComponent(), NnetComputation::PrecomputedIndexesInfo::input_indexes, KALDI_ASSERT, kaldi::nnet3::kBackprop, kaldi::nnet3::kBackpropNoModelUpdate, kaldi::nnet3::kPropagate, ComputationExpander::misc_info_, ComputationExpander::nnet_, NnetComputation::PrecomputedIndexesInfo::output_indexes, and Component::PrecomputeIndexes().

Referenced by ComputationExpander::Expand().

3679  {
3680  // for each element of 'component_precomputed_indexes',
3681  // we will try to work out the command-index of the associated
3682  // Propagate() command and of the associated Backprop() command,
3683  // if it exists.
3684  // We expect that each such element will be associated with
3685  // exactly one Propagate() command and at most one Backprop() command.
3686  int32 num_commands = computation_.commands.size(),
3687  num_precomputed_indexes = computation_.component_precomputed_indexes.size();
3688 
3689  std::vector<bool> need_backprop(num_precomputed_indexes, false);
3690 
3691  std::vector<int32> component_index(num_precomputed_indexes, -1);
3692 
3693  for (int32 command_index = 0; command_index < num_commands; command_index++) {
3694  const NnetComputation::Command &c = computation_.commands[command_index];
3695 
3696  if (c.command_type == kPropagate && c.arg2 > 0) {
3697  KALDI_ASSERT(c.arg2 < num_precomputed_indexes);
3698  component_index[c.arg2] = c.arg1;
3699  }
3700  if ((c.command_type == kBackprop ||
3701  c.command_type == kBackpropNoModelUpdate) && c.arg2 > 0) {
3702  KALDI_ASSERT(c.arg2 < num_precomputed_indexes);
3703  need_backprop[c.arg2] = true;
3704  }
3705  }
3706 
3707  for (size_t p = 1;
3709  ++p)
3713  num_precomputed_indexes);
3714 
3715  for (int32 p = 1; p < num_precomputed_indexes; ++p) {
3716  const NnetComputation::PrecomputedIndexesInfo &old_info =
3718  NnetComputation::PrecomputedIndexesInfo &new_info =
3720  KALDI_ASSERT(!old_info.input_indexes.empty() &&
3721  !old_info.output_indexes.empty() &&
3722  "Input/output indexes not present in precomputed info of "
3723  "computation to be expanded.");
3724  // note: we could place these expanded indexes into 'new_info.input_indexes'
3725  // and 'new_info.output_indexes', but we actually don't need to keep them
3726  // there, because they are only required to be kept in computations where
3727  // the n indexes consist of the set (0, 1), and the computation we're
3728  // creating has more distinct n indexes than that.
3729  std::vector<Index> input_indexes, output_indexes;
3730  ExpandIndexes(old_info.input_indexes, &input_indexes);
3731  ExpandIndexes(old_info.output_indexes, &output_indexes);
3732  KALDI_ASSERT(component_index[p] >= 0);
3733  const Component *component = nnet_.GetComponent(component_index[p]);
3734  ComponentPrecomputedIndexes *expanded_precomputed_indexes =
3735  component->PrecomputeIndexes(misc_info_, input_indexes,
3736  output_indexes, need_backprop[p]);
3737  // this object should not be null because it was not NULL the
3738  // last time we generated it from the same component, for the
3739  // same computation.
3740  KALDI_ASSERT(expanded_precomputed_indexes != NULL);
3741  new_info.data = expanded_precomputed_indexes;
3742  }
3743 }
const MiscComputationInfo & misc_info_
void ExpandIndexes(const std::vector< Index > &indexes, std::vector< Index > *indexes_expanded) const
std::vector< Command > commands
Component * GetComponent(int32 c)
Return component indexed c. Not a copy; not owned by caller.
Definition: nnet-nnet.cc:150
virtual ComponentPrecomputedIndexes * PrecomputeIndexes(const MiscComputationInfo &misc_info, const std::vector< Index > &input_indexes, const std::vector< Index > &output_indexes, bool need_backprop) const
This function must return NULL for simple Components.
std::vector< PrecomputedIndexesInfo > component_precomputed_indexes
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void ComputeSubmatrixInfo ( )
private

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

References NnetComputation::MatrixDebugInfo::cindexes, NnetComputation::SubMatrixInfo::col_offset, ComputationExpander::computation_, ComputationExpander::expanded_computation_, ComputationExpander::GetNewMatrixLocationInfo(), NnetComputation::GetSubmatrixStrings(), KALDI_ERR, NnetComputation::matrix_debug_info, NnetComputation::SubMatrixInfo::matrix_index, ComputationExpander::nnet_, NnetComputation::SubMatrixInfo::num_cols, NnetComputation::SubMatrixInfo::num_rows, NnetComputation::Print(), NnetComputation::SubMatrixInfo::row_offset, and NnetComputation::submatrices.

Referenced by ComputationExpander::Expand().

3640  {
3641  int32 num_submatrices = computation_.submatrices.size();
3642  expanded_computation_->submatrices.resize(num_submatrices);
3643  // Sub-matrix zero is a special case; it's the empty submatrix.
3645  for (int32 s = 1; s < num_submatrices; s++) {
3646  const NnetComputation::SubMatrixInfo &info_in = computation_.submatrices[s];
3647  int32 m = info_in.matrix_index;
3648  const NnetComputation::MatrixDebugInfo &debug_info_in =
3650 
3651  // we may need to change the row_offset and num_rows.
3652  int32 first_row_in = info_in.row_offset,
3653  last_row_in = first_row_in + info_in.num_rows - 1;
3654  if (!(debug_info_in.cindexes[first_row_in].second.n == 0 &&
3655  debug_info_in.cindexes[last_row_in].second.n == 1)) {
3656  std::ostringstream computation_ss;
3657  std::vector<std::string> submat_strings;
3658  computation_.GetSubmatrixStrings(nnet_, &submat_strings);
3659  computation_.Print(computation_ss, nnet_);
3660  KALDI_ERR << "Submatrix s" << s << " = " << submat_strings[s]
3661  << " has strange dimensions. Computation is: "
3662  << computation_ss.str();
3663  }
3664 
3665  int32 first_row_out = GetNewMatrixLocationInfo(m, first_row_in),
3666  last_row_out = GetNewMatrixLocationInfo(m, last_row_in),
3667  new_num_rows = (last_row_out + 1 - first_row_out);
3668 
3669  NnetComputation::SubMatrixInfo &info_out =
3671  info_out.matrix_index = m;
3672  info_out.row_offset = first_row_out;
3673  info_out.num_rows = new_num_rows;
3674  info_out.col_offset = info_in.col_offset;
3675  info_out.num_cols = info_in.num_cols;
3676  }
3677 }
std::vector< MatrixDebugInfo > matrix_debug_info
void GetSubmatrixStrings(const Nnet &nnet, std::vector< std::string > *submat_strings) const
std::vector< SubMatrixInfo > submatrices
#define KALDI_ERR
Definition: kaldi-error.h:127
void Print(std::ostream &os, const Nnet &nnet) const
int32 GetNewMatrixLocationInfo(int32 old_matrix_index, int32 old_row_index) const
This function is used in mapping row-indexes into matrices, from the old to the new computation...
void EnsureDebugInfoExists ( int32  submatrix_index)
private
void Expand ( )

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

References ComputationExpander::computation_, ComputationExpander::ComputeCommands(), ComputationExpander::ComputeDebugInfo(), ComputationExpander::ComputeMatrixInfo(), ComputationExpander::ComputePrecomputedIndexes(), ComputationExpander::ComputeSubmatrixInfo(), ComputationExpander::expanded_computation_, ComputationExpander::InitStrideInfo(), NnetComputation::matrix_debug_info, ComputationExpander::need_debug_info_, and NnetComputation::need_model_derivative.

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

3576  {
3577  InitStrideInfo();
3579  if (need_debug_info_)
3580  ComputeDebugInfo();
3581  else
3585  ComputeCommands();
3586 
3589 }
std::vector< MatrixDebugInfo > matrix_debug_info
void ExpandIndexes ( const std::vector< Index > &  indexes,
std::vector< Index > *  indexes_expanded 
) const
private

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

References kaldi::nnet3::ConvertNumNValues(), kaldi::nnet3::FindNStride(), KALDI_ASSERT, and ComputationExpander::num_n_values_.

Referenced by ComputationExpander::ComputePrecomputedIndexes().

3799  {
3800  bool full_check = false;
3801  int32 n_stride = FindNStride(indexes, full_check);
3802  KALDI_ASSERT(n_stride > 0);
3803  ConvertNumNValues(n_stride, 2, num_n_values_,
3804  indexes, indexes_expanded);
3805 }
static void ConvertNumNValues(int32 n_stride, int32 old_N, int32 new_N, const std::vector< Index > &indexes_in, std::vector< Index > *indexes_out)
static int32 FindNStride(const std::vector< Index > &indexes, bool full_check)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
void ExpandRowRangesCommand ( const NnetComputation::Command c_in,
NnetComputation::Command c_out 
)
private

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

References NnetComputation::Command::arg1, NnetComputation::Command::arg2, NnetComputation::Command::arg3, ComputationExpander::computation_, ComputationExpander::expanded_computation_, ComputationExpander::GetNewSubmatLocationInfo(), NnetComputation::indexes_ranges, KALDI_ASSERT, rnnlm::n, ComputationExpander::num_n_values_, and NnetComputation::submatrices.

Referenced by ComputationExpander::ComputeCommands().

3431  {
3432  // we need to expand the pairs of row-indexes in c_in.arg2, and put the index
3433  // of the resulting vector<int> in expanded_computation_->indexes_ranges, in
3434  // 'c_out->arg2'.
3435 
3436  int32 s1 = c_in.arg1, s2 = c_in.arg2,
3437  num_rows_old = computation_.submatrices[s1].num_rows,
3438  num_rows_new = expanded_computation_->submatrices[s1].num_rows;
3439  KALDI_ASSERT(static_cast<size_t>(c_in.arg3) <
3440  computation_.indexes_ranges.size());
3441  int32 num_n_values = num_n_values_;
3442 
3443  int32 old_arg3 = c_out->arg3;
3444  c_out->arg3 = expanded_computation_->indexes_ranges.size();
3446  std::vector<std::pair<int32, int32> >());
3447  std::vector<std::pair<int32, int32> > &new_indexes_ranges =
3449  const std::vector<std::pair<int32, int32> > &old_indexes_ranges =
3450  computation_.indexes_ranges[old_arg3];
3451  // old_indexes_ranges is a vector that has the same size as the num-rows of
3452  // submatrix s1. It contains pairs that are either two copies of the same
3453  // value (in practice the pair (-1, -1)), or pairs (begin-row-index,
3454  // end-row-index) representing the (begin,end) of a range in submatrix s2.
3455  // Note: end-row-index is one past the end of the range, as for C++ iterators.
3456 
3457  KALDI_ASSERT(static_cast<int32>(old_indexes_ranges.size()) == num_rows_old);
3458 
3459  new_indexes_ranges.resize(num_rows_new,
3460  std::pair<int32,int32>(-1, -1));
3461 
3462  for (int32 i1 = 0; i1 < num_rows_old; i1++) {
3463  int32 new_i1_n0, n_stride1;
3464  if (GetNewSubmatLocationInfo(s1, i1, &new_i1_n0, &n_stride1)) {
3465  // GetNewSubmatLocationInfo() returns true if this corresponds to
3466  // a Cindex with n == 0.
3467  int32 i2_begin = old_indexes_ranges[i1].first,
3468  i2_end = old_indexes_ranges[i1].second;
3469  if (i2_end == i2_begin)
3470  continue; // (-1, -1) pair, meaning an empty range.
3471  // 'new_indexes_ranges' is filled with (-1, -1) pairs as a
3472  // default so we don't have to do anything for these
3473  // elements.
3474  int32 i2_last = i2_end - 1;
3475  int32 new_i2_n0_begin, new_i2_n0_last,
3476  n_stride2; // only 1 stride variable; both calls will output
3477  // the same value.
3478 
3479  bool ans1 = GetNewSubmatLocationInfo(s2, i2_begin, &new_i2_n0_begin,
3480  &n_stride2),
3481  ans2 = GetNewSubmatLocationInfo(s2, i2_last, &new_i2_n0_last,
3482  &n_stride2);
3483  KALDI_ASSERT(ans1 && ans2 && new_i2_n0_last >= new_i2_n0_begin &&
3484  new_i2_n0_begin >= 0 && n_stride1 > 0 && n_stride2 > 0);
3485  // source should also be for n==0, because we don't (or at least
3486  // shouldn't) create computations that mix up the 'n' values
3487 
3488 
3489  int32 new_i1 = new_i1_n0,
3490  new_i2_begin = new_i2_n0_begin,
3491  new_i2_end = new_i2_n0_last + 1;
3492  for (int32 n = 0; n < num_n_values;
3493  n++, new_i1 += n_stride1, new_i2_begin += n_stride2,
3494  new_i2_end += n_stride2) {
3495  new_indexes_ranges[new_i1].first = new_i2_begin;
3496  new_indexes_ranges[new_i1].second = new_i2_end;
3497  }
3498  }
3499  }
3500 }
std::vector< SubMatrixInfo > submatrices
struct rnnlm::@11::@12 n
bool GetNewSubmatLocationInfo(int32 submat_index, int32 old_row_index, int32 *new_row_index, int32 *n_stride) const
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::vector< std::vector< std::pair< int32, int32 > > > indexes_ranges
void ExpandRowsCommand ( const NnetComputation::Command c_in,
NnetComputation::Command c_out 
)
private

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

References NnetComputation::Command::alpha, NnetComputation::Command::arg1, NnetComputation::Command::arg2, NnetComputation::Command::arg3, ComputationExpander::computation_, ComputationExpander::expanded_computation_, ComputationExpander::GetNewSubmatLocationInfo(), NnetComputation::indexes, KALDI_ASSERT, rnnlm::n, ComputationExpander::num_n_values_, and NnetComputation::submatrices.

Referenced by ComputationExpander::ComputeCommands().

3303  {
3304  // we need to expand the row-indexes in c_in.arg3, and put the index of the
3305  // resulting vector<int> in expanded_computation_->indexes, in 'c_out->arg3'.
3306 
3307  int32 s1 = c_in.arg1, s2 = c_in.arg2;
3308 
3309  // The command that gets called is something like
3310  // submat1.AddRows(submat2, indexes) if submat1 is the submatrix referred to in
3311  // 's1' and submat2 is the submatrix referred to in 's2'.
3312  // 'indexes' has the same size as the num-rows of submat1, and the values
3313  // in the vector are row-indexes into s2.
3314  int32 old_arg3 = c_out->arg3;
3315  c_out->arg3 = expanded_computation_->indexes.size();
3316  c_out->alpha = c_in.alpha;
3317  expanded_computation_->indexes.push_back(std::vector<int32>());
3318  std::vector<int32> &new_indexes = expanded_computation_->indexes.back();
3319  const std::vector<int32> &old_indexes = computation_.indexes[old_arg3];
3320 
3321  int32 old_size = old_indexes.size(),
3322  num_n_values = num_n_values_,
3323  new_s1_size = expanded_computation_->submatrices[s1].num_rows,
3324  new_s2_size = expanded_computation_->submatrices[s2].num_rows;
3325 
3326  KALDI_ASSERT(old_size == computation_.submatrices[s1].num_rows);
3327 
3328  new_indexes.resize(new_s1_size, -1);
3329 
3330 
3331  // A note on the variable names: i1 and i2 are indexes into the destination
3332  // submatrix and the source submatrix respectively, of the CopyRows or AddRows
3333  // command.
3334  // "n0" in the variable name means that this corresponds to an Index with n==0.
3335  // things without "new" in the name refer to the old computation; things with
3336  // "new" in the name refer to the computation that we are generating.
3337  for (int32 i1 = 0; i1 < old_size; i1++) {
3338  int32 new_i1_n0, n_stride1;
3339  if (GetNewSubmatLocationInfo(s1, i1, &new_i1_n0, &n_stride1)) {
3340  // GetNewSubmatLocationInfo() returns true if this corresponds to
3341  // a Cindex with n == 0.
3342  int32 i2 = old_indexes[i1]; // note: i2 is the row index into submatrix s2.
3343  int32 new_i2_n0, n_stride2;
3344  if (i2 < 0) { // if i2 is -1, we'll just leave any relevant positions in
3345  // 'new_indexes' with -1's in them.
3346  continue;
3347  } else {
3348  bool ans = GetNewSubmatLocationInfo(s2, i2, &new_i2_n0, &n_stride2);
3349  KALDI_ASSERT(ans); // source should also be for n==0, because we don't
3350  // (or at least shouldn't) create computations that
3351  // mix up the 'n' values
3352 
3353  int32 new_i1 = new_i1_n0, new_i2 = new_i2_n0;
3354  for (int32 n = 0; n < num_n_values;
3355  ++n, new_i1 += n_stride1, new_i2 += n_stride2) {
3356  KALDI_ASSERT(new_i1 < new_s1_size && new_i2 < new_s2_size);
3357  new_indexes[new_i1] = new_i2;
3358  }
3359  }
3360  }
3361  }
3362 }
std::vector< SubMatrixInfo > submatrices
struct rnnlm::@11::@12 n
bool GetNewSubmatLocationInfo(int32 submat_index, int32 old_row_index, int32 *new_row_index, int32 *n_stride) const
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
std::vector< std::vector< int32 > > indexes
void ExpandRowsMultiCommand ( const NnetComputation::Command c_in,
NnetComputation::Command c_out 
)
private

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

References NnetComputation::Command::arg1, NnetComputation::Command::arg2, ComputationExpander::computation_, ComputationExpander::expanded_computation_, ComputationExpander::GetNewSubmatLocationInfo(), NnetComputation::indexes_multi, KALDI_ASSERT, rnnlm::n, ComputationExpander::num_n_values_, and NnetComputation::submatrices.

Referenced by ComputationExpander::ComputeCommands().

3366  {
3367  // we need to expand the (submatrix,row)-index pairs in c_in.arg2, and put the
3368  // index of the resulting vector<int> in expanded_computation_->indexes_multi,
3369  // in 'c_out->arg2'.
3370 
3371  int32 s1 = c_in.arg1,
3372  num_rows_old = computation_.submatrices[s1].num_rows,
3373  num_rows_new = expanded_computation_->submatrices[s1].num_rows;
3374 
3375  KALDI_ASSERT(num_rows_old % 2 == 0);
3376  int32 num_n_values = num_n_values_;
3377 
3378  int32 old_arg2 = c_out->arg2;
3379  c_out->arg2 = expanded_computation_->indexes_multi.size();
3381  std::vector<std::pair<int32, int32> >());
3382  std::vector<std::pair<int32, int32> > &new_indexes_multi =
3384  const std::vector<std::pair<int32, int32> > &old_indexes_multi =
3385  computation_.indexes_multi[old_arg2];
3386  // old_indexes_multi is a vector that has the same size as the num-rows
3387  // of submatrix s1. It contains pairs that are either (-1, -1), or
3388  // pairs (submatrix-index, row-index) referring to other submatrices
3389  // in the computation.
3390 
3391  KALDI_ASSERT(static_cast<int32>(old_indexes_multi.size()) == num_rows_old);
3392 
3393 
3394  new_indexes_multi.resize(num_rows_new,
3395  std::pair<int32,int32>(-1, -1));
3396 
3397  for (int32 i1 = 0; i1 < num_rows_old; i1++) {
3398  int32 new_i1_n0, n_stride1;
3399  if (GetNewSubmatLocationInfo(s1, i1, &new_i1_n0, &n_stride1)) {
3400  // GetNewSubmatLocationInfo() returns true if this corresponds to
3401  // a Cindex with n == 0.
3402  int32 s2 = old_indexes_multi[i1].first,
3403  i2 = old_indexes_multi[i1].second;
3404  int32 new_i2_n0, n_stride2;
3405  if (s2 < 0) { // if s2 is -1, we don't have to do anything... we'd have
3406  // to fill any relevant positions in 'new_indexes_multi'
3407  // with (-1,-1)'s, but it's filled with that by default.
3408  continue;
3409  } else {
3410  bool ans = GetNewSubmatLocationInfo(s2, i2, &new_i2_n0, &n_stride2);
3411  KALDI_ASSERT(ans); // source should also be for n==0, because we don't
3412  // (or at least shouldn't) create computations that
3413  // mix up the 'n' values
3414 
3415  int32 new_i1 = new_i1_n0, new_i2 = new_i2_n0;
3416 
3417  for (int32 n = 0; n < num_n_values;
3418  n++, new_i1 += n_stride1, new_i2 += n_stride2) {
3419  new_indexes_multi[new_i1].first = s2;
3420  new_indexes_multi[new_i1].second = new_i2;
3421  }
3422  }
3423  }
3424  }
3425 }
std::vector< std::vector< std::pair< int32, int32 > > > indexes_multi
std::vector< SubMatrixInfo > submatrices
struct rnnlm::@11::@12 n
bool GetNewSubmatLocationInfo(int32 submat_index, int32 old_row_index, int32 *new_row_index, int32 *n_stride) const
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
int32 GetNewMatrixLocationInfo ( int32  old_matrix_index,
int32  old_row_index 
) const
private

This function is used in mapping row-indexes into matrices, from the old to the new computation.

Parameters
[in]matrix_indexThe matrix-index > 0, for which we are mapping row-indexes. The matrix-indexes are the same in the old and new computations.
[in]old_row_indexThe old row-index into the matrix.
Returns
This function returns the row-index where the cindex referred to in 'old_matrix_index' will reside in the new, expanded computation, WITH THE CAVEAT THAT if the old cindex had n == 1, we'll output the location of the cindex with n == num_n_values_ - 1. This happens to be what we want (it maps the last n value on the input to the last n value on the output.

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

References ComputationExpander::computation_, KALDI_ASSERT, NnetComputation::matrix_debug_info, ComputationExpander::n_stride_, and ComputationExpander::num_n_values_.

Referenced by ComputationExpander::ComputeDebugInfo(), ComputationExpander::ComputeSubmatrixInfo(), and ComputationExpander::GetNewSubmatLocationInfo().

3765  {
3766  // to understand 'block_size', read the comment for FindNStride().
3767  int32 n_stride = n_stride_[matrix_index],
3768  old_num_n_values = 2, new_num_n_values = num_n_values_,
3769  old_block_size = old_num_n_values * n_stride,
3770  new_block_size = new_num_n_values * n_stride,
3771  block_index = old_row_index / old_block_size,
3772  offset_within_block = old_row_index % old_block_size;
3773 
3774  // within each block, we can show, given our assumptions, that
3775  // we must first have a sub-block of 'n_stride' values all with
3776  // n == 0, then another sub-clock of 'n_stride' values all with
3777  // n == 1, and so on. [except there is no 'and so on' for the
3778  // input computation, where we expect the 'n' values to be the
3779  // set {0, 1}.]
3780  int32 old_n_value = offset_within_block / n_stride,
3781  index_within_subblock = offset_within_block % n_stride;
3782  const std::vector<Cindex> &cindexes =
3783  computation_.matrix_debug_info[matrix_index].cindexes;
3784  KALDI_ASSERT(old_n_value == cindexes[old_row_index].second.n &&
3785  (old_n_value == 0 || old_n_value == 1));
3786  // Search for CAVEAT in the comment for this function to see what this is
3787  // about. Mapping old_n_value == 1 -> new_n_value == new_num_n_values - 1
3788  // just happens to be useful for the way we use this function... it maps the
3789  // end of an old submatrix to the end of a new submatrix.
3790  int32 new_n_value = (old_n_value == 0 ? 0 : new_num_n_values - 1);
3791 
3792  return block_index * new_block_size + index_within_subblock +
3793  new_n_value * n_stride;
3794 }
std::vector< MatrixDebugInfo > matrix_debug_info
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
bool GetNewSubmatLocationInfo ( int32  submat_index,
int32  old_row_index,
int32 *  new_row_index,
int32 *  n_stride 
) const
private

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

References NnetComputation::MatrixDebugInfo::cindexes, ComputationExpander::computation_, ComputationExpander::expanded_computation_, ComputationExpander::GetNewMatrixLocationInfo(), NnetComputation::matrix_debug_info, ComputationExpander::n_stride_, and NnetComputation::submatrices.

Referenced by ComputationExpander::ExpandRowRangesCommand(), ComputationExpander::ExpandRowsCommand(), and ComputationExpander::ExpandRowsMultiCommand().

3748  {
3749  int32 matrix_index = computation_.submatrices[submat_index].matrix_index,
3750  old_row_offset = computation_.submatrices[submat_index].row_offset,
3751  new_row_offset = expanded_computation_->submatrices[submat_index].row_offset;
3752 
3753  const NnetComputation::MatrixDebugInfo &debug_info_in =
3754  computation_.matrix_debug_info[matrix_index];
3755  if (debug_info_in.cindexes[old_row_index + old_row_offset].second.n != 0)
3756  return false;
3757  *new_row_index = (GetNewMatrixLocationInfo(matrix_index,
3758  old_row_index + old_row_offset) -
3759  new_row_offset);
3760  *n_stride = n_stride_[matrix_index];
3761  return true;
3762 }
std::vector< MatrixDebugInfo > matrix_debug_info
std::vector< SubMatrixInfo > submatrices
int32 GetNewMatrixLocationInfo(int32 old_matrix_index, int32 old_row_index) const
This function is used in mapping row-indexes into matrices, from the old to the new computation...
void InitStrideInfo ( )
private

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

References NnetComputation::MatrixDebugInfo::cindexes, ComputationExpander::computation_, kaldi::nnet3::FindNStride(), KALDI_ASSERT, KALDI_ERR, NnetComputation::matrices, NnetComputation::matrix_debug_info, and ComputationExpander::n_stride_.

Referenced by ComputationExpander::Expand().

3551  {
3552  // note: the zeroth matrix is not a real matrix, it's the empty matrix.
3553  int32 num_matrices = computation_.matrices.size();
3554  n_stride_.resize(num_matrices);
3555  n_stride_[0] = 0;
3556 
3557  // the input computation to class ComputationExpander is required to
3558  // have its debug info set up.
3560  for (int32 m = 1; m < num_matrices; m++) {
3561  int32 num_rows = computation_.matrices[m].num_rows;
3562  const NnetComputation::MatrixDebugInfo &debug_info = computation_.matrix_debug_info[m];
3563  KALDI_ASSERT(debug_info.cindexes.size() == num_rows);
3564  bool full_check = true; // TODO: eventually change this to false.
3565  int32 n_stride = FindNStride(debug_info.cindexes, full_check);
3566  if (n_stride == 0) {
3567  KALDI_ERR << "Problem encountered in 'shortcut' compilation: the computation "
3568  << "does not have the expected structure. Try compiling with "
3569  << "--use-shortcut=false.";
3570  }
3571  n_stride_[m] = n_stride;
3572  }
3573 }
std::vector< MatrixDebugInfo > matrix_debug_info
static int32 FindNStride(const std::vector< Index > &indexes, bool full_check)
std::vector< MatrixInfo > matrices
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169

Member Data Documentation

const MiscComputationInfo& misc_info_
private
bool need_debug_info_
private

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

Referenced by ComputationExpander::Expand().


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