Component Class Referenceabstract

Abstract base-class for neural-net components. More...

#include <nnet-component-itf.h>

Inheritance diagram for Component:

Public Member Functions

virtual void * Propagate (const ComponentPrecomputedIndexes *indexes, const CuMatrixBase< BaseFloat > &in, CuMatrixBase< BaseFloat > *out) const =0
 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 =0
 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 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 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 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 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 std::string Type () const =0
 Returns a string such as "SigmoidComponent", describing the type of the object. More...
 
virtual void InitFromConfig (ConfigLine *cfl)=0
 Initialize, from a ConfigLine object. More...
 
virtual int32 InputDim () const =0
 Returns input-dimension of this component. More...
 
virtual int32 OutputDim () const =0
 Returns output-dimension of this component. More...
 
virtual int32 Properties () const =0
 Return bitmask of the component's properties. More...
 
virtual ComponentCopy () const =0
 Copies component (deep copy). More...
 
virtual void Read (std::istream &is, bool binary)=0
 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 =0
 Write component to stream. More...
 
virtual std::string Info () const
 Returns some text-form information about this component, for diagnostics. 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 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...
 
virtual void ConsolidateMemory ()
 This virtual function relates to memory management, and avoiding fragmentation. More...
 
 Component ()
 
virtual ~Component ()
 

Static Public Member Functions

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...
 

Private Member Functions

 KALDI_DISALLOW_COPY_AND_ASSIGN (Component)
 

Detailed Description

Abstract base-class for neural-net components.

Definition at line 114 of file nnet-component-itf.h.

Constructor & Destructor Documentation

◆ Component()

Component ( )
inline

Definition at line 396 of file nnet-component-itf.h.

396 { }

◆ ~Component()

virtual ~Component ( )
inlinevirtual

Definition at line 398 of file nnet-component-itf.h.

References KALDI_DISALLOW_COPY_AND_ASSIGN.

398 { }

Member Function Documentation

◆ Add()

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

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 in CompositeComponent, ScaleAndOffsetComponent, ConstantFunctionComponent, PerElementOffsetComponent, PerElementScaleComponent, PermuteComponent, ClipGradientComponent, LinearComponent, NaturalGradientAffineComponent, ConstantComponent, NonlinearComponent, RepeatedAffineComponent, BlockAffineComponent, TdnnComponent, TdnnComponent, BackpropTruncationComponent, AffineComponent, LstmNonlinearityComponent, TimeHeightConvolutionComponent, TimeHeightConvolutionComponent, BatchNormComponent, ConvolutionComponent, and RestrictedAttentionComponent.

Definition at line 370 of file nnet-component-itf.h.

Referenced by kaldi::nnet3::AddNnet(), kaldi::nnet3::AddNnetComponents(), kaldi::nnet3::ApplyL2Regularization(), BatchNormComponent::Copy(), ClipGradientComponent::Copy(), ScaleAndOffsetComponent::Copy(), ConstantComponent::IsComputable(), LinearComponent::LinearComponent(), NaturalGradientAffineComponent::NaturalGradientAffineComponent(), AffineComponent::Properties(), BackpropTruncationComponent::Properties(), BlockAffineComponent::Properties(), RepeatedAffineComponent::Properties(), PerElementScaleComponent::Properties(), PerElementOffsetComponent::Properties(), ConstantFunctionComponent::Properties(), kaldi::nnet3::TestNnetComponentAddScale(), and CompositeComponent::Type().

370 {};

◆ Backprop()

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
pure 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.

Implemented in CompositeComponent, ScaleAndOffsetComponent, ConstantFunctionComponent, PerElementOffsetComponent, PerElementScaleComponent, PermuteComponent, ClipGradientComponent, SumBlockComponent, NoOpComponent, FixedBiasComponent, FixedScaleComponent, SumGroupComponent, SpecAugmentTimeMaskComponent, FixedAffineComponent, LinearComponent, GeneralDropoutComponent, DropoutMaskComponent, LogSoftmaxComponent, SoftmaxComponent, ConstantComponent, RepeatedAffineComponent, BlockAffineComponent, MaxpoolingComponent, BackpropTruncationComponent, TdnnComponent, TdnnComponent, AffineComponent, StatisticsPoolingComponent, RectifiedLinearComponent, LstmNonlinearityComponent, TanhComponent, TimeHeightConvolutionComponent, TimeHeightConvolutionComponent, SigmoidComponent, StatisticsExtractionComponent, BatchNormComponent, ElementwiseProductComponent, ConvolutionComponent, RestrictedAttentionComponent, DropoutComponent, NormalizeComponent, DistributeComponent, and PnormComponent.

Referenced by NnetComputer::ExecuteCommand(), kaldi::nnet3::TestSimpleComponentDataDerivative(), kaldi::nnet3::TestSimpleComponentModelDerivative(), and kaldi::nnet3::TestSimpleComponentPropagateProperties().

◆ ConsolidateMemory()

virtual void ConsolidateMemory ( )
inlinevirtual

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 in ScaleAndOffsetComponent, NaturalGradientPerElementScaleComponent, ConstantFunctionComponent, LinearComponent, NaturalGradientAffineComponent, ConstantComponent, NaturalGradientRepeatedAffineComponent, NonlinearComponent, TdnnComponent, TdnnComponent, LstmNonlinearityComponent, TimeHeightConvolutionComponent, and TimeHeightConvolutionComponent.

Definition at line 394 of file nnet-component-itf.h.

Referenced by kaldi::nnet3::ConsolidateMemory(), ConstantComponent::IsComputable(), LinearComponent::LinearComponent(), NaturalGradientAffineComponent::NaturalGradientAffineComponent(), ScaleAndOffsetComponent::NumParameters(), LstmNonlinearityComponent::Properties(), ConstantFunctionComponent::Properties(), NaturalGradientRepeatedAffineComponent::Type(), and NaturalGradientPerElementScaleComponent::Type().

394 { }

◆ Copy()

virtual Component* Copy ( ) const
pure virtual

Copies component (deep copy).

Implemented in CompositeComponent, ScaleAndOffsetComponent, NaturalGradientPerElementScaleComponent, ConstantFunctionComponent, PerElementOffsetComponent, PerElementScaleComponent, PermuteComponent, ClipGradientComponent, SumBlockComponent, NoOpComponent, FixedBiasComponent, FixedScaleComponent, SumGroupComponent, SpecAugmentTimeMaskComponent, FixedAffineComponent, LinearComponent, GeneralDropoutComponent, NaturalGradientAffineComponent, DropoutMaskComponent, LogSoftmaxComponent, SoftmaxComponent, ConstantComponent, NaturalGradientRepeatedAffineComponent, RepeatedAffineComponent, BlockAffineComponent, MaxpoolingComponent, BackpropTruncationComponent, TdnnComponent, TdnnComponent, AffineComponent, StatisticsPoolingComponent, LstmNonlinearityComponent, RectifiedLinearComponent, TanhComponent, TimeHeightConvolutionComponent, TimeHeightConvolutionComponent, StatisticsExtractionComponent, SigmoidComponent, BatchNormComponent, ElementwiseProductComponent, ConvolutionComponent, RestrictedAttentionComponent, DropoutComponent, DistributeComponent, NormalizeComponent, and PnormComponent.

Referenced by ModelCollapser::GetDiagonallyPreModifiedComponentIndex(), ModelCollapser::GetScaledComponentIndex(), kaldi::nnet3::TestNnetComponentAddScale(), kaldi::nnet3::TestNnetComponentCopy(), kaldi::nnet3::TestNnetComponentUpdatable(), kaldi::nnet3::TestNnetComponentVectorizeUnVectorize(), kaldi::nnet3::TestSimpleComponentModelDerivative(), and kaldi::nnet3::TestSimpleComponentPropagateProperties().

◆ DeleteMemo()

virtual void DeleteMemo ( void *  memo) const
inlinevirtual

This virtual function only needs to be overwritten by Components that return a non-NULL memo from their Propagate() function.

It's called by NnetComputer in cases where Propagate returns a memo but there will be no backprop to consume it.

Reimplemented in SpecAugmentTimeMaskComponent, GeneralDropoutComponent, BatchNormComponent, and RestrictedAttentionComponent.

Definition at line 376 of file nnet-component-itf.h.

References KALDI_ASSERT.

Referenced by NnetComputer::ExecuteCommand(), NnetComputer::SaveMemo(), kaldi::nnet3::TestSimpleComponentDataDerivative(), kaldi::nnet3::TestSimpleComponentModelDerivative(), and kaldi::nnet3::TestSimpleComponentPropagateProperties().

376 { KALDI_ASSERT(memo == NULL); }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ GetInputIndexes()

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 in DropoutMaskComponent, ConstantComponent, TdnnComponent, TdnnComponent, StatisticsPoolingComponent, TimeHeightConvolutionComponent, TimeHeightConvolutionComponent, StatisticsExtractionComponent, RestrictedAttentionComponent, and DistributeComponent.

Definition at line 206 of file nnet-component-itf.cc.

Referenced by ComputationGraphBuilder::AddDependencies(), and kaldi::nnet3::ComputeComputationGraph().

208  {
209  input_indexes->resize(1);
210  (*input_indexes)[0] = output_index;
211 }

◆ Info()

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 in CompositeComponent, ScaleAndOffsetComponent, NaturalGradientPerElementScaleComponent, ConstantFunctionComponent, PerElementOffsetComponent, PerElementScaleComponent, PermuteComponent, ClipGradientComponent, SumBlockComponent, NoOpComponent, FixedBiasComponent, FixedScaleComponent, SpecAugmentTimeMaskComponent, FixedAffineComponent, LinearComponent, GeneralDropoutComponent, NaturalGradientAffineComponent, DropoutMaskComponent, ConstantComponent, NonlinearComponent, RepeatedAffineComponent, UpdatableComponent, BackpropTruncationComponent, BlockAffineComponent, MaxpoolingComponent, TdnnComponent, TdnnComponent, AffineComponent, LstmNonlinearityComponent, TimeHeightConvolutionComponent, TimeHeightConvolutionComponent, BatchNormComponent, DropoutComponent, ConvolutionComponent, RestrictedAttentionComponent, and NormalizeComponent.

Definition at line 199 of file nnet-component-itf.cc.

References ComponentPrecomputedIndexes::Type().

Referenced by PermuteComponent::Add(), NoOpComponent::Copy(), ClipGradientComponent::Copy(), FixedAffineComponent::Info(), FixedScaleComponent::Info(), FixedBiasComponent::Info(), LinearComponent::LinearComponent(), NaturalGradientAffineComponent::NaturalGradientAffineComponent(), AffineComponent::OrthonormalConstraint(), DropoutComponent::OutputDim(), BlockAffineComponent::OutputDim(), RepeatedAffineComponent::OutputDim(), ConstantComponent::OutputDim(), DropoutMaskComponent::OutputDim(), GeneralDropoutComponent::OutputDim(), SpecAugmentTimeMaskComponent::OutputDim(), SumBlockComponent::OutputDim(), PerElementScaleComponent::OutputDim(), PerElementOffsetComponent::OutputDim(), ConstantFunctionComponent::OutputDim(), ScaleAndOffsetComponent::OutputDim(), BackpropTruncationComponent::Properties(), kaldi::nnet3::TestNnetComponentAddScale(), kaldi::nnet3::TestNnetComponentCopy(), FixedAffineComponent::Type(), FixedScaleComponent::Type(), FixedBiasComponent::Type(), and kaldi::nnet3::UnitTestNnetComponent().

199  {
200  std::stringstream stream;
201  stream << Type() << ", input-dim=" << InputDim()
202  << ", output-dim=" << OutputDim();
203  return stream.str();
204 }
virtual int32 OutputDim() const =0
Returns output-dimension of this component.
virtual std::string Type() const =0
Returns a string such as "SigmoidComponent", describing the type of the object.
virtual int32 InputDim() const =0
Returns input-dimension of this component.

◆ InitFromConfig()

◆ InputDim()

◆ IsComputable()

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 in DropoutMaskComponent, ConstantComponent, TdnnComponent, TdnnComponent, StatisticsPoolingComponent, TimeHeightConvolutionComponent, TimeHeightConvolutionComponent, StatisticsExtractionComponent, RestrictedAttentionComponent, and DistributeComponent.

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

Referenced by ComputationGraphBuilder::ComputeComputableInfo(), and ComputationGraphBuilder::PruneDependencies().

216  {
217  // the default Component dependency is for an output index to map directly to
218  // the same input index, which is required to compute the output.
219  if (!input_index_set(output_index))
220  return false;
221  if (used_inputs) {
222  used_inputs->clear();
223  used_inputs->push_back(output_index);
224  }
225  return true;
226 }

◆ KALDI_DISALLOW_COPY_AND_ASSIGN()

KALDI_DISALLOW_COPY_AND_ASSIGN ( Component  )
private

◆ NewComponentOfType()

Component * NewComponentOfType ( const std::string &  type)
static

Returns a new Component of the given type e.g.

"SoftmaxComponent", or NULL if no such component type exists.

Definition at line 98 of file nnet-component-itf.cc.

References KALDI_ASSERT, and Component::Type().

Referenced by kaldi::nnet3::GenerateRandomSimpleComponent(), CompositeComponent::InitFromConfig(), and Nnet::ProcessComponentConfigLine().

98  {
99  Component *ans = NULL;
100  if (component_type == "SigmoidComponent") {
101  ans = new SigmoidComponent();
102  } else if (component_type == "TanhComponent") {
103  ans = new TanhComponent();
104  } else if (component_type == "SoftmaxComponent") {
105  ans = new SoftmaxComponent();
106  } else if (component_type == "LogSoftmaxComponent") {
107  ans = new LogSoftmaxComponent();
108  } else if (component_type == "RectifiedLinearComponent") {
109  ans = new RectifiedLinearComponent();
110  } else if (component_type == "NormalizeComponent") {
111  ans = new NormalizeComponent();
112  } else if (component_type == "PnormComponent") {
113  ans = new PnormComponent();
114  } else if (component_type == "AffineComponent") {
115  ans = new AffineComponent();
116  } else if (component_type == "LinearComponent") {
117  ans = new LinearComponent();
118  } else if (component_type == "NaturalGradientAffineComponent") {
119  ans = new NaturalGradientAffineComponent();
120  } else if (component_type == "PerElementScaleComponent") {
121  ans = new PerElementScaleComponent();
122  } else if (component_type == "NaturalGradientPerElementScaleComponent") {
123  ans = new NaturalGradientPerElementScaleComponent();
124  } else if (component_type == "PerElementOffsetComponent") {
125  ans = new PerElementOffsetComponent();
126  } else if (component_type == "SumGroupComponent") {
127  ans = new SumGroupComponent();
128  } else if (component_type == "FixedAffineComponent") {
129  ans = new FixedAffineComponent();
130  } else if (component_type == "FixedScaleComponent") {
131  ans = new FixedScaleComponent();
132  } else if (component_type == "FixedBiasComponent") {
133  ans = new FixedBiasComponent();
134  } else if (component_type == "NoOpComponent") {
135  ans = new NoOpComponent();
136  } else if (component_type == "ClipGradientComponent") {
137  ans = new ClipGradientComponent();
138  } else if (component_type == "ElementwiseProductComponent") {
139  ans = new ElementwiseProductComponent();
140  } else if (component_type == "ConvolutionComponent") {
141  ans = new ConvolutionComponent();
142  } else if (component_type == "TdnnComponent") {
143  ans = new TdnnComponent();
144  } else if (component_type == "MaxpoolingComponent") {
145  ans = new MaxpoolingComponent();
146  } else if (component_type == "PermuteComponent") {
147  ans = new PermuteComponent();
148  } else if (component_type == "DistributeComponent") {
149  ans = new DistributeComponent();
150  } else if (component_type == "CompositeComponent") {
151  ans = new CompositeComponent();
152  } else if (component_type == "RepeatedAffineComponent") {
153  ans = new RepeatedAffineComponent();
154  } else if (component_type == "BlockAffineComponent") {
155  ans = new BlockAffineComponent();
156  } else if (component_type == "NaturalGradientRepeatedAffineComponent") {
157  ans = new NaturalGradientRepeatedAffineComponent();
158  } else if (component_type == "StatisticsExtractionComponent") {
159  ans = new StatisticsExtractionComponent();
160  } else if (component_type == "StatisticsPoolingComponent") {
161  ans = new StatisticsPoolingComponent();
162  } else if (component_type == "ConstantFunctionComponent") {
163  ans = new ConstantFunctionComponent();
164  } else if (component_type == "ConstantComponent") {
165  ans = new ConstantComponent();
166  } else if (component_type == "DropoutComponent") {
167  ans = new DropoutComponent();
168  } else if (component_type == "DropoutMaskComponent") {
169  ans = new DropoutMaskComponent();
170  } else if (component_type == "GeneralDropoutComponent") {
171  ans = new GeneralDropoutComponent();
172  } else if (component_type == "SpecAugmentTimeMaskComponent") {
173  ans = new SpecAugmentTimeMaskComponent();
174  } else if (component_type == "BackpropTruncationComponent") {
175  ans = new BackpropTruncationComponent();
176  } else if (component_type == "LstmNonlinearityComponent") {
177  ans = new LstmNonlinearityComponent();
178  } else if (component_type == "BatchNormComponent") {
179  ans = new BatchNormComponent();
180  } else if (component_type == "TimeHeightConvolutionComponent") {
181  ans = new TimeHeightConvolutionComponent();
182  } else if (component_type == "RestrictedAttentionComponent") {
183  ans = new RestrictedAttentionComponent();
184  } else if (component_type == "SumBlockComponent") {
185  ans = new SumBlockComponent();
186  } else if (component_type == "GruNonlinearityComponent") {
187  ans = new GruNonlinearityComponent();
188  } else if (component_type == "OutputGruNonlinearityComponent") {
189  ans = new OutputGruNonlinearityComponent();
190  } else if (component_type == "ScaleAndOffsetComponent") {
191  ans = new ScaleAndOffsetComponent();
192  }
193  if (ans != NULL) {
194  KALDI_ASSERT(component_type == ans->Type());
195  }
196  return ans;
197 }
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185

◆ OutputDim()

◆ PrecomputeIndexes()

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

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 in SpecAugmentTimeMaskComponent, GeneralDropoutComponent, TdnnComponent, TdnnComponent, BackpropTruncationComponent, StatisticsPoolingComponent, TimeHeightConvolutionComponent, TimeHeightConvolutionComponent, StatisticsExtractionComponent, RestrictedAttentionComponent, and DistributeComponent.

Definition at line 302 of file nnet-component-itf.h.

References ComponentPrecomputedIndexes::Copy(), ComponentPrecomputedIndexes::Read(), ComponentPrecomputedIndexes::ReadNew(), ComponentPrecomputedIndexes::Type(), and ComponentPrecomputedIndexes::Write().

Referenced by ComputationExpander::ComputePrecomputedIndexes(), kaldi::nnet3::GetPrecomputedIndexes(), and Compiler::SetUpPrecomputedIndexes().

306  { return NULL; }

◆ Propagate()

virtual void* Propagate ( const ComponentPrecomputedIndexes indexes,
const CuMatrixBase< BaseFloat > &  in,
CuMatrixBase< BaseFloat > *  out 
) const
pure 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.

Implemented in CompositeComponent, ScaleAndOffsetComponent, ConstantFunctionComponent, PerElementOffsetComponent, PerElementScaleComponent, PermuteComponent, ClipGradientComponent, SumBlockComponent, NoOpComponent, FixedBiasComponent, FixedScaleComponent, SumGroupComponent, SpecAugmentTimeMaskComponent, FixedAffineComponent, LinearComponent, GeneralDropoutComponent, DropoutMaskComponent, LogSoftmaxComponent, SoftmaxComponent, ConstantComponent, RepeatedAffineComponent, BlockAffineComponent, MaxpoolingComponent, BackpropTruncationComponent, TdnnComponent, TdnnComponent, AffineComponent, StatisticsPoolingComponent, RectifiedLinearComponent, LstmNonlinearityComponent, TanhComponent, TimeHeightConvolutionComponent, TimeHeightConvolutionComponent, SigmoidComponent, StatisticsExtractionComponent, BatchNormComponent, ElementwiseProductComponent, ConvolutionComponent, RestrictedAttentionComponent, DropoutComponent, NormalizeComponent, DistributeComponent, and PnormComponent.

Referenced by NnetComputer::ExecuteCommand(), FixedBiasComponent::OutputDim(), kaldi::nnet3::TestSimpleComponentDataDerivative(), kaldi::nnet3::TestSimpleComponentModelDerivative(), and kaldi::nnet3::TestSimpleComponentPropagateProperties().

◆ Properties()

virtual int32 Properties ( ) const
pure virtual

Return bitmask of the component's properties.

These properties depend only on the component's type. See enum ComponentProperties.

Implemented in CompositeComponent, ScaleAndOffsetComponent, ConstantFunctionComponent, PerElementOffsetComponent, PerElementScaleComponent, PermuteComponent, ClipGradientComponent, SumBlockComponent, NoOpComponent, FixedBiasComponent, FixedScaleComponent, SumGroupComponent, SpecAugmentTimeMaskComponent, FixedAffineComponent, LinearComponent, GeneralDropoutComponent, DropoutMaskComponent, LogSoftmaxComponent, SoftmaxComponent, ConstantComponent, RepeatedAffineComponent, BlockAffineComponent, MaxpoolingComponent, BackpropTruncationComponent, TdnnComponent, TdnnComponent, AffineComponent, StatisticsPoolingComponent, RectifiedLinearComponent, LstmNonlinearityComponent, TanhComponent, TimeHeightConvolutionComponent, TimeHeightConvolutionComponent, SigmoidComponent, StatisticsExtractionComponent, BatchNormComponent, ElementwiseProductComponent, ConvolutionComponent, RestrictedAttentionComponent, DropoutComponent, NormalizeComponent, DistributeComponent, and PnormComponent.

Referenced by Compiler::AddBackwardStepComponent(), Compiler::AddForwardStepComponent(), kaldi::nnet3::AddNnetComponents(), kaldi::nnet3::ApplyL2Regularization(), ComputationChecker::CheckComputationIndexes(), kaldi::nnet3::ComponentDotProducts(), kaldi::nnet3::ComputeCommandAttributes(), Compiler::ComputeDerivNeeded(), ModelUpdateConsolidator::ConsolidateModelUpdate(), ModelUpdateConsolidator::ConsolidateUpdateForComponent(), NnetComputer::DebugAfterExecute(), NnetComputer::DebugBeforeExecute(), kaldi::nnet3::DotProduct(), NnetComputer::ExecuteCommand(), kaldi::nnet3::FreezeNaturalGradient(), kaldi::nnet3::GetPrecomputedIndexes(), Compiler::GetStrideType(), CompositeComponent::InitFromConfig(), VariableMergingOptimizer::MergeVariables(), DerivativeTimeLimiter::ModifyCommand(), kaldi::nnet3::NnetParametersAreIdentical(), kaldi::nnet3::NumParameters(), kaldi::nnet3::NumUpdatableComponents(), kaldi::nnet3::PerturbParams(), MaxChangeStats::Print(), kaldi::nnet3::PrintVectorPerUpdatableComponent(), ComputationStepsComputer::ProcessComponentStep(), kaldi::nnet3::SetLearningRate(), kaldi::nnet3::SetNnetAsGradient(), kaldi::nnet3::TestNnetComponentUpdatable(), kaldi::nnet3::TestNnetComponentVectorizeUnVectorize(), kaldi::nnet3::TestSimpleComponentDataDerivative(), kaldi::nnet3::TestSimpleComponentModelDerivative(), kaldi::nnet3::TestSimpleComponentPropagateProperties(), kaldi::nnet3::UnVectorizeNnet(), kaldi::nnet3::UpdateNnetWithMaxChange(), and kaldi::nnet3::VectorizeNnet().

◆ Read()

◆ ReadNew()

Component * ReadNew ( std::istream &  is,
bool  binary 
)
static

Read component from stream (works out its type). Dies on error.

Definition at line 84 of file nnet-component-itf.cc.

References KALDI_ERR, Component::Read(), and kaldi::ReadToken().

Referenced by Nnet::Read(), CompositeComponent::Read(), and kaldi::nnet3::TestNnetComponentIo().

84  {
85  std::string token;
86  ReadToken(is, binary, &token); // e.g. "<SigmoidComponent>".
87  token.erase(0, 1); // erase "<".
88  token.erase(token.length()-1); // erase ">".
89  Component *ans = NewComponentOfType(token);
90  if (!ans)
91  KALDI_ERR << "Unknown component type " << token;
92  ans->Read(is, binary);
93  return ans;
94 }
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
#define KALDI_ERR
Definition: kaldi-error.h:147
static Component * NewComponentOfType(const std::string &type)
Returns a new Component of the given type e.g.

◆ ReorderIndexes()

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

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 in TdnnComponent, TdnnComponent, StatisticsPoolingComponent, TimeHeightConvolutionComponent, TimeHeightConvolutionComponent, StatisticsExtractionComponent, and RestrictedAttentionComponent.

Definition at line 272 of file nnet-component-itf.h.

Referenced by StatisticsExtractionComponent::Copy(), StatisticsPoolingComponent::Copy(), kaldi::nnet3::GetPrecomputedIndexes(), and ComputationStepsComputer::ProcessComponentStep().

273  {}

◆ Scale()

virtual void Scale ( BaseFloat  scale)
inlinevirtual

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 in CompositeComponent, ScaleAndOffsetComponent, ConstantFunctionComponent, PerElementOffsetComponent, PerElementScaleComponent, PermuteComponent, ClipGradientComponent, LinearComponent, NaturalGradientAffineComponent, ConstantComponent, NonlinearComponent, RepeatedAffineComponent, BlockAffineComponent, TdnnComponent, TdnnComponent, BackpropTruncationComponent, AffineComponent, LstmNonlinearityComponent, TimeHeightConvolutionComponent, TimeHeightConvolutionComponent, BatchNormComponent, ConvolutionComponent, and RestrictedAttentionComponent.

Definition at line 360 of file nnet-component-itf.h.

Referenced by BatchNormComponent::Copy(), ClipGradientComponent::Copy(), ScaleAndOffsetComponent::Copy(), ConstantComponent::IsComputable(), LinearComponent::LinearComponent(), NaturalGradientAffineComponent::NaturalGradientAffineComponent(), AffineComponent::Properties(), BackpropTruncationComponent::Properties(), BlockAffineComponent::Properties(), RepeatedAffineComponent::Properties(), PerElementScaleComponent::Properties(), PerElementOffsetComponent::Properties(), ConstantFunctionComponent::Properties(), kaldi::nnet3::ScaleNnet(), kaldi::nnet3::TestNnetComponentAddScale(), kaldi::nnet3::TestNnetComponentUpdatable(), kaldi::nnet3::TestNnetComponentVectorizeUnVectorize(), kaldi::nnet3::TestSimpleComponentModelDerivative(), and CompositeComponent::Type().

360 {};

◆ StoreStats()

virtual void StoreStats ( const CuMatrixBase< BaseFloat > &  in_value,
const CuMatrixBase< BaseFloat > &  out_value,
void *  memo 
)
inlinevirtual

This function may store stats on average activation values, and for some component types, the average value of the derivative of the nonlinearity.

It only does something for those components that have nonzero Properties()&kStoresStats.

Parameters
[in]in_valueThe input to the Propagate() function. Note: if the component sets the flag kPropagateInPlace, this should not be used; the empty matrix will be provided here if in-place propagation was used.
[in]out_valueThe output of the Propagate() function.
[in]memoThe 'memo' returned by the Propagate() function; this will usually be NULL.

Reimplemented in SoftmaxComponent, RectifiedLinearComponent, TanhComponent, SigmoidComponent, BatchNormComponent, and RestrictedAttentionComponent.

Definition at line 186 of file nnet-component-itf.h.

Referenced by CompositeComponent::Backprop(), SigmoidComponent::Copy(), BatchNormComponent::DeleteMemo(), NnetComputer::ExecuteCommand(), TanhComponent::Properties(), RectifiedLinearComponent::Properties(), and SoftmaxComponent::Properties().

188  { }

◆ Type()

virtual std::string Type ( ) const
pure virtual

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

Implemented in CompositeComponent, ScaleAndOffsetComponent, NaturalGradientPerElementScaleComponent, ConstantFunctionComponent, PerElementOffsetComponent, PerElementScaleComponent, PermuteComponent, ClipGradientComponent, SumBlockComponent, NoOpComponent, FixedBiasComponent, FixedScaleComponent, SumGroupComponent, SpecAugmentTimeMaskComponent, FixedAffineComponent, LinearComponent, GeneralDropoutComponent, NaturalGradientAffineComponent, DropoutMaskComponent, LogSoftmaxComponent, SoftmaxComponent, ConstantComponent, NaturalGradientRepeatedAffineComponent, RepeatedAffineComponent, BlockAffineComponent, MaxpoolingComponent, BackpropTruncationComponent, TdnnComponent, TdnnComponent, AffineComponent, StatisticsPoolingComponent, RectifiedLinearComponent, LstmNonlinearityComponent, TanhComponent, TimeHeightConvolutionComponent, TimeHeightConvolutionComponent, SigmoidComponent, StatisticsExtractionComponent, BatchNormComponent, ElementwiseProductComponent, ConvolutionComponent, RestrictedAttentionComponent, DropoutComponent, NormalizeComponent, DistributeComponent, and PnormComponent.

Referenced by ComputationChecker::CheckComputationCompression(), kaldi::nnet3::ConvertRepeatedToBlockAffine(), UpdatableComponent::Info(), NonlinearComponent::Info(), NonlinearComponent::InitFromConfig(), CompositeComponent::InitFromConfig(), Component::NewComponentOfType(), kaldi::nnet3::NnetParametersAreIdentical(), MemoryCompressionOptimizer::ProcessMatrix(), NonlinearComponent::Read(), UpdatableComponent::ReadUpdatableCommon(), kaldi::nnet3::TestSimpleComponentDataDerivative(), kaldi::nnet3::TestSimpleComponentModelDerivative(), kaldi::nnet3::TestSimpleComponentPropagateProperties(), kaldi::nnet3::UnitTestConvertRepeatedToBlockAffine(), kaldi::nnet3::UnitTestConvertRepeatedToBlockAffineComposite(), NonlinearComponent::Write(), and UpdatableComponent::WriteUpdatableCommon().

◆ Write()

◆ ZeroStats()

virtual void ZeroStats ( )
inlinevirtual

Components that provide an implementation of StoreStats should also provide an implementation of ZeroStats(), to set those stats to zero.

Other components that store other types of statistics (e.g. regarding gradient clipping) should implement ZeroStats() also.

Reimplemented in CompositeComponent, PermuteComponent, ClipGradientComponent, NonlinearComponent, BackpropTruncationComponent, LstmNonlinearityComponent, BatchNormComponent, and RestrictedAttentionComponent.

Definition at line 195 of file nnet-component-itf.h.

Referenced by BatchNormComponent::Copy(), LstmNonlinearityComponent::Properties(), BackpropTruncationComponent::Properties(), ClipGradientComponent::Properties(), CompositeComponent::Type(), kaldi::nnet3::ZeroComponentStats(), and CompositeComponent::ZeroStats().

195 { }

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