Poplar and PopLibs
Tensor.hpp
1// Copyright (c) 2016 Graphcore Ltd. All rights reserved.
2
3#ifndef poplar_Tensor_hpp
4#define poplar_Tensor_hpp
5#include <memory>
6#include <poplar/ArrayRef.hpp>
7#include <poplar/Interval.hpp>
8#include <poplar/Type.hpp>
9#include <poplar/TypeTraits.hpp>
10#include <vector>
11
12namespace poplar {
13
14struct VariableInterval;
15namespace core {
16class Tensor;
17}
18
22enum class UpsampleMethod {
23 REPEAT
35};
36
38class Tensor {
39public:
40 Tensor();
41 Tensor(const Tensor &other);
42 Tensor(Tensor &&other) noexcept;
43 const Tensor &operator=(const Tensor &other) &;
44 Tensor &operator=(Tensor &&other) &noexcept;
45 ~Tensor();
46
51
57 Tensor operator[](std::size_t i) const &;
58 Tensor &&operator[](std::size_t i) &&;
59
67 Tensor slice(std::size_t begin, std::size_t end, unsigned dimension) const &;
68 Tensor &&slice(std::size_t begin, std::size_t end, unsigned dimension) &&;
69
76 Tensor slice(std::size_t begin, std::size_t end) const {
77 return slice(begin, end, 0);
78 }
79
86 Tensor slice(const Interval &region, unsigned dimension = 0) const {
87 return slice(region.begin(), region.end(), dimension);
88 }
89
98 Tensor slice(ArrayRef<std::size_t> begin, ArrayRef<std::size_t> end) const;
99
107 std::vector<Tensor> slices(ArrayRef<Interval> intervals,
108 unsigned dimension = 0) const;
109
120 std::vector<Tensor>
121 slices(const std::vector<std::vector<Interval>> &intervals,
122 unsigned dimension = 0) const;
123
133 std::vector<Tensor> slices(const poplar::ArrayRef<unsigned> &indices,
134 unsigned dimension = 0) const;
135
143 Tensor index(ArrayRef<std::size_t> indices) const;
144
151
160 Tensor flatten(unsigned dimBegin, unsigned dimEnd) const;
161
171 Tensor reshape(ArrayRef<std::size_t> shape) const;
172
188 Tensor dimShuffle(ArrayRef<unsigned> permutation) const;
189
203 Tensor dimShufflePartial(ArrayRef<unsigned> source,
204 ArrayRef<unsigned> destination) const;
205
217 Tensor dimRoll(unsigned dimIdx, unsigned newIdx = 0) const {
218 return dimShufflePartial({dimIdx}, {newIdx});
219 }
220
272 Tensor reshapePartial(unsigned beginIndex, unsigned endIndex,
273 ArrayRef<std::size_t> newDims) const;
274
285 Tensor expand(ArrayRef<std::size_t> indices) const;
286
294 Tensor squeeze(ArrayRef<std::size_t> indices) const;
295
300 Tensor transpose() const { return dimShuffle({1, 0}); }
301
311 Tensor subSample(unsigned stride, unsigned dimension) const;
312
326 Tensor upsample(unsigned scale, unsigned dimension,
327 UpsampleMethod method) const;
328
338 Tensor broadcast(unsigned N, unsigned dimension) const;
339
349 Tensor reinterpret(const Type &type) const;
350
356 Tensor reverse(unsigned dimension) const;
357
359 std::size_t numElements() const;
360
365 std::size_t dim(unsigned i) const;
366
371 std::vector<std::size_t> shape() const;
372
377 unsigned rank() const;
378
380 bool isContiguous() const;
381
388 bool containsAliases() const;
389
394 bool containsConstant() const;
395
403
410 const std::vector<Interval> getContiguousRegions() const;
411
418 const std::vector<VariableInterval> getVarRegions() const;
419
426 template <typename T> bool getConstantValue(T *val) const {
427 return getConstantData(val, TypeTraits::make<T>());
428 }
429
434 bool intersectsWith(const Tensor &other) const;
435
440 std::ostream &output(std::ostream &os) const;
441
446 std::ostream &outputRegions(std::ostream &os) const;
447
450 std::string shapeToString() const;
451
453 void dump() const;
454
456 void dumpRegions() const;
457
478 std::string getVarStr() const;
480 std::string getDebugStr() const;
481
482 // Implementation
483 Tensor(std::unique_ptr<core::Tensor>);
484 core::Tensor &getImpl() const { return *impl; }
485 std::unique_ptr<core::Tensor> *getPImpl() { return &impl; }
486 bool valid() const { return impl.get() != nullptr; }
487
516
518 bool hasMetadata() const;
519
520private:
521 std::unique_ptr<core::Tensor> impl;
522 bool getConstantData(void *dst, const TypeTraits &traits) const;
523};
524
525bool operator==(const Tensor &a, const Tensor &b);
526bool inline operator!=(const Tensor &a, const Tensor &b) { return !(a == b); }
527
535Tensor concat(ArrayRef<Tensor> ts, unsigned dimension = 0);
536
545inline Tensor concat(const Tensor &first, const Tensor &second,
546 unsigned dimension = 0) {
547 return concat({first, second}, dimension);
548}
549
557Tensor append(const Tensor &first, const Tensor &second, unsigned dimension);
564inline Tensor append(const Tensor &first, const Tensor &second) {
565 return append(first, second, 0);
566}
567
573std::ostream &operator<<(std::ostream &os, const Tensor &tensor);
574
575} // namespace poplar
576
577#endif // poplar_Tensor_hpp
References to arrays.
A reference to a subset of tensor elements.
Definition: Tensor.hpp:38
Tensor flatten(unsigned dimBegin, unsigned dimEnd) const
Flatten a subset of the dimensions of a tensor.
Tensor flatten() const
Flatten the tensor.
std::ostream & outputRegions(std::ostream &os) const
Display the regions of the tensor on a stream.
std::size_t numElements() const
Get the total number of elements in the tensor.
Tensor broadcast(unsigned N, unsigned dimension) const
Broadcast/repeat the tensor along a specified dimension.
void dumpRegions() const
Display the regions of the tensor.
std::ostream & output(std::ostream &os) const
Display the expression representing the tensor on a stream.
Tensor operator[](std::size_t i) const &
Get the sub-tensor indexed by i in the first dimension of the tensor.
Tensor slice(ArrayRef< std::size_t > begin, ArrayRef< std::size_t > end) const
Get the sub-tensor given by slicing the tensor in multiple dimensions, starting at dimension 0.
bool isContiguous() const
Get whether the tensor is contiguous.
Tensor reshape(ArrayRef< std::size_t > shape) const
Reshape the tensor.
bool getConstantValue(T *val) const
Read a single element of data from a tensor if it is a constant.
Definition: Tensor.hpp:426
Tensor squeeze(ArrayRef< std::size_t > indices) const
Reduce dimension of tensor by removing singleton dimensions at specified indices of tensor.
Tensor dimRoll(unsigned dimIdx, unsigned newIdx=0) const
Roll a specified dimension to the specified dimension.
Definition: Tensor.hpp:217
Type elementType() const
Get the element type information for this tensor.
bool intersectsWith(const Tensor &other) const
Return whether this tensor intersects with another tensor.
bool hasMetadata() const
Check that the tensor has a metadata tensor.
bool containsAliases() const
Get whether the tensor contains an alias to the same storage location.
Tensor upsample(unsigned scale, unsigned dimension, UpsampleMethod method) const
Upsample the tensor.
Tensor getMetadata() const
getMetadata() retrieves the metadata tensor associated with the data tensor.
std::vector< Tensor > slices(const poplar::ArrayRef< unsigned > &indices, unsigned dimension=0) const
Get a vector of slices.
Tensor subSample(unsigned stride, unsigned dimension) const
Sub-sample the tensor.
bool isParallelWriteable() const
Get whether the elements of this tensor can be written in parallel.
bool containsConstant() const
Get whether the tensor contains any constant tensors.
Tensor index(ArrayRef< std::size_t > indices) const
Get the sub-tensor indexed by the specified indices.
Tensor reinterpret(const Type &type) const
Reinterpret the tensor as a new type.
std::string shapeToString() const
Report the shape of a Tensor as a string.
Tensor slice(std::size_t begin, std::size_t end, unsigned dimension) const &
Get the sub-tensor given by a specific range [begin, end) in one dimension of the tensor.
unsigned rank() const
Get the rank of the tensor.
std::vector< Tensor > slices(ArrayRef< Interval > intervals, unsigned dimension=0) const
Get a vector of slices.
std::string getDebugStr() const
Get the debug name associated with the tensor.
Tensor dimShufflePartial(ArrayRef< unsigned > source, ArrayRef< unsigned > destination) const
Permute some of a tensor's dimensions.
const std::vector< Interval > getContiguousRegions() const
Get the contiguous regions of a tensor.
std::size_t dim(unsigned i) const
Get a dimension of the tensor.
Tensor dimShuffle(ArrayRef< unsigned > permutation) const
Permute the dimensions of a tensor.
Tensor transpose() const
Transpose a 2-dimensional tensor.
Definition: Tensor.hpp:300
void dump() const
Display the expression representing the tensor.
Tensor expand(ArrayRef< std::size_t > indices) const
Expand tensor by adding singleton dimensions at specified indices of tensor.
Tensor slice(std::size_t begin, std::size_t end) const
Get the sub-tensor given by a specific range [begin, end) in the first dimension of the tensor.
Definition: Tensor.hpp:76
std::vector< std::size_t > shape() const
Get the shape of the tensor.
std::string getVarStr() const
getVarStr() and getDebugStr() retrieve a summary (limited to the first underlying variable) of inform...
Tensor reshapePartial(unsigned beginIndex, unsigned endIndex, ArrayRef< std::size_t > newDims) const
Reshape a range of dimensions of a tensor.
Tensor reverse(unsigned dimension) const
reverse this tensor along a specified dimension.
std::vector< Tensor > slices(const std::vector< std::vector< Interval > > &intervals, unsigned dimension=0) const
Get a vector of slices.
Tensor slice(const Interval &region, unsigned dimension=0) const
Get the sub-tensor given by a specific range [begin, end) in one dimension of the tensor.
Definition: Tensor.hpp:86
const std::vector< VariableInterval > getVarRegions() const
Get the contiguous regions of a tensor with reference to the variables allocated in the graph.
Class representing device data types.
Definition: Type.hpp:42
Poplar classes and functions.
Definition: ArrayRef.hpp:14
std::ostream & operator<<(std::ostream &os, const DebugNameAndId &dnai)
Display the path name of the DebugNameAndId.
Tensor concat(ArrayRef< Tensor > ts, unsigned dimension=0)
Concatenate several tensors.
UpsampleMethod
Enum passed to Tensor::upsample(unsigned scale, unsigned dimension) specifying the upsampling method.
Definition: Tensor.hpp:22
@ REPEAT
If dimension is of size s, for every i in [0, s), repeats the subtensor at index i scale times.
Tensor append(const Tensor &first, const Tensor &second, unsigned dimension)
Append a tensor as an element to another tensor.
A structure to provide information about arithmetic (integer and floating point) types.
Definition: TypeTraits.hpp:22