Mneme
Mneme a tool allowing recording the execution of a GPU (CUDA/HIP) kernel and replaying that kernel as an independent executable.
Loading...
Searching...
No Matches
MnemeAnnotation.hpp
Go to the documentation of this file.
1#pragma once
2//===----------------------------------------------------------------------===//
3// mneme_annotate.h - User-facing annotation API for Mneme
4//
5// This header declares mneme::annotate(ptr, Metadata{...}) and sub-region
6// overloads such as mneme::annotate(Span(ptr, count), Metadata{...}).
7// It is intentionally *interface-only*: no lambdas / comparators yet.
8//
9// Example:
10// double* p = ...;
11// mneme::annotate(p, mneme::Metadata{
12// .dtype = mneme::Type::Builtin,
13// .builtin = mneme::BuiltinDType::F64,
14// .threshold = 0.1,
15// .threshold_kind = mneme::ThresholdKind::Absolute,
16// .norm = mneme::Norm::Linf,
17// });
18//
19// You can also use the typed helper:
20// mneme::annotate<double>(p, mneme::Metadata{ .threshold = 0.1 });
21//
22// Or annotate a sub-region of 128 elements:
23// mneme::annotate(mneme::Span(p + 64, 128),
24// mneme::Metadata{ .threshold = 0.1 });
25//
26//===----------------------------------------------------------------------===//
27
28#include <cstddef>
29#include <cstdint>
30#include <optional>
31#include <string>
32#include <type_traits>
33#include <utility>
34
35namespace mneme {
36
37// Built-in scalar dtypes Mneme knows about today.
38enum class BuiltinDType : std::uint8_t {
39 U8 = 0,
50};
51
52// The size in bytes of one element of builtin, or 0 if builtin is not one of
53// the enumerators, as when it was read from a corrupt file.
54constexpr std::size_t builtinByteWidth(BuiltinDType builtin) {
55 switch (builtin) {
58 return 1;
62 return 2;
66 return 4;
70 return 8;
71 }
72 return 0;
73}
74
75// How to interpret `threshold`.
76enum class ThresholdKind : std::uint8_t {
79};
80
81// If the pointer represents a collection, which norm to use for aggregation.
82enum class Norm : std::uint8_t {
83 None = 0, // interpret threshold per-element / per-scalar
87};
88
89// Core metadata attached to a pointer/region.
90struct Metadata {
91 // Builtin dtype (used when dtype == Builtin).
93
94 // Error threshold semantics.
95 double threshold = 0.0;
97
98 // Aggregation semantics (for arrays/regions). Optional.
100
101 // Optional user tag (for reporting / grouping). Not interpreted by Mneme.
102 std::optional<std::string> tag = std::nullopt;
103};
104
105inline bool operator==(const Metadata &LHS, const Metadata &RHS) {
106 return LHS.builtin == RHS.builtin && LHS.threshold == RHS.threshold &&
107 LHS.threshold_kind == RHS.threshold_kind && LHS.norm == RHS.norm &&
108 LHS.tag == RHS.tag;
109}
110
111inline bool operator!=(const Metadata &LHS, const Metadata &RHS) {
112 return !(LHS == RHS);
113}
114
115// --------- Builtin dtype mapping helpers (optional sugar) ------------------
116
117namespace detail {
119} // namespace detail
120
123};
124
125template <> struct builtin_dtype_of<float> {
127};
128template <> struct builtin_dtype_of<double> {
130};
131
132template <> struct builtin_dtype_of<std::int8_t> {
134};
135template <> struct builtin_dtype_of<std::uint8_t> {
137};
138template <> struct builtin_dtype_of<std::int16_t> {
140};
141template <> struct builtin_dtype_of<std::uint16_t> {
143};
144template <> struct builtin_dtype_of<std::int32_t> {
146};
147template <> struct builtin_dtype_of<std::uint32_t> {
149};
150template <> struct builtin_dtype_of<std::int64_t> {
152};
153template <> struct builtin_dtype_of<std::uint64_t> {
155};
156
157// Whether T has a specialization of builtin_dtype_of, rather than falling back
158// to U8.
159template <class T>
161 : std::bool_constant<!std::is_base_of_v<detail::unmapped_builtin_dtype,
162 builtin_dtype_of<T>>> {};
163
164template <class T>
166
167// A view of Count contiguous elements of type T starting at Data.
168template <class T> class Span {
169 template <class Container>
170 using ElementOf =
171 std::remove_pointer_t<decltype(std::declval<Container &>().data())>;
172
173 template <class Container>
174 using EnableIfContainerOf = std::enable_if_t<
175 !std::is_same_v<std::remove_cv_t<Container>, Span> &&
176 std::is_convertible_v<ElementOf<Container> (*)[], T (*)[]> &&
177 std::is_convertible_v<decltype(std::declval<Container &>().size()),
178 std::size_t>>;
179
180public:
181 constexpr Span(T *Data, std::size_t Count) : Data(Data), Count(Count) {}
182
183 template <std::size_t N>
184 constexpr Span(T (&Array)[N]) : Data(Array), Count(N) {}
185
186 template <class Container, class = EnableIfContainerOf<Container>>
187 constexpr Span(Container &C) : Data(C.data()), Count(C.size()) {}
188
189 template <class U,
190 class = std::enable_if_t<std::is_convertible_v<U (*)[], T (*)[]>>>
191 constexpr Span(const Span<U> &Other)
192 : Data(Other.data()), Count(Other.size()) {}
193
194 constexpr T *data() const { return Data; }
195 constexpr std::size_t size() const { return Count; }
196 constexpr std::size_t size_bytes() const { return Count * sizeof(T); }
197
198 constexpr Span subspan(std::size_t Offset, std::size_t SubCount) const {
199 return Span(Data + Offset, SubCount);
200 }
201
202private:
203 T *Data;
204 std::size_t Count;
205};
206
207template <class Container>
208Span(Container &) -> Span<
209 std::remove_pointer_t<decltype(std::declval<Container &>().data())>>;
210
211// --------- User-facing API ------------------------------------------------
212
213// Primary user API: annotate a pointer with metadata.
214void annotate(const void *ptr, Metadata md);
215
216// Annotate a sub-region beginning at ptr and spanning bytes bytes.
217void annotate(const void *ptr, std::size_t bytes, Metadata md);
218
219// Typed helper: sets builtin dtype automatically when T maps to a known
220// BuiltinDType. If T is unknown, this will leave builtin=Unknown (still useful
221// if you set dtype=Custom later).
222template <class T,
223 std::enable_if_t<!std::is_void_v<std::remove_cv_t<T>>, int> = 0>
224inline void annotate(T *ptr, Metadata md = {}) {
225 // If the user didn't specify dtype explicitly, keep default Builtin.
226 // If they *did* specify Custom, we don't override anything here.
227 md.builtin = builtin_dtype_of<std::remove_cv_t<T>>::value;
228 annotate(static_cast<const void *>(ptr), std::move(md));
229}
230
231// Annotate the elements Region views, with builtin set from T.
232template <class T> inline void annotate(Span<T> Region, Metadata Md) {
234 "mneme::annotate(Span<T>, Metadata): T has no BuiltinDType "
235 "mapping; use annotate(const void *, std::size_t bytes, "
236 "Metadata) and set Metadata::builtin explicitly");
238 annotate(static_cast<const void *>(Region.data()), Region.size_bytes(),
239 std::move(Md));
240}
241
242} // namespace mneme
Definition MnemeAnnotation.hpp:168
constexpr Span(Container &C)
Definition MnemeAnnotation.hpp:187
constexpr T * data() const
Definition MnemeAnnotation.hpp:194
constexpr Span(T(&Array)[N])
Definition MnemeAnnotation.hpp:184
constexpr std::size_t size() const
Definition MnemeAnnotation.hpp:195
constexpr Span(const Span< U > &Other)
Definition MnemeAnnotation.hpp:191
constexpr std::size_t size_bytes() const
Definition MnemeAnnotation.hpp:196
constexpr Span subspan(std::size_t Offset, std::size_t SubCount) const
Definition MnemeAnnotation.hpp:198
constexpr Span(T *Data, std::size_t Count)
Definition MnemeAnnotation.hpp:181
Definition MnemeAnnotation.cpp:3
constexpr std::size_t builtinByteWidth(BuiltinDType builtin)
Definition MnemeAnnotation.hpp:54
bool operator==(const Metadata &LHS, const Metadata &RHS)
Definition MnemeAnnotation.hpp:105
void annotate(const void *ptr, Metadata md)
Definition MnemeAnnotation.cpp:5
constexpr bool has_builtin_dtype_v
Definition MnemeAnnotation.hpp:165
ThresholdKind
Definition MnemeAnnotation.hpp:76
@ Relative
Definition MnemeAnnotation.hpp:78
@ Absolute
Definition MnemeAnnotation.hpp:77
Norm
Definition MnemeAnnotation.hpp:82
@ Linf
Definition MnemeAnnotation.hpp:86
@ None
Definition MnemeAnnotation.hpp:83
@ L2
Definition MnemeAnnotation.hpp:85
@ L1
Definition MnemeAnnotation.hpp:84
bool operator!=(const Metadata &LHS, const Metadata &RHS)
Definition MnemeAnnotation.hpp:111
Span(Container &) -> Span< std::remove_pointer_t< decltype(std::declval< Container & >().data())> >
BuiltinDType
Definition MnemeAnnotation.hpp:38
@ F64
Definition MnemeAnnotation.hpp:49
@ U64
Definition MnemeAnnotation.hpp:45
@ F32
Definition MnemeAnnotation.hpp:48
@ F16
Definition MnemeAnnotation.hpp:47
@ I8
Definition MnemeAnnotation.hpp:40
@ U8
Definition MnemeAnnotation.hpp:39
@ I16
Definition MnemeAnnotation.hpp:42
@ U32
Definition MnemeAnnotation.hpp:43
@ I32
Definition MnemeAnnotation.hpp:44
@ I64
Definition MnemeAnnotation.hpp:46
@ U16
Definition MnemeAnnotation.hpp:41
Definition MnemeAnnotation.hpp:90
ThresholdKind threshold_kind
Definition MnemeAnnotation.hpp:96
BuiltinDType builtin
Definition MnemeAnnotation.hpp:92
double threshold
Definition MnemeAnnotation.hpp:95
Norm norm
Definition MnemeAnnotation.hpp:99
std::optional< std::string > tag
Definition MnemeAnnotation.hpp:102
static constexpr BuiltinDType value
Definition MnemeAnnotation.hpp:129
static constexpr BuiltinDType value
Definition MnemeAnnotation.hpp:126
static constexpr BuiltinDType value
Definition MnemeAnnotation.hpp:139
static constexpr BuiltinDType value
Definition MnemeAnnotation.hpp:145
static constexpr BuiltinDType value
Definition MnemeAnnotation.hpp:151
static constexpr BuiltinDType value
Definition MnemeAnnotation.hpp:133
static constexpr BuiltinDType value
Definition MnemeAnnotation.hpp:142
static constexpr BuiltinDType value
Definition MnemeAnnotation.hpp:148
static constexpr BuiltinDType value
Definition MnemeAnnotation.hpp:154
static constexpr BuiltinDType value
Definition MnemeAnnotation.hpp:136
Definition MnemeAnnotation.hpp:121
static constexpr BuiltinDType value
Definition MnemeAnnotation.hpp:122
Definition MnemeAnnotation.hpp:118
Definition MnemeAnnotation.hpp:162