Poplar and PopLibs
Recurrent.hpp
Go to the documentation of this file.
1// Copyright (c) 2017 Graphcore Ltd. All rights reserved.
6#ifndef popnn_Recurrent_hpp
7#define popnn_Recurrent_hpp
8
9/*
10 * Vanilla RNN layer implementation:
11 *
12 * ------ ---- ----------------
13 * x -->| Wff |----------->| + |------->| Non linearity |----------> y
14 * ------ ---- ---------------- |
15 * /\ |
16 * | ------ |
17 * --------| Wfb |<------------------
18 * -----
19 *
20 *
21 * In general, the RNN can be run over a set of sequence steps. The
22 * multiplication with Wff can be done in parallel for any subset or even the
23 * full set of sequence steps. The recurrent part must be done a step at a
24 * time.
25 *
26 * In the code below:
27 * Wff is named weightsInput
28 * Wfb is named weightsFeedback
29 */
30
31#include <poplar/Graph.hpp>
32#include <poplar/Program.hpp>
34
35namespace poplin {
36
37class PlanningCache;
38
39} // namespace poplin
40
41namespace popnn {
43namespace rnn {
44
49std::vector<std::pair<poplin::MatMulParams, poplar::OptionFlags>>
50getMatMulPrePlanParameters(std::size_t numSteps, std::size_t batchSize,
51 std::size_t inputSize, std::size_t outputSize,
52 const poplar::Type &dType,
53 const poplar::Type &partialsType = poplar::FLOAT,
54 bool inferenceOnly = false,
55 bool hasFeedforwardWeights = true);
56
60uint64_t getFwdFlops(unsigned sequenceSize, unsigned batchSize,
61 unsigned inputSize, unsigned outputSize,
62 bool weightInput = true);
66uint64_t getBwdFlops(unsigned sequenceSize, unsigned batchSize,
67 unsigned inputSize, unsigned outputSize,
68 bool calcInputGrad = true);
72uint64_t getWuFlops(unsigned sequenceSize, unsigned batchSize,
73 unsigned inputSize, unsigned outputSize);
74
96poplar::Tensor createInput(poplar::Graph &graph, unsigned numSteps,
97 unsigned batchSize, unsigned inputSize,
98 unsigned outputSize, const poplar::Type &dType,
99 const poplar::Type &partialsType = poplar::FLOAT,
100 bool inferenceOnly = false,
101 const poplar::DebugContext &debugContext = {},
102 poplin::PlanningCache *planningCache = nullptr);
103
127 unsigned batchSize, unsigned outputSize,
128 poplar::program::Sequence &prog, bool initState,
129 bool inferenceOnly,
130 const poplar::DebugContext &debugContext = {},
131 poplin::PlanningCache *planningCache = nullptr);
132
137
159 poplar::Graph &graph, unsigned sequenceSize, unsigned batchSize,
160 unsigned inputSize, unsigned outputSize, const poplar::Type &dType,
161 const poplar::Type &partialsType = poplar::FLOAT,
162 bool inferenceOnly = false, const poplar::DebugContext &debugContext = {},
163 poplin::PlanningCache *planningCache = nullptr);
164
179 poplar::Graph &graph, unsigned batchSize, unsigned outputSize,
180 const poplar::Type &dType, const poplar::Type &partialsType = poplar::FLOAT,
181 bool inferenceOnly = false, const poplar::DebugContext &debugContext = {},
182 poplin::PlanningCache *planningCache = nullptr);
183
215 poplar::Graph &graph, const poplar::Tensor &actIn,
216 const poplar::Tensor &weights, poplar::program::Sequence &prog,
217 const poplar::Type &partialsType = poplar::FLOAT,
218 bool inferenceOnly = false, const poplar::DebugContext &debugContext = {},
219 poplin::PlanningCache *planningCache = nullptr);
220
254 poplar::Graph &graph, const poplar::Tensor &feedFwdIn,
255 const poplar::Tensor &initState, const poplar::Tensor &feedbackWeights,
256 const poplar::Tensor &biases, poplar::program::Sequence &prog,
257 popnn::NonLinearityType nonLinearityType,
258 const poplar::Type &partialsType = poplar::FLOAT,
259 bool inferenceOnly = false, const poplar::DebugContext &debugContext = {},
260 poplin::PlanningCache *planningCache = nullptr);
261
275 unsigned batchSize, unsigned outputSize,
277 const poplar::DebugContext &debugContext = {},
278 poplin::PlanningCache *planningCache = nullptr);
279
285/* ------ ---- -----
286 * <---| Wfb |<------| + |<---------| NL |<------- (bwd:gradientOut
287 * ------ ---- ----- for final step)
288 * | (bwd:gradientOut)
289 * \|/
290 * -----
291 * | Wff |
292 * ------
293 * |
294 * Wfb are the feedback weights
295 * Wff are the input weights
296 */
314std::pair<poplar::Tensor, poplar::Tensor> backwardGradientStep(
315 poplar::Graph &graph, const poplar::Tensor &nextLayerGrad,
316 const poplar::Tensor &bwdState, const poplar::Tensor &actOut,
317 const poplar::Tensor &weightsInput, const poplar::Tensor &weightsFeedback,
319 const poplar::Type &partialsType = poplar::FLOAT,
320 const poplar::DebugContext &debugContext = {},
321 poplin::PlanningCache *planningCache = nullptr);
322
323// clang-format off
324// To handle long lines in Doxygen for auto-linking of overloaded functions.
327// clang-format on
329 poplar::Graph &graph, const poplar::Tensor &nextLayerGrad,
330 const poplar::Tensor &bwdState, const poplar::Tensor &actOut,
331 const poplar::Tensor &weightsFeedback, poplar::program::Sequence &prog,
332 popnn::NonLinearityType nonLinearityType,
333 const poplar::Type &partialsType = poplar::FLOAT,
334 const poplar::DebugContext &debugContext = {},
335 poplin::PlanningCache *planningCache = nullptr);
336
364void paramDeltaUpdate(poplar::Graph &graph, const poplar::Tensor &bwdState,
365 const poplar::Tensor &actIn,
366 const poplar::Tensor &prevOut,
367 poplar::Tensor &weightsInputDeltasAcc,
368 poplar::Tensor &weightsFeedbackDeltasAcc,
369 poplar::Tensor &biasDeltasAcc,
371 const poplar::Type &partialsType = poplar::FLOAT,
372 const poplar::DebugContext &debugContext = {},
373 poplin::PlanningCache *planningCache = nullptr);
374
410 const poplar::Tensor &fwdStateInit, const poplar::Tensor *weightedIn,
411 const poplar::Tensor &biases, const poplar::Tensor &feedFwdWeights,
412 const poplar::Tensor &feedbackWeights, const poplar::Tensor &prevLayerActs,
413 const popnn::NonLinearityType &nonLinearityType,
414 const poplar::Type &partialsType, bool inferenceOnly,
415 const poplar::DebugContext &debugContext = {},
416 poplin::PlanningCache *planningCache = nullptr);
417
459std::tuple<poplar::Tensor, poplar::Tensor, poplar::Tensor, poplar::Tensor>
460rnnBwdSequence(poplar::Graph &graph, bool doWU, bool ignoreInputGradientCalc,
462 const poplar::Tensor &fwdStateInit,
463 const poplar::Tensor &fwdState, const poplar::Tensor &biases,
464 const poplar::Tensor &feedFwdWeights,
465 const poplar::Tensor &feedbackWeights,
466 const poplar::Tensor &outGradient, const poplar::Tensor &actIn,
467 const popnn::NonLinearityType &nonLinearityType,
468 const poplar::Type &partialsType,
469 const poplar::DebugContext &debugContext = {},
470 poplin::PlanningCache *planningCache = nullptr);
471
472} // namespace rnn
473} // namespace popnn
474
475#endif // popnn_Recurrent_hpp
Non-linearity operations.
DebugContext gathers the common external parameters of the context of an operation.
Definition: DebugContext.hpp:221
This class represents a graph program to be executed on the IPU.
Definition: Graph.hpp:52
A reference to a subset of tensor elements.
Definition: Tensor.hpp:38
Class representing device data types.
Definition: Type.hpp:42
Program that executes a sequence of programs.
Definition: Program.hpp:77
Type FLOAT
Device type: float
Linear algebra functions.
Definition: Cholesky.hpp:14
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.
poplar::Tensor getOutputFromFwdState(const poplar::Tensor &fwdState)
Extract previous output tensor from the hidden state.
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.
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 corres...
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.
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.
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.
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.
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.
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.
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.
std::pair< poplar::Tensor, poplar::Tensor > 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.
uint64_t getWuFlops(unsigned sequenceSize, unsigned batchSize, unsigned inputSize, unsigned outputSize)
Compute the total floating point operations for the weight update pass of RNN.
std::tuple< poplar::Tensor, poplar::Tensor, poplar::Tensor, poplar::Tensor > 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.
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.
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.
Functions used in neural networks.
Definition: BatchNorm.hpp:14
NonLinearityType
Definition: NonLinearityDef.hpp:11