Poplar and PopLibs
VertexIntrospector.hpp
1// Copyright (c) 2017 Graphcore Ltd. All rights reserved.
2
3#ifndef poplar_VertexIntrospector_hpp
4#define poplar_VertexIntrospector_hpp
5
6#include <poplar/GraphElements.hpp>
7#include <poplar/Target.hpp>
8#include <poplar/TypeTraits.hpp>
9#include <poplar/VectorLayout.hpp>
10#include <poplar/exceptions.hpp>
11
12#include <limits>
13#include <memory>
14#include <string>
15#include <vector>
16
17namespace poplar {
18namespace core {
19class VertexIntrospector;
20class FieldData;
21} // namespace core
22
39class FieldData {
40public:
41 struct SizeT {
42 std::size_t value;
43 std::size_t size() const { return value; }
44 };
45
46 FieldData(const FieldData &) = delete;
47 FieldData(FieldData &&) noexcept;
48 virtual ~FieldData();
49
55 unsigned rank() const;
56
63 std::size_t size() const;
64
72 std::size_t getSizeAtIndex(std::size_t i) const;
73
81 layout::Vector getProfilerVectorLayout(std::size_t nestingLevel) const;
82
87
91 SizeT operator[](std::size_t i) const { return SizeT{getSizeAtIndex(i)}; }
92
95 std::string name() const;
96
100 template <typename T> T getInitialValue(const Target &target) const {
101 static_assert(TypeTraits::isSimpleType<T>(),
102 "getInitialValue() called with invalid type");
103
104 if (rank() != 0) {
105 throw poplar_error("You may not call getInitialValue() on a non-scalar "
106 "(use getInitalValues() - with an 's')");
107 }
108 T result;
109 const auto traits = TypeTraits::make<T>();
110 getInitialValues(target, reinterpret_cast<void *>(&result), traits);
111 return result;
112 }
113
118 template <typename T>
119 std::vector<T> getInitialValues(const Target &target) const {
120 if (rank() == 0) {
121 throw poplar_error("You may not call getInitialValues() on a scalar "
122 "(use getInitalValue() - no 's')");
123 }
124
125 std::vector<T> result(size());
126
127 // Resolve whether this function was called
128 // with `T := SCALAR` or `T := std::vector<SCALAR>`:
129 getInitialValuesOverload(target, result);
130
131 return result;
132 }
133
134 // Implementation
135 FieldData(std::unique_ptr<core::FieldData> fd);
136
137private:
138 std::unique_ptr<core::FieldData> impl;
139
140 // This overload retrieves the initial value of any field with one index.
141 template <typename T>
142 void getInitialValuesOverload(const Target &target,
143 std::vector<T> &result) const {
144 static_assert(TypeTraits::isSimpleType<T>(),
145 "getInitialValues() called with invalid type");
146
147 if (rank() != 1) {
148 throw poplar_error("getInitialValues() must be called with a "
149 "std::vector<T> for 1D fields");
150 }
151
152 auto traits = TypeTraits::make<T>();
153 getInitialValues(target, result.data(), traits);
154 }
155
156 // This overload retrieves the initial values of any field with
157 // two indices, for example, Vector<Input<Vector<scalar>>>
158 template <typename T>
159 void getInitialValuesOverload(const Target &target,
160 std::vector<std::vector<T>> &result) const {
161 static_assert(TypeTraits::isSimpleType<T>(),
162 "getInitialValues() called with invalid type");
163
164 if (rank() != 2) {
165 throw poplar_error("getInitialValues() must be called with a "
166 "std::vector<std::vector<T>> for 1D fields");
167 }
168
169 auto traits = TypeTraits::make<T>();
170 for (auto i = 0u; i < result.size(); ++i) {
171 result[i] = std::vector<T>(getSizeAtIndex(i));
172 // It is not an error to return empty vectors here
173 // (for example, some tests in poplin generate empty worklists).
174 if (!result[i].empty()) {
175 getInitialValues(target, result[i].data(), traits, i);
176 }
177 }
178 }
179
180 void getInitialValues(
181 const Target &target, void *dst, const TypeTraits &traits,
182 std::size_t index = std::numeric_limits<std::size_t>::max()) const;
183};
184
189public:
196 FieldData getFieldInfo(const std::string &name) const;
197
201
202 // Implementation
203 VertexIntrospector(std::unique_ptr<core::VertexIntrospector> impl);
205 const core::VertexIntrospector &getImpl() const { return *impl; }
206
207private:
208 std::unique_ptr<core::VertexIntrospector> impl;
209};
210
211} // end namespace poplar
212
213#endif // poplar_VertexIntrospector_hpp
A reference to a compute set within a graph.
Definition: GraphElements.hpp:131
Information about a vertex field, including its size and its initial value, if set.
Definition: VertexIntrospector.hpp:39
T getInitialValue(const Target &target) const
Get the inital value for a scalar field.
Definition: VertexIntrospector.hpp:100
SizeT operator[](std::size_t i) const
Determine size of field.
Definition: VertexIntrospector.hpp:91
std::string name() const
Return the name of the vertex field.
layout::Vector getProfilerVectorLayout(std::size_t nestingLevel) const
Determine the layout for vector fields.
std::vector< T > getInitialValues(const Target &target) const
Get the initial value for a 1D or 2D vector field.
Definition: VertexIntrospector.hpp:119
std::size_t size() const
Determine the size of the field.
std::size_t getSizeAtIndex(std::size_t i) const
Determine the size of the sub-vector for 2D fields.
layout::VectorList getProfilerVectorListLayout() const
For VectorList fields, return the layout.
unsigned rank() const
Return the rank of the field.
A target representation.
Definition: Target.hpp:69
Available to cycle estimators to inspect the shape and initial values of a vertex's fields.
Definition: VertexIntrospector.hpp:188
ComputeSet getComputeSet() const
Return the compute set that this vertex is in.
FieldData getFieldInfo(const std::string &name) const
Information about the vertex field.
half2 max(half2 src0, half2 src1)
Targets the f16v2max instruction.
Definition: ipu_intrinsics:333
Vector
An enumeration used to state what type of pointer is used for a Vector vertex field.
Definition: VectorLayout.hpp:15
VectorList
An enumeration used to state what type of pointer is used for a VectorList vertex field.
Definition: VectorLayout.hpp:31
Poplar classes and functions.
Definition: ArrayRef.hpp:14
Base class for Poplar exceptions.
Definition: exceptions.hpp:16