Poplar and PopLibs
Util.hpp
Go to the documentation of this file.
1// Copyright (c) 2016 Graphcore Ltd. All rights reserved.
8#ifndef poputil_Util_hpp
9#define poputil_Util_hpp
10
11#include <algorithm>
12#include <cassert>
13#include <climits>
14#include <poplar/Device.hpp>
15#include <poplar/Graph.hpp>
16#include <poplar/Interval.hpp>
17#include <poplar/Program.hpp>
18#include <poplar/Quarter.hpp>
19#include <poplar/Target.hpp>
20#include <poplar/Tensor.hpp>
21#include <string>
22#include <vector>
23
25namespace poputil {
26
27void mergeAdjacentRegions(std::vector<poplar::Interval> &regions);
28
29void mergeAdjacentRegions(std::vector<std::vector<poplar::Interval>> &mapping);
30
33std::vector<poplar::Interval>
34flattenIntervals(const std::vector<std::vector<poplar::Interval>> &intervals);
35
40std::vector<std::vector<poplar::Interval>>
41splitRegions(const std::vector<poplar::Interval> &regions, unsigned grainSize,
42 unsigned maxPartitions, unsigned minElementsPerPartition = 0,
43 unsigned maxElementsPerPartition = UINT_MAX,
44 unsigned maxElementsPerRegion = UINT_MAX);
45
49std::vector<std::vector<poplar::Interval>> splitRegionsBetweenWorkers(
50 const poplar::Target &target, const std::vector<poplar::Interval> &regions,
51 unsigned grainSize, unsigned minElementsPerPartition = 0,
52 unsigned maxElementsPerPartition = UINT_MAX,
53 unsigned maxElementsPerRegion = UINT_MAX);
54
60std::vector<std::vector<std::vector<poplar::Interval>>>
61splitRegions(const std::vector<std::vector<poplar::Interval>> &regions,
62 unsigned grainSize, unsigned maxPartitions,
63 unsigned minElementsPerPartition = 0,
64 unsigned maxElementsPerPartition = UINT_MAX,
65 unsigned maxElementsPerRegion = UINT_MAX);
66
70std::vector<std::vector<std::vector<poplar::Interval>>>
72 const poplar::Target &target,
73 const std::vector<std::vector<poplar::Interval>> &regions,
74 unsigned grainSize, unsigned minElementsPerPartition = 0,
75 unsigned maxElementsPerPartition = UINT_MAX,
76 unsigned maxElementsPerRegion = UINT_MAX);
77
80template <class T>
81std::vector<T> unflattenIndex(const std::vector<T> &shape, std::size_t index) {
82 std::vector<T> coord(shape.size());
83
84 for (std::size_t i = shape.size(); i > 0; --i) {
85 coord[i - 1] = index % shape[i - 1];
86 index /= shape[i - 1];
87 }
88
89 assert(index == 0);
90 return coord;
91}
92
95template <class T>
96std::size_t flattenIndex(const std::vector<T> &shape,
97 const std::vector<T> &indices) {
98 auto rank = shape.size();
99 assert(indices.size() == rank);
100 std::size_t index = 0;
101 for (unsigned i = 0; i != rank; ++i) {
102 index = index * shape[i] + indices[i];
103 }
104 return index;
105}
106
109 const std::vector<std::vector<poplar::Interval>> &seq);
110
116 const poplar::DebugContext &debugContext = {},
119
133cloneN(poplar::Graph &graph, const poplar::Tensor &t, unsigned N,
134 const poplar::DebugContext &debugContext = {},
137
144std::vector<int> balancedPartition(int rangeUpperBound, int splitCount);
145
153double castToDeviceHalfValue(const poplar::Target &target, double input);
154
169bool checkAccuracyWhenCast(const poplar::Target &target, double input,
170 poplar::Type inputType, poplar::Type outputType,
171 double tolerance);
172
187 const std::vector<std::size_t> &factors,
188 unsigned startDim = 0);
189
199poplar::Tensor unfactorDims(const poplar::Tensor &t, unsigned numDims,
200 unsigned startDim = 0);
201
202// Create metadata for use with FP8 data types
204createConstantMetadataTensor(poplar::Graph &graph,
205 poplar::QuarterMetadata::Format fp8Format,
206 int fp8Scale);
207
209createVariableMetadataTensor(poplar::Graph &graph,
210 poplar::QuarterMetadata::Format fp8Format,
211 int fp8Scale);
212
231std::vector<poplar::Interval>
232calculateUnshufflingIntervals(const std::vector<poplar::Interval> &intervals);
233
234} // end namespace poputil
235
236#endif // poputil_Util_hpp
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 target representation.
Definition: Target.hpp:69
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
TensorCloneMethod
Define behaviour when a Tensor is cloned.
Definition: TensorCloneMethod.hpp:13
@ PRESERVE_ORDER_UNLESS_ALIASES
Preserve the ordering of the original tensor unless it contains aliases.
General utility functions for building graphs.
Definition: GfloatExprUtil.hpp:23
std::size_t intervalSequenceNumElements(const std::vector< std::vector< poplar::Interval > > &seq)
Return the total number of elements in the interval sequence.
std::vector< poplar::Interval > calculateUnshufflingIntervals(const std::vector< poplar::Interval > &intervals)
Calculate the un-shuffling intervals based on the given intervals.
poplar::Tensor factorDims(const poplar::Tensor &t, const std::vector< std::size_t > &factors, unsigned startDim=0)
Factors the outermost dimensions of tensor t by the values given in factors.
poplar::Tensor cloneN(poplar::Graph &graph, const poplar::Tensor &t, unsigned N, const poplar::DebugContext &debugContext={}, poplar::TensorCloneMethod method=poplar::TensorCloneMethod::PRESERVE_ORDER_UNLESS_ALIASES)
Clone a tensor N times.
std::vector< int > balancedPartition(int rangeUpperBound, int splitCount)
Split a range.
bool checkAccuracyWhenCast(const poplar::Target &target, double input, poplar::Type inputType, poplar::Type outputType, double tolerance)
Check accuracy of a cast operation.
poplar::Tensor duplicate(poplar::Graph &graph, const poplar::Tensor &in, poplar::program::Sequence &p, const poplar::DebugContext &debugContext={}, poplar::TensorCloneMethod method=poplar::TensorCloneMethod::PRESERVE_ORDER_UNLESS_ALIASES)
Copy a tensor's data to a new tensor.
std::vector< poplar::Interval > flattenIntervals(const std::vector< std::vector< poplar::Interval > > &intervals)
Flatten a vector of vectors of intervals to a vector, maintaining ordering.
std::vector< std::vector< poplar::Interval > > splitRegionsBetweenWorkers(const poplar::Target &target, const std::vector< poplar::Interval > &regions, unsigned grainSize, unsigned minElementsPerPartition=0, unsigned maxElementsPerPartition=UINT_MAX, unsigned maxElementsPerRegion=UINT_MAX)
Given a set of contiguous regions per tile, partition these regions between workers on that tile whil...
std::size_t flattenIndex(const std::vector< T > &shape, const std::vector< T > &indices)
Given a list of indices into a tensor, return the corresponding index in a flattened version of the t...
Definition: Util.hpp:96
std::vector< T > unflattenIndex(const std::vector< T > &shape, std::size_t index)
Given an index into a flattened tensor, returns the indices into the dimensions of the original tenso...
Definition: Util.hpp:81
poplar::Tensor unfactorDims(const poplar::Tensor &t, unsigned numDims, unsigned startDim=0)
The opposite of factorDims().
std::vector< std::vector< poplar::Interval > > splitRegions(const std::vector< poplar::Interval > &regions, unsigned grainSize, unsigned maxPartitions, unsigned minElementsPerPartition=0, unsigned maxElementsPerPartition=UINT_MAX, unsigned maxElementsPerRegion=UINT_MAX)
Given a set of contiguous regions, partition these regions while trying to balance the number of elem...
double castToDeviceHalfValue(const poplar::Target &target, double input)
Cast a double precision value to a value exactly representable in device HALF type.