Poplar and PopLibs
popnn::rnn Namespace Reference

Functions for Recurrent Neural Networks (RNN) More...

Classes

struct  RnnParams
 Structure of Recurrent Neural Network (RNN) parameters which allows for any customized implementation of the cellular part of the RNN. More...
 
struct  RnnSlice
 Tensors required for processing a single time step. More...
 
struct  StateSequence
 Structure that associates a particular state tensor with a user-defined output tensor. More...
 

Typedefs

using LoopBodyType = std::function< poplar::program::Sequence(poplar::Graph &graph, const TimeStepState &time, const RnnBatchwiseFlags &, std::vector< poplar::Tensor > &, const RnnSlice &slice, std::vector< poplar::Tensor > &, poplar::program::Sequence *, const poplar::DebugNameAndId &)>
 Create loop body function for the given shard. More...
 
using GatherBodyType = std::function< poplar::program::Sequence(poplar::Graph &graph, const RnnSlice &slice, unsigned stepsPerGather, poplar::program::Sequence *, const poplar::DebugNameAndId &)>
 Create gather body function for the given shard. More...
 

Functions

std::vector< std::pair< poplin::MatMulParams, poplar::OptionFlags > > getMatMulPrePlanParameters (std::size_t numSteps, std::size_t batchSize, std::size_t inputSize, std::size_t outputSize, const poplar::Type &dType, const poplar::Type &partialsType=poplar::FLOAT, bool inferenceOnly=false, bool hasFeedforwardWeights=true)
 Predict what matrix multiplications will be needed for the given parameters and return list of corresponding matmul parameters and options.
 
uint64_t getFwdFlops (unsigned sequenceSize, unsigned batchSize, unsigned inputSize, unsigned outputSize, bool weightInput=true)
 Compute the total floating point operations for the forward pass of RNN.
 
uint64_t getBwdFlops (unsigned sequenceSize, unsigned batchSize, unsigned inputSize, unsigned outputSize, bool calcInputGrad=true)
 Compute the total floating point operations for the backward pass of RNN.
 
uint64_t getWuFlops (unsigned sequenceSize, unsigned batchSize, unsigned inputSize, unsigned outputSize)
 Compute the total floating point operations for the weight update pass of RNN.
 
poplar::Tensor createInput (poplar::Graph &graph, unsigned numSteps, unsigned batchSize, unsigned inputSize, unsigned outputSize, const poplar::Type &dType, const poplar::Type &partialsType=poplar::FLOAT, bool inferenceOnly=false, const poplar::DebugContext &debugContext={}, poplin::PlanningCache *planningCache=nullptr)
 Create a tensor which is input to a vanilla RNN. More...
 
poplar::Tensor createFwdState (poplar::Graph &graph, const poplar::Type &dType, unsigned batchSize, unsigned outputSize, poplar::program::Sequence &prog, bool initState, bool inferenceOnly, const poplar::DebugContext &debugContext={}, poplin::PlanningCache *planningCache=nullptr)
 Create initial state for a vanilla RNN. More...
 
poplar::Tensor getOutputFromFwdState (const poplar::Tensor &fwdState)
 Extract previous output tensor from the hidden state. More...
 
poplar::Tensor createWeightsInput (poplar::Graph &graph, unsigned sequenceSize, unsigned batchSize, unsigned inputSize, unsigned outputSize, const poplar::Type &dType, const poplar::Type &partialsType=poplar::FLOAT, bool inferenceOnly=false, const poplar::DebugContext &debugContext={}, poplin::PlanningCache *planningCache=nullptr)
 Create the weights used to weight the input of a vanilla RNN layer. More...
 
poplar::Tensor createWeightsFeedback (poplar::Graph &graph, unsigned batchSize, unsigned outputSize, const poplar::Type &dType, const poplar::Type &partialsType=poplar::FLOAT, bool inferenceOnly=false, const poplar::DebugContext &debugContext={}, poplin::PlanningCache *planningCache=nullptr)
 Create the weights used in the recurrent part of a vanilla RNN layer. More...
 
poplar::Tensor forwardWeightInput (poplar::Graph &graph, const poplar::Tensor &actIn, const poplar::Tensor &weights, poplar::program::Sequence &prog, const poplar::Type &partialsType=poplar::FLOAT, bool inferenceOnly=false, const poplar::DebugContext &debugContext={}, poplin::PlanningCache *planningCache=nullptr)
 Perform the feedforward part of a RNN layer. More...
 
poplar::Tensor forwardIterate (poplar::Graph &graph, const poplar::Tensor &feedFwdIn, const poplar::Tensor &initState, const poplar::Tensor &feedbackWeights, const poplar::Tensor &biases, poplar::program::Sequence &prog, popnn::NonLinearityType nonLinearityType, const poplar::Type &partialsType=poplar::FLOAT, bool inferenceOnly=false, const poplar::DebugContext &debugContext={}, poplin::PlanningCache *planningCache=nullptr)
 Perform the feedback part of the RNN layer. More...
 
poplar::Tensor createBwdState (poplar::Graph &graph, const poplar::Type &dType, unsigned batchSize, unsigned outputSize, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext={}, poplin::PlanningCache *planningCache=nullptr)
 Create initial state for backward pass of a vanilla RNN. More...
 
std::pair< poplar::Tensor, poplar::TensorbackwardGradientStep (poplar::Graph &graph, const poplar::Tensor &nextLayerGrad, const poplar::Tensor &bwdState, const poplar::Tensor &actOut, const poplar::Tensor &weightsInput, const poplar::Tensor &weightsFeedback, poplar::program::Sequence &prog, popnn::NonLinearityType nonLinearityType, const poplar::Type &partialsType=poplar::FLOAT, const poplar::DebugContext &debugContext={}, poplin::PlanningCache *planningCache=nullptr)
 Compute a single step of the backward pass of a vanilla RNN layer. More...
 
poplar::Tensor backwardGradientStep (poplar::Graph &graph, const poplar::Tensor &nextLayerGrad, const poplar::Tensor &bwdState, const poplar::Tensor &actOut, const poplar::Tensor &weightsFeedback, poplar::program::Sequence &prog, popnn::NonLinearityType nonLinearityType, const poplar::Type &partialsType=poplar::FLOAT, const poplar::DebugContext &debugContext={}, poplin::PlanningCache *planningCache=nullptr)
 Same as backwardGradientStep(poplar::Graph&, const poplar::Tensor&, const poplar::Tensor&, const poplar::Tensor&, const poplar::Tensor&, const poplar::Tensor &, poplar::program::Sequence&, popnn::NonLinearityType, const poplar::Type&, const poplar::DebugContext&, poplin::PlanningCache*) with the difference that the input gradients are not computed.
 
void paramDeltaUpdate (poplar::Graph &graph, const poplar::Tensor &bwdState, const poplar::Tensor &actIn, const poplar::Tensor &prevOut, poplar::Tensor &weightsInputDeltasAcc, poplar::Tensor &weightsFeedbackDeltasAcc, poplar::Tensor &biasDeltasAcc, poplar::program::Sequence &prog, const poplar::Type &partialsType=poplar::FLOAT, const poplar::DebugContext &debugContext={}, poplin::PlanningCache *planningCache=nullptr)
 Update parameter deltas for a vanilla RNN step. More...
 
poplar::Tensor rnnFwdSequence (poplar::Graph &graph, poplar::program::Sequence &prog, const poplar::Tensor &fwdStateInit, const poplar::Tensor *weightedIn, const poplar::Tensor &biases, const poplar::Tensor &feedFwdWeights, const poplar::Tensor &feedbackWeights, const poplar::Tensor &prevLayerActs, const popnn::NonLinearityType &nonLinearityType, const poplar::Type &partialsType, bool inferenceOnly, const poplar::DebugContext &debugContext={}, poplin::PlanningCache *planningCache=nullptr)
 Perform the forward part of the RNN layer. More...
 
std::tuple< poplar::Tensor, poplar::Tensor, poplar::Tensor, poplar::TensorrnnBwdSequence (poplar::Graph &graph, bool doWU, bool ignoreInputGradientCalc, poplar::program::Sequence &prog, const poplar::Tensor &fwdStateInit, const poplar::Tensor &fwdState, const poplar::Tensor &biases, const poplar::Tensor &feedFwdWeights, const poplar::Tensor &feedbackWeights, const poplar::Tensor &outGradient, const poplar::Tensor &actIn, const popnn::NonLinearityType &nonLinearityType, const poplar::Type &partialsType, const poplar::DebugContext &debugContext={}, poplin::PlanningCache *planningCache=nullptr)
 Perform the feedback part of the RNN layer. More...
 
poplar::Tensor createInitialState (poplar::Graph &graph, const RnnParams &params, bool isOutput, unsigned multiple, unsigned numShards, const poplar::DebugContext &debugContext={})
 Create state tensor to be used in all recurrences of the RNN. More...
 
poplar::Tensor createRecurrentTensor (poplar::Graph &graph, const RnnParams &params, unsigned size, unsigned numShards, const poplar::DebugContext &debugContext={})
 Create recurrent tensor of shape [timeSteps, batchSize, size] suitable for slicing and/or sharding of the outermost dimension. More...
 
poplar::Tensor createInputTensor (poplar::Graph &graph, const RnnParams &params, unsigned numShards, const poplar::DebugContext &debugContext={})
 Create input tensor of shape [timeSteps, batchSize, inputSize] suitable for slicing and/or sharding of the outermost dimension. More...
 
poplar::Tensor createOutputTensor (poplar::Graph &graph, const RnnParams &params, unsigned numShards, const poplar::DebugContext &debugContext={})
 Create a standard output tensor of shape [timeSteps, batchSize, outputSize] suitable for slicing and/or sharding of the outermost dimension. More...
 
poplar::Tensor createOutputTensor (poplar::Graph &graph, const RnnParams &params, unsigned multiple, unsigned numShards, const poplar::DebugContext &debugContext={})
 Create a single output tensor with multiple (standard) output tensors concatenated along the outermost (timeSteps) dimension. More...
 
poplar::Tensor shiftRnnTensor (poplar::Graph &graph, const RnnParams &params, const poplar::Tensor &tBase, const poplar::Tensor &tSingle, poplar::program::Sequence &prog, unsigned numShards, const poplar::DebugContext &debugContext={})
 Create a single-step shifted RNN tensor from an input tensor. More...
 
std::vector< poplar::TensorRnn (poplar::Graph &graph, const RnnParams &params, bool reverse, const std::vector< poplar::Tensor > &initState, const StateSequence &stateSequence, const std::vector< poplar::Tensor > &inputs, const poplar::Tensor *interimIn, poplar::Tensor *interimOut, const std::vector< poplar::Tensor > &outputs, const std::vector< poplar::Tensor > &created, poplar::program::Sequence &prog, const LoopBodyType &loopFn, unsigned numShards, poplar::OptionFlags &options, const poplar::DebugContext &debugContext={})
 Run custom Recurrent Neural Net cell implementation recurrently. More...
 
std::vector< poplar::TensorRnn (poplar::Graph &graph, const RnnParams &params, const std::vector< poplar::Tensor > &initState, const StateSequence &stateSequence, const std::vector< poplar::Tensor > &inputs, const poplar::Tensor &interimIn, const unsigned numTemps, poplar::program::Sequence &prog, const LoopBodyType &loopFn, const std::vector< poplar::Tensor > &gatherInputs, const GatherBodyType &gatherFn, unsigned numShards, unsigned stepsPerGather, poplar::OptionFlags &options, const poplar::DebugContext &debugContext={})
 Run custom Recurrent Neural Net cell callback at every time step in decrementing order. More...
 

Detailed Description

Functions for Recurrent Neural Networks (RNN)

Typedef Documentation

◆ GatherBodyType

using popnn::rnn::GatherBodyType = typedef std::function<poplar::program::Sequence( poplar::Graph &graph, const RnnSlice &slice, unsigned stepsPerGather, poplar::program::Sequence *, const poplar::DebugNameAndId &)>

Create gather body function for the given shard.

Parameters
graphThe graph object
sliceThe input/output tensors for a specific shard
stepsPerGatherThe time step interval between calls to this function.
progThe program initialization sequence
dnaiDebug name and Id
Returns
Gather body function for the given shard.

◆ LoopBodyType

using popnn::rnn::LoopBodyType = typedef std::function<poplar::program::Sequence( poplar::Graph &graph, const TimeStepState &time, const RnnBatchwiseFlags &, std::vector<poplar::Tensor> &, const RnnSlice &slice, std::vector<poplar::Tensor> &, poplar::program::Sequence *, const poplar::DebugNameAndId &)>

Create loop body function for the given shard.

Parameters
graphThe graph object.
shardIdxThe tensor that specifies the starting sequence index for the current shard.
seqIdxThe tensor that iterates over the range of input sequences that are mapped on the current shard, beginning from 0.
batchwiseFlagsFlags that indicate batches for which the current step is within the batchwise step limit.
stateState tensors.
sliceThe input/output tensors for a specific shard.
createdThe output tensors which are created by this function.
progThe program initialization sequence.
dnaiDebug name and Id.
Returns
Loop body function for the given shard.

Function Documentation

◆ backwardGradientStep()

std::pair< poplar::Tensor, poplar::Tensor > popnn::rnn::backwardGradientStep ( poplar::Graph graph,
const poplar::Tensor nextLayerGrad,
const poplar::Tensor bwdState,
const poplar::Tensor actOut,
const poplar::Tensor weightsInput,
const poplar::Tensor weightsFeedback,
poplar::program::Sequence prog,
popnn::NonLinearityType  nonLinearityType,
const poplar::Type partialsType = poplar::FLOAT,
const poplar::DebugContext debugContext = {},
poplin::PlanningCache *  planningCache = nullptr 
)

Compute a single step of the backward pass of a vanilla RNN layer.

Two gradient outputs are produced. The first gradient is at the input of the RNN layer for the step. The second gradient is at the adder and can be used to backward propagate through the earlier steps.

Parameters
graphThe graph object.
nextLayerGradThe loss gradient fed as input to this step.
bwdStateThe gradient state for the previous step.
actOutThe output activation.
weightsInputThe input weights.
weightsFeedbackThe feedback weights.
progThe control program to which programs are added.
nonLinearityTypeThe type of non-linearity.
partialsTypeThe data type used in intermediate calculations.
debugContextOptional debug information.
planningCacheThe matmul planning cache.
Returns
A pair of tensors. The first tensor is the loss gradient at the input layer. The second tensor is the backward state needed to run the next backward step.

◆ createBwdState()

poplar::Tensor popnn::rnn::createBwdState ( poplar::Graph graph,
const poplar::Type dType,
unsigned  batchSize,
unsigned  outputSize,
poplar::program::Sequence prog,
const poplar::DebugContext debugContext = {},
poplin::PlanningCache *  planningCache = nullptr 
)

Create initial state for backward pass of a vanilla RNN.

Parameters
graphThe graph object.
dTypeThe data type of the created tensor.
batchSizeThe number of batch elements processed.
outputSizeThe number of output activations.
progThe control program.
debugContextOptional debug information.
planningCacheThe matmul planning cache.
Returns
Tile mapped initial state tensor.

◆ createFwdState()

poplar::Tensor popnn::rnn::createFwdState ( poplar::Graph graph,
const poplar::Type dType,
unsigned  batchSize,
unsigned  outputSize,
poplar::program::Sequence prog,
bool  initState,
bool  inferenceOnly,
const poplar::DebugContext debugContext = {},
poplin::PlanningCache *  planningCache = nullptr 
)

Create initial state for a vanilla RNN.

The state, apart from the activations, is initialised by the control program.

The number of hidden states may depend on whether the RNN is used for inference or training.

Parameters
graphThe graph object.
dTypeThe data type of the created tensor.
batchSizeThe number of batch elements.
outputSizeThe output (hidden) of each sequence element.
progThe control program.
initStateIf true, indicates that the state should be initialised.
inferenceOnlyIndicates whether the RNN layer is for inference only. If true, we can ignore backwards and weight update passes.
debugContextOptional debug information.
planningCacheThe matmul planning cache.
Returns
A 2D tensor of shape [batchSize, outputSize]

◆ createInitialState()

poplar::Tensor popnn::rnn::createInitialState ( poplar::Graph graph,
const RnnParams params,
bool  isOutput,
unsigned  multiple,
unsigned  numShards,
const poplar::DebugContext debugContext = {} 
)

Create state tensor to be used in all recurrences of the RNN.

The tensor shape is [multiple, batchSize, size] where size is determined by whether the state tensor is an input or output tensor, depending on isOutput. If the RNN is sharded, a tensor of this shape is created for each shard.

Parameters
graphThe graph object.
paramsThe RNN parameters.
isOutputFlag that indicates that the state tensor will be an output tensor. If false, indicates that this is an input tensor.
multipleThe number of state variables that are concatenated into a single state tensor.
numShardsThe number of shards to be used.
debugContextDebug information.
Returns
State tensor of shape [multiple, batchSize, size].

◆ createInput()

poplar::Tensor popnn::rnn::createInput ( poplar::Graph graph,
unsigned  numSteps,
unsigned  batchSize,
unsigned  inputSize,
unsigned  outputSize,
const poplar::Type dType,
const poplar::Type partialsType = poplar::FLOAT,
bool  inferenceOnly = false,
const poplar::DebugContext debugContext = {},
poplin::PlanningCache *  planningCache = nullptr 
)

Create a tensor which is input to a vanilla RNN.

The layout of the tensor is best for a multiplication of the input weight matrix with the given number of steps.

Parameters
graphThe graph object.
numStepsThe number of steps used in the forward weighting of the input.
batchSizeThe number of batch elements.
inputSizeThe size of the input for each sequence step.
outputSizeThe output (hidden) size of each sequence element.
inferenceOnlyIndicates whether the RNN layer is for inference only. If true, we can ignore the backwards and weight update passes.
dTypeThe data type of the created tensor.
partialsTypeThe data type of intermediate calculations.
debugContextOptional debug information.
planningCacheThe matmul planning cache.
Returns
Tensor of shape [numSteps, batchSize, inputSize]

◆ createInputTensor()

poplar::Tensor popnn::rnn::createInputTensor ( poplar::Graph graph,
const RnnParams params,
unsigned  numShards,
const poplar::DebugContext debugContext = {} 
)

Create input tensor of shape [timeSteps, batchSize, inputSize] suitable for slicing and/or sharding of the outermost dimension.

Parameters
graphThe graph object.
paramsThe RNN parameters.
numShardsThe number of shards to be used.
debugContextDebug information.
Returns
Input tensor of shape [timeSteps, batchSize, inputSize].

◆ createOutputTensor() [1/2]

poplar::Tensor popnn::rnn::createOutputTensor ( poplar::Graph graph,
const RnnParams params,
unsigned  multiple,
unsigned  numShards,
const poplar::DebugContext debugContext = {} 
)

Create a single output tensor with multiple (standard) output tensors concatenated along the outermost (timeSteps) dimension.

The concatenated tensor is of shape [multiple * timeSteps, batchSize, outputSize] which is suitable for slicing and/or sharding along the outermost dimension.

Parameters
graphThe graph object.
paramsThe RNN parameters.
multipleThe number of standard output tensors to be concatenated.
numShardsThe number of shards to be used.
debugContextDebug information.
Returns
Output tensor of shape [multiple * timeSteps, batchSize, outputSize].

◆ createOutputTensor() [2/2]

poplar::Tensor popnn::rnn::createOutputTensor ( poplar::Graph graph,
const RnnParams params,
unsigned  numShards,
const poplar::DebugContext debugContext = {} 
)

Create a standard output tensor of shape [timeSteps, batchSize, outputSize] suitable for slicing and/or sharding of the outermost dimension.

Parameters
graphThe graph object.
paramsThe RNN parameters.
numShardsThe number of shards to be used.
debugContextDebug information.
Returns
Output tensor of shape [timeSteps, batchSize, outputSize].

◆ createRecurrentTensor()

poplar::Tensor popnn::rnn::createRecurrentTensor ( poplar::Graph graph,
const RnnParams params,
unsigned  size,
unsigned  numShards,
const poplar::DebugContext debugContext = {} 
)

Create recurrent tensor of shape [timeSteps, batchSize, size] suitable for slicing and/or sharding of the outermost dimension.

Parameters
graphThe graph object.
paramsThe RNN parameters.
sizeThe innermost dimension of the tensor.
numShardsThe number of shards to be used.
debugContextDebug information.
Returns
Recurrent tensor of shape [timeSteps, batchSize, size].

◆ createWeightsFeedback()

poplar::Tensor popnn::rnn::createWeightsFeedback ( poplar::Graph graph,
unsigned  batchSize,
unsigned  outputSize,
const poplar::Type dType,
const poplar::Type partialsType = poplar::FLOAT,
bool  inferenceOnly = false,
const poplar::DebugContext debugContext = {},
poplin::PlanningCache *  planningCache = nullptr 
)

Create the weights used in the recurrent part of a vanilla RNN layer.

Parameters
graphThe graph object.
batchSizeThe number of batch elements.
outputSizeThe output (hidden) size of each sequence.
dTypeThe data type of the created tensor.
partialsTypeThe data type of partial results in the computation
inferenceOnlyIndicates whether the RNN layer is for inference only. If true, we can ignore backwards and weight update passes.
debugContextOptional debug information.
planningCacheThe matmul planning cache.

◆ createWeightsInput()

poplar::Tensor popnn::rnn::createWeightsInput ( poplar::Graph graph,
unsigned  sequenceSize,
unsigned  batchSize,
unsigned  inputSize,
unsigned  outputSize,
const poplar::Type dType,
const poplar::Type partialsType = poplar::FLOAT,
bool  inferenceOnly = false,
const poplar::DebugContext debugContext = {},
poplin::PlanningCache *  planningCache = nullptr 
)

Create the weights used to weight the input of a vanilla RNN layer.

The best tile mapping of the weight tensor is when it matches the sequence size in the input activation tensor used to multiply the input weights.

Parameters
graphThe graph object.
sequenceSizeThe number of sequence steps used in the forward weighting of the input. The best tile mapping is when this matches the sequence size of the input activation tensor.
batchSizeThe number of batch elements.
inputSizeThe input size of each sequence.
outputSizeThe output (hidden) size of each sequence.
dTypeThe data type of the created tensor.
partialsTypeThe data type of partial results in the computation.
inferenceOnlyIndicates whether the RNN layer is for inference only. If true, we can ignore backwards and weight update passes.
debugContextOptional debug information.
planningCacheThe matmul planning cache.

◆ forwardIterate()

poplar::Tensor popnn::rnn::forwardIterate ( poplar::Graph graph,
const poplar::Tensor feedFwdIn,
const poplar::Tensor initState,
const poplar::Tensor feedbackWeights,
const poplar::Tensor biases,
poplar::program::Sequence prog,
popnn::NonLinearityType  nonLinearityType,
const poplar::Type partialsType = poplar::FLOAT,
bool  inferenceOnly = false,
const poplar::DebugContext debugContext = {},
poplin::PlanningCache *  planningCache = nullptr 
)

Perform the feedback part of the RNN layer.

The feedback part of the RNN layer must be preceded by the feedforward part of the RNN layer to complete the layer.

See also
forwardWeightInput

The following definitions apply:

  • numSteps is the number of steps.
  • batchSize is the number of batch elements.
  • inputSize is the size of the input for each step.
  • outputSize is the size of the output for each step.
Parameters
graphThe graph object.
feedFwdInThe input to this function (output from the feedforward part of the RNN layer.
initStateThe initial state of the RNN layer, which means the the previous output.
feedbackWeightsThe feedback weights.
biasesThe biases.
progThe program sequence. Programs added by this function are appended to this program sequence.
nonLinearityTypeThe non linearity used for the output activations
partialsTypeThe data type for intermediates.
inferenceOnlyIndicates whether the RNN layer is for inference only. If true, we can ignore backwards and weight update passes.
debugContextOptional debug information.
planningCacheThe matmul planning cache.
Returns
The output activations of the RNN layer.

◆ forwardWeightInput()

poplar::Tensor popnn::rnn::forwardWeightInput ( poplar::Graph graph,
const poplar::Tensor actIn,
const poplar::Tensor weights,
poplar::program::Sequence prog,
const poplar::Type partialsType = poplar::FLOAT,
bool  inferenceOnly = false,
const poplar::DebugContext debugContext = {},
poplin::PlanningCache *  planningCache = nullptr 
)

Perform the feedforward part of a RNN layer.

The feedforward part of the RNN layer must be followed by the feedback part to complete the RNN layer. In other words, the output must be fed as the feedforward input to the feedback part.

See also
forwardIterate

The following definitions apply:

  • numSteps is the number of sequence steps.
  • batchSize is the number of batch elements.
  • inputSize is the size of the input for each step.
  • outputSize is the size of the output for each step.
Parameters
graphThe graph object.
actInThe input activation tensor with shape [numSteps, batchSize, inputSize].
weightsFeedforward weights with shape [outputSize, inputSize].
progThe program sequence. Programs added by this function are appended to this program sequence.
partialsTypeThe data type for intermediates.
inferenceOnlyIndicates whether the RNN layer is for inference only. If true, we can ignore backwards and weight update passes.
debugContextOptional debug information.
planningCacheThe matmul planning cache.
Returns
The output tensor with shape [numSteps, batchSize, outputSize].

◆ getOutputFromFwdState()

poplar::Tensor popnn::rnn::getOutputFromFwdState ( const poplar::Tensor fwdState)

Extract previous output tensor from the hidden state.

The returned tensor is a view of the tensor and can be used to initialise the tensor, if required.

◆ paramDeltaUpdate()

void popnn::rnn::paramDeltaUpdate ( poplar::Graph graph,
const poplar::Tensor bwdState,
const poplar::Tensor actIn,
const poplar::Tensor prevOut,
poplar::Tensor weightsInputDeltasAcc,
poplar::Tensor weightsFeedbackDeltasAcc,
poplar::Tensor biasDeltasAcc,
poplar::program::Sequence prog,
const poplar::Type partialsType = poplar::FLOAT,
const poplar::DebugContext debugContext = {},
poplin::PlanningCache *  planningCache = nullptr 
)

Update parameter deltas for a vanilla RNN step.

The parameter deltas updated are:

  • Feedback Weights
  • Input Weights
  • Bias The new deltas computed for this step are added to the accumulated deltas from previous steps. The caller must zero the accumulated tensors at the first call if the tensors to maintain the result are in-place.
Parameters
graphThe graph object.
bwdStateThe gradient state for this step.
actInThe input activations for this step.
prevOutThe previous RNN output activations for this step.
weightsInputDeltasAccThe previous weights input deltas tensor. This tensor must be tile-mapped. The deltas from this step are added to this tensor.
weightsFeedbackDeltasAccThe previous feedback weights deltas tensor. This tensor must be tile-mapped. The deltas from this step are added to this tensor.
biasDeltasAccThe previous bias deltas tensor. This tensor must be tile-mapped. The deltas from this step are added to this tensor.
progThe control program to which programs are added.
partialsTypeThe data type used in intermediate calculations.
debugContextOptional debug information.
planningCacheThe matmul planning cache.

◆ Rnn() [1/2]

std::vector< poplar::Tensor > popnn::rnn::Rnn ( poplar::Graph graph,
const RnnParams params,
bool  reverse,
const std::vector< poplar::Tensor > &  initState,
const StateSequence stateSequence,
const std::vector< poplar::Tensor > &  inputs,
const poplar::Tensor interimIn,
poplar::Tensor interimOut,
const std::vector< poplar::Tensor > &  outputs,
const std::vector< poplar::Tensor > &  created,
poplar::program::Sequence prog,
const LoopBodyType loopFn,
unsigned  numShards,
poplar::OptionFlags options,
const poplar::DebugContext debugContext = {} 
)

Run custom Recurrent Neural Net cell implementation recurrently.

RNN options

  • codeReuse (true, false) [=false]

    If true, the custom RNN implementation defined by the loopFn parameter will be reused by every shard. If false, the RNN code is duplicated for every shard.

Parameters
graphThe graph to which the RNN cell belongs.
paramsThe RNN parameters.
reverseProcess tensors in reverse order, so beginning with the last element.
initStateThe state tensors that specify the initial states.
stateSequenceOptionally, specify that the recurrent updates of a state tensor need to be stored in a user-defined output tensor.
inputsThe input tensors for each recurrence.
*interimInPointer to the intermediate inputs to cell computation.
*interimOutPointer to the intermediate outputs from cell computation.
outputThe output tensor for each recurrence. Each tensor must be defined prior to calling Rnn().
createdThe output tensor that is allocated by the custom implementation defined in loopFn.
progProgram sequence.
loopFnThe loop body function for RNN cell computation which is invoked for every shard.
numShardsThe number of shards to be used.
optionsThe RNN implementation options. See createInput().
debugContextOptional debug information.

◆ Rnn() [2/2]

std::vector< poplar::Tensor > popnn::rnn::Rnn ( poplar::Graph graph,
const RnnParams params,
const std::vector< poplar::Tensor > &  initState,
const StateSequence stateSequence,
const std::vector< poplar::Tensor > &  inputs,
const poplar::Tensor interimIn,
const unsigned  numTemps,
poplar::program::Sequence prog,
const LoopBodyType loopFn,
const std::vector< poplar::Tensor > &  gatherInputs,
const GatherBodyType gatherFn,
unsigned  numShards,
unsigned  stepsPerGather,
poplar::OptionFlags options,
const poplar::DebugContext debugContext = {} 
)

Run custom Recurrent Neural Net cell callback at every time step in decrementing order.

At each time step, create a temporary variable and pass it to the gatherFn callback function which gets called at a time step interval determined by the stepsPerGather parameter.

RNN options

  • codeReuse (true, false) [=false]

    If true, the custom RNN implementation defined by the loopFn parameter will be reused by every shard. If false, the RNN code is duplicated for every shard.

Parameters
graphThe graph to which the RNN cell belongs.
paramsThe RNN parameters.
initStateThe state tensors that specify the initial states.
stateSequenceOptionally, specify that the recurrent updates of a state tensor need to be stored in a user-defined output tensor.
inputsThe input tensors to the loop body function loopFn
interimInThe intermediate inputs to cell computation.
numTempsThe number of temporary variables of shape [batchSize, size] per time step which are to be passed to the gatherFn callback function.
progProgram sequence.
loopFnFunction for RNN cell computation which is invoked for every time step.
gatherInputsThe input tensors to the gather body function gatherFn.
gatherFnThe gather body function which processes the temporary buffer generated by the loop body function loopFn with the time step interval between calls determined by the stepsPerGather parameter.
numShardsThe number of shards to be used.
stepsPerGatherThe time step interval used by the gatherFn callback.
optionsThe RNN implementation options. See createInput().
debugContextOptional debug information.

◆ rnnBwdSequence()

std::tuple< poplar::Tensor, poplar::Tensor, poplar::Tensor, poplar::Tensor > popnn::rnn::rnnBwdSequence ( poplar::Graph graph,
bool  doWU,
bool  ignoreInputGradientCalc,
poplar::program::Sequence prog,
const poplar::Tensor fwdStateInit,
const poplar::Tensor fwdState,
const poplar::Tensor biases,
const poplar::Tensor feedFwdWeights,
const poplar::Tensor feedbackWeights,
const poplar::Tensor outGradient,
const poplar::Tensor actIn,
const popnn::NonLinearityType nonLinearityType,
const poplar::Type partialsType,
const poplar::DebugContext debugContext = {},
poplin::PlanningCache *  planningCache = nullptr 
)

Perform the feedback part of the RNN layer.

The feedback part of the RNN layer must be preceded by the feedforward part of the RNN layer to complete the layer.

See also
forwardWeightInput

The following definitions apply:

  • numSteps is the number of steps.
  • batchSize is the number of batch elements.
  • inputSize is the size of the input for each step.
  • outputSize is the size of the output for each step.
Parameters
graphThe graph object.
doWUCalculate weight updates.
ignoreInputGradientCalcDo not calculate the gradients over the input weights.
progThe control program.
fwdStateInitThe forward state tensor for initial step.
fwdStateThe forward state tensor for all steps [0:seqSize).
biasesThe biases.
feedFwdWeightsThe input weights.
feedbackWeightsThe feedback weights.
outGradientThe gradient from next layer.
actInThe activations from the previous layer, so the output from the feedforward part of the RNN layer.
nonLinearityTypeThe type of non-linearity used for the output activations.
partialsTypeThe data type for intermediates.
debugContextOptional debug information.
planningCacheThe matmul planning cache.
Returns
Returns four tensors:
  • gradients for the previous layer
  • input weight deltas
  • output weight deltas
  • bias deltas When doWU is false, the weight and bias deltas are not calculated.

◆ rnnFwdSequence()

poplar::Tensor popnn::rnn::rnnFwdSequence ( poplar::Graph graph,
poplar::program::Sequence prog,
const poplar::Tensor fwdStateInit,
const poplar::Tensor weightedIn,
const poplar::Tensor biases,
const poplar::Tensor feedFwdWeights,
const poplar::Tensor feedbackWeights,
const poplar::Tensor prevLayerActs,
const popnn::NonLinearityType nonLinearityType,
const poplar::Type partialsType,
bool  inferenceOnly,
const poplar::DebugContext debugContext = {},
poplin::PlanningCache *  planningCache = nullptr 
)

Perform the forward part of the RNN layer.

The feedback part of the RNN layer must be preceded by the feedforward part of the RNN layer to complete the layer.

See also
forwardWeightInput

The following definitions apply:

  • numSteps is the number of steps
  • batchSize is the number of batch elements.
  • inputSize is the size of the input for each step.
  • outputSize is the size of the output for each step.
Parameters
graphThe graph object.
progThe control program.
fwdStateInitThe forward state tensor for initial step.
weightedInThe preweighted input, or nullptr if feedFwdWeights is to be applied.
biasesThe biases.
feedFwdWeightsThe input weights.
feedbackWeightsThe feedback weights.
prevLayerActsThe activations from previous layer (output from feedforward part of the RNN layer.
nonLinearityTypeThe type of non-linearity used for the output activations.
partialsTypeThe data type for intermediates.
inferenceOnlyIndicates whether the RNN layer is for inference only. If true, we can ignore backwards and weight update passes.
debugContextOptional debug information.
planningCacheThe matmul planning cache.
Returns
Forward state tensor for all steps [0:seqSize).

◆ shiftRnnTensor()

poplar::Tensor popnn::rnn::shiftRnnTensor ( poplar::Graph graph,
const RnnParams params,
const poplar::Tensor tBase,
const poplar::Tensor tSingle,
poplar::program::Sequence prog,
unsigned  numShards,
const poplar::DebugContext debugContext = {} 
)

Create a single-step shifted RNN tensor from an input tensor.

For an input tensor tBase, n-th iteration of the RNN tensor points to the n-1-th iteration of tBase. For the 0-th iteration of the RNN tensor, a copy is made from the provided tensor tSingle tensor.

Parameters
graphThe graph object.
paramsThe RNN parameters.
tBaseThe tensor to shift.
tSingleThe tensor to be copied in the 0-th iteration.
progThe program to add a tensor copy.
numShardsThe number of shards to be used.
debugContextDebug information.
Returns
RNN tensor which is a single-step shifted version of tBase.