Poplar and PopLibs
Program.hpp
1// Copyright (c) 2016 Graphcore Ltd. All rights reserved.
2// Definitions of program classes.
3
4#ifndef poplar_Program_hpp
5#define poplar_Program_hpp
6
7#include <cstdint>
8#include <memory>
9#include <ostream>
10#include <poplar/DataStream.hpp>
11#include <poplar/DebugContext.hpp>
12#include <poplar/GraphElements.hpp>
13#include <poplar/OptionFlags.hpp>
14#include <poplar/StringRef.hpp>
15#include <poplar/SyncType.hpp>
16#include <poplar/Tensor.hpp>
17#include <poplar/TypeTraits.hpp>
18
19namespace poplar {
20namespace core {
21class ProgramImpl;
22}
23
24namespace program {
25
30class Program {
31public:
32 Program();
33 Program(const Program &p);
34 Program(Program &&p) noexcept;
35 Program &operator=(const Program &p);
36 Program &operator=(Program &&p) noexcept;
37 virtual ~Program();
38
39 // Check if program is empty
40 bool isEmpty() const { return impl.get() == nullptr; }
41
42 // Implementation
43 core::ProgramImpl &getImpl() { return *impl; }
44 const core::ProgramImpl &getImpl() const { return *impl; }
45
46 friend bool operator==(const Program &lhs, const Program &rhs);
47
48protected:
49 std::unique_ptr<core::ProgramImpl> impl;
50};
51
52bool operator==(const Program &lhs, const Program &rhs);
53
56class Execute : public Program {
57public:
62 explicit Execute(ComputeSet cs, const DebugContext &debugContext = {});
63
73 Execute(ComputeSet cs, Tensor t, const DebugContext &debugContext = {});
74};
75
77class Sequence : public Program {
78 template <class... T> void add_many(const Program &first, T &&...rest) {
79 add(first);
80 add_many(rest...);
81 }
82 void add_many() {}
83 void init();
84 void init(const DebugContext &debugContext);
85
86public:
89 Sequence(const DebugContext &debugContext = {}) { init(debugContext); }
90
105 Sequence(std::initializer_list<Program> programs,
106 const DebugContext &debugContext = {}) {
107 init(debugContext);
108 for (const auto &p : programs) {
109 add(p);
110 }
111 }
112
116 void add(const Program &p);
117};
118
124class Repeat : public Program {
125public:
131 Repeat(unsigned count, const Program &prog,
132 const DebugContext &debugContext = {});
133};
134
144class RepeatWhileFalse : public Program {
145public:
157 RepeatWhileFalse(const Program &cond, Tensor predicate, const Program &body,
158 const DebugContext &debugContext = {});
159};
160
170class RepeatWhileTrue : public Program {
171public:
183 RepeatWhileTrue(const Program &cond, Tensor predicate, const Program &body,
184 const DebugContext &debugContext = {});
185};
186
189class Loop : public Program {
190public:
195 Loop(const Program &prog, const DebugContext &debugContext = {});
196};
197
200class If : public Program {
201public:
214 If(Tensor predicate, const Program &trueBody, const Program &falseBody,
215 const DebugContext &debugContext = {});
216};
217
228class Switch : public Program {
229 Switch(Tensor control, const Program &defaultCaseBody,
230 const bool unreachableDefault, const DebugContext &debugContext = {});
231
232public:
238 Switch(Tensor control,
239 const std::vector<std::pair<std::int32_t, Program>> &cases,
240 const DebugContext &debugContext = {});
246 Switch(Tensor control,
247 const std::vector<std::pair<std::int32_t, Program>> &cases,
248 const Program &defaultCaseBody, const DebugContext &debugContext = {});
253 Switch(Tensor control, const DebugContext &debugContext = {});
259 Switch(Tensor control, const Program &defaultCaseBody,
260 const DebugContext &debugContext = {});
261
266 Switch &add(std::int32_t value, const Program &body);
267
270 Tensor control,
271 const std::vector<std::pair<std::int32_t, Program>> &cases,
272 const DebugContext &debugContext = {});
277 static Switch
279 const DebugContext &debugContext = {});
280};
281
283class Copy : public Program {
284 Copy(const DataStream &stream, Tensor dst, bool rearrangeOnHost,
285 Tensor offset, size_t repeats, bool optimiseMemory,
286 const OptionFlags &options = {}, const DebugContext &debugContext = {});
287 Copy(Tensor src, const DataStream &stream, bool rearrangeOnHost,
288 Tensor offset, size_t repeats, bool optimiseMemory,
289 const OptionFlags &options = {}, const DebugContext &debugContext = {});
290
291public:
303 Copy(Tensor src, Tensor dst, bool dontOutline = false,
304 const DebugContext &debugContext = {});
305
306 // allow long lines for link
307 // clang-format off
322 // clang-format on
323 Copy(const DataStream &stream, Tensor dst, bool optimiseMemory = false,
324 const DebugContext &debugContext = {});
325
326 // allow long lines for link
327 // clang-format off
342 // clang-format on
343 Copy(Tensor src, const DataStream &stream, bool optimiseMemory = false,
344 const DebugContext &debugContext = {});
345
352 Copy(const RemoteBuffer &buffer, Tensor dst,
353 const DebugContext &debugContext = {});
354
385 Copy(const RemoteBuffer &buffer, Tensor dst, Tensor offset,
386 const DebugContext &debugContext = {});
387
394 Copy(Tensor src, const RemoteBuffer &buffer,
395 const DebugContext &debugContext = {});
396
425 Copy(Tensor src, const RemoteBuffer &buffer, Tensor offset,
426 const DebugContext &debugContext = {});
427
441 Copy(const FunctionBuffer &buffer, const Function &function,
442 const DebugContext &debugContext = {});
443};
444
446class CrossReplicaCopy : public Program {
447public:
470 std::map<unsigned, unsigned> replicaMap,
471 const DebugContext &debugContext = {});
472};
473
539class WriteUndef : public Program {
540public:
541 WriteUndef(Tensor t, const DebugContext &debugContext = {});
542};
543
552public:
553 AssumeEqualAcrossReplicas(Tensor t, const DebugContext &debugContext = {});
554};
555
560class Block : public Program {
561public:
562 Block(const Program &p, const DebugContext &debugContext = {});
563};
564
568class Sync : public Program {
569public:
572 Sync(SyncType type, const DebugContext &debugContext = {});
573};
574
578class Call : public Program {
579public:
584 Call(Function f, const DebugContext &debugContext = {});
585
590 // arguments
594 Call(HostFunction f, ArrayRef<Tensor> inputs, ArrayRef<Tensor> outputs,
595 const DebugContext &debugContext = {});
596};
597
598class PrintTensor : public Program {
599public:
607 PrintTensor(Tensor t, const DebugContext &debugContext = {});
613 PrintTensor(StringRef title, Tensor t, const DebugContext &debugContext = {});
614};
615
616class ErrorProgram : public Program {
617public:
626 ErrorProgram(StringRef message, Tensor debugTensor,
627 const DebugContext &debugContext = {});
628};
629
630class Abort : public Program {
631public:
636 Abort(const DebugContext &debugContext = {});
637
643 Abort(const std::string &message, const DebugContext &debugContext = {});
644};
645
646class AbortOnCondition : public Program {
647public:
653 AbortOnCondition(Tensor predicate, const DebugContext &debugContext = {});
654
662 AbortOnCondition(Tensor predicate, const std::string &message,
663 const DebugContext &debugContext = {});
664};
665
669void dumpProgram(const Graph &graph, const Program &program, std::ostream &out);
670
671} // namespace program
672} // namespace poplar
673#endif // poplar_Program_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 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
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 reference to a subset of tensor elements.
Definition: Tensor.hpp:38
A program to mark a tensor as equal across replicas.
Definition: Program.hpp:551
A program to scope another program.
Definition: Program.hpp:560
A program to perform a function call to a previously stored program.
Definition: Program.hpp:578
Call(HostFunction f, ArrayRef< Tensor > inputs, ArrayRef< Tensor > outputs, const DebugContext &debugContext={})
Call a function in the host.
Call(Function f, const DebugContext &debugContext={})
Call the function.
A program that copies data.
Definition: Program.hpp:283
Copy(Tensor src, const RemoteBuffer &buffer, const DebugContext &debugContext={})
Construct a program to copy a tensor to a remote buffer.
Copy(const RemoteBuffer &buffer, Tensor dst, Tensor offset, const DebugContext &debugContext={})
Construct a program to copy a remote buffer to a tensor.
Copy(const FunctionBuffer &buffer, const Function &function, const DebugContext &debugContext={})
Construct a program to copy the contents of a FunctionBuffer to a Function.
Copy(Tensor src, const DataStream &stream, bool optimiseMemory=false, const DebugContext &debugContext={})
Construct a program to copy a tensor to a data stream.
Copy(Tensor src, const RemoteBuffer &buffer, Tensor offset, const DebugContext &debugContext={})
Construct a program to copy a tensor to a remote buffer.
Copy(const DataStream &stream, Tensor dst, bool optimiseMemory=false, const DebugContext &debugContext={})
Construct a program to copy from a data stream to a tensor.
Copy(Tensor src, Tensor dst, bool dontOutline=false, const DebugContext &debugContext={})
Construct a program to copy data from one tensor to another.
Copy(const RemoteBuffer &buffer, Tensor dst, const DebugContext &debugContext={})
Construct a program to copy a remote buffer to a tensor.
A program that copies tensors between replicated sub-graphs.
Definition: Program.hpp:446
CrossReplicaCopy(Tensor src, Tensor dst, std::map< unsigned, unsigned > replicaMap, const DebugContext &debugContext={})
Constructor to create a program to copy a tensor to the equivalent tensor in a different replica sub-...
Program that executes a compute set in the graph.
Definition: Program.hpp:56
Execute(ComputeSet cs, Tensor t, const DebugContext &debugContext={})
Construct a graph execution program and write the exit status to a scalar tensor.
Execute(ComputeSet cs, const DebugContext &debugContext={})
Construct a graph execution program.
A program that runs one of two programs depending on the value of a scalar tensor.
Definition: Program.hpp:200
If(Tensor predicate, const Program &trueBody, const Program &falseBody, const DebugContext &debugContext={})
A program that executes trueBody or falseBody depending on the value of predicate.
A program that executes for an indefinite number of iterations.
Definition: Program.hpp:189
Loop(const Program &prog, const DebugContext &debugContext={})
Construct a program which repeats indefinitely.
This class represents a control program that executes operations on the graph.
Definition: Program.hpp:30
A program that executes a program repeatedly while a condition is false.
Definition: Program.hpp:144
RepeatWhileFalse(const Program &cond, Tensor predicate, const Program &body, const DebugContext &debugContext={})
Construct a repeat-while-false program.
A program that executes a program repeatedly while a condition is true.
Definition: Program.hpp:170
RepeatWhileTrue(const Program &cond, Tensor predicate, const Program &body, const DebugContext &debugContext={})
Construct a repeat-while-true program.
A program that repeatedly executes for a fixed number of iterations.
Definition: Program.hpp:124
Repeat(unsigned count, const Program &prog, const DebugContext &debugContext={})
Construct a repeat program.
Program that executes a sequence of programs.
Definition: Program.hpp:77
void add(const Program &p)
Add a program to the end of the sequence.
Sequence(const DebugContext &debugContext={})
Construct an empty execution sequence (with optional debug context).
Definition: Program.hpp:89
Sequence(std::initializer_list< Program > programs, const DebugContext &debugContext={})
Construct an execution sequence from a list of programs.
Definition: Program.hpp:105
A program that runs one of many programs depending on the value of a tensor.
Definition: Program.hpp:228
Switch(Tensor control, const DebugContext &debugContext={})
Construct a switch with no cases and an empty default case.
Switch(Tensor control, const std::vector< std::pair< std::int32_t, Program > > &cases, const DebugContext &debugContext={})
Construct a switch with the specified set of cases and an empty default case.
Switch(Tensor control, const Program &defaultCaseBody, const DebugContext &debugContext={})
Construct a switch with no cases and the specified default case.
Switch(Tensor control, const std::vector< std::pair< std::int32_t, Program > > &cases, const Program &defaultCaseBody, const DebugContext &debugContext={})
Construct a switch with the specified set of cases and default case.
static Switch switchWithBoundsChecking(Tensor control, const std::vector< std::pair< std::int32_t, Program > > &cases, const DebugContext &debugContext={})
A helper function that causes the default case to throw an error.
Switch & add(std::int32_t value, const Program &body)
Add a case with the specified case value and body.
static Switch switchWithUnreachableDefault(Tensor control, const DebugContext &debugContext={})
This function lets the compiler assume the default case is unreachable.
A program to synchronise at a certain granularity dictated by the SyncType.
Definition: Program.hpp:568
Sync(SyncType type, const DebugContext &debugContext={})
A program to mark a tensor as containing an undefined value.
Definition: Program.hpp:539
Poplar classes and functions.
Definition: ArrayRef.hpp:14
SyncType
An enumeration used to state what type of synchronisation a Sync program represents.
Definition: SyncType.hpp:13