Poplar and PopLibs
Rnn.hpp
Go to the documentation of this file.
1// Copyright (c) 2021 Graphcore Ltd. All rights reserved.
6#ifndef popnn_Rnn_hpp
7#define popnn_Rnn_hpp
8
9#include <cassert>
10#include <cstdint>
11#include <poplar/Graph.hpp>
12#include <poplar/Program.hpp>
13#include <poplar/Tensor.hpp>
14#include <poputil/DebugInfo.hpp>
15
16namespace popnn {
17namespace rnn {
18
22struct RnnParams {
25
27 std::size_t batchSize;
28
30 std::size_t maxTimeSteps;
31
33 std::size_t timeSteps;
34
40
45 std::vector<std::size_t> layerSizes;
46
47 RnnParams(poplar::Type dataType, std::size_t batchSize, std::size_t timeSteps,
48 std::vector<std::size_t> layerSizes);
49
51 std::size_t maxTimeSteps, const poplar::Tensor &varTimeSteps,
52 std::vector<std::size_t> layerSizes);
53
54 // Return the maximum number of shards.
55 std::size_t getMaxShards(const poplar::Graph &graph) const;
56
57 // Return the number of bytes of the input per tile.
58 std::size_t getInputBytesPerTile(const poplar::Graph &graph) const;
59
60 // Return the number of bytes of the output per tile.
61 std::size_t getOutputBytesPerTile(const poplar::Graph &graph) const;
62
63 // Indicate that time steps are determined by tensor variable.
64 bool variableTimeSteps() const;
65
66 // Indicate that time steps are determined by tensor variable for each batch.
67 bool batchVariableTimeSteps() const;
68};
69
89createInitialState(poplar::Graph &graph, const RnnParams &params, bool isOutput,
90 unsigned multiple, unsigned numShards,
91 const poplar::DebugContext &debugContext = {});
92
106 unsigned size, unsigned numShards,
107 const poplar::DebugContext &debugContext = {});
108
121 unsigned numShards,
122 const poplar::DebugContext &debugContext = {});
123
137 unsigned numShards,
138 const poplar::DebugContext &debugContext = {});
139
159 unsigned multiple, unsigned numShards,
160 const poplar::DebugContext &debugContext = {});
161
179 const poplar::Tensor &tBase,
180 const poplar::Tensor &tSingle,
182 unsigned numShards,
183 const poplar::DebugContext &debugContext = {});
184
192struct RnnSlice {
193 std::vector<poplar::Tensor> inputs;
194 poplar::Tensor interimIn;
195 poplar::Tensor interimOut;
196 std::vector<poplar::Tensor> outputs;
197};
198
199/* Flags set per batch if the current step is within the batchwise step limit.
200 * The component tensor(s) are of type `dataType` and shape [`batchSize`].
201 */
202struct RnnBatchwiseFlags {
203 poplar::Tensor mask;
204 poplar::Tensor inverse;
205
206 bool valid() const { return mask.valid(); };
207};
208
209struct TimeStepState {
210 poplar::Tensor begin;
211 poplar::Tensor counter;
212 poplar::Tensor variableSeqFlag;
213};
214
235 poplar::Graph &graph, const TimeStepState &time, const RnnBatchwiseFlags &,
236 std::vector<poplar::Tensor> &, const RnnSlice &slice,
237 std::vector<poplar::Tensor> &, poplar::program::Sequence *,
238 const poplar::DebugNameAndId &)>;
239
252 poplar::Graph &graph, const RnnSlice &slice, unsigned stepsPerGather,
254
265 poplar::Tensor output;
266 std::size_t stateIndex;
267};
268
304std::vector<poplar::Tensor>
305Rnn(poplar::Graph &graph, const RnnParams &params, bool reverse,
306 const std::vector<poplar::Tensor> &initState,
307 const StateSequence &stateSequence,
308 const std::vector<poplar::Tensor> &inputs, const poplar::Tensor *interimIn,
309 poplar::Tensor *interimOut, const std::vector<poplar::Tensor> &outputs,
310 const std::vector<poplar::Tensor> &created, poplar::program::Sequence &prog,
311 const LoopBodyType &loopFn, unsigned numShards,
312 poplar::OptionFlags &options,
313 const poplar::DebugContext &debugContext = {});
314
357std::vector<poplar::Tensor>
358Rnn(poplar::Graph &graph, const RnnParams &params,
359 const std::vector<poplar::Tensor> &initState,
360 const StateSequence &stateSequence,
361 const std::vector<poplar::Tensor> &inputs, const poplar::Tensor &interimIn,
362 const unsigned numTemps, poplar::program::Sequence &prog,
363 const LoopBodyType &loopFn, const std::vector<poplar::Tensor> &gatherInputs,
364 const GatherBodyType &gatherFn, unsigned numShards, unsigned stepsPerGather,
365 poplar::OptionFlags &options,
366 const poplar::DebugContext &debugContext = {});
367
368} // namespace rnn
369} // namespace popnn
370
371#endif // #ifndef popnn_Rnn_hpp
Poplibs generic debug info structure.
DebugContext gathers the common external parameters of the context of an operation.
Definition: DebugContext.hpp:221
DebugNameAndId bundles a name and a DebugId to facilitate their propagation through function calls.
Definition: DebugContext.hpp:142
This class represents a graph program to be executed on the IPU.
Definition: Graph.hpp:52
A set of option/value string flags to be used in various APIs.
Definition: OptionFlags.hpp:24
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
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...
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/...
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.
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 o...
std::vector< poplar::Tensor > 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.
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.
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 &)> LoopBodyType
Create loop body function for the given shard.
Definition: Rnn.hpp:238
std::function< poplar::program::Sequence(poplar::Graph &graph, const RnnSlice &slice, unsigned stepsPerGather, poplar::program::Sequence *, const poplar::DebugNameAndId &)> GatherBodyType
Create gather body function for the given shard.
Definition: Rnn.hpp:253
Functions used in neural networks.
Definition: BatchNorm.hpp:14
Structure of Recurrent Neural Network (RNN) parameters which allows for any customized implementation...
Definition: Rnn.hpp:22
std::size_t maxTimeSteps
The maximum number of RNN time steps.
Definition: Rnn.hpp:30
std::vector< std::size_t > layerSizes
For each RNN layer, the layer size parameter needs to be specified for the input and the output.
Definition: Rnn.hpp:45
poplar::Type dataType
The datatype used for the RNN.
Definition: Rnn.hpp:24
std::size_t timeSteps
Definition: Rnn.hpp:33
poplar::Tensor varTimeSteps
The run-time number of RNN time steps of dimension [batchSize] If this tensor is default constructed,...
Definition: Rnn.hpp:39
std::size_t batchSize
The batch size.
Definition: Rnn.hpp:27
Tensors required for processing a single time step.
Definition: Rnn.hpp:192
Structure that associates a particular state tensor with a user-defined output tensor.
Definition: Rnn.hpp:264