NnetComputer Class Reference

class NnetComputer is responsible for executing the computation described in the "computation" object. More...

#include <nnet-compute.h>

Collaboration diagram for NnetComputer:

Classes

struct  CommandDebugInfo
 

Public Member Functions

 NnetComputer (const NnetComputeOptions &options, const NnetComputation &computation, const Nnet &nnet, Nnet *nnet_to_update)
 Constructor. More...
 
 NnetComputer (const NnetComputeOptions &options, const NnetComputation &computation, Nnet *nnet, Nnet *nnet_to_update)
 This version of the constructor accepts a pointer to 'nnet' instead of a const reference. More...
 
 NnetComputer (const NnetComputer &other)
 Copy constructor. More...
 
void AcceptInput (const std::string &node_name, CuMatrix< BaseFloat > *input)
 e.g. More...
 
void AcceptInputs (const Nnet &nnet, const std::vector< NnetIo > &io)
 This convenience function calls AcceptInput() in turn on all the inputs in the training example. More...
 
void Run ()
 This does either the forward or backward computation, depending when it is called (in a typical computation, the first time you call this it will do the forward computation; then you'll take the outputs and provide derivatives; and the second time you call it, it will do the backward computation. More...
 
const CuMatrixBase< BaseFloat > & GetOutput (const std::string &node_name)
 
void GetOutputDestructive (const std::string &output_name, CuMatrix< BaseFloat > *output)
 
 ~NnetComputer ()
 

Private Member Functions

void Init ()
 
void ExecuteCommand ()
 
int32 GetIoMatrixIndex (const std::string &node_name, bool is_output)
 
void CheckNoPendingIo ()
 
CuSubMatrix< BaseFloatGetSubMatrix (int32 submatrix_index)
 
void GetPointers (int32 indexes_multi_index, int32 num_cols, CuArray< BaseFloat *> *pointers)
 
void GetPointers (int32 indexes_multi_index, int32 num_cols, CuArray< const BaseFloat *> *pointers)
 
void DebugBeforeExecute (int32 command, CommandDebugInfo *info)
 
void DebugAfterExecute (int32 command, const CommandDebugInfo &info, double command_execution_time)
 
void SaveMemo (int32 memo_index, const Component &c, void *memo)
 
void * GetMemo (int32 memo_index)
 
NnetComputeroperator= (const NnetComputer &other)
 

Static Private Member Functions

static BaseFloat MatrixStddev (const CuMatrixBase< BaseFloat > &m)
 
static BaseFloat ParameterStddev (const Component &c)
 

Private Attributes

const NnetComputeOptionsoptions_
 
const NnetComputationcomputation_
 
const Nnetnnet_
 
int32 program_counter_
 
std::vector< int32pending_commands_
 
Nnetnnet_to_store_stats_
 
Nnetnnet_to_update_
 
bool debug_
 
std::vector< CommandAttributescommand_attributes_
 
std::vector< std::string > submatrix_strings_
 
std::vector< std::string > command_strings_
 
std::vector< CuMatrix< BaseFloat > > matrices_
 
std::vector< void * > memos_
 
std::vector< CuCompressedMatrixBase * > compressed_matrices_
 

Detailed Description

class NnetComputer is responsible for executing the computation described in the "computation" object.

You call in sequence, the constructor, then AcceptInput() [or AcceptInputs()], then Run(), then GetOutput() [and if applicable, AcceptOutputDeriv], then if there is a backward computation, Run() [then, if applicable, GetInputDeriv()].

Definition at line 59 of file nnet-compute.h.

Constructor & Destructor Documentation

◆ NnetComputer() [1/3]

NnetComputer ( const NnetComputeOptions options,
const NnetComputation computation,
const Nnet nnet,
Nnet nnet_to_update 
)

Constructor.

nnet_to_update will be NULL if you are not doing model update or model-derivative computation. You must call computation.ComputeCudaIndexes() before calling this function.

Caution: there is another constructor that takes a pointer for 'nnet', be careful not to mix these up.

Definition at line 28 of file nnet-compute.cc.

References NnetComputer::Init().

31  :
32  options_(options), computation_(computation), nnet_(nnet),
33  program_counter_(0), nnet_to_store_stats_(nnet_to_update),
34  nnet_to_update_(nnet_to_update) {
35  Init();
36 }
const NnetComputeOptions & options_
Definition: nnet-compute.h:132
const NnetComputation & computation_
Definition: nnet-compute.h:133

◆ NnetComputer() [2/3]

NnetComputer ( const NnetComputeOptions options,
const NnetComputation computation,
Nnet nnet,
Nnet nnet_to_update 
)

This version of the constructor accepts a pointer to 'nnet' instead of a const reference.

The difference is that this version will, for storing statistics (the StoreStats() function of class Component), use 'nnet' instead of 'nnet_to_update' (if specified).

Definition at line 38 of file nnet-compute.cc.

References NnetComputer::Init().

41  :
42  options_(options), computation_(computation), nnet_(*nnet),
44  nnet_to_update_(nnet_to_update) {
45  Init();
46 }
const NnetComputeOptions & options_
Definition: nnet-compute.h:132
const NnetComputation & computation_
Definition: nnet-compute.h:133

◆ NnetComputer() [3/3]

NnetComputer ( const NnetComputer other)

Copy constructor.

May not be used if memos are stored with this object (which is only a possibility if backprop will take place, and in these situations you won't normally be wanting to use the copy constructor anyway; the copy constructor is more useful for things like RNNLM lattice rescoring).

Definition at line 189 of file nnet-compute.cc.

References KALDI_ERR, and NnetComputer::memos_.

189  :
190  options_(other.options_),
191  computation_(other.computation_),
192  nnet_(other.nnet_),
193  program_counter_(other.program_counter_),
194  pending_commands_(other.pending_commands_),
195  nnet_to_store_stats_(other.nnet_to_store_stats_),
196  nnet_to_update_(other.nnet_to_update_),
197  debug_(other.debug_),
198  command_attributes_(other.command_attributes_),
199  submatrix_strings_(other.submatrix_strings_),
200  command_strings_(other.command_strings_),
201  matrices_(other.matrices_),
202  memos_(other.memos_) {
203  // Note: this is the same as the default copy constructor, except for the check below.
204  if (!memos_.empty()) {
205  KALDI_ERR << "You cannot use the copy constructor of NnetComputer if "
206  "memos are used.";
207  }
208 }
const NnetComputeOptions & options_
Definition: nnet-compute.h:132
std::vector< CommandAttributes > command_attributes_
Definition: nnet-compute.h:153
std::vector< int32 > pending_commands_
Definition: nnet-compute.h:141
#define KALDI_ERR
Definition: kaldi-error.h:147
const NnetComputation & computation_
Definition: nnet-compute.h:133
std::vector< CuMatrix< BaseFloat > > matrices_
Definition: nnet-compute.h:160
std::vector< std::string > submatrix_strings_
Definition: nnet-compute.h:155
std::vector< std::string > command_strings_
Definition: nnet-compute.h:157
std::vector< void * > memos_
Definition: nnet-compute.h:165

◆ ~NnetComputer()

Definition at line 680 of file nnet-compute.cc.

References NnetComputer::compressed_matrices_, and rnnlm::i.

680  {
681  // Delete any pointers that are present in compressed_matrices_. Actually
682  // they should all already have been deallocated and set to NULL if the
683  // compuation was run to completion; we do this in case someone ran
684  // the forward propagation but not the backprop.
685  for (size_t i = 0; i < compressed_matrices_.size(); i++)
686  delete compressed_matrices_[i];
687 }
std::vector< CuCompressedMatrixBase * > compressed_matrices_
Definition: nnet-compute.h:173

Member Function Documentation

◆ AcceptInput()

void AcceptInput ( const std::string &  node_name,
CuMatrix< BaseFloat > *  input 
)

e.g.

AcceptInput ("input", &input_mat), or for derivatives w.r.t. the output, AcceptInput("output", output_deriv_mat). Will crash if there is no input or output node with the given name. This function is destructive of "input" as it takes it using the Swap function of CuMatrix. Must have the same number of rows as the corresponding input described in the ComputationRequest e.g. the indexes.size() in the corresponding IoSpecification.

Definition at line 547 of file nnet-compute.cc.

References NnetComputer::computation_, NnetComputer::GetIoMatrixIndex(), KALDI_ERR, kaldi::kDefaultStride, kaldi::kStrideEqualNumCols, kaldi::kUndefined, NnetComputation::matrices, NnetComputer::matrices_, NnetComputation::MatrixInfo::num_cols, NnetComputation::MatrixInfo::num_rows, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), CuMatrix< Real >::Resize(), CuMatrixBase< Real >::Stride(), and NnetComputation::MatrixInfo::stride_type.

Referenced by NnetComputer::AcceptInputs(), DecodableNnetLoopedOnlineBase::AdvanceChunk(), DecodableNnetSimpleLooped::AdvanceChunk(), NnetBatchComputer::Compute(), kaldi::nnet3::ComputeObjectiveFunction(), BatchedXvectorComputer::ComputeOneBatch(), DecodableNnetSimple::DoNnetComputation(), NnetDiscriminativeComputeObjf::ProcessOutputs(), NnetChainTrainer::ProcessOutputs(), NnetChainComputeProb::ProcessOutputs(), NnetDiscriminativeTrainer::ProcessOutputs(), kaldi::nnet3::RunNnetComputation(), kaldi::nnet3::UnitTestNnetCompute(), kaldi::nnet3::UnitTestNnetInputDerivatives(), kaldi::nnet3::UnitTestNnetModelDerivatives(), and kaldi::nnet3::UnitTestNnetOptimizeWithOptions().

548  {
549  bool is_output = false;
550  int32 matrix_index = GetIoMatrixIndex(node_name, is_output);
551 
552  const NnetComputation::MatrixInfo &matrix_info =
553  computation_.matrices[matrix_index];
554  if (input->NumRows() != matrix_info.num_rows) {
555  KALDI_ERR << "Num-rows mismatch for input '" << node_name
556  << "': " << matrix_info.num_rows
557  << " in computation-request, " << input->NumRows()
558  << " provided.";
559  }
560  if (input->NumCols() != matrix_info.num_cols) {
561  KALDI_ERR << "Num-cols mismatch for input '" << node_name
562  << "': " << matrix_info.num_cols
563  << " in computation-request, " << input->NumCols()
564  << " provided.";
565  }
566  if (matrix_info.stride_type == kDefaultStride ||
567  input->Stride() == input->NumCols()) {
568  matrices_[matrix_index].Swap(input);
569  } else {
570  matrices_[matrix_index].Resize(matrix_info.num_rows,
571  matrix_info.num_cols,
573  matrices_[matrix_index].CopyFromMat(*input);
574  input->Resize(0, 0);
575  }
576 }
kaldi::int32 int32
std::vector< MatrixInfo > matrices
#define KALDI_ERR
Definition: kaldi-error.h:147
int32 GetIoMatrixIndex(const std::string &node_name, bool is_output)
const NnetComputation & computation_
Definition: nnet-compute.h:133
std::vector< CuMatrix< BaseFloat > > matrices_
Definition: nnet-compute.h:160

◆ AcceptInputs()

void AcceptInputs ( const Nnet nnet,
const std::vector< NnetIo > &  io 
)

This convenience function calls AcceptInput() in turn on all the inputs in the training example.

It needs "nnet" only in order to distinguish inputs from outputs.

Definition at line 663 of file nnet-compute.cc.

References NnetComputer::AcceptInput(), CuMatrixBase< Real >::CopyFromGeneralMat(), NnetIo::features, Nnet::GetNodeIndex(), rnnlm::i, Nnet::IsInputNode(), KALDI_ERR, kaldi::kUndefined, NnetIo::name, GeneralMatrix::NumCols(), and GeneralMatrix::NumRows().

Referenced by NnetLdaStatsAccumulator::AccStats(), NnetComputerFromEg::Compute(), NnetDiscriminativeComputeObjf::Compute(), NnetChainComputeProb::Compute(), NnetComputeProb::Compute(), NnetDiscriminativeTrainer::Train(), NnetChainTrainer::TrainInternal(), NnetTrainer::TrainInternal(), NnetChainTrainer::TrainInternalBackstitch(), and NnetTrainer::TrainInternalBackstitch().

664  {
665  for (size_t i = 0; i < io_vec.size(); i++) {
666  const NnetIo &io = io_vec[i];
667  int32 node_index = nnet.GetNodeIndex(io.name);
668  if (node_index == -1)
669  KALDI_ERR << "No node named '" << io.name << "' in nnet.";
670  if (nnet.IsInputNode(node_index)) {
671  CuMatrix<BaseFloat> cu_input(io.features.NumRows(),
672  io.features.NumCols(),
673  kUndefined);
674  cu_input.CopyFromGeneralMat(io.features);
675  this->AcceptInput(io.name, &cu_input);
676  }
677  }
678 }
kaldi::int32 int32
void AcceptInput(const std::string &node_name, CuMatrix< BaseFloat > *input)
e.g.
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ CheckNoPendingIo()

void CheckNoPendingIo ( )
private

Definition at line 597 of file nnet-compute.cc.

References NnetComputation::commands, NnetComputer::computation_, Nnet::GetNodeName(), rnnlm::i, kaldi::nnet3::kAcceptInput, KALDI_ERR, kaldi::nnet3::kProvideOutput, NnetComputer::nnet_, NnetComputer::pending_commands_, and NnetComputer::program_counter_.

Referenced by NnetComputer::Run().

597  {
598  const std::vector<NnetComputation::Command> &c = computation_.commands;
599  while (program_counter_ < static_cast<int32>(c.size()) &&
600  (c[program_counter_].command_type == kAcceptInput ||
601  c[program_counter_].command_type == kProvideOutput)) {
604  }
605  for (size_t i = 0; i < pending_commands_.size(); i++) {
606  // the order here doesn't really matter; we go from back to front
607  // as it's more efficient, not that efficiency really matters here.
608  int32 command = pending_commands_[i];
609  if (c[command].command_type == kAcceptInput) {
610  // we can't ignore if we needed input from the user that hasn't been
611  // provided.
612  int32 node = c[command].arg2;
613  KALDI_ERR << "Cannot run computation-- we did not get input for node '"
614  << nnet_.GetNodeName(node) << "'";
615  }
616  }
617  pending_commands_.clear();
618 }
const std::string & GetNodeName(int32 node_index) const
returns individual node name.
Definition: nnet-nnet.cc:684
kaldi::int32 int32
std::vector< Command > commands
std::vector< int32 > pending_commands_
Definition: nnet-compute.h:141
#define KALDI_ERR
Definition: kaldi-error.h:147
const NnetComputation & computation_
Definition: nnet-compute.h:133

◆ DebugAfterExecute()

void DebugAfterExecute ( int32  command,
const CommandDebugInfo info,
double  command_execution_time 
)
private

Definition at line 116 of file nnet-compute.cc.

References NnetComputation::Command::arg1, NnetComputer::command_attributes_, NnetComputer::command_strings_, NnetComputation::Command::command_type, NnetComputation::commands, NnetComputer::CommandDebugInfo::components_parameter_stddev, NnetComputer::computation_, Nnet::GetComponent(), Nnet::GetComponentName(), NnetComputer::GetSubMatrix(), rnnlm::i, NnetComputation::IsWholeMatrix(), KALDI_ASSERT, KALDI_LOG, kaldi::nnet3::kBackprop, kaldi::nnet3::kUpdatableComponent, NnetComputer::matrices_, NnetComputer::CommandDebugInfo::matrices_written_stddevs, NnetComputer::MatrixStddev(), NnetComputer::nnet_, NnetComputer::ParameterStddev(), Component::Properties(), NnetComputer::CommandDebugInfo::submatrices_written_stddevs, and NnetComputer::submatrix_strings_.

Referenced by NnetComputer::Run().

118  {
119  std::ostringstream os;
120  os << command_strings_[command] << "\t|\t";
121  {
122  const std::vector<int32> &matrices_written =
123  command_attributes_[command].matrices_written;
124  size_t size = matrices_written.size();
125  KALDI_ASSERT(info.matrices_written_stddevs.size() == size);
126  for (size_t i = 0; i < size; i++) {
127  int32 m = matrices_written[i];
128  BaseFloat old_stddev = info.matrices_written_stddevs[i],
129  stddev = MatrixStddev(matrices_[m]);
130  os << 'm' << m << ": " << old_stddev << "->" << stddev << " ";
131  }
132  }
133  {
134  const std::vector<int32> &submatrices_written =
135  command_attributes_[command].submatrices_written;
136  size_t size = submatrices_written.size();
137  KALDI_ASSERT(info.submatrices_written_stddevs.size() == size);
138  for (size_t i = 0; i < size; i++) {
139  int32 s = submatrices_written[i];
140  if (!computation_.IsWholeMatrix(s)) {
141  const CuSubMatrix<BaseFloat> submat(GetSubMatrix(s));
142  BaseFloat old_stddev = info.submatrices_written_stddevs[i],
143  stddev = MatrixStddev(submat);
144  os << submatrix_strings_[s] << ": " << old_stddev << "->"
145  << stddev << " ";
146  }
147  }
148  }
149  const NnetComputation::Command &c = computation_.commands[command];
150  if (c.command_type == kBackprop) {
151  const Component *component = nnet_.GetComponent(c.arg1);
152  if (component->Properties() & kUpdatableComponent) {
153  const std::string &component_name = nnet_.GetComponentName(c.arg1);
154  os << component_name << ": " << info.components_parameter_stddev
155  << "->" << ParameterStddev(*component) << " ";
156  }
157  }
158  os << "\t|\t time: " << command_exec_time << " secs";
159  KALDI_LOG << os.str();
160 }
std::vector< CommandAttributes > command_attributes_
Definition: nnet-compute.h:153
kaldi::int32 int32
std::vector< Command > commands
float BaseFloat
Definition: kaldi-types.h:29
static BaseFloat ParameterStddev(const Component &c)
Definition: nnet-compute.cc:75
const std::string & GetComponentName(int32 component_index) const
returns individual component name.
Definition: nnet-nnet.cc:689
const NnetComputation & computation_
Definition: nnet-compute.h:133
Component * GetComponent(int32 c)
Return component indexed c. Not a copy; not owned by caller.
Definition: nnet-nnet.cc:150
std::vector< CuMatrix< BaseFloat > > matrices_
Definition: nnet-compute.h:160
std::vector< std::string > submatrix_strings_
Definition: nnet-compute.h:155
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::vector< std::string > command_strings_
Definition: nnet-compute.h:157
static BaseFloat MatrixStddev(const CuMatrixBase< BaseFloat > &m)
Definition: nnet-compute.cc:68
#define KALDI_LOG
Definition: kaldi-error.h:153
CuSubMatrix< BaseFloat > GetSubMatrix(int32 submatrix_index)
bool IsWholeMatrix(int32 submatrix_index) const

◆ DebugBeforeExecute()

void DebugBeforeExecute ( int32  command,
CommandDebugInfo info 
)
private

Definition at line 82 of file nnet-compute.cc.

References NnetComputation::Command::arg1, NnetComputer::command_attributes_, NnetComputation::Command::command_type, NnetComputation::commands, NnetComputer::CommandDebugInfo::components_parameter_stddev, NnetComputer::computation_, Nnet::GetComponent(), NnetComputer::GetSubMatrix(), rnnlm::i, NnetComputation::IsWholeMatrix(), kaldi::nnet3::kBackprop, kaldi::nnet3::kUpdatableComponent, NnetComputer::matrices_, NnetComputer::CommandDebugInfo::matrices_written_stddevs, NnetComputer::MatrixStddev(), NnetComputer::nnet_, NnetComputer::ParameterStddev(), Component::Properties(), and NnetComputer::CommandDebugInfo::submatrices_written_stddevs.

Referenced by NnetComputer::Run().

83  {
84  {
85  const std::vector<int32> &matrices_written =
86  command_attributes_[command].matrices_written;
87  size_t size = matrices_written.size();
88  info->matrices_written_stddevs.resize(size);
89  for (size_t i = 0; i < size; i++) {
90  int32 m = matrices_written[i];
91  info->matrices_written_stddevs[i] = MatrixStddev(matrices_[m]);
92  }
93  }
94  {
95  const std::vector<int32> &submatrices_written =
96  command_attributes_[command].submatrices_written;
97  size_t size = submatrices_written.size();
98  info->submatrices_written_stddevs.resize(size);
99  for (size_t i = 0; i < size; i++) {
100  int32 s = submatrices_written[i];
101  if (!computation_.IsWholeMatrix(s)) {
102  const CuSubMatrix<BaseFloat> submat(GetSubMatrix(s));
103  info->submatrices_written_stddevs[i] = MatrixStddev(submat);
104  }
105  }
106  }
107  const NnetComputation::Command &c = computation_.commands[command];
108  if (c.command_type == kBackprop) {
109  const Component *component = nnet_.GetComponent(c.arg1);
110  if (component->Properties() & kUpdatableComponent)
111  info->components_parameter_stddev = ParameterStddev(*component);
112  }
113 }
std::vector< CommandAttributes > command_attributes_
Definition: nnet-compute.h:153
kaldi::int32 int32
std::vector< Command > commands
static BaseFloat ParameterStddev(const Component &c)
Definition: nnet-compute.cc:75
const NnetComputation & computation_
Definition: nnet-compute.h:133
Component * GetComponent(int32 c)
Return component indexed c. Not a copy; not owned by caller.
Definition: nnet-nnet.cc:150
std::vector< CuMatrix< BaseFloat > > matrices_
Definition: nnet-compute.h:160
static BaseFloat MatrixStddev(const CuMatrixBase< BaseFloat > &m)
Definition: nnet-compute.cc:68
CuSubMatrix< BaseFloat > GetSubMatrix(int32 submatrix_index)
bool IsWholeMatrix(int32 submatrix_index) const

◆ ExecuteCommand()

void ExecuteCommand ( )
private

Definition at line 210 of file nnet-compute.cc.

References CuMatrixBase< Real >::AddMat(), CuMatrixBase< Real >::AddRowRanges(), CuMatrixBase< Real >::AddRows(), CuMatrixBase< Real >::AddToRows(), NnetComputation::Command::alpha, NnetComputation::Command::arg1, NnetComputation::Command::arg2, NnetComputation::Command::arg3, NnetComputation::Command::arg4, NnetComputation::Command::arg5, NnetComputation::Command::arg6, NnetComputation::Command::arg7, Component::Backprop(), NnetComputer::command_strings_, NnetComputation::Command::command_type, NnetComputation::commands, NnetComputation::component_precomputed_indexes, NnetComputer::compressed_matrices_, NnetComputer::computation_, CuMatrixBase< Real >::CopyFromMat(), CuMatrixBase< Real >::CopyRows(), CuCompressedMatrixBase::CopyToMat(), CuMatrixBase< Real >::CopyToRows(), NnetComputer::debug_, Component::DeleteMemo(), NnetComputation::GetCommandStrings(), Nnet::GetComponent(), Nnet::GetComponentName(), NnetComputer::GetMemo(), NnetComputer::GetPointers(), NnetComputer::GetSubMatrix(), NnetComputation::indexes_cuda, NnetComputation::indexes_ranges_cuda, kaldi::nnet3::kAddRowRanges, kaldi::nnet3::kAddRows, kaldi::nnet3::kAddRowsMulti, kaldi::nnet3::kAddToRowsMulti, KALDI_ASSERT, KALDI_ERR, KALDI_LOG, KALDI_WARN, 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::kSetConst, kaldi::nnet3::kSwapMatrix, kaldi::kUndefined, kaldi::nnet3::kUpdatableComponent, NnetComputation::matrices, NnetComputer::matrices_, NnetComputation::need_model_derivative, kaldi::NewCuCompressedMatrix(), NnetComputer::nnet_, NnetComputer::nnet_to_store_stats_, NnetComputer::nnet_to_update_, CuCompressedMatrixBase::NumCols(), CuMatrixBase< Real >::NumCols(), CuCompressedMatrixBase::NumRows(), NVTX_RANGE, NnetComputer::program_counter_, Component::Propagate(), Component::Properties(), NnetComputer::SaveMemo(), CuMatrixBase< Real >::Scale(), CuMatrixBase< Real >::Set(), CuMatrixBase< Real >::SetZero(), Component::StoreStats(), and NnetComputation::submatrices.

Referenced by NnetComputer::Run().

210  {
211  const NnetComputation::Command &c = computation_.commands[program_counter_];
212  int32 m1, m2;
213  try {
214  switch (c.command_type) {
215  case kAllocMatrix:
216  m1 = computation_.submatrices[c.arg1].matrix_index;
217  matrices_[m1].Resize(computation_.matrices[m1].num_rows,
218  computation_.matrices[m1].num_cols,
219  kUndefined,
220  computation_.matrices[m1].stride_type);
221  break;
222  case kDeallocMatrix:
223  m1 = computation_.submatrices[c.arg1].matrix_index;
224  matrices_[m1].Resize(0, 0);
225  break;
226  case kSwapMatrix:
227  m1 = computation_.submatrices[c.arg1].matrix_index;
228  m2 = computation_.submatrices[c.arg2].matrix_index;
229  matrices_[m1].Swap(&(matrices_[m2]));
230  break;
231  case kSetConst: {
232  CuSubMatrix<BaseFloat> s(GetSubMatrix(c.arg1));
233  if (c.alpha == 0.0) s.SetZero();
234  else s.Set(c.alpha);
235  break;
236  }
237  case kPropagate: {
238  NVTX_RANGE("NnetComputer::ExecuteCommand::kPropagate");
239  const Component *component = nnet_.GetComponent(c.arg1);
240  ComponentPrecomputedIndexes *indexes =
242  const CuSubMatrix<BaseFloat> input(GetSubMatrix(c.arg3));
243  CuSubMatrix<BaseFloat> output(GetSubMatrix(c.arg4));
244  void *memo = component->Propagate(indexes, input, &output);
245  if (c.arg6) { // need to store stats.
247  Component *stats_component = nnet_to_store_stats_->GetComponent(c.arg1);
248  bool was_in_place = (c.arg3 == c.arg4);
249  // if propagate was in-place, provide empty matrix and not 'input', as
250  // input is no longer valid.
251  const CuSubMatrix<BaseFloat> maybe_input(
252  GetSubMatrix(was_in_place ? 0 : c.arg3));
253  stats_component->StoreStats(maybe_input, output, memo);
254  }
255  SaveMemo(c.arg5, *component, memo);
256  break;
257  }
258  case kBackprop:
259  case kBackpropNoModelUpdate: {
260  NVTX_RANGE("NnetComputer::ExecuteCommand::kBackpropNoModelUpdate");
261  std::ostringstream debug_str;
262  KALDI_ASSERT(nnet_to_update_ != NULL);
263  debug_str << nnet_.GetComponentName(c.arg1);
264  const Component *component = nnet_.GetComponent(c.arg1);
266  Component *upd_component = NULL;
267  if (c.command_type == kBackprop) { // this block sets 'upd_component'
268  Nnet *nnet_to_update;
269  if (component->Properties()&kUpdatableComponent) {
270  nnet_to_update = (computation_.need_model_derivative ?
271  nnet_to_update_ : NULL);
272  } else {
273  // Some non-updatable components, such as CompositeComponent, store
274  // stats in the backprop. For other types of non-updatable
275  // component, this arg won't matter.
276  nnet_to_update = nnet_to_store_stats_;
277  }
278  if (nnet_to_update)
279  upd_component = nnet_to_update->GetComponent(c.arg1);
280  }
281  ComponentPrecomputedIndexes *indexes =
283  const CuSubMatrix<BaseFloat> in_value(GetSubMatrix(c.arg3));
284  const CuSubMatrix<BaseFloat> out_value(GetSubMatrix(c.arg4));
285  const CuSubMatrix<BaseFloat> out_deriv(GetSubMatrix(c.arg5));
286  CuSubMatrix<BaseFloat> in_deriv(GetSubMatrix(c.arg6));
287  void *memo = GetMemo(c.arg7);
288  component->Backprop(debug_str.str(), indexes,
289  in_value, out_value, out_deriv,
290  memo, upd_component,
291  c.arg6 == 0 ? NULL : &in_deriv);
292  if (memo != NULL)
293  component->DeleteMemo(memo);
294  break;
295  }
296  case kMatrixCopy: {
297  CuSubMatrix<BaseFloat> dest(GetSubMatrix(c.arg1));
298  const CuSubMatrix<BaseFloat> src(GetSubMatrix(c.arg2));
299  dest.CopyFromMat(src);
300  if (c.alpha != 1.0)
301  dest.Scale(c.alpha); // note: in principle in future we could write a
302  // kernel which would do this in one operation.
303  break;
304  }
305  case kMatrixAdd: {
306  CuSubMatrix<BaseFloat> dest(GetSubMatrix(c.arg1));
307  const CuSubMatrix<BaseFloat> src(GetSubMatrix(c.arg2));
308  dest.AddMat(c.alpha, src);
309  break;
310  }
311  case kAddRows: {
312  CuSubMatrix<BaseFloat> dest(GetSubMatrix(c.arg1));
313  const CuSubMatrix<BaseFloat> src(GetSubMatrix(c.arg2));
314  const CuArray<int32> &indexes = computation_.indexes_cuda[c.arg3];
315  dest.AddRows(c.alpha, src, indexes);
316  break;
317  }
318  case kCopyRows: {
319  CuSubMatrix<BaseFloat> dest(GetSubMatrix(c.arg1));
320  const CuSubMatrix<BaseFloat> src(GetSubMatrix(c.arg2));
321  const CuArray<int32> &indexes = computation_.indexes_cuda[c.arg3];
322  BaseFloat alpha = c.alpha;
323  if (alpha != 1.0) { // for now we're faking the 'alpha' thing because the CopyRows
324  if (alpha == 0.0) break; // command doesn't take that argument.
325  dest.Scale(1.0 / alpha);
326  dest.CopyRows(src, indexes);
327  dest.Scale(c.alpha);
328  } else {
329  dest.CopyRows(src, indexes);
330  }
331  break;
332  }
333  case kCopyRowsMulti: {
334  CuSubMatrix<BaseFloat> dest(GetSubMatrix(c.arg1));
335  CuArray<const BaseFloat*> pointers;
336  GetPointers(c.arg2, dest.NumCols(), &pointers);
337  BaseFloat alpha = c.alpha;
338  if (alpha != 1.0) { // for now we're faking the 'alpha' thing because the CopyRows
339  if (alpha == 0.0) break; // command doesn't take that argument.
340  dest.Scale(1.0 / alpha);
341  dest.CopyRows(pointers);
342  dest.Scale(c.alpha);
343  } else {
344  dest.CopyRows(pointers);
345  }
346  break;
347  }
348  case kCopyToRowsMulti: {
349  // If c.alpha is not 1.0, this command is not supported.
350  KALDI_ASSERT(c.alpha == 1.0);
351  CuSubMatrix<BaseFloat> src(GetSubMatrix(c.arg1));
352  CuArray<BaseFloat*> pointers;
353  GetPointers(c.arg2, src.NumCols(), &pointers);
354  src.CopyToRows(pointers);
355  break;
356  }
357  case kAddRowsMulti: {
358  CuSubMatrix<BaseFloat> dest(GetSubMatrix(c.arg1));
359  CuArray<const BaseFloat*> pointers;
360  GetPointers(c.arg2, dest.NumCols(), &pointers);
361  dest.AddRows(c.alpha, pointers);
362  break;
363  }
364  case kAddToRowsMulti: {
365  CuSubMatrix<BaseFloat> src(GetSubMatrix(c.arg1));
366  CuArray<BaseFloat*> pointers;
367  GetPointers(c.arg2, src.NumCols(), &pointers);
368  src.AddToRows(c.alpha, pointers);
369  break;
370  }
371  case kAddRowRanges: {
372  CuSubMatrix<BaseFloat> dest(GetSubMatrix(c.arg1));
373  const CuSubMatrix<BaseFloat> src(GetSubMatrix(c.arg2));
374  const CuArray<Int32Pair> &pairs = computation_.indexes_ranges_cuda[c.arg3];
375  BaseFloat alpha = c.alpha;
376  if (alpha != 1.0) { // for now we're faking the 'alpha' thing
377  // because the AddRowRanges
378  if (alpha == 0.0) break; // command doesn't take that argument.
379  dest.Scale(1.0 / alpha);
380  dest.AddRowRanges(src, pairs);
381  dest.Scale(c.alpha);
382  } else {
383  dest.AddRowRanges(src, pairs);
384  }
385  break;
386  }
387  case kCompressMatrix:
388  // This does nothing if CUDA is not in use.
389 #if HAVE_CUDA == 1
390  if (CuDevice::Instantiate().Enabled()) {
391  if (compressed_matrices_.empty())
392  compressed_matrices_.resize(matrices_.size(), NULL);
393  int32 m = computation_.submatrices[c.arg1].matrix_index;
394  KALDI_ASSERT(compressed_matrices_[m] == NULL &&
395  matrices_[m].NumRows() != 0);
396  BaseFloat range = c.alpha;
397  bool truncate = (c.arg3 != 0);
399  static_cast<CuCompressedMatrixType>(c.arg2),
400  range, truncate);
401  compressed_matrices_[m]->CopyFromMat(matrices_[m]);
402  matrices_[m].Resize(0, 0);
403  }
404 #endif
405  break;
406  case kDecompressMatrix:
407 #if HAVE_CUDA == 1
408  if (CuDevice::Instantiate().Enabled()) {
409  int32 m = computation_.submatrices[c.arg1].matrix_index;
410  CuCompressedMatrixBase *compressed_matrix =
412  KALDI_ASSERT(compressed_matrix != NULL &&
413  matrices_[m].NumRows() == 0);
414  matrices_[m].Resize(compressed_matrix->NumRows(),
415  compressed_matrix->NumCols(),
416  kUndefined,
417  computation_.matrices[m].stride_type);
418  compressed_matrix->CopyToMat(&(matrices_[m]));
419  delete compressed_matrix;
420  compressed_matrices_[m] = NULL;
421  }
422 #endif
423  break;
425  case kNoOperationLabel:
426  break;
427  case kGotoLabel:
428  KALDI_ASSERT(computation_.commands[c.arg1].command_type == kNoOperationLabel);
429  program_counter_ = c.arg1;
430  break;
431  default:
432  KALDI_ERR << "Invalid command in computation";
433  }
434  } catch (...) {
435  if (!debug_) {
436  std::string preamble;
438  KALDI_WARN << "Printing some background info since error was detected";
439  KALDI_LOG << preamble;
440  for (int32 prev_c = 0; prev_c < program_counter_; prev_c++)
441  KALDI_LOG << command_strings_[prev_c];
442  }
443  // the following will re-throw the error, but now we've printed more info
444  // about what went wrong.
445  KALDI_ERR << "Error running command " << command_strings_[program_counter_];
446  }
447 }
void GetPointers(int32 indexes_multi_index, int32 num_cols, CuArray< BaseFloat *> *pointers)
void * GetMemo(int32 memo_index)
kaldi::int32 int32
std::vector< MatrixInfo > matrices
std::vector< Command > commands
float BaseFloat
Definition: kaldi-types.h:29
std::vector< CuCompressedMatrixBase * > compressed_matrices_
Definition: nnet-compute.h:173
void GetCommandStrings(const Nnet &nnet, std::string *preamble, std::vector< std::string > *command_strings) const
std::vector< CuArray< int32 > > indexes_cuda
std::vector< SubMatrixInfo > submatrices
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
std::vector< CuArray< Int32Pair > > indexes_ranges_cuda
const std::string & GetComponentName(int32 component_index) const
returns individual component name.
Definition: nnet-nnet.cc:689
const NnetComputation & computation_
Definition: nnet-compute.h:133
Component * GetComponent(int32 c)
Return component indexed c. Not a copy; not owned by caller.
Definition: nnet-nnet.cc:150
std::vector< CuMatrix< BaseFloat > > matrices_
Definition: nnet-compute.h:160
std::vector< PrecomputedIndexesInfo > component_precomputed_indexes
void SaveMemo(int32 memo_index, const Component &c, void *memo)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
#define NVTX_RANGE(name)
Definition: cu-common.h:143
std::vector< std::string > command_strings_
Definition: nnet-compute.h:157
#define KALDI_LOG
Definition: kaldi-error.h:153
CuSubMatrix< BaseFloat > GetSubMatrix(int32 submatrix_index)
CuCompressedMatrixBase * NewCuCompressedMatrix(CuCompressedMatrixType t, BaseFloat range, bool truncat)
This function allocates a new CuCompressedMatrix with type determined by t, and with the &#39;range&#39; and ...

◆ GetIoMatrixIndex()

int32 GetIoMatrixIndex ( const std::string &  node_name,
bool  is_output 
)
private

Definition at line 620 of file nnet-compute.cc.

References NnetComputation::commands, NnetComputer::computation_, Nnet::GetNodeIndex(), rnnlm::i, NnetComputation::IsWholeMatrix(), kaldi::nnet3::kAcceptInput, KALDI_ERR, kaldi::nnet3::kNoOperationMarker, kaldi::nnet3::kProvideOutput, NnetComputer::nnet_, NnetComputer::pending_commands_, NnetComputer::program_counter_, and NnetComputation::submatrices.

Referenced by NnetComputer::AcceptInput(), NnetComputer::GetOutput(), and NnetComputer::GetOutputDestructive().

620  {
621  const std::vector<NnetComputation::Command> &c = computation_.commands;
622  int32 node_index = nnet_.GetNodeIndex(node_name);
623  if (node_index == -1)
624  KALDI_ERR << "No node named '" << node_name << "'in network.";
625  // first make sure all the I/O commands that we immediately expect, are listed
626  // in 'pending_commands_'.
627  while (program_counter_ < static_cast<int32>(computation_.commands.size()) &&
628  ((c[program_counter_].command_type == kAcceptInput ||
629  c[program_counter_].command_type == kProvideOutput ||
630  c[program_counter_].command_type == kNoOperationMarker))) {
631  if (c[program_counter_].command_type != kNoOperationMarker)
634  }
635  for (size_t i = 0; i < pending_commands_.size(); i++) {
636  int32 command = pending_commands_[i];
637  bool this_command_is_output =
638  (c[command].command_type == kProvideOutput);
639  int32 this_submatrix_index = c[command].arg1,
640  this_node_index = c[command].arg2;
641  if (this_command_is_output == is_output && node_index == this_node_index) {
642  if (!is_output) {
643  pending_commands_.erase(pending_commands_.begin() + i);
644  // don't erase the command for outputs, as that would prevent things
645  // from being output twice, which is an unnecessary restriction.
646  }
647  if (!(computation_.IsWholeMatrix(this_submatrix_index)))
648  KALDI_ERR << "Getting input or output that is not a whole matrix "
649  << "(probably some optimization code needs to be changed)";
650  return computation_.submatrices[this_submatrix_index].matrix_index;
651  }
652  }
653  // if you get the following error it will likely be a bug in the calling code,
654  // or possibly due to giving the wrong egs.
655  KALDI_ERR << "Could not "
656  << (is_output ? "provide output " : "accept input ")
657  << "for network node " << node_name
658  << " (it is not expected at this point in the computation)";
659  return 0; // Suppress compiler warnings; this line will never be reached.
660 }
kaldi::int32 int32
std::vector< Command > commands
std::vector< int32 > pending_commands_
Definition: nnet-compute.h:141
std::vector< SubMatrixInfo > submatrices
#define KALDI_ERR
Definition: kaldi-error.h:147
const NnetComputation & computation_
Definition: nnet-compute.h:133
int32 GetNodeIndex(const std::string &node_name) const
returns index associated with this node name, or -1 if no such index.
Definition: nnet-nnet.cc:466
bool IsWholeMatrix(int32 submatrix_index) const

◆ GetMemo()

void * GetMemo ( int32  memo_index)
inlineprivate

Definition at line 176 of file nnet-compute.cc.

References KALDI_ERR, and NnetComputer::memos_.

Referenced by NnetComputer::ExecuteCommand().

176  {
177  if (memo_index == 0) {
178  return NULL;
179  } else {
180  if (static_cast<size_t>(memo_index) >= memos_.size())
181  KALDI_ERR << "Memo requested that was not generated.";
182  void *ans = memos_[memo_index];
183  memos_[memo_index] = NULL;
184  return ans;
185  }
186 }
#define KALDI_ERR
Definition: kaldi-error.h:147
std::vector< void * > memos_
Definition: nnet-compute.h:165

◆ GetOutput()

const CuMatrixBase< BaseFloat > & GetOutput ( const std::string &  node_name)

◆ GetOutputDestructive()

void GetOutputDestructive ( const std::string &  output_name,
CuMatrix< BaseFloat > *  output 
)

Definition at line 587 of file nnet-compute.cc.

References NnetComputer::GetIoMatrixIndex(), KALDI_ASSERT, and NnetComputer::matrices_.

Referenced by DecodableNnetLoopedOnlineBase::AdvanceChunk(), DecodableNnetSimpleLooped::AdvanceChunk(), NnetBatchComputer::Compute(), BatchedXvectorComputer::ComputeOneBatch(), DecodableNnetSimple::DoNnetComputation(), and kaldi::nnet3::RunNnetComputation().

588  {
589  bool is_output = true;
590  int32 matrix_index = GetIoMatrixIndex(node_name, is_output);
591  KALDI_ASSERT(matrices_[matrix_index].NumRows() != 0);
592  matrices_[matrix_index].Swap(output);
593  matrices_[matrix_index].Resize(0, 0);
594 }
kaldi::int32 int32
int32 GetIoMatrixIndex(const std::string &node_name, bool is_output)
std::vector< CuMatrix< BaseFloat > > matrices_
Definition: nnet-compute.h:160
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ GetPointers() [1/2]

void GetPointers ( int32  indexes_multi_index,
int32  num_cols,
CuArray< BaseFloat *> *  pointers 
)
private

Definition at line 459 of file nnet-compute.cc.

References NnetComputer::computation_, CuArray< T >::CopyFromVec(), CuMatrixBase< Real >::Data(), NnetComputer::GetSubMatrix(), rnnlm::i, NnetComputation::indexes_multi, KALDI_ASSERT, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), kaldi::RandInt(), and CuMatrixBase< Real >::Stride().

Referenced by NnetComputer::ExecuteCommand(), and NnetComputer::GetPointers().

461  {
462  KALDI_ASSERT(static_cast<size_t>(indexes_multi_index)
463  < computation_.indexes_multi.size());
464  const std::vector<std::pair<int32,int32> > &pairs =
465  computation_.indexes_multi[indexes_multi_index];
466  int32 size = pairs.size();
467  std::vector<BaseFloat*> vec(size);
468 
469  // the map "lookup" maps from submatrix index to the Data()
470  // pointer of that submatrix, and the corresponding Stride().
471  unordered_map<int32, std::pair<BaseFloat*, int32> > lookup;
472 
473  for (int32 i = 0; i < size; i++) {
474  int32 submatrix_index = pairs[i].first, row = pairs[i].second;
475  if (submatrix_index != -1) {
476  unordered_map<int32, std::pair<BaseFloat*, int32> >::iterator
477  iter = lookup.find(submatrix_index);
478  if (iter == lookup.end()) {
479  CuSubMatrix<BaseFloat> m = GetSubMatrix(submatrix_index);
480  lookup[submatrix_index] = std::pair<BaseFloat*, int32>(m.Data(),
481  m.Stride());
482  iter = lookup.find(submatrix_index);
483  }
484  BaseFloat *data = iter->second.first;
485  int32 stride = iter->second.second;
486  vec[i] = data + (row * stride);
487  } else {
488  // -1 is a marker that will be translated to NULL.
489  vec[i] = NULL;
490  }
491  }
492 #ifdef KALDI_PARANOID
493  for (int32 i = 0; i < size; i += 30 + RandInt(0, 9)) {
494  // Do a pseudo-random spot check that the row-indexes are not out of range.
495  int32 submatrix_index = pairs[i].first, row = pairs[i].second;
496  if (submatrix_index != -1) {
497  CuSubMatrix<BaseFloat> m = GetSubMatrix(submatrix_index);
498  KALDI_ASSERT(row >= 0 && row < m.NumRows() && num_cols == m.NumCols());
499  }
500  }
501 #endif
502  pointers->CopyFromVec(vec);
503 }
kaldi::int32 int32
float BaseFloat
Definition: kaldi-types.h:29
std::vector< std::vector< std::pair< int32, int32 > > > indexes_multi
const NnetComputation & computation_
Definition: nnet-compute.h:133
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
CuSubMatrix< BaseFloat > GetSubMatrix(int32 submatrix_index)
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95

◆ GetPointers() [2/2]

void GetPointers ( int32  indexes_multi_index,
int32  num_cols,
CuArray< const BaseFloat *> *  pointers 
)
private

Definition at line 505 of file nnet-compute.cc.

References NnetComputer::GetPointers().

507  {
508  GetPointers(indexes_multi_index, num_cols,
509  reinterpret_cast<CuArray<BaseFloat*>*>(pointers));
510 }
void GetPointers(int32 indexes_multi_index, int32 num_cols, CuArray< BaseFloat *> *pointers)

◆ GetSubMatrix()

CuSubMatrix< BaseFloat > GetSubMatrix ( int32  submatrix_index)
private

Definition at line 449 of file nnet-compute.cc.

References NnetComputer::computation_, KALDI_PARANOID_ASSERT, NnetComputer::matrices_, and NnetComputation::submatrices.

Referenced by NnetComputer::DebugAfterExecute(), NnetComputer::DebugBeforeExecute(), NnetComputer::ExecuteCommand(), and NnetComputer::GetPointers().

449  {
450  KALDI_PARANOID_ASSERT(static_cast<size_t>(submatrix_index) <
451  computation_.submatrices.size());
452  const NnetComputation::SubMatrixInfo &info =
453  computation_.submatrices[submatrix_index];
454  const CuMatrix<BaseFloat> &mat = matrices_[info.matrix_index];
455  return CuSubMatrix<BaseFloat>(
456  mat, info.row_offset, info.num_rows, info.col_offset, info.num_cols);
457 }
std::vector< SubMatrixInfo > submatrices
#define KALDI_PARANOID_ASSERT(cond)
Definition: kaldi-error.h:206
const NnetComputation & computation_
Definition: nnet-compute.h:133
std::vector< CuMatrix< BaseFloat > > matrices_
Definition: nnet-compute.h:160

◆ Init()

void Init ( )
private

Definition at line 48 of file nnet-compute.cc.

References NnetComputer::command_attributes_, NnetComputer::command_strings_, NnetComputer::computation_, kaldi::nnet3::ComputeCommandAttributes(), NnetComputeOptions::debug, NnetComputer::debug_, NnetComputation::GetCommandStrings(), NnetComputation::GetSubmatrixStrings(), kaldi::GetVerboseLevel(), NnetComputation::indexes, NnetComputation::indexes_cuda, NnetComputation::indexes_ranges, NnetComputation::indexes_ranges_cuda, ComputationVariables::Init(), KALDI_ASSERT, KALDI_LOG, NnetComputation::matrices, NnetComputer::matrices_, NnetComputer::nnet_, NnetComputer::options_, and NnetComputer::submatrix_strings_.

Referenced by NnetComputer::NnetComputer().

48  {
51  "You must call NnetComputation::ComputeCudaIndexes() before "
52  "executing the computation.");
53  matrices_.resize(computation_.matrices.size());
54  debug_ = (options_.debug || GetVerboseLevel() >= 5);
55  if (debug_) {
56  ComputationVariables variables;
57  variables.Init(computation_);
60  std::string preamble;
62  KALDI_LOG << preamble;
64  }
65 }
int32 GetVerboseLevel()
Get verbosity level, usually set via command line &#39;–verbose=&#39; switch.
Definition: kaldi-error.h:60
const NnetComputeOptions & options_
Definition: nnet-compute.h:132
std::vector< CommandAttributes > command_attributes_
Definition: nnet-compute.h:153
std::vector< MatrixInfo > matrices
void ComputeCommandAttributes(const Nnet &nnet, const NnetComputation &computation, const ComputationVariables &vars, std::vector< CommandAttributes > *attributes)
void GetCommandStrings(const Nnet &nnet, std::string *preamble, std::vector< std::string > *command_strings) const
std::vector< CuArray< int32 > > indexes_cuda
std::vector< CuArray< Int32Pair > > indexes_ranges_cuda
const NnetComputation & computation_
Definition: nnet-compute.h:133
std::vector< CuMatrix< BaseFloat > > matrices_
Definition: nnet-compute.h:160
std::vector< std::string > submatrix_strings_
Definition: nnet-compute.h:155
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
std::vector< std::string > command_strings_
Definition: nnet-compute.h:157
std::vector< std::vector< int32 > > indexes
#define KALDI_LOG
Definition: kaldi-error.h:153
void GetSubmatrixStrings(const Nnet &nnet, std::vector< std::string > *submat_strings) const
std::vector< std::vector< std::pair< int32, int32 > > > indexes_ranges

◆ MatrixStddev()

BaseFloat MatrixStddev ( const CuMatrixBase< BaseFloat > &  m)
staticprivate

Definition at line 68 of file nnet-compute.cc.

References kaldi::kTrans, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), and kaldi::TraceMatMat().

Referenced by NnetComputer::DebugAfterExecute(), and NnetComputer::DebugBeforeExecute().

68  {
69  if (m.NumRows() == 0)
70  return 0.0;
71  return std::sqrt(TraceMatMat(m, m, kTrans) / (m.NumRows() * m.NumCols()));
72 }
Real TraceMatMat(const MatrixBase< Real > &A, const MatrixBase< Real > &B, MatrixTransposeType trans)
We need to declare this here as it will be a friend function.

◆ operator=()

NnetComputer& operator= ( const NnetComputer other)
private

◆ ParameterStddev()

BaseFloat ParameterStddev ( const Component c)
staticprivate

Definition at line 75 of file nnet-compute.cc.

References UpdatableComponent::DotProduct(), KALDI_ASSERT, and UpdatableComponent::NumParameters().

Referenced by NnetComputer::DebugAfterExecute(), and NnetComputer::DebugBeforeExecute().

75  {
76  const UpdatableComponent *uc = dynamic_cast<const UpdatableComponent*>(&c);
77  KALDI_ASSERT(uc != NULL &&
78  "Attempting to get parameter stddev of non-updatable component");
79  return std::sqrt(uc->DotProduct(*uc) / uc->NumParameters());
80 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Run()

void Run ( )

This does either the forward or backward computation, depending when it is called (in a typical computation, the first time you call this it will do the forward computation; then you'll take the outputs and provide derivatives; and the second time you call it, it will do the backward computation.

There used to be two separate functions Forward() and Backward().

Definition at line 512 of file nnet-compute.cc.

References NnetComputer::CheckNoPendingIo(), NnetComputation::commands, NnetComputer::computation_, NnetComputer::debug_, NnetComputer::DebugAfterExecute(), NnetComputer::DebugBeforeExecute(), Timer::Elapsed(), NnetComputer::ExecuteCommand(), kaldi::nnet3::kAcceptInput, KALDI_ERR, kaldi::nnet3::kProvideOutput, NnetComputer::nnet_, NVTX_RANGE, NnetComputation::Print(), and NnetComputer::program_counter_.

Referenced by DecodableNnetLoopedOnlineBase::AdvanceChunk(), DecodableNnetSimpleLooped::AdvanceChunk(), NnetDiscriminativeComputeObjf::Compute(), NnetChainComputeProb::Compute(), NnetComputeProb::Compute(), NnetBatchComputer::Compute(), BatchedXvectorComputer::ComputeOneBatch(), DecodableNnetSimple::DoNnetComputation(), kaldi::nnet3::RunNnetComputation(), NnetDiscriminativeTrainer::Train(), NnetChainTrainer::TrainInternal(), NnetTrainer::TrainInternal(), NnetChainTrainer::TrainInternalBackstitch(), NnetTrainer::TrainInternalBackstitch(), kaldi::nnet3::UnitTestNnetCompute(), kaldi::nnet3::UnitTestNnetInputDerivatives(), kaldi::nnet3::UnitTestNnetModelDerivatives(), and kaldi::nnet3::UnitTestNnetOptimizeWithOptions().

512  {
513  NVTX_RANGE(__func__);
514  const std::vector<NnetComputation::Command> &c = computation_.commands;
515  int32 num_commands = c.size();
516 
517  if (program_counter_ >= num_commands) {
518  computation_.Print(std::cerr, nnet_);
519  KALDI_ERR << "Running computation that has finished: program-counter="
520  << program_counter_;
521  }
523 
524  CommandDebugInfo info;
525  Timer timer;
526  double total_elapsed_previous = 0.0;
527 
528  for (; program_counter_ < num_commands; program_counter_++) {
529  if (c[program_counter_].command_type == kAcceptInput ||
530  c[program_counter_].command_type == kProvideOutput) {
531  // We have hit a part of the computation that requires user
532  // interaction, e.g. the end of the forward or backward phase.
533  break;
534  }
535  if (debug_)
537  ExecuteCommand();
538  if (debug_) {
539  double total_elapsed_now = timer.Elapsed();
541  total_elapsed_now - total_elapsed_previous);
542  total_elapsed_previous = total_elapsed_now;
543  }
544  }
545 }
void Print(std::ostream &os, const Nnet &nnet) const
kaldi::int32 int32
std::vector< Command > commands
void DebugBeforeExecute(int32 command, CommandDebugInfo *info)
Definition: nnet-compute.cc:82
#define KALDI_ERR
Definition: kaldi-error.h:147
const NnetComputation & computation_
Definition: nnet-compute.h:133
#define NVTX_RANGE(name)
Definition: cu-common.h:143
void DebugAfterExecute(int32 command, const CommandDebugInfo &info, double command_execution_time)

◆ SaveMemo()

void SaveMemo ( int32  memo_index,
const Component c,
void *  memo 
)
inlineprivate

Definition at line 163 of file nnet-compute.cc.

References Component::DeleteMemo(), and NnetComputer::memos_.

Referenced by NnetComputer::ExecuteCommand().

164  {
165  if (memo_index <= 0) {
166  if (memo != NULL) { // memo was returned but is not needed.
167  c.DeleteMemo(memo);
168  }
169  } else {
170  if (memos_.size() <= static_cast<size_t>(memo_index))
171  memos_.resize(memo_index + 1, NULL);
172  memos_[memo_index] = memo;
173  }
174 }
std::vector< void * > memos_
Definition: nnet-compute.h:165

Member Data Documentation

◆ command_attributes_

std::vector<CommandAttributes> command_attributes_
private

◆ command_strings_

std::vector<std::string> command_strings_
private

◆ compressed_matrices_

std::vector<CuCompressedMatrixBase*> compressed_matrices_
private

Definition at line 173 of file nnet-compute.h.

Referenced by NnetComputer::ExecuteCommand(), and NnetComputer::~NnetComputer().

◆ computation_

◆ debug_

bool debug_
private

◆ matrices_

◆ memos_

std::vector<void*> memos_
private

◆ nnet_

◆ nnet_to_store_stats_

Nnet* nnet_to_store_stats_
private

Definition at line 146 of file nnet-compute.h.

Referenced by NnetComputer::ExecuteCommand().

◆ nnet_to_update_

Nnet* nnet_to_update_
private

Definition at line 150 of file nnet-compute.h.

Referenced by NnetComputer::ExecuteCommand().

◆ options_

const NnetComputeOptions& options_
private

Definition at line 132 of file nnet-compute.h.

Referenced by NnetComputer::Init().

◆ pending_commands_

std::vector<int32> pending_commands_
private

◆ program_counter_

◆ submatrix_strings_

std::vector<std::string> submatrix_strings_
private

Definition at line 155 of file nnet-compute.h.

Referenced by NnetComputer::DebugAfterExecute(), and NnetComputer::Init().


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