Poplar and PopLibs
Engine.hpp
1// Copyright (c) 2015 Graphcore Ltd. All rights reserved.
2
3#ifndef poplar_Engine_hpp
4#define poplar_Engine_hpp
5
6#include <gccs/CompilerFeatures.hpp>
7#include <poplar/ArrayRef.hpp>
8#include <poplar/Executable.hpp>
9#include <poplar/Graph.hpp>
10#include <poplar/HostFunctionCallback.hpp>
11#include <poplar/Module.hpp>
12#include <poplar/OptionFlags.hpp>
13#include <poplar/ProfileValue.hpp>
14#include <poplar/Program.hpp>
15#include <poplar/Quarter.hpp>
16#include <poplar/RuntimeOptions.hpp>
17#include <poplar/StreamCallback.hpp>
18#include <poplar/StringRef.hpp>
19#include <poplar/Type.hpp>
20
21#include <functional>
22#include <iosfwd>
23#include <memory>
24#include <type_traits>
25#include <vector>
26
27namespace pva {
28class Report;
29}
30namespace poplar {
31namespace core {
32class Engine;
33class TimerTimePoint;
34} // namespace core
35
36class Preallocations;
37
38using ProgressFunc = std::function<void(int, int)>;
39
43const static unsigned WORKER_SCRATCH_SIZE = 48;
44
390class Engine {
391public:
407 using ProgressFunc = std::function<void(int, int)>;
408
434 Engine(Graph &&graph, ArrayRef<program::Program> progs,
435 const OptionFlags &opt = {},
436 ProgressFunc progressCallBack = ProgressFunc(),
437 const DebugContext &debugContext = {});
438 Engine(const Graph &graph, ArrayRef<program::Program> progs,
439 const OptionFlags &opt = {},
440 ProgressFunc progressCallBack = ProgressFunc(),
441 const DebugContext &debugContext = {});
442
468 Engine(Graph &&graph, program::Program prog, const OptionFlags &opt = {},
469 ProgressFunc progressCallBack = ProgressFunc(),
470 const DebugContext &debugContext = {});
471 Engine(const Graph &graph, program::Program prog, const OptionFlags &opt = {},
472 ProgressFunc progressCallBack = ProgressFunc(),
473 const DebugContext &debugContext = {});
474
486 Engine(Executable &&exe, const OptionFlags &opt = {});
487
488 // Move constructor
489 Engine(Engine &&) noexcept;
490
491 ~Engine();
492
500 void prepare(const Device &device);
501
510 void prepare(const Device &device, const RuntimeOptions &runOptions);
511
517 void deploy();
518
527 void load(const Device &device);
528
541 void run(unsigned prog = 0, const std::string &debugName = "");
542
552 void stop();
553
567 void run(unsigned prog, const std::string &debugName,
568 const RuntimeOptions &options);
569
579 void loadAndRun(const Device &device, unsigned prog = 0);
580
585 public:
586 TimerTimePoint() = default;
587
588 private:
589 std::shared_ptr<core::TimerTimePoint> impl;
590 explicit TimerTimePoint(Engine &e);
591 friend class Engine;
592 };
593
598
604 static std::string reportTiming(const TimerTimePoint &start,
605 const TimerTimePoint &end);
606
612 GC_DEPRECATED void resetExecutionProfile();
613
627 pva::Report getReport(bool reportExecution = true);
628
640
645
666 void printProfileSummary(std::ostream &outputStream,
667 const OptionFlags &opt = {});
668
691 void reportIntervals(std::ostream &outputStream);
692
709 void readTensor(StringRef handle, void *buf, void *bufEnd);
710
726 void readTensor(StringRef handle, QuarterMetadata &metadata, void *buf,
727 void *bufEnd);
728
740 template <class T>
741 void readTensor(StringRef handle, gccs::ArrayRef<T> buffer) {
742 using HostType = typename std::decay<T>::type;
743 static_assert(not std::is_const<T>::value,
744 "readTensor requires a writable buffer");
745 auto cast = [](T *p) {
746 return const_cast<void *>(static_cast<const void *>(p));
747 };
748 copyTensor(handle, equivalent_device_type<HostType>().value, true,
749 cast(buffer.begin()), cast(buffer.end()));
750 }
751
766 void writeTensor(StringRef handle, const void *buf, const void *bufEnd);
767
781 void writeTensor(StringRef handle, const QuarterMetadata &metadata,
782 const void *buf, const void *bufEnd);
783
794 template <class T> void writeTensor(StringRef handle, ArrayRef<T> buffer) {
795 using HostType = typename std::decay<T>::type;
796 auto cast = [](const T *p) {
797 return const_cast<void *>(static_cast<const void *>(p));
798 };
799 copyTensor(handle, equivalent_device_type<HostType>().value, false,
800 cast(buffer.begin()), cast(buffer.end()));
801 }
802
813 void connectStream(StringRef handle, void *begin, void *end);
814
829 void connectStream(StringRef handle,
830 const gccs::ArrayRef<QuarterMetadata> &metadata,
831 void *begin, void *end);
832
842 template <class T>
843 void connectStream(StringRef handle, const gccs::ArrayRef<T> &buffer) {
844 using HostType = typename std::decay<T>::type;
845 auto cast = [](T *p) {
846 return const_cast<void *>(static_cast<const void *>(p));
847 };
849 std::is_const<T>::value, cast(buffer.begin()),
850 cast(buffer.end()));
851 }
852
862 void connectStream(StringRef handle, void *p);
863
875
887 void connectStreamToCallback(StringRef handle, unsigned index,
889
903 void connectHostFunction(StringRef handle, unsigned index,
905
917 void copyFromRemoteBuffer(StringRef handle, void *w, uint64_t repeatIndex,
918 unsigned replicationIndex = 0);
919
920 template <class T>
921 void copyFromRemoteBuffer(StringRef handle, gccs::ArrayRef<T> buffer,
922 uint64_t repeatIndex,
923 unsigned replicationIndex = 0) {
924 static_assert(not std::is_const<T>::value,
925 "Engine::copyFromRemoteBuffer requires a writable buffer");
926 auto cast = [](T *p) {
927 return const_cast<void *>(static_cast<const void *>(p));
928 };
929 using HostType = typename std::decay<T>::type;
930 copyRemoteBuffer(handle, repeatIndex, replicationIndex,
932 cast(buffer.begin()), buffer.size());
933 }
934
946 void copyToRemoteBuffer(void *w, StringRef handle, uint64_t repeatIndex,
947 unsigned replicationIndex = 0);
948
949 template <class T>
950 void copyToRemoteBuffer(ArrayRef<T> buffer, StringRef handle,
951 uint64_t repeatIndex, unsigned replicationIndex = 0) {
952 auto cast = [](T *p) {
953 return const_cast<void *>(static_cast<const void *>(p));
954 };
955 using HostType = typename std::decay<T>::type;
956 copyRemoteBuffer(handle, repeatIndex, replicationIndex,
957 &equivalent_device_type<HostType>().value, false,
958 cast(buffer.begin()), buffer.size());
959 }
960
967 std::vector<std::string> listStreams() const;
968
973 void setPrintStream(std::ostream &stream);
974
980 void setPrintTensorStream(std::ostream &stream);
981
985
988 void serializeExecutable(std::ostream &out) const;
989
1034 void insertSimulatedError(ErrorCode error, ErrorLocation const &location);
1035
1043 void eraseSimulatedError(ErrorLocation const &location);
1044
1060
1084 std::vector<ErrorLocation>
1085 getSimulatedErrorLocations(unsigned programId, unsigned tile = ~0) const;
1086
1095 std::vector<ErrorLocation>
1096 getSimulatedErrorLocations(StringRef vertexName, unsigned tile = ~0) const;
1097
1098 // Implementation
1099 Engine(std::unique_ptr<core::Engine>);
1100 const core::Engine &getImpl() const { return *impl; }
1101
1102private:
1103 void connectStream(StringRef handle, const Type *type,
1104 const gccs::ArrayRef<QuarterMetadata> &metadatas,
1105 bool readOnly, void *begin, void *end);
1106
1107 void copyTensor(StringRef handle, const Type &type, bool read, void *begin,
1108 void *end);
1109
1110 void copyTensor(StringRef handle, const Type &type, QuarterMetadata *metadata,
1111 bool read, void *begin, void *end);
1112
1113 void readTensor(StringRef handle, void *metadata, void *buf, void *bufEnd);
1114
1115 void writeTensor(StringRef handle, void *metadata, const void *buf,
1116 const void *bufEnd);
1117
1118 void copyRemoteBuffer(StringRef handle, unsigned repeatIndex,
1119 unsigned replicationIndex, const Type *type, bool read,
1120 void *begin, size_t numElements);
1121
1122 std::unique_ptr<core::Engine> impl;
1123};
1124
1151Executable compileGraph(Graph &&graph, ArrayRef<program::Program> progs,
1152 const OptionFlags &opt = {},
1153 ProgressFunc progressCallBack = ProgressFunc(),
1154 const DebugContext &debugContext = {});
1155Executable compileGraph(const Graph &graph, ArrayRef<program::Program> progs,
1156 const OptionFlags &opt = {},
1157 ProgressFunc progressCallBack = ProgressFunc(),
1158 const DebugContext &debugContext = {});
1159
1185 const OptionFlags &opt = {},
1186 ProgressFunc progressCallBack = ProgressFunc(),
1187 const DebugContext &debugContext = {});
1188Module compileModule(const Graph &graph, program::Program prog,
1189 const OptionFlags &opt = {},
1190 ProgressFunc progressCallBack = ProgressFunc(),
1191 const DebugContext &debugContext = {});
1192
1221 const Preallocations &preallocations,
1222 const OptionFlags &opt = {},
1223 ProgressFunc progressCallBack = ProgressFunc(),
1224 const DebugContext &debugContext = {});
1225Module compileModule(const Graph &graph, program::Program prog,
1226 const Preallocations &preallocations,
1227 const OptionFlags &opt = {},
1228 ProgressFunc progressCallBack = ProgressFunc(),
1229 const DebugContext &debugContext = {});
1230
1260Executable compileGraph(Graph &&graph, ArrayRef<program::Program> progs,
1261 const Preallocations &preallocations,
1262 const OptionFlags &opt = {},
1263 ProgressFunc progressCallBack = ProgressFunc(),
1264 const DebugContext &debugContext = {});
1265Executable compileGraph(const Graph &graph, ArrayRef<program::Program> progs,
1266 const Preallocations &preallocations,
1267 const OptionFlags &opt = {},
1268 ProgressFunc progressCallBack = ProgressFunc(),
1269 const DebugContext &debugContext = {});
1270
1271} // End namespace poplar.
1272
1273#endif // poplar_Engine_hpp
References to arrays.
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
PImpl interface to core timing information.
Definition: Engine.hpp:584
A graph compute engine.
Definition: Engine.hpp:390
Engine(Graph &&graph, program::Program prog, const OptionFlags &opt={}, ProgressFunc progressCallBack=ProgressFunc(), const DebugContext &debugContext={})
Construct the engine from a graph and a program.
void resetExecutionProfile()
Reset execution profile.
void reportIntervals(std::ostream &outputStream)
Write a CSV data file to a specified output stream.
void setPrintStream(std::ostream &stream)
Set output stream for printf commands.
std::vector< ErrorLocation > getSimulatedErrorLocations(unsigned programId, unsigned tile=~0) const
Return the locations of a program from a program ID.
void clearSimulatedErrors()
Undo the effects of all Engine::insertSimulatedError() calls.
std::function< void(int, int)> ProgressFunc
Callback function used to to indicate engine compilation progress.
Definition: Engine.hpp:407
void run(unsigned prog=0, const std::string &debugName="")
Run the graph program.
Engine(Graph &&graph, ArrayRef< program::Program > progs, const OptionFlags &opt={}, ProgressFunc progressCallBack=ProgressFunc(), const DebugContext &debugContext={})
Construct the engine from a graph and a list of programs.
void readTensor(StringRef handle, gccs::ArrayRef< T > buffer)
Synchronous copy of a buffer of non-Quarter type data from a specific tensor in the device into a hos...
Definition: Engine.hpp:741
void deploy()
Load the engine.
void writeTensor(StringRef handle, const QuarterMetadata &metadata, const void *buf, const void *bufEnd)
Synchronous copy of a buffer of Quarter type data from the host to a specific tensor in the device.
void connectStream(StringRef handle, const gccs::ArrayRef< T > &buffer)
Connect a stream of non-Quarter type to a circular buffer in memory.
Definition: Engine.hpp:843
void readTensor(StringRef handle, void *buf, void *bufEnd)
Synchronous copy of a buffer of non-Quarter type data from a specific tensor in the device into a hos...
void stop()
Stop the graph program.
void connectStreamToCallback(StringRef handle, StreamCallbackHandle f)
Connect a stream to a callback taking a pointer to the location in memory to copy into/from.
void load(const Device &device)
Load the compiled program/graph onto a device.
void connectStream(StringRef handle, void *p)
Connect a stream of non-Quarter type to a fixed location in memory.
void eraseSimulatedError(ErrorLocation const &location)
Undo the effects of Engine::insertSimulatedError();.
void copyFromRemoteBuffer(StringRef handle, void *w, uint64_t repeatIndex, unsigned replicationIndex=0)
Copy from a remote buffer to a user buffer w.
void connectHostFunction(StringRef handle, unsigned index, HostCallbackHandle f)
Connect a HostFunction to a callback.
void connectStream(StringRef handle, const gccs::ArrayRef< QuarterMetadata > &metadata, void *begin, void *end)
Connect a stream of type Quarter to a circular buffer in memory.
void insertSimulatedError(ErrorCode error, ErrorLocation const &location)
Simulate an error.
void readTensor(StringRef handle, QuarterMetadata &metadata, void *buf, void *bufEnd)
Synchronous copy of a buffer of Quarter type data from a specific tensor in the device into a host-si...
void connectStreamToCallback(StringRef handle, unsigned index, StreamCallbackHandle f)
Connect a replicated stream to a callback taking a pointer to the location in memory to copy into/fro...
void prepare(const Device &device)
Prepare the device for loading.
void writeTensor(StringRef handle, ArrayRef< T > buffer)
Synchronous copy of a buffer of non-Quarter type data from the host to a specific tensor in the devic...
Definition: Engine.hpp:794
std::vector< std::string > listStreams() const
Return a list of all streams in the engine.
static std::string reportTiming(const TimerTimePoint &start, const TimerTimePoint &end)
Get a timing report for the measured interval.
void copyToRemoteBuffer(void *w, StringRef handle, uint64_t repeatIndex, unsigned replicationIndex=0)
Copy to a remote buffer from a user buffer w.
pva::Report getReport(bool reportExecution=true)
Get a PVA Report object that allows access to profiling data for the graph and the execution with thi...
void setPrintTensorStream(std::ostream &stream)
Set the output stream for PrintTensor programs.
void printProfileSummary(std::ostream &outputStream, const OptionFlags &opt={})
Get and print the summary of a report with the given options.
void connectStream(StringRef handle, void *begin, void *end)
Connect a stream of non-Quarter to a circular buffer in memory.
Engine(Executable &&exe, const OptionFlags &opt={})
Construct the engine from a precompiled executable.
void writeTensor(StringRef handle, const void *buf, const void *bufEnd)
Synchronous copy of a buffer of non-Quarter type data from the host to a specific tensor in the devic...
std::vector< ErrorLocation > getSimulatedErrorLocations(StringRef vertexName, unsigned tile=~0) const
Return the locations of a program from a vertex name.
void disableExecutionProfiling()
Pause execution profiling.
void enableExecutionProfiling()
Enable execution profiling.
void serializeExecutable(std::ostream &out) const
Serialize the executable used by the engine.
TimerTimePoint getTimeStamp()
Get a record of the current host and device time.
OptionFlags getEngineOptions() const
Returns the options the engine was created with.
void loadAndRun(const Device &device, unsigned prog=0)
Run the graph program.
An instance of poplar::Executable contains all of the information needed to run a program on an IPU d...
Definition: Executable.hpp:17
This class represents a graph program to be executed on the IPU.
Definition: Graph.hpp:52
Wrapper for HostCallback instances.
Definition: HostFunctionCallback.hpp:31
An instance of poplar::Moudle contains all of the information needed to run a program on an IPU devic...
Definition: Module.hpp:17
A set of option/value string flags to be used in various APIs.
Definition: OptionFlags.hpp:24
This class describes where external variables and other pre-existing allocations are in memory.
Definition: Preallocations.hpp:21
Quarter metadata type.
Definition: Quarter.hpp:37
A group of properties that are reconfigurable in each engine execution.
Definition: RuntimeOptions.hpp:124
Wrapper for StreamCallback instances.
Definition: StreamCallback.hpp:161
Class representing device data types.
Definition: Type.hpp:42
This class represents a control program that executes operations on the graph.
Definition: Program.hpp:30
Supported Option flags:
Poplar classes and functions.
Definition: ArrayRef.hpp:14
Executable compileGraph(Graph &&graph, ArrayRef< program::Program > progs, const OptionFlags &opt={}, ProgressFunc progressCallBack=ProgressFunc(), const DebugContext &debugContext={})
Compile the given graph and programs to make an executable that can be executed using a poplar::Engin...
ErrorCode
Unique error codes for all the possible errors.
Definition: Error.hpp:31
Module compileModule(Graph &&graph, program::Program prog, const OptionFlags &opt={}, ProgressFunc progressCallBack=ProgressFunc(), const DebugContext &debugContext={})
Compile the given graph and single program to make a module.
poplar::Tensor cast(poplar::Graph &graph, const poplar::Tensor &src, const poplar::Type &dstType, poplar::program::Sequence &prog, const poplar::DebugContext &debugContext={})
Cast elements of the specified src tensor to dstType, returning the result as a new tensor.
Uniquely identify the location of an error within a Poplar Device.
Definition: Error.hpp:97
Template structure to relate a host type to a device type.
Definition: Type.hpp:192