Poplar and PopLibs
Graph.hpp
1// Copyright (c) 2015 Graphcore Ltd. All rights reserved.
2
3#ifndef poplar_Graph_hpp
4#define poplar_Graph_hpp
5
6#include <poplar/CodeletFileType.hpp>
7#include <poplar/DataStream.hpp>
8#include <poplar/DebugContext.hpp>
9#include <poplar/Device.hpp>
10#include <poplar/Executable.hpp>
11#include <poplar/FunctionBufferMappingType.hpp>
12#include <poplar/GraphElements.hpp>
13#include <poplar/Interval.hpp>
14#include <poplar/LateInitCallback.hpp>
15#include <poplar/PerfEstimateFunc.hpp>
16#include <poplar/Program.hpp>
17#include <poplar/ReplicatedStreamMode.hpp>
18#include <poplar/SerializationFormat.hpp>
19#include <poplar/StringRef.hpp>
20#include <poplar/Target.hpp>
21#include <poplar/Tensor.hpp>
22#include <poplar/TensorCloneMethod.hpp>
23#include <poplar/TensorRearranger.hpp>
24#include <poplar/Type.hpp>
25#include <poplar/TypeTraits.hpp>
26#include <poplar/VariableMappingMethod.hpp>
27#include <poplar/VariableRef.hpp>
28#include <poplar/VertexIntrospector.hpp>
29#include <poplar/replication_factor.hpp>
30
31#include <cstring>
32#include <functional>
33#include <iosfwd>
34#include <memory>
35#include <numeric>
36#include <string>
37#include <vector>
38
39namespace poplar {
40namespace core {
41class GraphBuilder;
42}
43namespace program {
44class Program;
45}
46
47StringRef versionString();
48StringRef packageHash();
49
52class Graph {
53public:
54 Graph();
55
65 Graph(const Target &target, replication_factor r = replication_factor(1));
66
76 Graph(const Device &device, replication_factor r = replication_factor(1));
77 Graph(Graph &&) noexcept;
78 Graph &operator=(Graph &&) noexcept;
79 ~Graph();
80
82 const Target &getTarget() const;
83
111 bool addCodelets(StringRef src, CodeletFileType type = CodeletFileType::Auto,
112 StringRef compileFlags = "", StringRef targetName = "");
113
117 bool addCodelets(StringRef src, CodeletFileType type, StringRef compileFlags,
118 std::ostream &compileOutput, StringRef targetName = "");
119
128 bool addCodelets(ArrayRef<std::string> xs, StringRef compileFlags = "",
129 StringRef targetName = "") {
130 bool allAdded = true;
131 for (const auto &x : xs)
132 allAdded &=
133 addCodelets(x, CodeletFileType::Auto, compileFlags, targetName);
134 return allAdded;
135 }
136
144 void addCodelets(std::stringstream &stream, StringRef compileFlags = "",
146 StringRef targetName = "");
147 void addCodelets(std::stringstream &stream, StringRef compileFlags,
148 std::ostream &compileOutput,
150 StringRef targetName = "");
151
191 void addCodeletPlaceholder(std::istream &stream, StringRef targetName = "");
192
193 class ConnectionDesc {
194 enum class Kind { TensorEdge, ValueEdge, VectorTensorEdge };
195 Kind kind;
196 std::string field;
197 std::vector<Tensor> ts;
198 std::unique_ptr<char[]> val;
199 TypeTraits traits;
200 void connect(Graph &g, const VertexRef &v) const {
201 switch (kind) {
202 case Kind::TensorEdge:
203 g.connect(v[field], ts[0]);
204 break;
205 case Kind::ValueEdge:
206 g.connect(v[field], val.get(), traits);
207 break;
208 case Kind::VectorTensorEdge:
209 g.connect(v[field], ts);
210 }
211 }
212
213 public:
214 ConnectionDesc(StringRef field, Tensor t)
215 : kind(Kind::TensorEdge), field(field.begin(), field.end()),
216 ts({std::move(t)}), traits() {}
217
218 ConnectionDesc(StringRef field, ArrayRef<Tensor> tsArr)
219 : kind(Kind::VectorTensorEdge), field(field.begin(), field.end()),
220 ts(tsArr.begin(), tsArr.end()), traits() {}
221
222 template <typename T>
223 ConnectionDesc(StringRef field, T v,
224 typename std::enable_if<TypeTraits::isSimpleType<T>()>::type
225 * = nullptr)
226 : kind(Kind::ValueEdge), field(field.begin(), field.end()),
227 traits(TypeTraits::make<T>()) {
228 val.reset(new char[traits.size]);
229 std::memcpy(&val[0], &v, traits.size);
230 }
231
232 friend class Graph;
233 };
234
242 VertexRef addVertex(ComputeSet cs, StringRef vertexType);
243
271 VertexRef addVertex(ComputeSet cs, StringRef vertexType,
272 ArrayRef<ConnectionDesc> connections) {
273 auto v = addVertex(cs, vertexType);
274 for (const auto &c : connections) {
275 c.connect(*this, v);
276 }
277 return v;
278 }
279
305 unsigned incomingDownCount,
306 bool usesEastEdge, bool sendsXReq);
307
323 Tensor addVariable(const Type &type, ArrayRef<std::size_t> shape,
324 const DebugContext &debugContext = {});
325
349 Tensor addVariable(const Type &type, const Tensor &metadata,
350 ArrayRef<std::size_t> shape,
351 const DebugContext &debugContext = {});
352
364 Tensor addVariable(const Type &type, ArrayRef<std::size_t> shape,
365 VariableMappingMethod mappingMethod,
366 const DebugContext &debugContext = {});
367
387 Tensor addVariable(const Type &type, const Tensor &metadata,
388 ArrayRef<std::size_t> shape,
389 VariableMappingMethod mappingMethod,
390 const DebugContext &debugContext = {});
391
402 Tensor addExternalVariable(const Type &type, ArrayRef<std::size_t> shape,
403 StringRef symbol,
404 const DebugContext &debugContext = {});
405
426 template <typename T>
427 Tensor addConstant(const Type &type, const Tensor &metadata,
428 ArrayRef<std::size_t> shape, ArrayRef<T> values,
429 const DebugContext &debugContext = {"<const>"}) {
430 const auto elements = std::accumulate(
431 shape.begin(), shape.end(), std::size_t(1), std::multiplies<size_t>());
432 if (values.size() != elements) {
433 throw poplar_error("The number of constant values (" +
434 std::to_string(values.size()) +
435 ") does not match "
436 "the number of Tensor elements (" +
437 std::to_string(elements) + ")");
438 }
439 return addConstant(type, metadata, shape,
440 reinterpret_cast<const void *>(values.data()),
441 TypeTraits::make<T>(), false, debugContext);
442 }
443
456 template <typename T>
457 Tensor addConstant(const Type &type, ArrayRef<std::size_t> shape,
458 ArrayRef<T> values,
459 const DebugContext &debugContext = {"<const>"}) {
460 return addConstant(type, {}, shape, values, debugContext);
461 }
462
484 template <typename T>
485 Tensor
486 addConstant(const Type &type, const Tensor &metadata,
487 ArrayRef<std::size_t> shape, T val,
488 const DebugContext &debugContext = {"<const>"},
489 typename std::enable_if<TypeTraits::isSimpleType<T>()>::type * =
490 nullptr) {
491 return addConstant(type, metadata, shape,
492 reinterpret_cast<const void *>(&val),
493 TypeTraits::make<T>(), true, debugContext);
494 }
495
509 template <typename T>
510 Tensor
511 addConstant(const Type &type, ArrayRef<std::size_t> shape, T val,
512 const DebugContext &debugContext = {"<const>"},
513 typename std::enable_if<TypeTraits::isSimpleType<T>()>::type * =
514 nullptr) {
515 return addConstant(type, {}, shape, val, debugContext);
516 }
517
530 template <typename T>
531 Tensor
532 addConstant(const Type &type, ArrayRef<std::size_t> shape, const T *val,
533 const DebugContext &debugContext = {"<const>"},
534 typename std::enable_if<TypeTraits::isSimpleType<T>()>::type * =
535 nullptr) {
536 return addConstant(type, shape, static_cast<const void *>(val),
537 TypeTraits::make<T>(), false, debugContext);
538 }
539
540 Tensor addConstant(const Type &type, ArrayRef<std::size_t> shape,
541 const void *val, const TypeTraits &traits, bool broadcast,
542 const DebugContext &debugContext = {"<const>"});
543
544 Tensor addConstant(const Type &type, const Tensor &metadata,
545 ArrayRef<std::size_t> shape, const void *val,
546 const TypeTraits &traits, bool broadcast,
547 const DebugContext &debugContext = {"<const>"});
548
561 Tensor addConstantHalf(const Type &type, ArrayRef<std::size_t> shape,
562 uint16_t val,
563 const DebugContext &debugContext = {"<const>"}) {
564 return addConstant(type, shape, static_cast<const void *>(&val),
565 TypeTraits::make<IeeeHalf>(), true, debugContext);
566 }
567
581 Tensor addConstantHalf(const Type &type, ArrayRef<std::size_t> shape,
582 const uint16_t *val,
583 const DebugContext &debugContext = {"<const>"}) {
584 return addConstant(type, shape, static_cast<const void *>(val),
585 TypeTraits::make<IeeeHalf>(), false, debugContext);
586 }
587
606 Tensor clone(const Type &type, const Tensor &t,
607 const DebugContext &debugContext = {},
608 TensorCloneMethod method =
610
637 Tensor clone(const Type &type, const Tensor &metadata, const Tensor &t,
638 const DebugContext &debugContext = {},
639 TensorCloneMethod method =
641
667 Tensor cloneN(const Type &type, const Tensor &t, std::size_t N,
668 const DebugContext &debugContext = {},
669 TensorCloneMethod method =
671 TensorCloneDuplicationMethod duplicationMethod =
672 TensorCloneDuplicationMethod::DUPLICATE_BY_OUTER_DIMENSION);
673
707 Tensor cloneN(const Type &type, const Tensor &metadata, const Tensor &t,
708 std::size_t N, const DebugContext &debugContext = {},
709 TensorCloneMethod method =
711 TensorCloneDuplicationMethod duplicationMethod =
712 TensorCloneDuplicationMethod::DUPLICATE_BY_OUTER_DIMENSION);
713
730 Tensor clone(const Tensor &t, const DebugContext &debugContext = {},
731 TensorCloneMethod method =
733 return clone(t.elementType(), t, debugContext, method);
734 }
735
760 Tensor clone(const Tensor &metadata, const Tensor &t,
761 const DebugContext &debugContext = {},
762 TensorCloneMethod method =
764 return clone(t.elementType(), metadata, t, debugContext, method);
765 }
766
790 Tensor
791 cloneN(const Tensor &t, std::size_t N, const DebugContext &debugContext = {},
792 TensorCloneMethod method =
794 TensorCloneDuplicationMethod duplicationMethod =
795 TensorCloneDuplicationMethod::DUPLICATE_BY_OUTER_DIMENSION) {
796 return cloneN(t.elementType(), t, N, debugContext, method,
797 duplicationMethod);
798 }
799
831 Tensor
832 cloneN(const Tensor &metadata, const Tensor &t, std::size_t N,
833 const DebugContext &debugContext = {},
834 TensorCloneMethod method =
836 TensorCloneDuplicationMethod duplicationMethod =
837 TensorCloneDuplicationMethod::DUPLICATE_BY_OUTER_DIMENSION) {
838 return cloneN(t.elementType(), metadata, t, N, debugContext, method,
839 duplicationMethod);
840 }
841
867 void connect(FieldRef field, const Tensor &tensor);
868
877 template <typename T>
878 void connect(FieldRef field, T v,
879 typename std::enable_if<TypeTraits::isSimpleType<T>()>::type * =
880 nullptr) {
881 connect(field, &v, TypeTraits::make<T>());
882 }
883
894 void connect(FieldRef field, ArrayRef<Tensor> tensors) {
895 setFieldSize(field, tensors.size());
896 for (unsigned i = 0; i < tensors.size(); ++i) {
897 connect(field[i], tensors[i]);
898 }
899 }
900
907 void setPerfEstimate(const VertexRef &v, std::uint64_t cycles,
908 std::uint64_t flops = 0);
909
915 void setPerfEstimate(const VertexRef &v, const VertexPerfEstimate &estimate);
916
925 VertexPerfEstimate getPerfEstimate(const VertexRef &v) const;
926
932 void registerPerfEstimator(StringRef vertexTypeName, PerfEstimateFunc f);
933
939 unsigned getNumVertices(void) const;
940
947 ComputeSet addComputeSet(const DebugContext &debugContext = {});
948
954 void setFieldSize(FieldRef field, std::size_t size);
955
961 std::size_t getFieldSize(FieldRef field) const;
962
971 std::size_t getMaxFieldDim(StringRef vertexName, StringRef fieldName,
972 unsigned dimIndex) const;
973
978 double getMaxVertexFieldValue(StringRef vertexName,
979 StringRef fieldName) const;
980
987 template <typename T>
989 FieldRef field, T val,
990 typename std::enable_if<TypeTraits::isSimpleType<T>()>::type * =
991 nullptr) {
992 setInitialValue(field, static_cast<const void *>(&val),
993 TypeTraits::make<T>());
994 }
995
1018 template <typename T>
1020 FieldRef field, LateInitCallback<T> callback,
1021 typename std::enable_if<std::is_arithmetic<T>::value>::type * = nullptr) {
1022 setInitCallback<T>(field, callback, TypeTraits::make<T>());
1023 }
1024
1025 // Work arounds for template overload resolution limitations:
1026 // explicitly convert to ArrayRef ourselves:
1027 template <typename T>
1028 void setInitialValue(FieldRef field, const std::vector<T> &v) {
1029 setInitialValue(field, ArrayRef<T>(v));
1030 }
1031 template <typename T>
1032 void setInitialValue(FieldRef field, const std::initializer_list<T> &l) {
1033 setInitialValue(field, ArrayRef<T>(l));
1034 }
1035
1042 void setInitialValueHalf(FieldRef field, uint16_t val) {
1043 setInitialValue(field, static_cast<const void *>(&val),
1044 TypeTraits::make<IeeeHalf>());
1045 }
1046
1053 template <typename T> void setInitialValue(FieldRef field, ArrayRef<T> val) {
1054 setFieldSize(field, val.size());
1055
1056 for (unsigned i = 0; i < val.size(); ++i) {
1057 setInitialValue<T>(field[i], val[i]);
1058 }
1059 }
1060
1067 void setInitialValueHalf(FieldRef field, ArrayRef<uint16_t> val) {
1068 setFieldSize(field, val.size());
1069
1070 for (unsigned i = 0; i < val.size(); ++i) {
1071 setInitialValueHalf(field[i], val[i]);
1072 }
1073 }
1074
1083 template <typename T>
1085 const Tensor &t, T val,
1086 typename std::enable_if<TypeTraits::isSimpleType<T>()>::type * =
1087 nullptr) {
1088 setInitialValue(t, static_cast<const void *>(&val), TypeTraits::make<T>());
1089 }
1090
1100 const std::map<unsigned, unsigned> &vals);
1101
1102 template <typename T>
1103 void setInitialValue(const Tensor &t, ArrayRef<T> values) {
1104 Tensor f = t.flatten();
1105 if (f.numElements() != values.size()) {
1106 throw poplar_error("The number of values (" +
1107 std::to_string(values.size()) +
1108 ") does not match "
1109 "the number of Tensor elements (" +
1110 std::to_string(f.numElements()) + ")");
1111 }
1112 setInitialValue(f, static_cast<const void *>(values.data()),
1113 TypeTraits::make<T>());
1114 }
1115
1124 void setInitialValueHalf(const Tensor &t, uint16_t val) {
1125 setInitialValue(t, static_cast<const void *>(&val),
1126 TypeTraits::make<IeeeHalf>());
1127 }
1128
1129 void setInitialValueHalf(const Tensor &t, ArrayRef<uint16_t> values) {
1130 Tensor f = t.flatten();
1131 if (f.numElements() != values.size()) {
1132 throw poplar_error("The number of values (" +
1133 std::to_string(values.size()) +
1134 ") does not match "
1135 "the number of Tensor elements (" +
1136 std::to_string(f.numElements()) + ")");
1137 }
1138 setInitialValue(f, static_cast<const void *>(values.data()),
1139 TypeTraits::make<IeeeHalf>());
1140 }
1141
1161 void createHostWrite(StringRef handle, const Tensor &t,
1162 bool rearrangeOnHost = false);
1163
1179 void createHostRead(StringRef handle, const Tensor &t,
1180 bool rearrangeOnHost = false);
1181
1217 StringRef handle, const Type &elementType, std::size_t numElements,
1219 const OptionFlags &options = {});
1220
1242 DataStream addDeviceToHostFIFO(StringRef handle, const Type &elementType,
1243 std::size_t numElements,
1244 const OptionFlags &options = {});
1245
1263 RemoteBuffer addRemoteBuffer(StringRef handle, const Type &elementType,
1264 std::size_t numElements, std::size_t repeats = 1,
1265 bool rearrangeOnHost = false,
1266 bool optimiseMemory = false);
1267
1272 void outputVertexGraph(std::ostream &outputStream,
1273 ArrayRef<program::Program> progs = {}) const;
1274
1294 void outputComputeGraph(std::ostream &outputStream,
1295 ArrayRef<program::Program> progs = {}) const;
1296
1302 void setTileMapping(VertexRef v, unsigned tileNum);
1303
1309 void setTileMapping(const Tensor &t, unsigned tileNum);
1310
1311 using TileToTensorMapping = std::vector<std::vector<Interval>>;
1312
1327 TileToTensorMapping getTileMapping(const Tensor &t,
1328 bool requireComplete = true,
1329 bool allowExternal = false) const;
1330
1344 TileToTensorMapping getTileMapping(const Tensor &t, bool *isComplete,
1345 bool allowExternal = false) const;
1346
1355 TileToTensorMapping getVariableTileMapping(const Tensor &t) const;
1356
1366 void setTileMapping(const Tensor &t, const TileToTensorMapping &mapping);
1367
1374
1385
1406 std::vector<std::vector<Interval>>
1407 getSortedContiguousRegions(const Tensor &t, ArrayRef<Interval> regions,
1408 bool removeAliasedIntervals = false,
1409 std::vector<std::size_t> *aliases = nullptr) const;
1410
1445 void reorderToSimplify(Tensor *t, ArrayRef<Tensor *> ts,
1446 bool requireSimplestOrder = true) const;
1447
1467
1484
1502 void serializeTensors(std::ostream &out, ArrayRef<Tensor> tensors,
1503 SerializationFormat format) const;
1504
1527 std::vector<Tensor> deserializeTensors(std::istream &in,
1528 SerializationFormat format);
1529
1543 Graph createVirtualGraph(unsigned numTilesPerIPU);
1544
1565 Graph createVirtualGraph(unsigned lowerTile, unsigned upperTile);
1566
1585 Graph createVirtualGraph(const std::vector<unsigned> &perIpuTiles);
1586
1594
1598 unsigned getReplicationFactor() const;
1599
1604
1615 void serialize(std::ostream &out, SerializationFormat format) const;
1616
1630 void serialize(std::ostream &out, ArrayRef<program::Program> progs,
1631 SerializationFormat format) const;
1632
1645
1683 FunctionBufferMappingType mappingType);
1684
1696 using HostFunctionArgument = std::tuple<Type, std::size_t>;
1697 HostFunction addHostFunction(StringRef handle,
1698 ArrayRef<HostFunctionArgument> inputs,
1699 ArrayRef<HostFunctionArgument> outputs);
1700
1706 unsigned convertTileToTopLevelGraphTile(unsigned tileId) const;
1707
1713 unsigned convertTopLevelGraphTileToTile(unsigned topLevelGraphTileId) const;
1714
1720 unsigned convertVirtualTileToPhysicalTile(unsigned tileId) const;
1721
1730 unsigned convertPhysicalTileToVirtualTile(unsigned physicalTileId) const;
1731
1741 unsigned convertPhysicalTileToVirtualTile(unsigned ipuId,
1742 unsigned physicalTileId) const;
1743
1749 bool hasCodelet(StringRef codeletName) const;
1750
1751 // Implementation
1752 Graph(std::unique_ptr<core::GraphBuilder>, Target target);
1753 const core::GraphBuilder &getImpl() const { return *impl; }
1754 core::GraphBuilder &getImpl() { return *impl; }
1755
1756private:
1757 std::unique_ptr<core::GraphBuilder> impl;
1758 Target target;
1759
1760 void setInitialValue(FieldRef field, const void *val, const TypeTraits &);
1761 template <typename T>
1762 void setInitCallback(FieldRef field, LateInitCallback<T> callback,
1763 const TypeTraits &);
1764 void setInitialValue(const Tensor &t, const void *val, const TypeTraits &);
1765 void connect(FieldRef field, void *val, const TypeTraits &);
1766 void checkFieldSubgraph(const FieldRef &f) const;
1767 void checkVertexSubgraph(const VertexRef &v) const;
1768};
1769
1770} // End namespace poplar.
1771
1772#endif // poplar_Graph_hpp
A reference to a compute set within a graph.
Definition: GraphElements.hpp:131
An object representing a stream for communicating between the host and the device.
Definition: DataStream.hpp:26
DebugContext gathers the common external parameters of the context of an operation.
Definition: DebugContext.hpp:221
A device refers to a physical entity that can execute code.
Definition: Device.hpp:26
A reference to a field within a vertex instance.
Definition: GraphElements.hpp:75
A reference to a function buffer stored within a graph.
Definition: GraphElements.hpp:164
A reference to a function stored within a graph.
Definition: GraphElements.hpp:148
This class represents a graph program to be executed on the IPU.
Definition: Graph.hpp:52
unsigned convertTileToTopLevelGraphTile(unsigned tileId) const
Convert a tile ID in this graph to a tile ID in the top level graph.
bool isConstant(VariableRef v) const
Check whether a variable reference refers to a constant.
Tensor addConstantHalf(const Type &type, ArrayRef< std::size_t > shape, uint16_t val, const DebugContext &debugContext={"<const>"})
Add a constant to the graph, where the host data is type IEEE half.
Definition: Graph.hpp:561
Tensor addVariable(const Type &type, ArrayRef< std::size_t > shape, const DebugContext &debugContext={})
Add a variable to the graph.
Tensor addConstant(const Type &type, ArrayRef< std::size_t > shape, const T *val, const DebugContext &debugContext={"<const>"}, typename std::enable_if< TypeTraits::isSimpleType< T >()>::type *=nullptr)
Add a constant to the graph with multiple cell values.
Definition: Graph.hpp:532
Tensor addExternalVariable(const Type &type, ArrayRef< std::size_t > shape, StringRef symbol, const DebugContext &debugContext={})
Add an external variable to the graph.
Tensor addReplicationIndexConstant(const DebugContext &debugContext={})
Add a constant that is initialized with the replication index.
void serialize(std::ostream &out, SerializationFormat format) const
Serialize a graph to JSON or binary (CapnProto) format.
double getMaxVertexFieldValue(StringRef vertexName, StringRef fieldName) const
Find the maximum value that can be represented by an element of a field.
void setInitialValue(FieldRef field, T val, typename std::enable_if< TypeTraits::isSimpleType< T >()>::type *=nullptr)
Set the initial value of a field.
Definition: Graph.hpp:988
Tensor addVariable(const Type &type, ArrayRef< std::size_t > shape, VariableMappingMethod mappingMethod, const DebugContext &debugContext={})
Add a variable to the graph.
Tensor addConstant(const Type &type, const Tensor &metadata, ArrayRef< std::size_t > shape, T val, const DebugContext &debugContext={"<const>"}, typename std::enable_if< TypeTraits::isSimpleType< T >()>::type *=nullptr)
Add a constant to the graph with a type that may require metadata.
Definition: Graph.hpp:486
Tensor clone(const Type &type, const Tensor &metadata, const Tensor &t, const DebugContext &debugContext={}, TensorCloneMethod method=TensorCloneMethod::PRESERVE_ORDER_UNLESS_ALIASES)
Add a non-constant tensor to the graph that has the same size and tile mapping as Tensor t and that u...
void connect(FieldRef field, T v, typename std::enable_if< TypeTraits::isSimpleType< T >()>::type *=nullptr)
Connect a constant value to an input field.
Definition: Graph.hpp:878
unsigned convertPhysicalTileToVirtualTile(unsigned physicalTileId) const
Convert a physical tile ID to a tile ID in this graph.
void createHostRead(StringRef handle, const Tensor &t, bool rearrangeOnHost=false)
Mark a Tensor as being available as the source of device to host copies.
Tensor clone(const Tensor &metadata, const Tensor &t, const DebugContext &debugContext={}, TensorCloneMethod method=TensorCloneMethod::PRESERVE_ORDER_UNLESS_ALIASES)
Add a non-constant tensor to the graph that has the same size and tile mapping as Tensor t and that u...
Definition: Graph.hpp:760
VertexRef addExternalExchangeVertex(ComputeSet cs, StringRef vertexType, unsigned incomingDownCount, bool usesEastEdge, bool sendsXReq)
Add an external exchange vertex to the graph.
void setInitialValueHalf(FieldRef field, ArrayRef< uint16_t > val)
Set initial values of a vector field of type IEEE half.
Definition: Graph.hpp:1067
Tensor addConstant(const Type &type, const Tensor &metadata, ArrayRef< std::size_t > shape, ArrayRef< T > values, const DebugContext &debugContext={"<const>"})
Add a constant to the graph with a type that may require metadata.
Definition: Graph.hpp:427
Tensor addVariable(const Type &type, const Tensor &metadata, ArrayRef< std::size_t > shape, const DebugContext &debugContext={})
Add a variable to the graph with a type that may require metadata.
void setTileMapping(VertexRef v, unsigned tileNum)
Map a vertex to a specific tile on the device.
std::size_t getMaxFieldDim(StringRef vertexName, StringRef fieldName, unsigned dimIndex) const
Find the maximum size for a dimension of a field.
Tensor getVariable(VariableRef v) const
Get a tensor representing an entire variable.
unsigned convertTopLevelGraphTileToTile(unsigned topLevelGraphTileId) const
Convert a tile ID in the top level graph to a tile ID in this graph.
Graph getTopLevelGraph()
Return the top level graph.
std::vector< Tensor > deserializeTensors(std::istream &in, SerializationFormat format)
Deserialize a set of tensors from a CapnProto message.
std::vector< std::vector< Interval > > getSortedContiguousRegions(const Tensor &t, ArrayRef< Interval > regions, bool removeAliasedIntervals=false, std::vector< std::size_t > *aliases=nullptr) const
Get a list of sequences of intervals over a tensor such that each sequence represents a contiguous re...
DataStream addHostToDeviceFIFO(StringRef handle, const Type &elementType, std::size_t numElements, ReplicatedStreamMode replicatedMode=ReplicatedStreamMode::REPLICATE, const OptionFlags &options={})
Add a data stream to the graph for copying data from the host to the device.
void reorderToSimplify(Tensor *t, ArrayRef< Tensor * > ts, bool requireSimplestOrder=true) const
Reorder elements of a tensor to simplify the view on the data.
Tensor findUnbroadcastTensor(const Tensor &t) const
Attempt to determine the shape of a Tensor prior to it having been broadcast.
TileToTensorMapping getVariableTileMapping(const Tensor &t) const
Inspect the tile mapping of a tensor.
void setTileMapping(const Tensor &t, const TileToTensorMapping &mapping)
Set the tile mapping of a tensor based on an explicit map from tiles to tensor intervals.
unsigned getNumVertices(void) const
Get the number of vertices currently in the graph.
Graph createVirtualGraph(unsigned lowerTile, unsigned upperTile)
Create a "virtual" graph that uses a subset of the target's tiles.
void setInitialValue(const Tensor &t, T val, typename std::enable_if< TypeTraits::isSimpleType< T >()>::type *=nullptr)
Set the initial value of a tensor element.
Definition: Graph.hpp:1084
void outputComputeGraph(std::ostream &outputStream, ArrayRef< program::Program > progs={}) const
Output the compute graph to a stream in dot file format.
void setInitialValueHalf(const Tensor &t, uint16_t val)
Set the initial value of a tensor element of type IEEE half.
Definition: Graph.hpp:1124
unsigned convertVirtualTileToPhysicalTile(unsigned tileId) const
Convert a tile ID in this graph to a physical tile ID.
void setInitialValue(const Tensor &t, const std::map< unsigned, unsigned > &vals)
Set a replica based initial value of a tensor element.
Tensor cloneN(const Type &type, const Tensor &metadata, const Tensor &t, std::size_t N, const DebugContext &debugContext={}, TensorCloneMethod method=TensorCloneMethod::PRESERVE_ORDER_UNLESS_ALIASES, TensorCloneDuplicationMethod duplicationMethod=TensorCloneDuplicationMethod::DUPLICATE_BY_OUTER_DIMENSION)
Clone a tensor N times.
Tensor addConstant(const Type &type, ArrayRef< std::size_t > shape, ArrayRef< T > values, const DebugContext &debugContext={"<const>"})
Add a constant to the graph.
Definition: Graph.hpp:457
void addCodeletPlaceholder(std::istream &stream, StringRef targetName="")
Add a codelet from the description (JSON) to the graph.
std::size_t getFieldSize(FieldRef field) const
Get the size of a vector field.
RemoteBuffer addRemoteBuffer(StringRef handle, const Type &elementType, std::size_t numElements, std::size_t repeats=1, bool rearrangeOnHost=false, bool optimiseMemory=false)
Add a remote buffer to the graph.
bool hasCodelet(StringRef codeletName) const
Check if a graph contains a codelet with this name.
Graph createVirtualGraph(unsigned numTilesPerIPU)
Create a "virtual" graph using a subset of the target's tile.
Tensor addConstant(const Type &type, ArrayRef< std::size_t > shape, T val, const DebugContext &debugContext={"<const>"}, typename std::enable_if< TypeTraits::isSimpleType< T >()>::type *=nullptr)
Add a constant to the graph.
Definition: Graph.hpp:511
std::tuple< Type, std::size_t > HostFunctionArgument
Add a host function to the graph.
Definition: Graph.hpp:1696
void addCodelets(std::stringstream &stream, StringRef compileFlags="", CodeletFileType type=CodeletFileType::CppSource, StringRef targetName="")
Take a codelet contained within the stream and store it in a temporary file which we then use to comp...
Graph(const Target &target, replication_factor r=replication_factor(1))
Construct a graph object.
Graph(const Device &device, replication_factor r=replication_factor(1))
Construct a graph object.
Tensor cloneN(const Tensor &metadata, const Tensor &t, std::size_t N, const DebugContext &debugContext={}, TensorCloneMethod method=TensorCloneMethod::PRESERVE_ORDER_UNLESS_ALIASES, TensorCloneDuplicationMethod duplicationMethod=TensorCloneDuplicationMethod::DUPLICATE_BY_OUTER_DIMENSION)
Clone a tensor N times.
Definition: Graph.hpp:832
Tensor cloneN(const Tensor &t, std::size_t N, const DebugContext &debugContext={}, TensorCloneMethod method=TensorCloneMethod::PRESERVE_ORDER_UNLESS_ALIASES, TensorCloneDuplicationMethod duplicationMethod=TensorCloneDuplicationMethod::DUPLICATE_BY_OUTER_DIMENSION)
Clone a tensor N times.
Definition: Graph.hpp:791
void setTileMapping(const Tensor &t, unsigned tileNum)
Map a tensor slice to a specific tile on the device.
const Target & getTarget() const
Retrieve the target that this graph is targeting.
Function addFunction(const program::Program &program)
Add a function to the graph.
bool addCodelets(StringRef src, CodeletFileType type=CodeletFileType::Auto, StringRef compileFlags="", StringRef targetName="")
Add a codelet to the graph.
Tensor clone(const Tensor &t, const DebugContext &debugContext={}, TensorCloneMethod method=TensorCloneMethod::PRESERVE_ORDER_UNLESS_ALIASES)
Add a non-constant tensor to the graph that has the same size and tile mapping as Tensor t.
Definition: Graph.hpp:730
void registerPerfEstimator(StringRef vertexTypeName, PerfEstimateFunc f)
FunctionBuffer addFunctionBuffer(const Function &f, FunctionBufferMappingType mappingType)
Add a function buffer to the graph.
TileToTensorMapping getTileMapping(const Tensor &t, bool *isComplete, bool allowExternal=false) const
Inspect the tile mapping of a tensor.
TensorRearranger getSimplifyingRearranger(const Tensor &t) const
Get a rearranger object for simplifying the underlying representation of a tensor.
void connect(FieldRef field, const Tensor &tensor)
Connect a tensor to a vertex field.
void createHostWrite(StringRef handle, const Tensor &t, bool rearrangeOnHost=false)
Mark a Tensor as being available as the destination of host to device copies.
Graph createVirtualGraph(const std::vector< unsigned > &perIpuTiles)
Create a "virtual" graph that uses a subset of the target's tiles.
VertexRef addVertex(ComputeSet cs, StringRef vertexType)
Add a vertex to the graph.
void setInitialValue(FieldRef field, ArrayRef< T > val)
Set initial values of a vector field.
Definition: Graph.hpp:1053
VertexPerfEstimate getPerfEstimate(const VertexRef &v) const
Get the performance estimate for the specified vertex.
VertexRef addVertex(ComputeSet cs, StringRef vertexType, ArrayRef< ConnectionDesc > connections)
Add a vertex to the graph and connect graph elements to some of its fields.
Definition: Graph.hpp:271
Tensor addConstantHalf(const Type &type, ArrayRef< std::size_t > shape, const uint16_t *val, const DebugContext &debugContext={"<const>"})
Add a constant to the graph with multiple cell values, where the host data is type IEEE half.
Definition: Graph.hpp:581
unsigned getReplicationFactor() const
Return the replication factor of the graph.
void setInitCallback(FieldRef field, LateInitCallback< T > callback, typename std::enable_if< std::is_arithmetic< T >::value >::type *=nullptr)
Set the init callback for a field; the callback function will be called after graph construction and ...
Definition: Graph.hpp:1019
Tensor clone(const Type &type, const Tensor &t, const DebugContext &debugContext={}, TensorCloneMethod method=TensorCloneMethod::PRESERVE_ORDER_UNLESS_ALIASES)
Add a non-constant tensor to the graph that has the same size and tile mapping as Tensor t.
DataStream addDeviceToHostFIFO(StringRef handle, const Type &elementType, std::size_t numElements, const OptionFlags &options={})
Add a data stream to the graph for copying data from the device to the host.
void serialize(std::ostream &out, ArrayRef< program::Program > progs, SerializationFormat format) const
Serialize a graph to JSON or binary (CapnProto) format.
ComputeSet addComputeSet(const DebugContext &debugContext={})
Create a compute set within the graph.
void setFieldSize(FieldRef field, std::size_t size)
Set the size of a vector field.
void connect(FieldRef field, ArrayRef< Tensor > tensors)
Connect a vector of tensors to a vertex field.
Definition: Graph.hpp:894
Tensor cloneN(const Type &type, const Tensor &t, std::size_t N, const DebugContext &debugContext={}, TensorCloneMethod method=TensorCloneMethod::PRESERVE_ORDER_UNLESS_ALIASES, TensorCloneDuplicationMethod duplicationMethod=TensorCloneDuplicationMethod::DUPLICATE_BY_OUTER_DIMENSION)
Clone a tensor N times.
TileToTensorMapping getTileMapping(const Tensor &t, bool requireComplete=true, bool allowExternal=false) const
Inspect the tile mapping of a tensor.
void serializeTensors(std::ostream &out, ArrayRef< Tensor > tensors, SerializationFormat format) const
Serialize a set of tensors to JSON or CapnProto.
void outputVertexGraph(std::ostream &outputStream, ArrayRef< program::Program > progs={}) const
Output the vertex graph to a stream in dot file format.
Tensor addVariable(const Type &type, const Tensor &metadata, ArrayRef< std::size_t > shape, VariableMappingMethod mappingMethod, const DebugContext &debugContext={})
Add a variable to the graph with a type that may require metadata.
unsigned convertPhysicalTileToVirtualTile(unsigned ipuId, unsigned physicalTileId) const
Convert a physical tile ID to a tile ID in this graph.
void setPerfEstimate(const VertexRef &v, const VertexPerfEstimate &estimate)
Set the performance estimate for a vertex.
void setInitialValueHalf(FieldRef field, uint16_t val)
Set the initial value of a field of type IEEE half.
Definition: Graph.hpp:1042
void setPerfEstimate(const VertexRef &v, std::uint64_t cycles, std::uint64_t flops=0)
Set the performance estimate for a vertex.
A reference to a function in the host.
Definition: GraphElements.hpp:179
A set of option/value string flags to be used in various APIs.
Definition: OptionFlags.hpp:24
A remote buffer is a region of remote (meaning not on the IPU) memory that is used as a cache.
Definition: DataStream.hpp:55
A target representation.
Definition: Target.hpp:69
TensorRearranger can be used to re-order the view on a tensor and to undo that re-ordering.
Definition: TensorRearranger.hpp:19
A reference to a subset of tensor elements.
Definition: Tensor.hpp:38
Tensor flatten() const
Flatten the tensor.
std::size_t numElements() const
Get the total number of elements in the tensor.
Type elementType() const
Get the element type information for this tensor.
Class representing device data types.
Definition: Type.hpp:42
Type representing a reference to a variable in a graph.
Definition: VariableRef.hpp:12
A reference to a vertex within a graph.
Definition: GraphElements.hpp:27
This class represents a control program that executes operations on the graph.
Definition: Program.hpp:30
Poplar classes and functions.
Definition: ArrayRef.hpp:14
std::function< T(const VertexEdgeInfo &)> LateInitCallback
A callback function of this type can be specified for a field of a vertex, instead of specifying an i...
Definition: LateInitCallback.hpp:44
VariableMappingMethod
When variables are added to the graph, a tile mapping can be created.
Definition: VariableMappingMethod.hpp:13
CodeletFileType
Definition: CodeletFileType.hpp:10
@ Auto
Auto detect based on file name.
@ CppSource
A graph C++ source file.
FunctionBufferMappingType
Type for mapping of FunctionBuffer objects.
Definition: FunctionBufferMappingType.hpp:12
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.
ReplicatedStreamMode
Define how a stream is replicated when added to a replicated graph.
Definition: ReplicatedStreamMode.hpp:12
@ REPLICATE
Create a stream per replica.
std::function< VertexPerfEstimate(const VertexIntrospector &v, const Target &target)> PerfEstimateFunc
Functions of this type can be used as performance estimator callbacks for new vertex types.
Definition: PerfEstimateFunc.hpp:25
SerializationFormat
Format for serializing a Tensor or Graph object.
Definition: SerializationFormat.hpp:14
TensorCloneDuplicationMethod
Define behaviour when a Tensor is cloned and duplicated using Graph::cloneN.
Definition: TensorCloneMethod.hpp:38
A structure to provide information about arithmetic (integer and floating point) types.
Definition: TypeTraits.hpp:22
Base class for Poplar exceptions.
Definition: exceptions.hpp:16