TdnnComponent Class Reference

TdnnComponent is a more memory-efficient alternative to manually splicing several frames of input and then using a NaturalGradientAffineComponent or a LinearComponent. More...

#include <nnet-convolutional-component-temp.h>

Inheritance diagram for TdnnComponent:
Collaboration diagram for TdnnComponent:

Classes

class  PrecomputedIndexes
 

Public Member Functions

 TdnnComponent ()
 
 TdnnComponent (const TdnnComponent &other)
 
virtual int32 InputDim () const
 Returns input-dimension of this component. More...
 
virtual int32 OutputDim () const
 Returns output-dimension of this component. More...
 
virtual std::string Info () const
 Returns some text-form information about this component, for diagnostics. More...
 
virtual void InitFromConfig (ConfigLine *cfl)
 Initialize, from a ConfigLine object. More...
 
virtual std::string Type () const
 Returns a string such as "SigmoidComponent", describing the type of the object. More...
 
virtual int32 Properties () const
 Return bitmask of the component's properties. More...
 
virtual void * Propagate (const ComponentPrecomputedIndexes *indexes, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
 Propagate function. More...
 
virtual void Backprop (const std::string &debug_info, const ComponentPrecomputedIndexes *indexes, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, void *memo, Component *to_update, CuMatrixBase< BaseFloat > *in_deriv) const
 Backprop function; depending on which of the arguments 'to_update' and 'in_deriv' are non-NULL, this can compute input-data derivatives and/or perform model update. More...
 
virtual void Read (std::istream &is, bool binary)
 Read function (used after we know the type of the Component); accepts input that is missing the token that describes the component type, in case it has already been consumed. More...
 
virtual void Write (std::ostream &os, bool binary) const
 Write component to stream. More...
 
virtual ComponentCopy () const
 Copies component (deep copy). More...
 
virtual void ReorderIndexes (std::vector< Index > *input_indexes, std::vector< Index > *output_indexes) const
 This function only does something interesting for non-simple Components. More...
 
virtual void GetInputIndexes (const MiscComputationInfo &misc_info, const Index &output_index, std::vector< Index > *desired_indexes) const
 This function only does something interesting for non-simple Components. More...
 
virtual bool IsComputable (const MiscComputationInfo &misc_info, const Index &output_index, const IndexSet &input_index_set, std::vector< Index > *used_inputs) const
 This function only does something interesting for non-simple Components, and it exists to make it possible to manage optionally-required inputs. More...
 
virtual ComponentPrecomputedIndexesPrecomputeIndexes (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. More...
 
virtual void Scale (BaseFloat scale)
 This virtual function when called on – an UpdatableComponent scales the parameters by "scale" when called by an UpdatableComponent. More...
 
virtual void Add (BaseFloat alpha, const Component &other)
 This virtual function when called by – an UpdatableComponent adds the parameters of another updatable component, times some constant, to the current parameters. More...
 
virtual void PerturbParams (BaseFloat stddev)
 This function is to be used in testing. More...
 
virtual BaseFloat DotProduct (const UpdatableComponent &other) const
 Computes dot-product between parameters of two instances of a Component. More...
 
virtual int32 NumParameters () const
 The following new virtual function returns the total dimension of the parameters in this class. More...
 
virtual void Vectorize (VectorBase< BaseFloat > *params) const
 Turns the parameters into vector form. More...
 
virtual void UnVectorize (const VectorBase< BaseFloat > &params)
 Converts the parameters from vector form. More...
 
virtual void FreezeNaturalGradient (bool freeze)
 freezes/unfreezes NaturalGradient updates, if applicable (to be overriden by components that use Natural Gradient). More...
 
CuMatrixBase< BaseFloat > & LinearParams ()
 
CuVector< BaseFloat > & BiasParams ()
 
BaseFloat OrthonormalConstraint () const
 
void ConsolidateMemory ()
 This virtual function relates to memory management, and avoiding fragmentation. More...
 
 TdnnComponent ()
 
 TdnnComponent (const TdnnComponent &other)
 
virtual int32 InputDim () const
 Returns input-dimension of this component. More...
 
virtual int32 OutputDim () const
 Returns output-dimension of this component. More...
 
virtual std::string Info () const
 Returns some text-form information about this component, for diagnostics. More...
 
virtual void InitFromConfig (ConfigLine *cfl)
 Initialize, from a ConfigLine object. More...
 
virtual std::string Type () const
 Returns a string such as "SigmoidComponent", describing the type of the object. More...
 
virtual int32 Properties () const
 Return bitmask of the component's properties. More...
 
virtual void * Propagate (const ComponentPrecomputedIndexes *indexes, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const
 Propagate function. More...
 
virtual void Backprop (const std::string &debug_info, const ComponentPrecomputedIndexes *indexes, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, const CuMatrixBase< BaseFloat > &out_deriv, void *memo, Component *to_update, CuMatrixBase< BaseFloat > *in_deriv) const
 Backprop function; depending on which of the arguments 'to_update' and 'in_deriv' are non-NULL, this can compute input-data derivatives and/or perform model update. More...
 
virtual void Read (std::istream &is, bool binary)
 Read function (used after we know the type of the Component); accepts input that is missing the token that describes the component type, in case it has already been consumed. More...
 
virtual void Write (std::ostream &os, bool binary) const
 Write component to stream. More...
 
virtual ComponentCopy () const
 Copies component (deep copy). More...
 
virtual void ReorderIndexes (std::vector< Index > *input_indexes, std::vector< Index > *output_indexes) const
 This function only does something interesting for non-simple Components. More...
 
virtual void GetInputIndexes (const MiscComputationInfo &misc_info, const Index &output_index, std::vector< Index > *desired_indexes) const
 This function only does something interesting for non-simple Components. More...
 
virtual bool IsComputable (const MiscComputationInfo &misc_info, const Index &output_index, const IndexSet &input_index_set, std::vector< Index > *used_inputs) const
 This function only does something interesting for non-simple Components, and it exists to make it possible to manage optionally-required inputs. More...
 
virtual ComponentPrecomputedIndexesPrecomputeIndexes (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. More...
 
virtual void Scale (BaseFloat scale)
 This virtual function when called on – an UpdatableComponent scales the parameters by "scale" when called by an UpdatableComponent. More...
 
virtual void Add (BaseFloat alpha, const Component &other)
 This virtual function when called by – an UpdatableComponent adds the parameters of another updatable component, times some constant, to the current parameters. More...
 
virtual void PerturbParams (BaseFloat stddev)
 This function is to be used in testing. More...
 
virtual BaseFloat DotProduct (const UpdatableComponent &other) const
 Computes dot-product between parameters of two instances of a Component. More...
 
virtual int32 NumParameters () const
 The following new virtual function returns the total dimension of the parameters in this class. More...
 
virtual void Vectorize (VectorBase< BaseFloat > *params) const
 Turns the parameters into vector form. More...
 
virtual void UnVectorize (const VectorBase< BaseFloat > &params)
 Converts the parameters from vector form. More...
 
virtual void FreezeNaturalGradient (bool freeze)
 freezes/unfreezes NaturalGradient updates, if applicable (to be overriden by components that use Natural Gradient). More...
 
CuMatrixBase< BaseFloat > & LinearParams ()
 
CuVector< BaseFloat > & BiasParams ()
 
BaseFloat OrthonormalConstraint () const
 
void ConsolidateMemory ()
 This virtual function relates to memory management, and avoiding fragmentation. More...
 
- Public Member Functions inherited from UpdatableComponent
 UpdatableComponent (const UpdatableComponent &other)
 
 UpdatableComponent ()
 
virtual ~UpdatableComponent ()
 
virtual void SetUnderlyingLearningRate (BaseFloat lrate)
 Sets the learning rate of gradient descent- gets multiplied by learning_rate_factor_. More...
 
virtual void SetActualLearningRate (BaseFloat lrate)
 Sets the learning rate directly, bypassing learning_rate_factor_. More...
 
virtual void SetAsGradient ()
 Sets is_gradient_ to true and sets learning_rate_ to 1, ignoring learning_rate_factor_. More...
 
virtual BaseFloat LearningRateFactor ()
 
virtual void SetLearningRateFactor (BaseFloat lrate_factor)
 
void SetUpdatableConfigs (const UpdatableComponent &other)
 
BaseFloat LearningRate () const
 Gets the learning rate to be used in gradient descent. More...
 
BaseFloat MaxChange () const
 Returns the per-component max-change value, which is interpreted as the maximum change (in l2 norm) in parameters that is allowed per minibatch for this component. More...
 
void SetMaxChange (BaseFloat max_change)
 
BaseFloat L2Regularization () const
 Returns the l2 regularization constant, which may be set in any updatable component (usually from the config file). More...
 
void SetL2Regularization (BaseFloat a)
 
- Public Member Functions inherited from Component
virtual void StoreStats (const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_value, void *memo)
 This function may store stats on average activation values, and for some component types, the average value of the derivative of the nonlinearity. More...
 
virtual void ZeroStats ()
 Components that provide an implementation of StoreStats should also provide an implementation of ZeroStats(), to set those stats to zero. More...
 
virtual void DeleteMemo (void *memo) const
 This virtual function only needs to be overwritten by Components that return a non-NULL memo from their Propagate() function. More...
 
 Component ()
 
virtual ~Component ()
 

Private Member Functions

void Check () const
 
void UpdateNaturalGradient (const PrecomputedIndexes &indexes, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_deriv)
 
void UpdateSimple (const PrecomputedIndexes &indexes, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_deriv)
 
void Check () const
 
void UpdateNaturalGradient (const PrecomputedIndexes &indexes, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_deriv)
 
void UpdateSimple (const PrecomputedIndexes &indexes, const CuMatrixBase< BaseFloat > &in_value, const CuMatrixBase< BaseFloat > &out_deriv)
 

Static Private Member Functions

static CuSubMatrix< BaseFloatGetInputPart (const CuMatrixBase< BaseFloat > &input_matrix, int32 num_output_rows, int32 row_stride, int32 row_offset)
 
static void ModifyComputationIo (time_height_convolution::ConvolutionComputationIo *io)
 
static CuSubMatrix< BaseFloatGetInputPart (const CuMatrixBase< BaseFloat > &input_matrix, int32 num_output_rows, int32 row_stride, int32 row_offset)
 
static void ModifyComputationIo (time_height_convolution::ConvolutionComputationIo *io)
 

Private Attributes

std::vector< int32time_offsets_
 
CuMatrix< BaseFloatlinear_params_
 
CuVector< BaseFloatbias_params_
 
BaseFloat orthonormal_constraint_
 
bool use_natural_gradient_
 
OnlineNaturalGradient preconditioner_in_
 
OnlineNaturalGradient preconditioner_out_
 

Additional Inherited Members

- Static Public Member Functions inherited from Component
static ComponentReadNew (std::istream &is, bool binary)
 Read component from stream (works out its type). Dies on error. More...
 
static ComponentNewComponentOfType (const std::string &type)
 Returns a new Component of the given type e.g. More...
 
- Protected Member Functions inherited from UpdatableComponent
void InitLearningRatesFromConfig (ConfigLine *cfl)
 
std::string ReadUpdatableCommon (std::istream &is, bool binary)
 
void WriteUpdatableCommon (std::ostream &is, bool binary) const
 
- Protected Attributes inherited from UpdatableComponent
BaseFloat learning_rate_
 learning rate (typically 0.0..0.01) More...
 
BaseFloat learning_rate_factor_
 learning rate factor (normally 1.0, but can be set to another < value so that when < you call SetLearningRate(), that value will be scaled by this factor. More...
 
BaseFloat l2_regularize_
 L2 regularization constant. More...
 
bool is_gradient_
 True if this component is to be treated as a gradient rather than as parameters. More...
 
BaseFloat max_change_
 configuration value for imposing max-change More...
 

Detailed Description

TdnnComponent is a more memory-efficient alternative to manually splicing several frames of input and then using a NaturalGradientAffineComponent or a LinearComponent.

It does the splicing of the input itself, using mechanisms similar to what TimeHeightConvolutionComponent uses. The implementation is in nnet-tdnn-component.cc

Parameters inherited from UpdatableComponent (see comment above declaration of UpdadableComponent in nnet-component-itf.h for details): learning-rate, learning-rate-factor, max-change

Important parameters:

input-dim The input feature dimension (before splicing).

output-dim The output feature dimension

time-offsets E.g. time-offsets=-1,0,1 or time-offsets=-3,0,3. The time offsets that we require at the input to produce a given output. comparable to the offsets used in TDNNs. They must be unique (no repeats). use-bias Defaults to true, but set to false if you want this to be linear rather than affine in its input.

Extra parameters: orthonormal-constraint=0.0 If you set this to 1.0, then the linear_params_ matrix will be (approximately) constrained during training to have orthonormal rows (or columns, whichever is fewer).. it turns out the real name for this is a "semi-orthogonal" matrix. You can choose a positive nonzero value different than 1.0 to have a scaled semi-orthgonal matrix, i.e. with singular values at the selected value (e.g. 0.5, or 2.0). This is not enforced inside the component itself; you have to call ConstrainOrthonormal() from the training code to do this. All this component does is return the OrthonormalConstraint() value. If you set this to a negative value, it's like saying "for any value", i.e. it will constrain the parameter matrix to be closer to "any alpha" times a semi-orthogonal matrix, without changing its overall norm.

Initialization parameters: param-stddev Standard deviation of the linear parameters of the convolution. Defaults to sqrt(1.0 / (input-dim * the number of time-offsets)) bias-stddev Standard deviation of bias terms. default=0.0. You should not set this if you set use-bias=false.

Natural-gradient related options are below; you won't normally have to set these as the defaults are reasonable.

use-natural-gradient e.g. use-natural-gradient=false (defaults to true). You can set this to false to disable the natural gradient updates (you won't normally want to do this). rank-out Rank used in low-rank-plus-unit estimate of the Fisher-matrix factor that has the dimension (num-rows of linear_params_), which equals output_dim. It defaults to the minimum of 80, or half of the output dim. rank-in Rank used in low-rank-plus-unit estimate of the Fisher matrix factor which has the dimension (num-cols of the parameter matrix), which is input-dim times the number of time offsets. It defaults to the minimum of 20, or half the num-rows of the parameter matrix. num-samples-history This becomes the 'num_samples_history' configuration value of the natural gradient objects. The default value is 2000.0.

Definition at line 450 of file nnet-convolutional-component-temp.h.

Constructor & Destructor Documentation

◆ TdnnComponent() [1/4]

◆ TdnnComponent() [2/4]

TdnnComponent ( const TdnnComponent other)

Definition at line 38 of file nnet-tdnn-component.cc.

References TdnnComponent::Check().

39  :
40  UpdatableComponent(other), // initialize base-class
41  time_offsets_(other.time_offsets_),
42  linear_params_(other.linear_params_),
43  bias_params_(other.bias_params_),
44  orthonormal_constraint_(other.orthonormal_constraint_),
45  use_natural_gradient_(other.use_natural_gradient_),
46  preconditioner_in_(other.preconditioner_in_),
47  preconditioner_out_(other.preconditioner_out_) {
48  Check();
49 }

◆ TdnnComponent() [3/4]

◆ TdnnComponent() [4/4]

TdnnComponent ( const TdnnComponent other)

Member Function Documentation

◆ Add() [1/2]

void Add ( BaseFloat  alpha,
const Component other 
)
virtual

This virtual function when called by – an UpdatableComponent adds the parameters of another updatable component, times some constant, to the current parameters.

– a NonlinearComponent (or another component that stores stats, like BatchNormComponent)– it relates to adding stats. Otherwise it will normally do nothing.

Reimplemented from Component.

Definition at line 610 of file nnet-tdnn-component.cc.

References TdnnComponent::bias_params_, KALDI_ASSERT, and TdnnComponent::linear_params_.

611  {
612  const TdnnComponent *other =
613  dynamic_cast<const TdnnComponent*>(&other_in);
614  KALDI_ASSERT(other != NULL);
615  linear_params_.AddMat(alpha, other->linear_params_);
616  if (bias_params_.Dim() != 0)
617  bias_params_.AddVec(alpha, other->bias_params_);
618 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Add() [2/2]

virtual void Add ( BaseFloat  alpha,
const Component other 
)
virtual

This virtual function when called by – an UpdatableComponent adds the parameters of another updatable component, times some constant, to the current parameters.

– a NonlinearComponent (or another component that stores stats, like BatchNormComponent)– it relates to adding stats. Otherwise it will normally do nothing.

Reimplemented from Component.

◆ Backprop() [1/2]

void Backprop ( const std::string &  debug_info,
const ComponentPrecomputedIndexes indexes,
const CuMatrixBase< BaseFloat > &  in_value,
const CuMatrixBase< BaseFloat > &  out_value,
const CuMatrixBase< BaseFloat > &  out_deriv,
void *  memo,
Component to_update,
CuMatrixBase< BaseFloat > *  in_deriv 
) const
virtual

Backprop function; depending on which of the arguments 'to_update' and 'in_deriv' are non-NULL, this can compute input-data derivatives and/or perform model update.

Parameters
[in]debug_infoThe component name, to be printed out in any warning messages.
[in]indexesA pointer to some information output by this class's PrecomputeIndexes function (will be NULL for simple components, i.e. those that don't do things like splicing).
[in]in_valueThe matrix that was given as input to the Propagate function. Will be ignored (and may be empty) if Properties()&kBackpropNeedsInput == 0.
[in]out_valueThe matrix that was output from the Propagate function. Will be ignored (and may be empty) if Properties()&kBackpropNeedsOutput == 0
[in]out_derivThe derivative at the output of this component.
[in]memoThis will normally be NULL, but for component types that set the flag kUsesMemo, this will be the return value of the Propagate() function that corresponds to this Backprop() function. Ownership of any pointers is not transferred to the Backprop function; DeleteMemo() will be called to delete it.
[out]to_updateIf model update is desired, the Component to be updated, else NULL. Does not have to be identical to this. If supplied, you can assume that to_update->Properties() & kUpdatableComponent is nonzero.
[out]in_derivThe derivative at the input of this component, if needed (else NULL). If Properties()&kBackpropInPlace, may be the same matrix as out_deriv. If Properties()&kBackpropAdds, this is added to by the Backprop routine, else it is set. The component code chooses which mode to work in, based on convenience.

Implements Component.

Definition at line 213 of file nnet-tdnn-component.cc.

References CuMatrixBase< Real >::AddMatMat(), TdnnComponent::GetInputPart(), rnnlm::i, TdnnComponent::InputDim(), UpdatableComponent::is_gradient_, KALDI_ASSERT, kaldi::kNoTrans, UpdatableComponent::learning_rate_, TdnnComponent::linear_params_, CuMatrixBase< Real >::NumRows(), NVTX_RANGE, TdnnComponent::PrecomputedIndexes::row_offsets, TdnnComponent::PrecomputedIndexes::row_stride, TdnnComponent::time_offsets_, TdnnComponent::UpdateNaturalGradient(), TdnnComponent::UpdateSimple(), and TdnnComponent::use_natural_gradient_.

221  {
222  NVTX_RANGE("TdnnComponent::Backprop");
223  const PrecomputedIndexes *indexes =
224  dynamic_cast<const PrecomputedIndexes*>(indexes_in);
225  KALDI_ASSERT(indexes != NULL &&
226  indexes->row_offsets.size() == time_offsets_.size());
227  int32 num_offsets = time_offsets_.size(),
228  input_dim = InputDim();
229 
230  if (in_deriv != NULL) {
231  // Propagate the derivatives back to the input data.
232  for (int32 i = 0; i < num_offsets; i++) {
233  CuSubMatrix<BaseFloat> in_deriv_part =
234  GetInputPart(*in_deriv, out_deriv.NumRows(),
235  indexes->row_stride, indexes->row_offsets[i]);
236  CuSubMatrix<BaseFloat> linear_params_part(linear_params_,
237  0, linear_params_.NumRows(),
238  i * input_dim, input_dim);
239  // note: this component has the property kBackpropAdds, which is why the
240  // final 1.0 is there in the following call (otherwise we'd have to zero
241  // *in_deriv first).
242  in_deriv_part.AddMatMat(1.0, out_deriv, kNoTrans,
243  linear_params_part, kNoTrans, 1.0);
244  }
245  }
246 
247  if (to_update_in != NULL) {
248  TdnnComponent *to_update =
249  dynamic_cast<TdnnComponent*>(to_update_in);
250  KALDI_ASSERT(to_update != NULL);
251 
252  if (to_update->learning_rate_ == 0.0)
253  return;
254 
255  if (to_update->is_gradient_ || !to_update->use_natural_gradient_)
256  to_update->UpdateSimple(*indexes, in_value, out_deriv);
257  else
258  to_update->UpdateNaturalGradient(*indexes, in_value, out_deriv);
259  }
260 }
kaldi::int32 int32
static CuSubMatrix< BaseFloat > GetInputPart(const CuMatrixBase< BaseFloat > &input_matrix, int32 num_output_rows, int32 row_stride, int32 row_offset)
virtual int32 InputDim() const
Returns input-dimension of this component.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
#define NVTX_RANGE(name)
Definition: cu-common.h:143

◆ Backprop() [2/2]

virtual void Backprop ( const std::string &  debug_info,
const ComponentPrecomputedIndexes indexes,
const CuMatrixBase< BaseFloat > &  in_value,
const CuMatrixBase< BaseFloat > &  out_value,
const CuMatrixBase< BaseFloat > &  out_deriv,
void *  memo,
Component to_update,
CuMatrixBase< BaseFloat > *  in_deriv 
) const
virtual

Backprop function; depending on which of the arguments 'to_update' and 'in_deriv' are non-NULL, this can compute input-data derivatives and/or perform model update.

Parameters
[in]debug_infoThe component name, to be printed out in any warning messages.
[in]indexesA pointer to some information output by this class's PrecomputeIndexes function (will be NULL for simple components, i.e. those that don't do things like splicing).
[in]in_valueThe matrix that was given as input to the Propagate function. Will be ignored (and may be empty) if Properties()&kBackpropNeedsInput == 0.
[in]out_valueThe matrix that was output from the Propagate function. Will be ignored (and may be empty) if Properties()&kBackpropNeedsOutput == 0
[in]out_derivThe derivative at the output of this component.
[in]memoThis will normally be NULL, but for component types that set the flag kUsesMemo, this will be the return value of the Propagate() function that corresponds to this Backprop() function. Ownership of any pointers is not transferred to the Backprop function; DeleteMemo() will be called to delete it.
[out]to_updateIf model update is desired, the Component to be updated, else NULL. Does not have to be identical to this. If supplied, you can assume that to_update->Properties() & kUpdatableComponent is nonzero.
[out]in_derivThe derivative at the input of this component, if needed (else NULL). If Properties()&kBackpropInPlace, may be the same matrix as out_deriv. If Properties()&kBackpropAdds, this is added to by the Backprop routine, else it is set. The component code chooses which mode to work in, based on convenience.

Implements Component.

◆ BiasParams() [1/2]

◆ BiasParams() [2/2]

CuVector<BaseFloat>& BiasParams ( )
inline

◆ Check() [1/2]

void Check ( ) const
private

Definition at line 52 of file nnet-tdnn-component.cc.

References TdnnComponent::bias_params_, KALDI_ASSERT, TdnnComponent::linear_params_, and TdnnComponent::time_offsets_.

Referenced by TdnnComponent::Read(), and TdnnComponent::TdnnComponent().

52  {
53  KALDI_ASSERT(linear_params_.NumRows() > 0 &&
54  !time_offsets_.empty() &&
55  std::set<int32>(time_offsets_.begin(),
56  time_offsets_.end()).size() ==
57  time_offsets_.size() &&
58  linear_params_.NumCols() % time_offsets_.size() == 0 &&
59  (bias_params_.Dim() == 0 ||
60  bias_params_.Dim() == linear_params_.NumRows()));
61 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Check() [2/2]

void Check ( ) const
private

◆ ConsolidateMemory() [1/2]

void ConsolidateMemory ( )
virtual

This virtual function relates to memory management, and avoiding fragmentation.

It is called only once per model, after we do the first minibatch of training. The default implementation does nothing, but it can be overridden by child classes, where it may re-initialize certain quantities that may possibly have been allocated during the forward pass (e.g. certain statistics; OnlineNaturalGradient objects). We use our own CPU-based allocator (see cu-allocator.h) and since it can't do paging since we're not in control of the GPU page table, fragmentation can be a problem. The allocator always tries to put things in 'low-address memory' (i.e. at smaller memory addresses) near the beginning of the block it allocated, to avoid fragmentation; but if permanent things (belonging to the model) are allocated in the forward pass, they can permanently stay in high memory. This function helps to prevent that, by re-allocating those things into low-address memory (It's important that it's called after all the temporary buffers for the forward-backward have been freed, so that there is low-address memory available)).

Reimplemented from Component.

Definition at line 700 of file nnet-tdnn-component.cc.

References TdnnComponent::preconditioner_in_, TdnnComponent::preconditioner_out_, and OnlineNaturalGradient::Swap().

700  {
701  OnlineNaturalGradient temp_in(preconditioner_in_);
702  preconditioner_in_.Swap(&temp_in);
703  OnlineNaturalGradient temp_out(preconditioner_out_);
704  preconditioner_out_.Swap(&temp_out);
705 }
void Swap(OnlineNaturalGradient *other)

◆ ConsolidateMemory() [2/2]

void ConsolidateMemory ( )
virtual

This virtual function relates to memory management, and avoiding fragmentation.

It is called only once per model, after we do the first minibatch of training. The default implementation does nothing, but it can be overridden by child classes, where it may re-initialize certain quantities that may possibly have been allocated during the forward pass (e.g. certain statistics; OnlineNaturalGradient objects). We use our own CPU-based allocator (see cu-allocator.h) and since it can't do paging since we're not in control of the GPU page table, fragmentation can be a problem. The allocator always tries to put things in 'low-address memory' (i.e. at smaller memory addresses) near the beginning of the block it allocated, to avoid fragmentation; but if permanent things (belonging to the model) are allocated in the forward pass, they can permanently stay in high memory. This function helps to prevent that, by re-allocating those things into low-address memory (It's important that it's called after all the temporary buffers for the forward-backward have been freed, so that there is low-address memory available)).

Reimplemented from Component.

◆ Copy() [1/2]

◆ Copy() [2/2]

◆ DotProduct() [1/2]

BaseFloat DotProduct ( const UpdatableComponent other) const
virtual

Computes dot-product between parameters of two instances of a Component.

Can be used for computing parameter-norm of an UpdatableComponent.

Implements UpdatableComponent.

Definition at line 632 of file nnet-tdnn-component.cc.

References TdnnComponent::bias_params_, KALDI_ASSERT, kaldi::kTrans, TdnnComponent::linear_params_, kaldi::TraceMatMat(), and kaldi::VecVec().

633  {
634  const TdnnComponent *other =
635  dynamic_cast<const TdnnComponent*>(&other_in);
636  KALDI_ASSERT(other != NULL);
637  BaseFloat ans = TraceMatMat(linear_params_, other->linear_params_, kTrans);
638  if (bias_params_.Dim() != 0)
639  ans += VecVec(bias_params_, other->bias_params_);
640  return ans;
641 }
float BaseFloat
Definition: kaldi-types.h:29
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.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
Real VecVec(const VectorBase< Real > &a, const VectorBase< Real > &b)
Returns dot product between v1 and v2.
Definition: kaldi-vector.cc:37

◆ DotProduct() [2/2]

virtual BaseFloat DotProduct ( const UpdatableComponent other) const
virtual

Computes dot-product between parameters of two instances of a Component.

Can be used for computing parameter-norm of an UpdatableComponent.

Implements UpdatableComponent.

◆ FreezeNaturalGradient() [1/2]

void FreezeNaturalGradient ( bool  freeze)
virtual

freezes/unfreezes NaturalGradient updates, if applicable (to be overriden by components that use Natural Gradient).

Reimplemented from UpdatableComponent.

Definition at line 669 of file nnet-tdnn-component.cc.

References OnlineNaturalGradient::Freeze(), TdnnComponent::preconditioner_in_, and TdnnComponent::preconditioner_out_.

◆ FreezeNaturalGradient() [2/2]

virtual void FreezeNaturalGradient ( bool  freeze)
virtual

freezes/unfreezes NaturalGradient updates, if applicable (to be overriden by components that use Natural Gradient).

Reimplemented from UpdatableComponent.

◆ GetInputIndexes() [1/2]

virtual void GetInputIndexes ( const MiscComputationInfo misc_info,
const Index output_index,
std::vector< Index > *  desired_indexes 
) const
virtual

This function only does something interesting for non-simple Components.

For a given index at the output of the component, tells us what indexes are required at its input (note: "required" encompasses also optionally-required things; it will enumerate all things that we'd like to have). See also IsComputable().

Parameters
[in]misc_infoThis argument is supplied to handle things that the framework can't very easily supply: information like which time indexes are needed for AggregateComponent, which time-indexes are available at the input of a recurrent network, and so on. We will add members to misc_info as needed.
[in]output_indexThe Index at the output of the component, for which we are requesting the list of indexes at the component's input.
[out]desired_indexesA list of indexes that are desired at the input. are to be written to here. By "desired" we mean required or optionally-required.

The default implementation of this function is suitable for any SimpleComponent; it just copies the output_index to a single identical element in input_indexes.

Reimplemented from Component.

◆ GetInputIndexes() [2/2]

void GetInputIndexes ( const MiscComputationInfo misc_info,
const Index output_index,
std::vector< Index > *  desired_indexes 
) const
virtual

This function only does something interesting for non-simple Components.

For a given index at the output of the component, tells us what indexes are required at its input (note: "required" encompasses also optionally-required things; it will enumerate all things that we'd like to have). See also IsComputable().

Parameters
[in]misc_infoThis argument is supplied to handle things that the framework can't very easily supply: information like which time indexes are needed for AggregateComponent, which time-indexes are available at the input of a recurrent network, and so on. We will add members to misc_info as needed.
[in]output_indexThe Index at the output of the component, for which we are requesting the list of indexes at the component's input.
[out]desired_indexesA list of indexes that are desired at the input. are to be written to here. By "desired" we mean required or optionally-required.

The default implementation of this function is suitable for any SimpleComponent; it just copies the output_index to a single identical element in input_indexes.

Reimplemented from Component.

Definition at line 457 of file nnet-tdnn-component.cc.

References rnnlm::i, KALDI_ASSERT, kaldi::nnet3::kNoTime, Index::n, Index::t, TdnnComponent::time_offsets_, and Index::x.

460  {
461  KALDI_ASSERT(output_index.t != kNoTime);
462  size_t size = time_offsets_.size();
463  desired_indexes->resize(size);
464  for (size_t i = 0; i < size; i++) {
465  (*desired_indexes)[i].n = output_index.n;
466  (*desired_indexes)[i].t = output_index.t + time_offsets_[i];
467  (*desired_indexes)[i].x = output_index.x;
468  }
469 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
const int kNoTime
Definition: nnet-common.cc:573

◆ GetInputPart() [1/2]

static CuSubMatrix<BaseFloat> GetInputPart ( const CuMatrixBase< BaseFloat > &  input_matrix,
int32  num_output_rows,
int32  row_stride,
int32  row_offset 
)
staticprivate

◆ GetInputPart() [2/2]

CuSubMatrix< BaseFloat > GetInputPart ( const CuMatrixBase< BaseFloat > &  input_matrix,
int32  num_output_rows,
int32  row_stride,
int32  row_offset 
)
staticprivate

Definition at line 500 of file nnet-tdnn-component.cc.

References CuMatrixBase< Real >::Data(), KALDI_ASSERT, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), and CuMatrixBase< Real >::Stride().

Referenced by TdnnComponent::Backprop(), TdnnComponent::Propagate(), TdnnComponent::UpdateNaturalGradient(), and TdnnComponent::UpdateSimple().

504  {
505  KALDI_ASSERT(row_offset >= 0 && row_stride >= 1 &&
506  input_matrix.NumRows() >=
507  row_offset + (row_stride * num_output_rows) - (row_stride - 1));
508  // constructor takes args: (data, num_rows, num_cols, stride).
509  return CuSubMatrix<BaseFloat>(
510  input_matrix.Data() + input_matrix.Stride() * row_offset,
511  num_output_rows,
512  input_matrix.NumCols(),
513  input_matrix.Stride() * row_stride);
514 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Info() [1/2]

virtual std::string Info ( ) const
virtual

Returns some text-form information about this component, for diagnostics.

Starts with the type of the component. E.g. "SigmoidComponent dim=900", although most components will have much more info.

Reimplemented from UpdatableComponent.

◆ Info() [2/2]

std::string Info ( ) const
virtual

Returns some text-form information about this component, for diagnostics.

Starts with the type of the component. E.g. "SigmoidComponent dim=900", although most components will have much more info.

Reimplemented from UpdatableComponent.

Definition at line 63 of file nnet-tdnn-component.cc.

References TdnnComponent::bias_params_, OnlineNaturalGradient::GetAlpha(), OnlineNaturalGradient::GetNumSamplesHistory(), OnlineNaturalGradient::GetRank(), OnlineNaturalGradient::GetUpdatePeriod(), kaldi::GetVerboseLevel(), rnnlm::i, UpdatableComponent::Info(), TdnnComponent::linear_params_, TdnnComponent::orthonormal_constraint_, TdnnComponent::preconditioner_in_, TdnnComponent::preconditioner_out_, kaldi::nnet3::PrintParameterStats(), TdnnComponent::time_offsets_, and TdnnComponent::use_natural_gradient_.

63  {
64  std::ostringstream stream;
65  stream << UpdatableComponent::Info();
66  if (orthonormal_constraint_ != 0.0)
67  stream << ", orthonormal-constraint=" << orthonormal_constraint_;
68  stream << ", time-offsets=";
69  for (size_t i = 0; i < time_offsets_.size(); i++) {
70  if (i != 0) stream << ',';
71  stream << time_offsets_[i];
72  }
73  PrintParameterStats(stream, "linear-params", linear_params_,
74  false, // include_mean
75  true, // include_row_norms
76  true, // include_column_norms
77  GetVerboseLevel() >= 2); // include_singular_values
78  if (bias_params_.Dim() == 0) {
79  stream << ", has-bias=false";
80  } else {
81  PrintParameterStats(stream, "bias", bias_params_, true);
82  }
83  if (!use_natural_gradient_) {
84  stream << ", use-natural-gradient=false";
85  } else {
86  stream << ", rank-in=" << preconditioner_in_.GetRank()
87  << ", rank-out=" << preconditioner_out_.GetRank()
88  << ", num-samples-history=" << preconditioner_in_.GetNumSamplesHistory()
89  << ", update-period=" << preconditioner_in_.GetUpdatePeriod()
90  << ", alpha-in=" << preconditioner_in_.GetAlpha()
91  << ", alpha-out=" << preconditioner_out_.GetAlpha();
92  }
93  return stream.str();
94 }
int32 GetVerboseLevel()
Get verbosity level, usually set via command line &#39;–verbose=&#39; switch.
Definition: kaldi-error.h:60
virtual std::string Info() const
Returns some text-form information about this component, for diagnostics.
void PrintParameterStats(std::ostringstream &os, const std::string &name, const CuVectorBase< BaseFloat > &params, bool include_mean)
Print to &#39;os&#39; some information about the mean and standard deviation of some parameters, used in Info() functions in nnet-simple-component.cc.
Definition: nnet-parse.cc:157

◆ InitFromConfig() [1/2]

void InitFromConfig ( ConfigLine cfl)
virtual

Initialize, from a ConfigLine object.

Parameters
[in]cflA ConfigLine containing any parameters that are needed for initialization. For example: "dim=100 param-stddev=0.1"

Implements Component.

Definition at line 97 of file nnet-tdnn-component.cc.

References TdnnComponent::bias_params_, ConfigLine::GetValue(), UpdatableComponent::InitLearningRatesFromConfig(), KALDI_ERR, TdnnComponent::linear_params_, TdnnComponent::orthonormal_constraint_, TdnnComponent::preconditioner_in_, TdnnComponent::preconditioner_out_, OnlineNaturalGradient::SetAlpha(), OnlineNaturalGradient::SetNumSamplesHistory(), OnlineNaturalGradient::SetRank(), OnlineNaturalGradient::SetUpdatePeriod(), kaldi::SplitStringToIntegers(), TdnnComponent::time_offsets_, TdnnComponent::use_natural_gradient_, and ConfigLine::WholeLine().

97  {
98  // 1. Config values inherited from UpdatableComponent.
100 
101  // 2. Structural config values
102  std::string time_offsets;
103 
104  int32 input_dim = -1, output_dim = -1;
105 
106  bool ok = cfl->GetValue("time-offsets", &time_offsets) &&
107  cfl->GetValue("input-dim", &input_dim) &&
108  cfl->GetValue("output-dim", &output_dim);
109  if (!ok || input_dim <= 0 || output_dim <= 0 ||
110  !SplitStringToIntegers(time_offsets, ",", false, &time_offsets_) ||
111  time_offsets_.empty()) {
112  KALDI_ERR << "Bad initializer: there is a problem with "
113  "time-offsets, input-dim or output-dim (not defined?): "
114  << cfl->WholeLine();
115  }
116 
117  if (std::set<int32>(time_offsets_.begin(),
118  time_offsets_.end()).size() != time_offsets_.size()) {
119  KALDI_ERR << "Bad initializer: repeated time-offsets: "
120  << cfl->WholeLine();
121  }
122 
123  // 3. Parameter-initialization configs, "has-bias", and
124  // orthonormal-constraint.
126  BaseFloat param_stddev = -1, bias_mean = 0.0, bias_stddev = 1.0;
127  bool use_bias = true;
128  cfl->GetValue("param-stddev", &param_stddev);
129  cfl->GetValue("bias-stddev", &bias_stddev);
130  cfl->GetValue("bias-mean", &bias_mean);
131  cfl->GetValue("use-bias", &use_bias);
132  cfl->GetValue("orthonormal-constraint", &orthonormal_constraint_);
133  if (param_stddev < 0.0) {
134  param_stddev = 1.0 / sqrt(input_dim * time_offsets_.size());
135  }
136  // initialize the parameters.
137  linear_params_.Resize(output_dim,
138  input_dim * time_offsets_.size());
139  linear_params_.SetRandn();
140  linear_params_.Scale(param_stddev);
141 
142  if (use_bias) {
143  bias_params_.Resize(output_dim);
144  bias_params_.SetRandn();
145  bias_params_.Scale(bias_stddev);
146  bias_params_.Add(bias_mean);
147  } else {
148  bias_params_.Resize(0);
149  }
150 
151  // 4. Natural-gradient related configs.
152  use_natural_gradient_ = true;
153  int32 rank_out = -1, rank_in = -1;
154  BaseFloat alpha_out = 4.0, alpha_in = 4.0,
155  num_samples_history = 2000.0;
156  cfl->GetValue("use-natural-gradient", &use_natural_gradient_);
157  cfl->GetValue("rank-in", &rank_in);
158  cfl->GetValue("rank-out", &rank_out);
159  cfl->GetValue("alpha-in", &alpha_in);
160  cfl->GetValue("alpha-out", &alpha_out);
161  cfl->GetValue("num-samples-history", &num_samples_history);
162 
163  int32 spliced_input_dim =
164  input_dim * static_cast<int32>(time_offsets_.size());
165  if (rank_in < 0)
166  rank_in = std::min<int32>(20, (spliced_input_dim + 1) / 2);
167  preconditioner_in_.SetRank(rank_in);
168  if (rank_out < 0)
169  rank_out = std::min<int32>(80, (output_dim + 1) / 2);
170  preconditioner_out_.SetRank(rank_out);
171  preconditioner_in_.SetNumSamplesHistory(num_samples_history);
172  preconditioner_out_.SetNumSamplesHistory(num_samples_history);
173 
174  preconditioner_in_.SetAlpha(alpha_in);
175  preconditioner_out_.SetAlpha(alpha_out);
176 
179 }
bool SplitStringToIntegers(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< I > *out)
Split a string (e.g.
Definition: text-utils.h:68
void SetNumSamplesHistory(BaseFloat num_samples_history)
void InitLearningRatesFromConfig(ConfigLine *cfl)
kaldi::int32 int32
float BaseFloat
Definition: kaldi-types.h:29
#define KALDI_ERR
Definition: kaldi-error.h:147

◆ InitFromConfig() [2/2]

virtual void InitFromConfig ( ConfigLine cfl)
virtual

Initialize, from a ConfigLine object.

Parameters
[in]cflA ConfigLine containing any parameters that are needed for initialization. For example: "dim=100 param-stddev=0.1"

Implements Component.

◆ InputDim() [1/2]

virtual int32 InputDim ( ) const
inlinevirtual

Returns input-dimension of this component.

Implements Component.

Definition at line 459 of file nnet-convolutional-component.h.

References TimeHeightConvolutionComponent::linear_params_.

459  {
460  return linear_params_.NumCols() / static_cast<int32>(time_offsets_.size());
461  }
kaldi::int32 int32

◆ InputDim() [2/2]

virtual int32 InputDim ( ) const
inlinevirtual

Returns input-dimension of this component.

Implements Component.

Definition at line 459 of file nnet-convolutional-component-temp.h.

References TimeHeightConvolutionComponent::linear_params_.

Referenced by TdnnComponent::Backprop(), and TdnnComponent::Propagate().

459  {
460  return linear_params_.NumCols() / static_cast<int32>(time_offsets_.size());
461  }
kaldi::int32 int32

◆ IsComputable() [1/2]

virtual bool IsComputable ( const MiscComputationInfo misc_info,
const Index output_index,
const IndexSet input_index_set,
std::vector< Index > *  used_inputs 
) const
virtual

This function only does something interesting for non-simple Components, and it exists to make it possible to manage optionally-required inputs.

It tells the user whether a given output index is computable from a given set of input indexes, and if so, says which input indexes will be used in the computation.

Implementations of this function are required to have the property that adding an element to "input_index_set" can only ever change IsComputable from false to true, never vice versa.

Parameters
[in]misc_infoSome information specific to the computation, such as minimum and maximum times for certain components to do adaptation on; it's a place to put things that don't easily fit in the framework.
[in]output_indexThe index that is to be computed at the output of this Component.
[in]input_index_setThe set of indexes that is available at the input of this Component.
[out]used_inputsIf this is non-NULL and the output is computable this will be set to the list of input indexes that will actually be used in the computation.
Returns
Returns true iff this output is computable from the provided inputs.

The default implementation of this function is suitable for any SimpleComponent: it just returns true if output_index is in input_index_set, and if so sets used_inputs to vector containing that one Index.

Reimplemented from Component.

◆ IsComputable() [2/2]

bool IsComputable ( const MiscComputationInfo misc_info,
const Index output_index,
const IndexSet input_index_set,
std::vector< Index > *  used_inputs 
) const
virtual

This function only does something interesting for non-simple Components, and it exists to make it possible to manage optionally-required inputs.

It tells the user whether a given output index is computable from a given set of input indexes, and if so, says which input indexes will be used in the computation.

Implementations of this function are required to have the property that adding an element to "input_index_set" can only ever change IsComputable from false to true, never vice versa.

Parameters
[in]misc_infoSome information specific to the computation, such as minimum and maximum times for certain components to do adaptation on; it's a place to put things that don't easily fit in the framework.
[in]output_indexThe index that is to be computed at the output of this Component.
[in]input_index_setThe set of indexes that is available at the input of this Component.
[out]used_inputsIf this is non-NULL and the output is computable this will be set to the list of input indexes that will actually be used in the computation.
Returns
Returns true iff this output is computable from the provided inputs.

The default implementation of this function is suitable for any SimpleComponent: it just returns true if output_index is in input_index_set, and if so sets used_inputs to vector containing that one Index.

Reimplemented from Component.

Definition at line 472 of file nnet-tdnn-component.cc.

References rnnlm::i, KALDI_ASSERT, kaldi::nnet3::kNoTime, Index::t, and TdnnComponent::time_offsets_.

476  {
477  KALDI_ASSERT(output_index.t != kNoTime);
478  size_t size = time_offsets_.size();
479  Index index(output_index);
480 
481  if (used_inputs != NULL) {
482  used_inputs->clear();
483  used_inputs->reserve(size);
484  }
485  for (size_t i = 0; i < size; i++) {
486  index.t = output_index.t + time_offsets_[i];
487  if (input_index_set(index)) {
488  if (used_inputs != NULL) {
489  // This input index is available.
490  used_inputs->push_back(index);
491  }
492  } else {
493  return false;
494  }
495  }
496  return true;
497 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
const int kNoTime
Definition: nnet-common.cc:573

◆ LinearParams() [1/2]

◆ LinearParams() [2/2]

CuMatrixBase<BaseFloat>& LinearParams ( )
inline

◆ ModifyComputationIo() [1/2]

static void ModifyComputationIo ( time_height_convolution::ConvolutionComputationIo io)
staticprivate

◆ ModifyComputationIo() [2/2]

void ModifyComputationIo ( time_height_convolution::ConvolutionComputationIo io)
staticprivate

Definition at line 516 of file nnet-tdnn-component.cc.

References KALDI_ASSERT, rnnlm::n, ConvolutionComputationIo::num_t_in, ConvolutionComputationIo::reorder_t_in, ConvolutionComputationIo::t_step_in, and ConvolutionComputationIo::t_step_out.

Referenced by TdnnComponent::PrecomputeIndexes(), and TdnnComponent::ReorderIndexes().

517  {
518  if (io->t_step_out == 0) {
519  // the 't_step' values may be zero if there was only one (input or output)
520  // index so the time-stride could not be determined. This code fixes them
521  // up in that case. (If there was only one value, the stride is a
522  // don't-care actually).
523  if (io->t_step_in == 0)
524  io->t_step_in = 1;
525  io->t_step_out = io->t_step_in;
526  }
527  // At this point the t_step_{in,out} values will be nonzero.
528  KALDI_ASSERT(io->t_step_out % io->t_step_in == 0);
529  // The following affects the ordering of the input indexes; it allows us to
530  // reshape the input matrix in the way that we need to, in cases where there
531  // is subsampling. See the explanation where the variable was declared in
532  // class ConvolutionComputationIo.
533  io->reorder_t_in = io->t_step_out / io->t_step_in;
534 
535  // make sure that num_t_in is a multiple of io->reorder_t_in by rounding up.
536  int32 n = io->reorder_t_in;
537  io->num_t_in = n * ((io->num_t_in + n - 1) / n);
538 }
kaldi::int32 int32
struct rnnlm::@11::@12 n
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ NumParameters() [1/2]

int32 NumParameters ( ) const
virtual

The following new virtual function returns the total dimension of the parameters in this class.

Reimplemented from UpdatableComponent.

Definition at line 643 of file nnet-tdnn-component.cc.

References TdnnComponent::bias_params_, and TdnnComponent::linear_params_.

Referenced by TdnnComponent::UnVectorize(), and TdnnComponent::Vectorize().

643  {
644  // note: bias_param_.Dim() may actually be zero.
645  return linear_params_.NumRows() * linear_params_.NumCols() +
646  bias_params_.Dim();
647 }

◆ NumParameters() [2/2]

virtual int32 NumParameters ( ) const
virtual

The following new virtual function returns the total dimension of the parameters in this class.

Reimplemented from UpdatableComponent.

◆ OrthonormalConstraint() [1/2]

◆ OrthonormalConstraint() [2/2]

◆ OutputDim() [1/2]

virtual int32 OutputDim ( ) const
inlinevirtual

Returns output-dimension of this component.

Implements Component.

Definition at line 462 of file nnet-convolutional-component.h.

References TimeHeightConvolutionComponent::Info(), TimeHeightConvolutionComponent::InitFromConfig(), and TimeHeightConvolutionComponent::linear_params_.

462 { return linear_params_.NumRows(); }

◆ OutputDim() [2/2]

virtual int32 OutputDim ( ) const
inlinevirtual

◆ PerturbParams() [1/2]

void PerturbParams ( BaseFloat  stddev)
virtual

This function is to be used in testing.

It adds unit noise times "stddev" to the parameters of the component.

Implements UpdatableComponent.

Definition at line 620 of file nnet-tdnn-component.cc.

References TdnnComponent::bias_params_, kaldi::kUndefined, TdnnComponent::linear_params_, CuVectorBase< Real >::SetRandn(), and CuMatrixBase< Real >::SetRandn().

620  {
621  CuMatrix<BaseFloat> temp_mat(linear_params_.NumRows(),
622  linear_params_.NumCols(), kUndefined);
623  temp_mat.SetRandn();
624  linear_params_.AddMat(stddev, temp_mat);
625  if (bias_params_.Dim() != 0) {
626  CuVector<BaseFloat> temp_vec(bias_params_.Dim(), kUndefined);
627  temp_vec.SetRandn();
628  bias_params_.AddVec(stddev, temp_vec);
629  }
630 }

◆ PerturbParams() [2/2]

virtual void PerturbParams ( BaseFloat  stddev)
virtual

This function is to be used in testing.

It adds unit noise times "stddev" to the parameters of the component.

Implements UpdatableComponent.

◆ PrecomputeIndexes() [1/2]

virtual ComponentPrecomputedIndexes* PrecomputeIndexes ( const MiscComputationInfo misc_info,
const std::vector< Index > &  input_indexes,
const std::vector< Index > &  output_indexes,
bool  need_backprop 
) const
virtual

This function must return NULL for simple Components.

Returns a pointer to a class that may contain some precomputed component-specific and computation-specific indexes to be in used in the Propagate and Backprop functions.

Parameters
[in]misc_infoThis argument is supplied to handle things that the framework can't very easily supply: information like which time indexes are needed for AggregateComponent, which time-indexes are available at the input of a recurrent network, and so on. misc_info may not even ever be used here. We will add members to misc_info as needed.
[in]input_indexesA vector of indexes that explains what time-indexes (and other indexes) each row of the in/in_value/in_deriv matrices given to Propagate and Backprop will mean.
[in]output_indexesA vector of indexes that explains what time-indexes (and other indexes) each row of the out/out_value/out_deriv matrices given to Propagate and Backprop will mean.
[in]need_backpropTrue if we might need to do backprop with this component, so that if any different indexes are needed for backprop then those should be computed too.
Returns
Returns a child-class of class ComponentPrecomputedIndexes, or NULL if this component for does not need to precompute any indexes (e.g. if it is a simple component and does not care about indexes).

Reimplemented from Component.

◆ PrecomputeIndexes() [2/2]

ComponentPrecomputedIndexes * PrecomputeIndexes ( const MiscComputationInfo misc_info,
const std::vector< Index > &  input_indexes,
const std::vector< Index > &  output_indexes,
bool  need_backprop 
) const
virtual

This function must return NULL for simple Components.

Returns a pointer to a class that may contain some precomputed component-specific and computation-specific indexes to be in used in the Propagate and Backprop functions.

Parameters
[in]misc_infoThis argument is supplied to handle things that the framework can't very easily supply: information like which time indexes are needed for AggregateComponent, which time-indexes are available at the input of a recurrent network, and so on. misc_info may not even ever be used here. We will add members to misc_info as needed.
[in]input_indexesA vector of indexes that explains what time-indexes (and other indexes) each row of the in/in_value/in_deriv matrices given to Propagate and Backprop will mean.
[in]output_indexesA vector of indexes that explains what time-indexes (and other indexes) each row of the out/out_value/out_deriv matrices given to Propagate and Backprop will mean.
[in]need_backpropTrue if we might need to do backprop with this component, so that if any different indexes are needed for backprop then those should be computed too.
Returns
Returns a child-class of class ComponentPrecomputedIndexes, or NULL if this component for does not need to precompute any indexes (e.g. if it is a simple component and does not care about indexes).

Reimplemented from Component.

Definition at line 540 of file nnet-tdnn-component.cc.

References kaldi::nnet3::time_height_convolution::GetComputationIo(), kaldi::nnet3::time_height_convolution::GetIndexesForComputation(), rnnlm::i, KALDI_ASSERT, TdnnComponent::ModifyComputationIo(), rnnlm::n, kaldi::RandInt(), TdnnComponent::PrecomputedIndexes::row_offsets, TdnnComponent::PrecomputedIndexes::row_stride, and TdnnComponent::time_offsets_.

544  {
545  using namespace time_height_convolution;
546  // The following figures out a regular structure for the input and
547  // output indexes, in case there were gaps (which is unlikely in typical
548  // situations).
549  ConvolutionComputationIo io;
550  GetComputationIo(input_indexes, output_indexes, &io);
551  ModifyComputationIo(&io);
552 
553  if (RandInt(0, 10) == 0) {
554  // Spot check that the provided indexes have the required properties;
555  // this is like calling this->ReorderIndexes() and checking that it
556  // doesn't change anything.
557  std::vector<Index> modified_input_indexes,
558  modified_output_indexes;
559  GetIndexesForComputation(io, input_indexes, output_indexes,
560  &modified_input_indexes,
561  &modified_output_indexes);
562  KALDI_ASSERT(modified_input_indexes == input_indexes &&
563  modified_output_indexes == output_indexes);
564  }
565 
566 
567  PrecomputedIndexes *ans = new PrecomputedIndexes();
568  ans->row_stride = io.reorder_t_in;
569  int32 num_offsets = time_offsets_.size();
570  ans->row_offsets.resize(num_offsets);
571  for (int32 i = 0; i < num_offsets; i++) {
572  // For each offset, work out which row of the input has the same t value as
573  // the first t value in the output plus that offset. That becomes the start
574  // row of the corresponding sub-part of the input.
575  int32 time_offset = time_offsets_[i],
576  required_input_t = io.start_t_out + time_offset,
577  input_t = (required_input_t - io.start_t_in) / io.t_step_in;
578 
579  KALDI_ASSERT(required_input_t == io.start_t_in + io.t_step_in * input_t);
580  // input_t is a kind of normalized time offset in the input, relative to the
581  // first 't' value in the input and divided by the t-step in the input, so
582  // it's the numbering "as if" the input 't' values were numbered from 0,1,2.
583  // To turn input_t into an input row we need to take account of 'reorder_t_in'.
584  // If this is 1 then the input row is input_t times io.num_images.
585  // Otherwise it's a little more complicated and to understand it you should
586  // read the comment where 'reorder_t_in' is declared in convolution.h.
587  // Briefly: the part that is an integer multiple of 'reorder_t_in' gets
588  // multiplied by io.num_images; the remainder does not.
589 
590  int32 n = io.reorder_t_in,
591  input_t_multiple = n * (input_t / n), input_t_remainder = input_t % n;
592  // note: input_t == input_t_multiple + input_t_remainder .
593  int32 input_row_offset = input_t_multiple * io.num_images +
594  input_t_remainder;
595  ans->row_offsets[i] = input_row_offset;
596  }
597  return ans;
598 }
static void ModifyComputationIo(time_height_convolution::ConvolutionComputationIo *io)
kaldi::int32 int32
void GetIndexesForComputation(const ConvolutionComputationIo &io, const std::vector< Index > &orig_input_indexes, const std::vector< Index > &orig_output_indexes, std::vector< Index > *input_indexes, std::vector< Index > *output_indexes)
This function computes the reordered and possibly padded indexes corresponding to the computation in ...
struct rnnlm::@11::@12 n
void GetComputationIo(const std::vector< Index > &input_indexes, const std::vector< Index > &output_indexes, ConvolutionComputationIo *io)
This function takes lists of input and output indexes to a computation (e.g.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
int32 RandInt(int32 min_val, int32 max_val, struct RandomState *state)
Definition: kaldi-math.cc:95

◆ Propagate() [1/2]

virtual void* Propagate ( const ComponentPrecomputedIndexes indexes,
const CuMatrixBase< BaseFloat > &  in,
CuMatrixBase< BaseFloat > *  out 
) const
virtual

Propagate function.

Parameters
[in]indexesA pointer to some information output by this class's PrecomputeIndexes function (will be NULL for simple components, i.e. those that don't do things like splicing).
[in]inThe input to this component. Num-columns == InputDim().
[out]outThe output of this component. Num-columns == OutputDim(). Note: output of this component will be added to the initial value of "out" if Properties()&kPropagateAdds != 0; otherwise the output will be set and the initial value ignored. Each Component chooses whether it is more convenient implementation-wise to add or set, and the calling code has to deal with it.
Returns
Normally returns NULL, but may return a non-NULL value for components which have the flag kUsesMemo set. This value will be passed into the corresponding Backprop routine.

Implements Component.

◆ Propagate() [2/2]

void * Propagate ( const ComponentPrecomputedIndexes indexes,
const CuMatrixBase< BaseFloat > &  in,
CuMatrixBase< BaseFloat > *  out 
) const
virtual

Propagate function.

Parameters
[in]indexesA pointer to some information output by this class's PrecomputeIndexes function (will be NULL for simple components, i.e. those that don't do things like splicing).
[in]inThe input to this component. Num-columns == InputDim().
[out]outThe output of this component. Num-columns == OutputDim(). Note: output of this component will be added to the initial value of "out" if Properties()&kPropagateAdds != 0; otherwise the output will be set and the initial value ignored. Each Component chooses whether it is more convenient implementation-wise to add or set, and the calling code has to deal with it.
Returns
Normally returns NULL, but may return a non-NULL value for components which have the flag kUsesMemo set. This value will be passed into the corresponding Backprop routine.

Implements Component.

Definition at line 181 of file nnet-tdnn-component.cc.

References CuMatrixBase< Real >::AddMatMat(), TdnnComponent::bias_params_, CuMatrixBase< Real >::CopyRowsFromVec(), TdnnComponent::GetInputPart(), rnnlm::i, TdnnComponent::InputDim(), KALDI_ASSERT, kaldi::kNoTrans, kaldi::kTrans, TdnnComponent::linear_params_, CuMatrixBase< Real >::NumRows(), TdnnComponent::PrecomputedIndexes::row_offsets, TdnnComponent::PrecomputedIndexes::row_stride, and TdnnComponent::time_offsets_.

184  {
185  const PrecomputedIndexes *indexes =
186  dynamic_cast<const PrecomputedIndexes*>(indexes_in);
187  KALDI_ASSERT(indexes != NULL);
188 
189  if (bias_params_.Dim() != 0)
190  out->CopyRowsFromVec(bias_params_);
191  // if bias_params_.Dim() == 0 we don't need to zero 'out' at
192  // this point because in that case we set the flag kPropagateAdds,
193  // so the calling code knows that the Propagate function *adds to*
194  // the 'out' matrix, so it should (typicaly) be zeroed before calling
195  // Propagate().
196 
197  KALDI_ASSERT(indexes->row_offsets.size() == time_offsets_.size());
198 
199  int32 num_offsets = time_offsets_.size(),
200  input_dim = InputDim();
201  for (int32 i = 0; i < num_offsets; i++) {
202  CuSubMatrix<BaseFloat> in_part = GetInputPart(in, out->NumRows(),
203  indexes->row_stride,
204  indexes->row_offsets[i]);
205  CuSubMatrix<BaseFloat> linear_params_part(linear_params_,
206  0, linear_params_.NumRows(),
207  i * input_dim, input_dim);
208  out->AddMatMat(1.0, in_part, kNoTrans, linear_params_part, kTrans, 1.0);
209  }
210  return NULL;
211 }
kaldi::int32 int32
static CuSubMatrix< BaseFloat > GetInputPart(const CuMatrixBase< BaseFloat > &input_matrix, int32 num_output_rows, int32 row_stride, int32 row_offset)
virtual int32 InputDim() const
Returns input-dimension of this component.
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Properties() [1/2]

◆ Properties() [2/2]

◆ Read() [1/2]

void Read ( std::istream &  is,
bool  binary 
)
virtual

Read function (used after we know the type of the Component); accepts input that is missing the token that describes the component type, in case it has already been consumed.

Implements Component.

Definition at line 410 of file nnet-tdnn-component.cc.

References TdnnComponent::bias_params_, TdnnComponent::Check(), kaldi::nnet3::ExpectToken(), KALDI_ASSERT, TdnnComponent::linear_params_, TdnnComponent::orthonormal_constraint_, TdnnComponent::preconditioner_in_, TdnnComponent::preconditioner_out_, kaldi::ReadBasicType(), kaldi::ReadIntegerVector(), kaldi::ReadToken(), UpdatableComponent::ReadUpdatableCommon(), OnlineNaturalGradient::SetAlpha(), OnlineNaturalGradient::SetNumSamplesHistory(), OnlineNaturalGradient::SetRank(), OnlineNaturalGradient::SetUpdatePeriod(), TdnnComponent::time_offsets_, and TdnnComponent::use_natural_gradient_.

410  {
411  std::string token = ReadUpdatableCommon(is, binary);
412  ExpectToken(is, binary, "<TimeOffsets>");
413  ReadIntegerVector(is, binary, &time_offsets_);
414  ExpectToken(is, binary, "<LinearParams>");
415  linear_params_.Read(is, binary);
416  ExpectToken(is, binary, "<BiasParams>");
417  bias_params_.Read(is, binary);
418  ExpectToken(is, binary, "<OrthonormalConstraint>");
420  ExpectToken(is, binary, "<UseNaturalGradient>");
421  ReadBasicType(is, binary, &use_natural_gradient_);
422  int32 rank_in, rank_out;
423  BaseFloat alpha_in, alpha_out,
424  num_samples_history;
425  ExpectToken(is, binary, "<NumSamplesHistory>");
426  ReadBasicType(is, binary, &num_samples_history);
427  { // This can be simplified after a while. It's to read a format of the model
428  // that was never checked into master, but with which I (Dan) did many of
429  // the experiments while tuning the resnet TDNN-F.
430  std::string token;
431  ReadToken(is, binary, &token);
432  if (token == "<AlphaInOut>") {
433  ReadBasicType(is, binary, &alpha_in);
434  ReadBasicType(is, binary, &alpha_out);
435  } else {
436  KALDI_ASSERT(token == "<Alpha>");
437  ReadBasicType(is, binary, &alpha_in);
438  alpha_out = alpha_in;
439  }
440  }
441  preconditioner_in_.SetAlpha(alpha_in);
442  preconditioner_out_.SetAlpha(alpha_out);
443  ExpectToken(is, binary, "<RankInOut>");
444  ReadBasicType(is, binary, &rank_in);
445  ReadBasicType(is, binary, &rank_out);
446  preconditioner_in_.SetRank(rank_in);
447  preconditioner_out_.SetRank(rank_out);
448  preconditioner_in_.SetNumSamplesHistory(num_samples_history);
449  preconditioner_out_.SetNumSamplesHistory(num_samples_history);
450  // the update periods are not configurable.
453  ExpectToken(is, binary, "</TdnnComponent>");
454  Check();
455 }
void ReadBasicType(std::istream &is, bool binary, T *t)
ReadBasicType is the name of the read function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:55
void SetNumSamplesHistory(BaseFloat num_samples_history)
kaldi::int32 int32
void ReadToken(std::istream &is, bool binary, std::string *str)
ReadToken gets the next token and puts it in str (exception on failure).
Definition: io-funcs.cc:154
float BaseFloat
Definition: kaldi-types.h:29
void ReadIntegerVector(std::istream &is, bool binary, std::vector< T > *v)
Function for reading STL vector of integer types.
Definition: io-funcs-inl.h:232
static void ExpectToken(const std::string &token, const std::string &what_we_are_parsing, const std::string **next_token)
std::string ReadUpdatableCommon(std::istream &is, bool binary)
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ Read() [2/2]

virtual void Read ( std::istream &  is,
bool  binary 
)
virtual

Read function (used after we know the type of the Component); accepts input that is missing the token that describes the component type, in case it has already been consumed.

Implements Component.

◆ ReorderIndexes() [1/2]

void ReorderIndexes ( std::vector< Index > *  input_indexes,
std::vector< Index > *  output_indexes 
) const
virtual

This function only does something interesting for non-simple Components.

It provides an opportunity for a Component to reorder the or pad the indexes at its input and output. This might be useful, for instance, if a component requires a particular ordering of the indexes that doesn't correspond to their natural ordering. Components that might modify the indexes are required to return the kReordersIndexes flag in their Properties(). The ReorderIndexes() function is now allowed to insert blanks into the indexes. The 'blanks' must be of the form (n,kNoTime,x), where the marker kNoTime (a very negative number) is there where the 't' indexes normally live. The reason we don't just have, say, (-1,-1,-1), relates to the need to preserve a regular pattern over the 'n' indexes so that 'shortcut compilation' (c.f. ExpandComputation()) can work correctly

Parameters
[in,out]Indexesat the input of the Component.
[in,out]Indexesat the output of the Component

Reimplemented from Component.

Definition at line 351 of file nnet-tdnn-component.cc.

References kaldi::nnet3::time_height_convolution::GetComputationIo(), kaldi::nnet3::time_height_convolution::GetIndexesForComputation(), and TdnnComponent::ModifyComputationIo().

353  {
354  using namespace time_height_convolution;
355 
356  // The following figures out a regular structure for the input and
357  // output indexes, in case there were gaps (which is unlikely in typical
358  // situations).
359  ConvolutionComputationIo io;
360  GetComputationIo(*input_indexes, *output_indexes, &io);
361  ModifyComputationIo(&io);
362 
363  std::vector<Index> modified_input_indexes,
364  modified_output_indexes;
365  // The following call ensures that 'modified_input_indexes' and
366  // 'modified_output_indexes' have the required ordering (where t has the
367  // largest stride and each (n,x) pair is repeated for each 't' value), as well
368  // as doing padding (setting t values to kNoTime where it had to insert
369  // elements to ensure regular structure).
370  GetIndexesForComputation(io, *input_indexes, *output_indexes,
371  &modified_input_indexes,
372  &modified_output_indexes);
373 
374  // It will be quite rare that this function actually changes
375  // 'input_indexes' or 'output_indexes', because in most cases,
376  // the indexes will already have the required structure and
377  // ordering.
378  input_indexes->swap(modified_input_indexes);
379  output_indexes->swap(modified_output_indexes);
380 }
static void ModifyComputationIo(time_height_convolution::ConvolutionComputationIo *io)
void GetIndexesForComputation(const ConvolutionComputationIo &io, const std::vector< Index > &orig_input_indexes, const std::vector< Index > &orig_output_indexes, std::vector< Index > *input_indexes, std::vector< Index > *output_indexes)
This function computes the reordered and possibly padded indexes corresponding to the computation in ...
void GetComputationIo(const std::vector< Index > &input_indexes, const std::vector< Index > &output_indexes, ConvolutionComputationIo *io)
This function takes lists of input and output indexes to a computation (e.g.

◆ ReorderIndexes() [2/2]

virtual void ReorderIndexes ( std::vector< Index > *  input_indexes,
std::vector< Index > *  output_indexes 
) const
virtual

This function only does something interesting for non-simple Components.

It provides an opportunity for a Component to reorder the or pad the indexes at its input and output. This might be useful, for instance, if a component requires a particular ordering of the indexes that doesn't correspond to their natural ordering. Components that might modify the indexes are required to return the kReordersIndexes flag in their Properties(). The ReorderIndexes() function is now allowed to insert blanks into the indexes. The 'blanks' must be of the form (n,kNoTime,x), where the marker kNoTime (a very negative number) is there where the 't' indexes normally live. The reason we don't just have, say, (-1,-1,-1), relates to the need to preserve a regular pattern over the 'n' indexes so that 'shortcut compilation' (c.f. ExpandComputation()) can work correctly

Parameters
[in,out]Indexesat the input of the Component.
[in,out]Indexesat the output of the Component

Reimplemented from Component.

◆ Scale() [1/2]

void Scale ( BaseFloat  scale)
virtual

This virtual function when called on – an UpdatableComponent scales the parameters by "scale" when called by an UpdatableComponent.

– a Nonlinear component (or another component that stores stats, like BatchNormComponent)– it relates to scaling activation stats, not parameters. Otherwise it will normally do nothing.

Reimplemented from Component.

Definition at line 600 of file nnet-tdnn-component.cc.

References TdnnComponent::bias_params_, and TdnnComponent::linear_params_.

Referenced by ModelCollapser::GetScaledComponentIndex().

600  {
601  if (scale == 0.0) {
602  linear_params_.SetZero();
603  bias_params_.SetZero();
604  } else {
605  linear_params_.Scale(scale);
606  bias_params_.Scale(scale);
607  }
608 }

◆ Scale() [2/2]

virtual void Scale ( BaseFloat  scale)
virtual

This virtual function when called on – an UpdatableComponent scales the parameters by "scale" when called by an UpdatableComponent.

– a Nonlinear component (or another component that stores stats, like BatchNormComponent)– it relates to scaling activation stats, not parameters. Otherwise it will normally do nothing.

Reimplemented from Component.

◆ Type() [1/2]

virtual std::string Type ( ) const
inlinevirtual

Returns a string such as "SigmoidComponent", describing the type of the object.

Implements Component.

Definition at line 466 of file nnet-convolutional-component-temp.h.

466 { return "TdnnComponent"; }

◆ Type() [2/2]

virtual std::string Type ( ) const
inlinevirtual

Returns a string such as "SigmoidComponent", describing the type of the object.

Implements Component.

Definition at line 466 of file nnet-convolutional-component.h.

466 { return "TdnnComponent"; }

◆ UnVectorize() [1/2]

void UnVectorize ( const VectorBase< BaseFloat > &  params)
virtual

Converts the parameters from vector form.

Reimplemented from UpdatableComponent.

Definition at line 659 of file nnet-tdnn-component.cc.

References TdnnComponent::bias_params_, VectorBase< Real >::Dim(), KALDI_ASSERT, TdnnComponent::linear_params_, TdnnComponent::NumParameters(), and VectorBase< Real >::Range().

660  {
661  KALDI_ASSERT(params.Dim() == NumParameters());
662  int32 linear_size = linear_params_.NumRows() * linear_params_.NumCols(),
663  bias_size = bias_params_.Dim();
664  linear_params_.CopyRowsFromVec(params.Range(0, linear_size));
665  if (bias_size != 0)
666  bias_params_.CopyFromVec(params.Range(linear_size, bias_size));
667 }
kaldi::int32 int32
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
virtual int32 NumParameters() const
The following new virtual function returns the total dimension of the parameters in this class...

◆ UnVectorize() [2/2]

virtual void UnVectorize ( const VectorBase< BaseFloat > &  params)
virtual

Converts the parameters from vector form.

Reimplemented from UpdatableComponent.

◆ UpdateNaturalGradient() [1/2]

void UpdateNaturalGradient ( const PrecomputedIndexes indexes,
const CuMatrixBase< BaseFloat > &  in_value,
const CuMatrixBase< BaseFloat > &  out_deriv 
)
private

Definition at line 286 of file nnet-tdnn-component.cc.

References TdnnComponent::bias_params_, CuVectorBase< Real >::CopyColFromMat(), TdnnComponent::GetInputPart(), rnnlm::i, kaldi::kNoTrans, kaldi::kTrans, UpdatableComponent::learning_rate_, TdnnComponent::linear_params_, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), NVTX_RANGE, OnlineNaturalGradient::PreconditionDirections(), TdnnComponent::preconditioner_in_, TdnnComponent::preconditioner_out_, TdnnComponent::PrecomputedIndexes::row_offsets, TdnnComponent::PrecomputedIndexes::row_stride, and TdnnComponent::time_offsets_.

Referenced by TdnnComponent::Backprop().

289  {
290  NVTX_RANGE("UpdateNaturalGradient");
291 
292  int32 num_offsets = time_offsets_.size(),
293  num_rows = out_deriv.NumRows(),
294  input_dim = in_value.NumCols(),
295  spliced_input_dim = num_offsets * input_dim,
296  augmented_input_dim =
297  spliced_input_dim + (bias_params_.Dim() != 0 ? 1 : 0);
298 
299  // in_value_temp is the fully spliced input with a column of ones appended to
300  // it.
301  CuMatrix<BaseFloat> in_value_temp(num_rows,
302  augmented_input_dim);
303  if (bias_params_.Dim() != 0) {
304  // set the last column of in_value_temp to 1.0
305  in_value_temp.Range(0, num_rows, spliced_input_dim, 1).Set(1.0);
306  }
307 
308  for (int32 i = 0; i < num_offsets; i++) {
309  CuSubMatrix<BaseFloat> in_value_temp_part(in_value_temp,
310  0, num_rows,
311  i * input_dim, input_dim),
312  in_value_part = GetInputPart(in_value,
313  num_rows,
314  indexes.row_stride,
315  indexes.row_offsets[i]);
316  in_value_temp_part.CopyFromMat(in_value_part);
317  }
318 
319  CuMatrix<BaseFloat> out_deriv_temp(out_deriv);
320 
321  // These "scale" values get will get multiplied into the learning rate (faster
322  // than having the matrices scaled inside the preconditioning code).
323  BaseFloat in_scale, out_scale;
324 
325  preconditioner_in_.PreconditionDirections(&in_value_temp, &in_scale);
326  preconditioner_out_.PreconditionDirections(&out_deriv_temp, &out_scale);
327 
328  // "scale" is a scaling factor coming from the PreconditionDirections calls
329  // (it's faster to have them output a scaling factor than to have them scale
330  // their outputs).
331  BaseFloat scale = in_scale * out_scale,
332  local_lrate = scale * learning_rate_;
333 
334  if (bias_params_.Dim() != 0) {
335  // this "precon_ones" is what happens to the vector of 1's representing
336  // offsets, after multiplication by the preconditioner.
337  CuVector<BaseFloat> precon_ones(num_rows);
338  precon_ones.CopyColFromMat(in_value_temp, spliced_input_dim);
339  bias_params_.AddMatVec(local_lrate, out_deriv_temp, kTrans,
340  precon_ones, 1.0);
341  }
342 
343  CuSubMatrix<BaseFloat> in_value_precon_part(in_value_temp,
344  0, num_rows,
345  0, spliced_input_dim);
346 
347  linear_params_.AddMatMat(local_lrate, out_deriv_temp, kTrans,
348  in_value_precon_part, kNoTrans, 1.0);
349 }
kaldi::int32 int32
static CuSubMatrix< BaseFloat > GetInputPart(const CuMatrixBase< BaseFloat > &input_matrix, int32 num_output_rows, int32 row_stride, int32 row_offset)
float BaseFloat
Definition: kaldi-types.h:29
BaseFloat learning_rate_
learning rate (typically 0.0..0.01)
void PreconditionDirections(CuMatrixBase< BaseFloat > *X, BaseFloat *scale)
This call implements the main functionality of this class.
#define NVTX_RANGE(name)
Definition: cu-common.h:143

◆ UpdateNaturalGradient() [2/2]

void UpdateNaturalGradient ( const PrecomputedIndexes indexes,
const CuMatrixBase< BaseFloat > &  in_value,
const CuMatrixBase< BaseFloat > &  out_deriv 
)
private

◆ UpdateSimple() [1/2]

void UpdateSimple ( const PrecomputedIndexes indexes,
const CuMatrixBase< BaseFloat > &  in_value,
const CuMatrixBase< BaseFloat > &  out_deriv 
)
private

◆ UpdateSimple() [2/2]

void UpdateSimple ( const PrecomputedIndexes indexes,
const CuMatrixBase< BaseFloat > &  in_value,
const CuMatrixBase< BaseFloat > &  out_deriv 
)
private

Definition at line 262 of file nnet-tdnn-component.cc.

References CuMatrixBase< Real >::AddMatMat(), TdnnComponent::bias_params_, TdnnComponent::GetInputPart(), rnnlm::i, kaldi::kNoTrans, kaldi::kTrans, UpdatableComponent::learning_rate_, TdnnComponent::linear_params_, CuMatrixBase< Real >::NumCols(), CuMatrixBase< Real >::NumRows(), NVTX_RANGE, TdnnComponent::PrecomputedIndexes::row_offsets, TdnnComponent::PrecomputedIndexes::row_stride, and TdnnComponent::time_offsets_.

Referenced by TdnnComponent::Backprop().

265  {
266  NVTX_RANGE("UpdateSimple");
267 
268  if (bias_params_.Dim() != 0)
269  bias_params_.AddRowSumMat(learning_rate_, out_deriv);
270 
271  int32 input_dim = in_value.NumCols(),
272  num_offsets = time_offsets_.size();
273  for (int32 i = 0; i < num_offsets; i++) {
274  CuSubMatrix<BaseFloat> in_value_part =
275  GetInputPart(in_value, out_deriv.NumRows(),
276  indexes.row_stride,
277  indexes.row_offsets[i]);
278  CuSubMatrix<BaseFloat> linear_params_part(linear_params_,
279  0, linear_params_.NumRows(),
280  i * input_dim, input_dim);
281  linear_params_part.AddMatMat(learning_rate_, out_deriv, kTrans,
282  in_value_part, kNoTrans, 1.0);
283  }
284 }
kaldi::int32 int32
static CuSubMatrix< BaseFloat > GetInputPart(const CuMatrixBase< BaseFloat > &input_matrix, int32 num_output_rows, int32 row_stride, int32 row_offset)
BaseFloat learning_rate_
learning rate (typically 0.0..0.01)
#define NVTX_RANGE(name)
Definition: cu-common.h:143

◆ Vectorize() [1/2]

void Vectorize ( VectorBase< BaseFloat > *  params) const
virtual

Turns the parameters into vector form.

We put the vector form on the CPU, because in the kinds of situations where we do this, we'll tend to use too much memory for the GPU.

Reimplemented from UpdatableComponent.

Definition at line 649 of file nnet-tdnn-component.cc.

References TdnnComponent::bias_params_, VectorBase< Real >::Dim(), KALDI_ASSERT, TdnnComponent::linear_params_, TdnnComponent::NumParameters(), and VectorBase< Real >::Range().

650  {
651  KALDI_ASSERT(params->Dim() == NumParameters());
652  int32 linear_size = linear_params_.NumRows() * linear_params_.NumCols(),
653  bias_size = bias_params_.Dim();
654  params->Range(0, linear_size).CopyRowsFromMat(linear_params_);
655  if (bias_size != 0)
656  params->Range(linear_size, bias_size).CopyFromVec(bias_params_);
657 }
kaldi::int32 int32
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
virtual int32 NumParameters() const
The following new virtual function returns the total dimension of the parameters in this class...

◆ Vectorize() [2/2]

virtual void Vectorize ( VectorBase< BaseFloat > *  params) const
virtual

Turns the parameters into vector form.

We put the vector form on the CPU, because in the kinds of situations where we do this, we'll tend to use too much memory for the GPU.

Reimplemented from UpdatableComponent.

◆ Write() [1/2]

virtual void Write ( std::ostream &  os,
bool  binary 
) const
virtual

Write component to stream.

Implements Component.

◆ Write() [2/2]

void Write ( std::ostream &  os,
bool  binary 
) const
virtual

Write component to stream.

Implements Component.

Definition at line 382 of file nnet-tdnn-component.cc.

References TdnnComponent::bias_params_, OnlineNaturalGradient::GetAlpha(), OnlineNaturalGradient::GetNumSamplesHistory(), OnlineNaturalGradient::GetRank(), TdnnComponent::linear_params_, TdnnComponent::orthonormal_constraint_, TdnnComponent::preconditioner_in_, TdnnComponent::preconditioner_out_, TdnnComponent::time_offsets_, TdnnComponent::use_natural_gradient_, kaldi::WriteBasicType(), kaldi::WriteIntegerVector(), kaldi::WriteToken(), and UpdatableComponent::WriteUpdatableCommon().

382  {
383  WriteUpdatableCommon(os, binary); // Write opening tag and learning rate.
384  WriteToken(os, binary, "<TimeOffsets>");
385  WriteIntegerVector(os, binary, time_offsets_);
386  WriteToken(os, binary, "<LinearParams>");
387  linear_params_.Write(os, binary);
388  WriteToken(os, binary, "<BiasParams>");
389  bias_params_.Write(os, binary);
390  WriteToken(os, binary, "<OrthonormalConstraint>");
392  WriteToken(os, binary, "<UseNaturalGradient>");
394  int32 rank_in = preconditioner_in_.GetRank(),
395  rank_out = preconditioner_out_.GetRank();
397  alpha_out = preconditioner_out_.GetAlpha(),
398  num_samples_history = preconditioner_in_.GetNumSamplesHistory();
399  WriteToken(os, binary, "<NumSamplesHistory>");
400  WriteBasicType(os, binary, num_samples_history);
401  WriteToken(os, binary, "<AlphaInOut>");
402  WriteBasicType(os, binary, alpha_in);
403  WriteBasicType(os, binary, alpha_out);
404  WriteToken(os, binary, "<RankInOut>");
405  WriteBasicType(os, binary, rank_in);
406  WriteBasicType(os, binary, rank_out);
407  WriteToken(os, binary, "</TdnnComponent>");
408 }
kaldi::int32 int32
float BaseFloat
Definition: kaldi-types.h:29
void WriteToken(std::ostream &os, bool binary, const char *token)
The WriteToken functions are for writing nonempty sequences of non-space characters.
Definition: io-funcs.cc:134
void WriteUpdatableCommon(std::ostream &is, bool binary) const
void WriteIntegerVector(std::ostream &os, bool binary, const std::vector< T > &v)
Function for writing STL vectors of integer types.
Definition: io-funcs-inl.h:198
void WriteBasicType(std::ostream &os, bool binary, T t)
WriteBasicType is the name of the write function for bool, integer types, and floating-point types...
Definition: io-funcs-inl.h:34

Member Data Documentation

◆ bias_params_

◆ linear_params_

◆ orthonormal_constraint_

◆ preconditioner_in_

◆ preconditioner_out_

◆ time_offsets_

◆ use_natural_gradient_


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