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 2557 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 2559 of file nnet-optimize-utils.cc.

References KALDI_ASSERT.

2564  :
2565  nnet_(nnet), misc_info_(misc_info),
2566  computation_(computation),
2567  need_debug_info_(need_debug_info),
2568  num_n_values_(num_n_values),
2569  expanded_computation_(expanded_computation) {
2570  KALDI_ASSERT(num_n_values > 2);
2571  }
const MiscComputationInfo & misc_info_
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169

Member Function Documentation

void ComputeCommands ( )
private

Definition at line 2920 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::kCopyRows, kaldi::nnet3::kCopyRowsMulti, kaldi::nnet3::kCopyToRowsMulti, kaldi::nnet3::kDeallocMatrix, 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().

2920  {
2921  int32 num_commands = computation_.commands.size();
2922  expanded_computation_->commands.resize(num_commands);
2923  for (int32 command_index = 0; command_index < num_commands;
2924  command_index++) {
2925  const NnetComputation::Command &c = computation_.commands[command_index];
2926  NnetComputation::Command &c_out =
2927  expanded_computation_->commands[command_index];
2928  c_out = c;
2929  // Commands that only operate on submatrices, components and
2930  // precomputed-indexes do not have to be changed because we'll take care of
2931  // the expansion by suitably redefining the matrices and submatrices, and
2932  // recreating the precomputed-indexes.
2933  // However, commands that require, 'indexes', 'indexes_multi' or
2934  // 'indexes_ranges' do need to be modified.
2935  switch (c.command_type) {
2936  case kAllocMatrix:
2937  case kDeallocMatrix:
2938  case kSetConst:
2939  case kSwapMatrix:
2940  case kPropagate: case kBackprop:
2942  break;
2943  case kCopyRows: case kAddRows:
2944  ExpandRowsCommand(c, &c_out);
2945  break;
2946  case kCopyRowsMulti: case kAddRowsMulti:
2947  case kCopyToRowsMulti: case kAddToRowsMulti:
2948  ExpandRowsMultiCommand(c, &c_out);
2949  break;
2950  case kAddRowRanges:
2951  ExpandRowRangesCommand(c, &c_out);
2952  break;
2953  case kAcceptInput: case kProvideOutput: case kNoOperation:
2955  case kNoOperationLabel: case kGotoLabel:
2956  break;
2957  default:
2958  KALDI_ERR << "Un-handled command type";
2959  }
2960  }
2961 }
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 3021 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().

3021  {
3022  int32 num_matrices = computation_.matrices.size();
3023  KALDI_ASSERT(computation_.matrix_debug_info.size() == num_matrices);
3024  expanded_computation_->matrix_debug_info.resize(num_matrices);
3025  // Matrix zero is a special case; it's the empty matrix.
3028  int32 num_n_values = num_n_values_;
3029  for (int32 m = 1; m < num_matrices; m++) {
3030  const NnetComputation::MatrixDebugInfo &info_in =
3032  NnetComputation::MatrixDebugInfo &info_out =
3034  info_out.is_deriv = info_in.is_deriv;
3035  int32 num_rows_in = computation_.matrices[m].num_rows,
3036  num_rows_out = expanded_computation_->matrices[m].num_rows;
3037  KALDI_ASSERT(num_rows_in == info_in.cindexes.size());
3038  info_out.cindexes.resize(num_rows_out);
3039  const Cindex *cindexes_in = &(info_in.cindexes[0]);
3040  Cindex *cindexes_out = &(info_out.cindexes[0]);
3041  for (int32 r = 0; r < num_rows_in; r++) {
3042  if (info_in.cindexes[r].second.n == 0) {
3043  int32 new_r = GetNewMatrixLocationInfo(m, r),
3044  n_stride = n_stride_[m];
3045  for (int32 n = 0; n < num_n_values; n++) {
3046  int32 r_out = new_r + n * n_stride;
3047  cindexes_out[r_out] = cindexes_in[r];
3048  cindexes_out[r_out].second.n = n;
3049  }
3050  }
3051  }
3052  }
3053 }
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 3006 of file nnet-optimize-utils.cc.

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

Referenced by ComputationExpander::Expand().

3006  {
3007  int32 num_matrices = computation_.matrices.size();
3008  expanded_computation_->matrices.resize(num_matrices);
3009  // Matrix zero is a special case; it's the empty matrix.
3011  int32 old_num_n_values = 2,
3012  new_num_n_values = num_n_values_;
3013  for (int32 m = 1; m < num_matrices; m++) {
3015  expanded_computation_->matrices[m].num_rows =
3016  (computation_.matrices[m].num_rows / old_num_n_values) *
3017  new_num_n_values;
3018  }
3019 }
std::vector< MatrixInfo > matrices
void ComputePrecomputedIndexes ( )
private

Definition at line 3094 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().

3094  {
3095  // for each element of 'component_precomputed_indexes',
3096  // we will try to work out the command-index of the associated
3097  // Propagate() command and of the associated Backprop() command,
3098  // if it exists.
3099  // We expect that each such element will be associated with
3100  // exactly one Propagate() command and at most one Backprop() command.
3101  int32 num_commands = computation_.commands.size(),
3102  num_precomputed_indexes = computation_.component_precomputed_indexes.size();
3103 
3104  std::vector<bool> need_backprop(num_precomputed_indexes, false);
3105 
3106  std::vector<int32> component_index(num_precomputed_indexes, -1);
3107 
3108  for (int32 command_index = 0; command_index < num_commands; command_index++) {
3109  const NnetComputation::Command &c = computation_.commands[command_index];
3110 
3111  if (c.command_type == kPropagate && c.arg2 > 0) {
3112  KALDI_ASSERT(c.arg2 < num_precomputed_indexes);
3113  component_index[c.arg2] = c.arg1;
3114  }
3115  if ((c.command_type == kBackprop ||
3116  c.command_type == kBackpropNoModelUpdate) && c.arg2 > 0) {
3117  KALDI_ASSERT(c.arg2 < num_precomputed_indexes);
3118  need_backprop[c.arg2] = true;
3119  }
3120  }
3121 
3122  for (size_t p = 1;
3124  ++p)
3128  num_precomputed_indexes);
3129 
3130  for (int32 p = 1; p < num_precomputed_indexes; ++p) {
3131  const NnetComputation::PrecomputedIndexesInfo &old_info =
3133  NnetComputation::PrecomputedIndexesInfo &new_info =
3135  KALDI_ASSERT(!old_info.input_indexes.empty() &&
3136  !old_info.output_indexes.empty() &&
3137  "Input/output indexes not present in precomputed info of "
3138  "computation to be expanded.");
3139  // note: we could place these expanded indexes into 'new_info.input_indexes'
3140  // and 'new_info.output_indexes', but we actually don't need to keep them
3141  // there, because they are only required to be kept in computations where
3142  // the n indexes consist of the set (0, 1), and the computation we're
3143  // creating has more distinct n indexes than that.
3144  std::vector<Index> input_indexes, output_indexes;
3145  ExpandIndexes(old_info.input_indexes, &input_indexes);
3146  ExpandIndexes(old_info.output_indexes, &output_indexes);
3147  KALDI_ASSERT(component_index[p] >= 0);
3148  const Component *component = nnet_.GetComponent(component_index[p]);
3149  ComponentPrecomputedIndexes *expanded_precomputed_indexes =
3150  component->PrecomputeIndexes(misc_info_, input_indexes,
3151  output_indexes, need_backprop[p]);
3152  // this object should not be null because it was not NULL the
3153  // last time we generated it from the same component, for the
3154  // same computation.
3155  KALDI_ASSERT(expanded_precomputed_indexes != NULL);
3156  new_info.data = expanded_precomputed_indexes;
3157  }
3158 }
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 3055 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().

3055  {
3056  int32 num_submatrices = computation_.submatrices.size();
3057  expanded_computation_->submatrices.resize(num_submatrices);
3058  // Sub-matrix zero is a special case; it's the empty submatrix.
3060  for (int32 s = 1; s < num_submatrices; s++) {
3061  const NnetComputation::SubMatrixInfo &info_in = computation_.submatrices[s];
3062  int32 m = info_in.matrix_index;
3063  const NnetComputation::MatrixDebugInfo &debug_info_in =
3065 
3066  // we may need to change the row_offset and num_rows.
3067  int32 first_row_in = info_in.row_offset,
3068  last_row_in = first_row_in + info_in.num_rows - 1;
3069  if (!(debug_info_in.cindexes[first_row_in].second.n == 0 &&
3070  debug_info_in.cindexes[last_row_in].second.n == 1)) {
3071  std::ostringstream computation_ss;
3072  std::vector<std::string> submat_strings;
3073  computation_.GetSubmatrixStrings(nnet_, &submat_strings);
3074  computation_.Print(computation_ss, nnet_);
3075  KALDI_ERR << "Submatrix s" << s << " = " << submat_strings[s]
3076  << " has strange dimensions. Computation is: "
3077  << computation_ss.str();
3078  }
3079 
3080  int32 first_row_out = GetNewMatrixLocationInfo(m, first_row_in),
3081  last_row_out = GetNewMatrixLocationInfo(m, last_row_in),
3082  new_num_rows = (last_row_out + 1 - first_row_out);
3083 
3084  NnetComputation::SubMatrixInfo &info_out =
3086  info_out.matrix_index = m;
3087  info_out.row_offset = first_row_out;
3088  info_out.num_rows = new_num_rows;
3089  info_out.col_offset = info_in.col_offset;
3090  info_out.num_cols = info_in.num_cols;
3091  }
3092 }
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 2991 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().

2991  {
2992  InitStrideInfo();
2994  if (need_debug_info_)
2995  ComputeDebugInfo();
2996  else
3000  ComputeCommands();
3001 
3004 }
std::vector< MatrixDebugInfo > matrix_debug_info
void ExpandIndexes ( const std::vector< Index > &  indexes,
std::vector< Index > *  indexes_expanded 
) const
private

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

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

Referenced by ComputationExpander::ComputePrecomputedIndexes().

3214  {
3215  bool full_check = false;
3216  int32 n_stride = FindNStride(indexes, full_check);
3217  KALDI_ASSERT(n_stride > 0);
3218  ConvertNumNValues(n_stride, 2, num_n_values_,
3219  indexes, indexes_expanded);
3220 }
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 2845 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().

2847  {
2848  // we need to expand the pairs of row-indexes in c_in.arg2, and put the index
2849  // of the resulting vector<int> in expanded_computation_->indexes_ranges, in
2850  // 'c_out->arg2'.
2851 
2852  int32 s1 = c_in.arg1, s2 = c_in.arg2,
2853  num_rows_old = computation_.submatrices[s1].num_rows,
2854  num_rows_new = expanded_computation_->submatrices[s1].num_rows;
2855  KALDI_ASSERT(static_cast<size_t>(c_in.arg3) <
2856  computation_.indexes_ranges.size());
2857  int32 num_n_values = num_n_values_;
2858 
2859  int32 old_arg3 = c_out->arg3;
2860  c_out->arg3 = expanded_computation_->indexes_ranges.size();
2862  std::vector<std::pair<int32, int32> >());
2863  std::vector<std::pair<int32, int32> > &new_indexes_ranges =
2865  const std::vector<std::pair<int32, int32> > &old_indexes_ranges =
2866  computation_.indexes_ranges[old_arg3];
2867  // old_indexes_ranges is a vector that has the same size as the num-rows of
2868  // submatrix s1. It contains pairs that are either two copies of the same
2869  // value (in practice the pair (-1, -1)), or pairs (begin-row-index,
2870  // end-row-index) representing the (begin,end) of a range in submatrix s2.
2871  // Note: end-row-index is one past the end of the range, as for C++ iterators.
2872 
2873  KALDI_ASSERT(static_cast<int32>(old_indexes_ranges.size()) == num_rows_old);
2874 
2875  new_indexes_ranges.resize(num_rows_new,
2876  std::pair<int32,int32>(-1, -1));
2877 
2878  for (int32 i1 = 0; i1 < num_rows_old; i1++) {
2879  int32 new_i1_n0, n_stride1;
2880  if (GetNewSubmatLocationInfo(s1, i1, &new_i1_n0, &n_stride1)) {
2881  // GetNewSubmatLocationInfo() returns true if this corresponds to
2882  // a Cindex with n == 0.
2883  int32 i2_begin = old_indexes_ranges[i1].first,
2884  i2_end = old_indexes_ranges[i1].second;
2885  if (i2_end == i2_begin)
2886  continue; // (-1, -1) pair, meaning an empty range.
2887  // 'new_indexes_ranges' is filled with (-1, -1) pairs as a
2888  // default so we don't have to do anything for these
2889  // elements.
2890  int32 i2_last = i2_end - 1;
2891  int32 new_i2_n0_begin, new_i2_n0_last,
2892  n_stride2; // only 1 stride variable; both calls will output
2893  // the same value.
2894 
2895  bool ans1 = GetNewSubmatLocationInfo(s2, i2_begin, &new_i2_n0_begin,
2896  &n_stride2),
2897  ans2 = GetNewSubmatLocationInfo(s2, i2_last, &new_i2_n0_last,
2898  &n_stride2);
2899  KALDI_ASSERT(ans1 && ans2 && new_i2_n0_last >= new_i2_n0_begin &&
2900  new_i2_n0_begin >= 0 && n_stride1 > 0 && n_stride2 > 0);
2901  // source should also be for n==0, because we don't (or at least
2902  // shouldn't) create computations that mix up the 'n' values
2903 
2904 
2905  int32 new_i1 = new_i1_n0,
2906  new_i2_begin = new_i2_n0_begin,
2907  new_i2_end = new_i2_n0_last + 1;
2908  for (int32 n = 0; n < num_n_values;
2909  n++, new_i1 += n_stride1, new_i2_begin += n_stride2,
2910  new_i2_end += n_stride2) {
2911  new_indexes_ranges[new_i1].first = new_i2_begin;
2912  new_indexes_ranges[new_i1].second = new_i2_end;
2913  }
2914  }
2915  }
2916 }
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 2717 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().

2719  {
2720  // we need to expand the row-indexes in c_in.arg3, and put the index of the
2721  // resulting vector<int> in expanded_computation_->indexes, in 'c_out->arg3'.
2722 
2723  int32 s1 = c_in.arg1, s2 = c_in.arg2;
2724 
2725  // The command that gets called is something like
2726  // submat1.AddRows(submat2, indexes) if submat1 is the submatrix referred to in
2727  // 's1' and submat2 is the submatrix referred to in 's2'.
2728  // 'indexes' has the same size as the num-rows of submat1, and the values
2729  // in the vector are row-indexes into s2.
2730  int32 old_arg3 = c_out->arg3;
2731  c_out->arg3 = expanded_computation_->indexes.size();
2732  c_out->alpha = c_in.alpha;
2733  expanded_computation_->indexes.push_back(std::vector<int32>());
2734  std::vector<int32> &new_indexes = expanded_computation_->indexes.back();
2735  const std::vector<int32> &old_indexes = computation_.indexes[old_arg3];
2736 
2737  int32 old_size = old_indexes.size(),
2738  num_n_values = num_n_values_,
2739  new_s1_size = expanded_computation_->submatrices[s1].num_rows,
2740  new_s2_size = expanded_computation_->submatrices[s2].num_rows;
2741 
2742  KALDI_ASSERT(old_size == computation_.submatrices[s1].num_rows);
2743 
2744  new_indexes.resize(new_s1_size, -1);
2745 
2746 
2747  // A note on the variable names: i1 and i2 are indexes into the destination
2748  // submatrix and the source submatrix respectively, of the CopyRows or AddRows
2749  // command.
2750  // "n0" in the variable name means that this corresponds to an Index with n==0.
2751  // things without "new" in the name refer to the old computation; things with
2752  // "new" in the name refer to the computation that we are generating.
2753  for (int32 i1 = 0; i1 < old_size; i1++) {
2754  int32 new_i1_n0, n_stride1;
2755  if (GetNewSubmatLocationInfo(s1, i1, &new_i1_n0, &n_stride1)) {
2756  // GetNewSubmatLocationInfo() returns true if this corresponds to
2757  // a Cindex with n == 0.
2758  int32 i2 = old_indexes[i1]; // note: i2 is the row index into submatrix s2.
2759  int32 new_i2_n0, n_stride2;
2760  if (i2 < 0) { // if i2 is -1, we'll just leave any relevant positions in
2761  // 'new_indexes' with -1's in them.
2762  continue;
2763  } else {
2764  bool ans = GetNewSubmatLocationInfo(s2, i2, &new_i2_n0, &n_stride2);
2765  KALDI_ASSERT(ans); // source should also be for n==0, because we don't
2766  // (or at least shouldn't) create computations that
2767  // mix up the 'n' values
2768 
2769  int32 new_i1 = new_i1_n0, new_i2 = new_i2_n0;
2770  for (int32 n = 0; n < num_n_values;
2771  ++n, new_i1 += n_stride1, new_i2 += n_stride2) {
2772  KALDI_ASSERT(new_i1 < new_s1_size && new_i2 < new_s2_size);
2773  new_indexes[new_i1] = new_i2;
2774  }
2775  }
2776  }
2777  }
2778 }
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 2780 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().

2782  {
2783  // we need to expand the (submatrix,row)-index pairs in c_in.arg2, and put the
2784  // index of the resulting vector<int> in expanded_computation_->indexes_multi,
2785  // in 'c_out->arg2'.
2786 
2787  int32 s1 = c_in.arg1,
2788  num_rows_old = computation_.submatrices[s1].num_rows,
2789  num_rows_new = expanded_computation_->submatrices[s1].num_rows;
2790 
2791  KALDI_ASSERT(num_rows_old % 2 == 0);
2792  int32 num_n_values = num_n_values_;
2793 
2794  int32 old_arg2 = c_out->arg2;
2795  c_out->arg2 = expanded_computation_->indexes_multi.size();
2797  std::vector<std::pair<int32, int32> >());
2798  std::vector<std::pair<int32, int32> > &new_indexes_multi =
2800  const std::vector<std::pair<int32, int32> > &old_indexes_multi =
2801  computation_.indexes_multi[old_arg2];
2802  // old_indexes_multi is a vector that has the same size as the num-rows
2803  // of submatrix s1. It contains pairs that are either (-1, -1), or
2804  // pairs (submatrix-index, row-index) referring to other submatrices
2805  // in the computation.
2806 
2807  KALDI_ASSERT(static_cast<int32>(old_indexes_multi.size()) == num_rows_old);
2808 
2809 
2810  new_indexes_multi.resize(num_rows_new,
2811  std::pair<int32,int32>(-1, -1));
2812 
2813  for (int32 i1 = 0; i1 < num_rows_old; i1++) {
2814  int32 new_i1_n0, n_stride1;
2815  if (GetNewSubmatLocationInfo(s1, i1, &new_i1_n0, &n_stride1)) {
2816  // GetNewSubmatLocationInfo() returns true if this corresponds to
2817  // a Cindex with n == 0.
2818  int32 s2 = old_indexes_multi[i1].first,
2819  i2 = old_indexes_multi[i1].second;
2820  int32 new_i2_n0, n_stride2;
2821  if (s2 < 0) { // if s2 is -1, we don't have to do anything... we'd have
2822  // to fill any relevant positions in 'new_indexes_multi'
2823  // with (-1,-1)'s, but it's filled with that by default.
2824  continue;
2825  } else {
2826  bool ans = GetNewSubmatLocationInfo(s2, i2, &new_i2_n0, &n_stride2);
2827  KALDI_ASSERT(ans); // source should also be for n==0, because we don't
2828  // (or at least shouldn't) create computations that
2829  // mix up the 'n' values
2830 
2831  int32 new_i1 = new_i1_n0, new_i2 = new_i2_n0;
2832 
2833  for (int32 n = 0; n < num_n_values;
2834  n++, new_i1 += n_stride1, new_i2 += n_stride2) {
2835  new_indexes_multi[new_i1].first = s2;
2836  new_indexes_multi[new_i1].second = new_i2;
2837  }
2838  }
2839  }
2840  }
2841 }
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 3179 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().

3180  {
3181  // to understand 'block_size', read the comment for FindNStride().
3182  int32 n_stride = n_stride_[matrix_index],
3183  old_num_n_values = 2, new_num_n_values = num_n_values_,
3184  old_block_size = old_num_n_values * n_stride,
3185  new_block_size = new_num_n_values * n_stride,
3186  block_index = old_row_index / old_block_size,
3187  offset_within_block = old_row_index % old_block_size;
3188 
3189  // within each block, we can show, given our assumptions, that
3190  // we must first have a sub-block of 'n_stride' values all with
3191  // n == 0, then another sub-clock of 'n_stride' values all with
3192  // n == 1, and so on. [except there is no 'and so on' for the
3193  // input computation, where we expect the 'n' values to be the
3194  // set {0, 1}.]
3195  int32 old_n_value = offset_within_block / n_stride,
3196  index_within_subblock = offset_within_block % n_stride;
3197  const std::vector<Cindex> &cindexes =
3198  computation_.matrix_debug_info[matrix_index].cindexes;
3199  KALDI_ASSERT(old_n_value == cindexes[old_row_index].second.n &&
3200  (old_n_value == 0 || old_n_value == 1));
3201  // Search for CAVEAT in the comment for this function to see what this is
3202  // about. Mapping old_n_value == 1 -> new_n_value == new_num_n_values - 1
3203  // just happens to be useful for the way we use this function... it maps the
3204  // end of an old submatrix to the end of a new submatrix.
3205  int32 new_n_value = (old_n_value == 0 ? 0 : new_num_n_values - 1);
3206 
3207  return block_index * new_block_size + index_within_subblock +
3208  new_n_value * n_stride;
3209 }
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 3161 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().

3163  {
3164  int32 matrix_index = computation_.submatrices[submat_index].matrix_index,
3165  old_row_offset = computation_.submatrices[submat_index].row_offset,
3166  new_row_offset = expanded_computation_->submatrices[submat_index].row_offset;
3167 
3168  const NnetComputation::MatrixDebugInfo &debug_info_in =
3169  computation_.matrix_debug_info[matrix_index];
3170  if (debug_info_in.cindexes[old_row_index + old_row_offset].second.n != 0)
3171  return false;
3172  *new_row_index = (GetNewMatrixLocationInfo(matrix_index,
3173  old_row_index + old_row_offset) -
3174  new_row_offset);
3175  *n_stride = n_stride_[matrix_index];
3176  return true;
3177 }
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 2966 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().

2966  {
2967  // note: the zeroth matrix is not a real matrix, it's the empty matrix.
2968  int32 num_matrices = computation_.matrices.size();
2969  n_stride_.resize(num_matrices);
2970  n_stride_[0] = 0;
2971 
2972  // the input computation to class ComputationExpander is required to
2973  // have its debug info set up.
2975  for (int32 m = 1; m < num_matrices; m++) {
2976  int32 num_rows = computation_.matrices[m].num_rows;
2977  const NnetComputation::MatrixDebugInfo &debug_info = computation_.matrix_debug_info[m];
2978  KALDI_ASSERT(debug_info.cindexes.size() == num_rows);
2979  bool full_check = true; // TODO: eventually change this to false.
2980  int32 n_stride = FindNStride(debug_info.cindexes, full_check);
2981  if (n_stride == 0) {
2982  KALDI_ERR << "Problem encountered in 'shortcut' compilation: the computation "
2983  << "does not have the expected structure. Try compiling with "
2984  << "--use-shortcut=false.";
2985  }
2986  n_stride_[m] = n_stride;
2987  }
2988 }
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 2710 of file nnet-optimize-utils.cc.

Referenced by ComputationExpander::Expand().


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