Poplar and PopLibs
GraphFunction.hpp
Go to the documentation of this file.
1// Copyright (c) 2017 Graphcore Ltd. All rights reserved.
13#ifndef poputil_GraphFunction_hpp
14#define poputil_GraphFunction_hpp
15#include <poplar/DebugContext.hpp>
16#include <poplar/Graph.hpp>
17#include <poplar/Program.hpp>
18
19namespace poputil {
20
22namespace graphfn {
23
25enum ArgType { InputArg, OutputArg, InOutArg, CreatedArg };
26
27struct ArgSig {
28 ArgType type;
29 poplar::Tensor similarTensor;
30 std::string debugName;
31 ArgSig(ArgType type, poplar::Tensor tensor, std::string debugName)
32 : type(type), similarTensor(std::move(tensor)),
33 debugName(std::move(debugName)) {}
34};
35
36inline ArgSig input(poplar::Tensor similar, std::string debugName = "") {
37 return ArgSig(InputArg, std::move(similar), std::move(debugName));
38}
39
40inline ArgSig inout(poplar::Tensor similar, std::string debugName = "") {
41 return ArgSig(InOutArg, std::move(similar), std::move(debugName));
42}
43
44inline ArgSig output(poplar::Tensor similar, std::string debugName = "") {
45 return ArgSig(OutputArg, std::move(similar), std::move(debugName));
46}
47
48inline ArgSig created(std::string debugName = "") {
49 return ArgSig(CreatedArg, poplar::Tensor(), std::move(debugName));
50}
51
52using Signature = std::vector<ArgSig>;
53
54class VoidFunction {
55 poplar::Graph &graph;
56 Signature sig;
57 bool inlined;
60 std::vector<poplar::Tensor> params;
61
62public:
63 VoidFunction(VoidFunction &&fn);
64 VoidFunction(poplar::Graph &graph, Signature sig,
65 std::function<void(std::vector<poplar::Tensor> &,
67 f,
68 bool inlined = false,
69 const poplar::DebugContext &debugContext = {});
70
71 VoidFunction(poplar::Graph &graph, Signature sig,
72 std::function<void(std::vector<poplar::Tensor> &,
75 f,
76 bool inlined = false,
77 const poplar::DebugContext &debugContext = {});
78
79 void operator()(std::vector<poplar::Tensor> &args,
81 const poplar::DebugContext &dc = {});
82};
83
84class ProgramFunction {
85 VoidFunction voidFunc;
86
87public:
88 ProgramFunction(
89 poplar::Graph &graph, Signature sig,
90 std::function<poplar::program::Program(std::vector<poplar::Tensor> &)> f,
91 bool inlined = false, const poplar::DebugContext &debugContext = {});
92
93 ProgramFunction(
94 poplar::Graph &graph, Signature sig,
95 std::function<poplar::program::Program(std::vector<poplar::Tensor> &,
97 f,
98 bool inlined = false, const poplar::DebugContext &debugContext = {});
99
101 operator()(std::vector<poplar::Tensor> &args,
102 const poplar::DebugContext &debugContext = {});
103};
104
105class TensorFunction {
106 VoidFunction voidFunc;
107
108public:
109 TensorFunction(poplar::Graph &graph, Signature sig,
110 std::function<poplar::Tensor(std::vector<poplar::Tensor> &,
112 f,
113 bool inlined = false,
114 const poplar::DebugContext &debugContext = {});
115
116 TensorFunction(poplar::Graph &graph, Signature sig,
117 std::function<poplar::Tensor(std::vector<poplar::Tensor> &,
119 const poplar::DebugNameAndId &)>
120 f,
121 bool inlined = false,
122 const poplar::DebugContext &debugContext = {});
123
124 poplar::Tensor operator()(std::vector<poplar::Tensor> &args,
126 const poplar::DebugContext &debugContext = {});
127};
128
129} // namespace graphfn
130} // namespace poputil
131
132#endif // poputil_GraphFunction_hpp
DebugContext gathers the common external parameters of the context of an operation.
Definition: DebugContext.hpp:221
DebugNameAndId bundles a name and a DebugId to facilitate their propagation through function calls.
Definition: DebugContext.hpp:142
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
A reference to a subset of tensor elements.
Definition: Tensor.hpp:38
This class represents a control program that executes operations on the graph.
Definition: Program.hpp:30
Program that executes a sequence of programs.
Definition: Program.hpp:77
ArgType
Type of argument to function program.
Definition: GraphFunction.hpp:25
General utility functions for building graphs.
Definition: GfloatExprUtil.hpp:23