8#ifndef poputil_Util_hpp
9#define poputil_Util_hpp
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>
27void mergeAdjacentRegions(std::vector<poplar::Interval> ®ions);
29void mergeAdjacentRegions(std::vector<std::vector<poplar::Interval>> &mapping);
33std::vector<poplar::Interval>
40std::vector<std::vector<poplar::Interval>>
41splitRegions(
const std::vector<poplar::Interval> ®ions,
unsigned grainSize,
42 unsigned maxPartitions,
unsigned minElementsPerPartition = 0,
43 unsigned maxElementsPerPartition = UINT_MAX,
44 unsigned maxElementsPerRegion = UINT_MAX);
50 const poplar::Target &target,
const std::vector<poplar::Interval> ®ions,
51 unsigned grainSize,
unsigned minElementsPerPartition = 0,
52 unsigned maxElementsPerPartition = UINT_MAX,
53 unsigned maxElementsPerRegion = UINT_MAX);
60std::vector<std::vector<std::vector<poplar::Interval>>>
61splitRegions(
const std::vector<std::vector<poplar::Interval>> ®ions,
62 unsigned grainSize,
unsigned maxPartitions,
63 unsigned minElementsPerPartition = 0,
64 unsigned maxElementsPerPartition = UINT_MAX,
65 unsigned maxElementsPerRegion = UINT_MAX);
70std::vector<std::vector<std::vector<poplar::Interval>>>
73 const std::vector<std::vector<poplar::Interval>> ®ions,
74 unsigned grainSize,
unsigned minElementsPerPartition = 0,
75 unsigned maxElementsPerPartition = UINT_MAX,
76 unsigned maxElementsPerRegion = UINT_MAX);
81std::vector<T>
unflattenIndex(
const std::vector<T> &shape, std::size_t index) {
82 std::vector<T> coord(shape.size());
84 for (std::size_t i = shape.size(); i > 0; --i) {
85 coord[i - 1] = index % shape[i - 1];
86 index /= shape[i - 1];
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];
109 const std::vector<std::vector<poplar::Interval>> &seq);
187 const std::vector<std::size_t> &factors,
188 unsigned startDim = 0);
200 unsigned startDim = 0);
205 poplar::QuarterMetadata::Format fp8Format,
210 poplar::QuarterMetadata::Format fp8Format,
231std::vector<poplar::Interval>
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 > ®ions, 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 > ®ions, 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.