Poplar and PopLibs
Convolution.hpp
Go to the documentation of this file.
1// Copyright (c) 2016 Graphcore Ltd. All rights reserved.
8#ifndef poplin_Convolution_hpp
9#define poplin_Convolution_hpp
10#include "ConvParams.hpp"
11
12#include <poplar/Graph.hpp>
13#include <poplar/OptionFlags.hpp>
14#include <poplar/Program.hpp>
15#include <set>
16#include <tuple>
17
18namespace poplin {
19
22class PlanningCache;
23
26uint64_t getFwdFlops(const ConvParams &params);
29uint64_t getBwdFlops(const ConvParams &params);
32uint64_t getWuFlops(const ConvParams &params);
33
47 const ConvParams &params);
48
62 const ConvParams &params);
63
77 const ConvParams &params);
78
156/*[INTERNAL]
157 * * `numIPUs` Integer [=target.getNumIPUs()]
158 *
159 * Number of IPUs to be used.
160 *
161 * * `remapOutputTensor` (true, false) [=true]
162 *
163 * If true, the output of the convolution is remapped if the output
164 * is detected to have a poor layout. The convolutions planner will try
165 * to map the channels in groups of 16, 8 or 4. This typically results
166 * in better performance for the operation(s) consuming the output
167 * of the convolution.
168 *
169 *
170 * * `planConstraints` JSON string
171 *
172 * Constraints on the chosen convolution plan. Example:
173 *
174 * {"0": {"transform": {"swapOperands": true},
175 * "partition": {"fieldSplit":{"1": 4},
176 * "inChanSplit": 4,
177 * "outChanSplit": {"parallel": 4}}
178 * }
179 * }
180 *
181 * Where the outer-most index in the plan is an index into the plan
182 * hierarchy, and any multi-dimensional fields are sparsely indexed
183 * objects. Therefore, constraining dimension 1 of fieldSplit to be 4 is
184 * specified as:
185 *
186 * {"fieldSplit": {"1": 4}}
187 *
188 * This is only implemented for `partitioning` and for the `swapOperands`
189 * transform for now.
190 *
191 * * `planConstraintsOutputFilename` String
192 *
193 * If set, plan constraints for each plan used by a convolution will be
194 * saved to file. The file path will be the value of this option appended
195 * with _FWD, _BWD, or _WU (depending on the pass), with a file extension
196 * of .json. The content of these files may be used as input to the
197 * `planConstraints` option (above). The constraints will be complete,
198 * meaning they can only be satisfied by one specific plan - this allows
199 * reliable reproduction regardless of changes to the planner.
200 *
201 * * `partialsType.interIPU` (half, float) [=`partialsType`]
202 *
203 * Data type of inter-IPU partials. If the type specified
204 * is smaller than the output type then the option is ignored and the
205 * output type is used instead.
206 *
207 * * `partialsType.interTile` (half, float) [=`partialsType`]
208 *
209 * Data type of inter-tile partials. If the type specified
210 * is smaller than the output type then the option is ignored and the
211 * output type is used instead.
212 *
213 * * `tilesPerIPU` Integer [=target.getTilesPerIPU()]
214 *
215 * Number of tiles per IPU to be used.
216 *
217 * * `gatherConvOutput` (true, false) [=false]
218 *
219 * Gather output of the matrix multiply into a single variable
220 *
221 * * 'experimental.slicVmac16' (true, false) [=false]
222 *
223 * Restricts convolution planner to use SLIC/VMAC vertices with
224 * grouping of 16
225 *
226 * * 'disableSRForAMPVertices' (true, false) [=false]
227 *
228 * Disable stochastic rounding for vertices that use AMP
229 *
230 */
239poplar::Tensor createWeights(poplar::Graph &graph, const ConvParams &params,
240 const poplar::DebugContext &debugContext = {},
241 const poplar::OptionFlags &options = {},
242 PlanningCache *cache = nullptr);
243
255createBiases(poplar::Graph &graph, const poplar::Tensor &activations,
256 const poplar::DebugContext &debugContext = {"biases"});
257
274createBiases(poplar::Graph &graph, const poplar::Tensor &activations,
275 const ConvParams &params,
276 const poplar::DebugContext &debugContext = {"biases"},
277 const poplar::OptionFlags &options = {},
278 PlanningCache *cache = nullptr);
279
295poplar::Tensor createInput(poplar::Graph &graph, const ConvParams &params,
296 const poplar::DebugContext &debugContext = {},
297 const poplar::OptionFlags &options = {},
298 PlanningCache *cache = nullptr);
299
316poplar::Tensor createConvOutput(poplar::Graph &graph, const ConvParams &params,
317 const poplar::DebugContext &debugContext = {},
318 const poplar::OptionFlags &options = {},
319 PlanningCache *cache = nullptr);
320
346 const poplar::Tensor &weights,
347 const ConvParams &params,
348 bool transposeAndFlipWeights,
350 const poplar::DebugContext &debugContext = {},
351 const poplar::OptionFlags &options = {},
352 PlanningCache *cache = nullptr);
353
378 const poplar::Tensor &weights,
379 const poplar::Tensor &out, const ConvParams &params,
380 bool transposeAndFlipWeights,
382 const poplar::DebugContext &debugContext = {},
383 const poplar::OptionFlags &options = {},
384 PlanningCache *cache = nullptr);
385
386using ConvPlanParams = std::tuple<const poplar::Target *, const ConvParams,
387 const poplar::OptionFlags *>;
400void preplanConvolutions(const std::set<ConvPlanParams> &convs,
401 PlanningCache &cache);
402
417 const std::set<ConvPlanParams> &convs,
418 PlanningCache &cache);
419
435 const poplar::Tensor &weightsIn,
436 const poplar::Tensor &weightsOut,
438 const poplar::DebugContext &debugContext = {},
439 const poplar::OptionFlags &options = {});
440
462 const poplar::Tensor &activations,
463 const ConvParams &params, poplar::program::Sequence &prog,
464 const poplar::DebugContext &debugContext = {},
465 const poplar::OptionFlags &options = {},
466 PlanningCache *cache = nullptr);
467
488 const poplar::Tensor &zDeltas,
489 const poplar::Tensor &weights,
490 const poplar::Tensor &activations,
491 ConvParams params, const poplar::Tensor &scale,
493 const poplar::DebugContext &debugContext = {},
494 const poplar::OptionFlags &options = {},
495 PlanningCache *cache = nullptr);
496
517 poplar::Graph &graph, const poplar::Tensor &zDeltas,
518 const poplar::Tensor &weights, const poplar::Tensor &activations,
519 ConvParams params, float scale, poplar::program::Sequence &prog,
520 const poplar::DebugContext &debugContext = {},
521 const poplar::OptionFlags &options = {}, PlanningCache *cache = nullptr);
522
537 const poplar::Tensor &biases,
538 const poplar::Tensor &scale,
539 const poplar::OptionFlags &options,
541 const poplar::DebugContext &debugContext = {});
542
557 const poplar::Tensor &biases, float scale,
558 const poplar::OptionFlags &options,
560 const poplar::DebugContext &debugContext = {});
561
570void addBias(poplar::Graph &graph, const poplar::Tensor &in,
571 const poplar::Tensor &biases, poplar::program::Sequence &prog,
572 const poplar::DebugContext &debugContext = {});
573
584void reportPlanInfo(std::ostream &out, const poplar::Graph &graph,
585 const ConvParams &params,
586 const poplar::OptionFlags &options = {},
587 PlanningCache *cache = nullptr);
588
590struct PlanCosts {
591 std::size_t cycles;
592 std::size_t memory;
593};
594
608 const ConvParams &params,
609 const poplar::OptionFlags &options = {},
610 PlanningCache *cache = nullptr);
611
622void reportWeightUpdatePlanInfo(std::ostream &out, const poplar::Graph &graph,
623 const ConvParams &fwdParams,
624 const poplar::OptionFlags &fwdOptions = {},
625 PlanningCache *cache = nullptr);
626
640 poplar::Graph &graph, poplar::Tensor weights, const ConvParams &params,
642 const poplar::DebugContext &debugContext = {},
643 const poplar::OptionFlags &options = {}, PlanningCache *cache = nullptr);
644
652
653struct Plan;
654
655class PlanningCacheImpl;
656class PlanningCache {
657public:
658 PlanningCache();
659 ~PlanningCache();
660
662 std::size_t size() const;
663
664 std::unique_ptr<PlanningCacheImpl> impl;
665};
666
667} // namespace poplin
668
669#endif // poplin_Convolution_hpp
Data types for convolution parameters.
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 set of option/value string flags to be used in various APIs.
Definition: OptionFlags.hpp:24
A target representation.
Definition: Target.hpp:69
A reference to a subset of tensor elements.
Definition: Tensor.hpp:38
Program that executes a sequence of programs.
Definition: Program.hpp:77
Linear algebra functions.
Definition: Cholesky.hpp:14
double getWuPerfectCycleCount(const poplar::Graph &graph, const ConvParams &params)
Calculate the number of cycles to perform the weight update pass assuming maximal utilisation of the ...
void convolutionWeightUpdate(poplar::Graph &graph, const poplar::Tensor &zDeltas, const poplar::Tensor &weights, const poplar::Tensor &activations, ConvParams params, const poplar::Tensor &scale, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext={}, const poplar::OptionFlags &options={}, PlanningCache *cache=nullptr)
Append operations to a poplar::Program to generate and apply the weight update.
poplar::Tensor calculateWeightDeltas(poplar::Graph &graph, const poplar::Tensor &zDeltas, const poplar::Tensor &activations, const ConvParams &params, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext={}, const poplar::OptionFlags &options={}, PlanningCache *cache=nullptr)
Append an operation to a poplar::Program to generate the tensor of weight deltas.
poplar::Tensor convolution(poplar::Graph &graph, const poplar::Tensor &in, const poplar::Tensor &weights, const ConvParams &params, bool transposeAndFlipWeights, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext={}, const poplar::OptionFlags &options={}, PlanningCache *cache=nullptr)
Convolve an input with a set of weights.
void convolutionValidateOptions(const poplar::OptionFlags &options)
Provides an interface to validate the convolution options.
uint64_t getWuFlops(const ConvParams &params)
Calculate minimum number of floating point operations required to perform the weight update pass conv...
poplar::Tensor createBiases(poplar::Graph &graph, const poplar::Tensor &activations, const poplar::DebugContext &debugContext={"biases"})
Create a bias tensor suitable for input to the addBias() function.
poplar::Tensor fullyConnectedWeightTranspose(poplar::Graph &graph, poplar::Tensor weights, const ConvParams &params, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext={}, const poplar::OptionFlags &options={}, PlanningCache *cache=nullptr)
Arranges the weights (activations) such that they are suited for the backward pass in a fully connect...
void addBias(poplar::Graph &graph, const poplar::Tensor &in, const poplar::Tensor &biases, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext={})
Adds a program to prog which adds biases to activations tensor.
uint64_t getFwdFlops(const ConvParams &params)
Calculate the minimum number of floating point operations required to perform the forward pass convol...
void convolutionWithOutput(poplar::Graph &graph, const poplar::Tensor &in, const poplar::Tensor &weights, const poplar::Tensor &out, const ConvParams &params, bool transposeAndFlipWeights, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext={}, const poplar::OptionFlags &options={}, PlanningCache *cache=nullptr)
Convolve an input with a set of weights into a pre-allocated output tensor.
void reportWeightUpdatePlanInfo(std::ostream &out, const poplar::Graph &graph, const ConvParams &fwdParams, const poplar::OptionFlags &fwdOptions={}, PlanningCache *cache=nullptr)
Report the convolution plan corresponding to the weight update pass given the forward pass params and...
void convolutionBiasUpdate(poplar::Graph &graph, const poplar::Tensor &zDeltas, const poplar::Tensor &biases, const poplar::Tensor &scale, const poplar::OptionFlags &options, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext={})
Add a program to update biases tensor with the gradients derived from the zDeltas tensor.
poplar::Tensor createInput(poplar::Graph &graph, const ConvParams &params, const poplar::DebugContext &debugContext={}, const poplar::OptionFlags &options={}, PlanningCache *cache=nullptr)
Create an input tensor for a convolution.
poplar::Tensor createConvOutput(poplar::Graph &graph, const ConvParams &params, const poplar::DebugContext &debugContext={}, const poplar::OptionFlags &options={}, PlanningCache *cache=nullptr)
Create an output tensor for a convolution.
void preplanConvolutions(const std::set< ConvPlanParams > &convs, PlanningCache &cache)
uint64_t getBwdFlops(const ConvParams &params)
Calculate the minimum number of floating point operations required to perform the backward pass convo...
double getBwdPerfectCycleCount(const poplar::Graph &graph, const ConvParams &params)
Calculate the number of cycles to perform the backward pass assuming maximal utilisation of the targe...
PlanCosts reportPlanEstimatedCosts(const poplar::Graph &graph, const ConvParams &params, const poplar::OptionFlags &options={}, PlanningCache *cache=nullptr)
Report the estimated cycles and memory costs of the convolution plan corresponding to the params and ...
void weightsTransposeChansFlipXY(poplar::Graph &graph, const poplar::Tensor &weightsIn, const poplar::Tensor &weightsOut, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext={}, const poplar::OptionFlags &options={})
Copy the weights in weightsIn into weightsOut such that each element of the kernel is transposed with...
poplar::Tensor createWeights(poplar::Graph &graph, const ConvParams &params, const poplar::DebugContext &debugContext={}, const poplar::OptionFlags &options={}, PlanningCache *cache=nullptr)
Create a weight tensor suitable for use with convolution()
double getFwdPerfectCycleCount(const poplar::Graph &graph, const ConvParams &params)
Calculate the number of cycles to perform the forward pass assuming maximal utilisation of target har...
void reportPlanInfo(std::ostream &out, const poplar::Graph &graph, const ConvParams &params, const poplar::OptionFlags &options={}, PlanningCache *cache=nullptr)
Report the convolution plan corresponding to the params and options provided.
Structure for estimated costs returned by reportPlanEstimatedCosts()
Definition: Convolution.hpp:590