-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdispatcher.h
More file actions
317 lines (271 loc) · 12.4 KB
/
dispatcher.h
File metadata and controls
317 lines (271 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
#ifndef INFINI_OPS_DISPATCHER_H_
#define INFINI_OPS_DISPATCHER_H_
#include <iostream>
#include <optional>
#include <string_view>
#include <vector>
#include "common/traits.h"
#include "data_type.h"
#include "device.h"
namespace infini::ops {
// -----------------------------------------------------------------------------
// Core Generic Runtime Dispatchers
// -----------------------------------------------------------------------------
namespace detail {
// Implements the dispatch body over a resolved `List<head, tail...>`.
template <typename ValueType, typename Functor, typename... Args, auto head,
auto... tail>
auto DispatchFuncImpl(ValueType value, Functor &&func,
std::string_view context_str, List<head, tail...>,
Args &&...args) {
using ReturnType = decltype(std::forward<Functor>(func)(
ValueTag<static_cast<ValueType>(head)>{}, std::forward<Args>(args)...));
// Path for void functions.
if constexpr (std::is_void_v<ReturnType>) {
bool handled = ((value == static_cast<ValueType>(tail)
? (std::forward<Functor>(func)(
ValueTag<tail>{}, std::forward<Args>(args)...),
true)
: false) ||
... ||
(value == static_cast<ValueType>(head)
? (std::forward<Functor>(func)(
ValueTag<head>{}, std::forward<Args>(args)...),
true)
: false));
if (!handled) {
// TODO(lzm): change to logging.
std::cerr << "dispatch error (void): value " << static_cast<int>(value)
<< " not supported in the context: " << context_str << "\n";
std::abort();
}
}
// Path for non-void functions.
else {
std::optional<ReturnType> result;
bool handled = ((value == static_cast<ValueType>(tail)
? (result.emplace(std::forward<Functor>(func)(
ValueTag<tail>{}, std::forward<Args>(args)...)),
true)
: false) ||
... ||
(value == static_cast<ValueType>(head)
? (result.emplace(std::forward<Functor>(func)(
ValueTag<head>{}, std::forward<Args>(args)...)),
true)
: false));
if (handled) {
return *result;
}
// TODO(lzm): change to logging.
std::cerr << "dispatch error (non-void): value " << static_cast<int>(value)
<< " not supported in the context: " << context_str << "\n";
std::abort();
return ReturnType{};
}
}
// Deduces `head`/`tail` from a `List` type via partial specialization,
// then forwards to `DispatchFuncImpl`.
template <typename ValueType, typename Functor, typename FilteredList,
typename ArgsTuple>
struct DispatchFuncUnwrap;
template <typename ValueType, typename Functor, auto head, auto... tail,
typename... Args>
struct DispatchFuncUnwrap<ValueType, Functor, List<head, tail...>,
std::tuple<Args...>> {
static auto call(ValueType value, Functor &&func,
std::string_view context_str, Args &&...args) {
return DispatchFuncImpl(value, std::forward<Functor>(func), context_str,
List<head, tail...>{}, std::forward<Args>(args)...);
}
};
// Empty-list specialization
template <typename ValueType, typename Functor, typename... Args>
struct DispatchFuncUnwrap<ValueType, Functor, List<>, std::tuple<Args...>> {
static auto call(ValueType value, Functor &&, std::string_view context_str,
Args &&...) {
// TODO(lzm): change to logging.
std::cerr << "dispatch error: no allowed values registered for value "
<< static_cast<int64_t>(value)
<< " in the context: " << context_str << "\n";
std::abort();
}
};
} // namespace detail
// (Single Dispatch) Dispatches a runtime value to a compile-time functor.
template <typename ValueType, ValueType... all_values, typename Functor,
typename... Args>
auto DispatchFunc(ValueType value, Functor &&func,
std::string_view context_str = "", Args &&...args) {
using FilteredPack = typename Filter<Functor, std::tuple<Args...>, List<>,
all_values...>::type;
return detail::DispatchFuncUnwrap<
ValueType, Functor, FilteredPack,
std::tuple<Args...>>::call(value, std::forward<Functor>(func),
context_str, std::forward<Args>(args)...);
}
// (Multi-Dispatch) Dispatches a vector of runtime values to a compile-time
// functor.
// Base Case: All Dimensions Resolved
template <typename Functor, typename... Args, auto... items>
auto DispatchFunc(const std::vector<int64_t> &values, size_t /*index*/,
Functor &&func, std::string_view /*context_str*/,
List<items...>, Args &&...args) {
return std::forward<Functor>(func)(List<items...>{},
std::forward<Args>(args)...);
}
// Forward declaration of the recursive multi-dispatch overload.
template <typename FirstList, typename... RestLists, typename Functor,
typename... Args, auto... items>
auto DispatchFunc(const std::vector<int64_t> &values, size_t index,
Functor &&func, std::string_view context_str, List<items...>,
Args &&...args);
// Adapter used in the recursive multi-dispatch case: given a resolved value
// `val` recurse into the next dimension.
template <typename RestListsPack, typename Functor, auto... items>
struct MultiDispatchRecurseAdapter;
template <typename... RestLists, typename Functor, auto... items>
struct MultiDispatchRecurseAdapter<TypePack<RestLists...>, Functor, items...> {
const std::vector<int64_t> &values;
size_t next_index;
Functor &func;
std::string_view context_str;
template <auto val, typename... Args>
auto operator()(ValueTag<val>, Args &&...args) const {
return DispatchFunc<RestLists...>(values, next_index, func, context_str,
List<items..., val>{},
std::forward<Args>(args)...);
}
};
template <typename RestListsPack, typename Functor, typename... Args,
auto... items, auto... allowed>
auto MultiDispatchFirstDim(const std::vector<int64_t> &values, size_t index,
Functor &func, std::string_view context_str,
List<items...>, List<allowed...>, Args &&...args) {
static_assert(sizeof...(allowed) > 0,
"`DispatchFunc` dimension list is empty");
using EnumType = std::common_type_t<decltype(allowed)...>;
MultiDispatchRecurseAdapter<RestListsPack, Functor, items...> adapter{
values, index + 1, func, context_str};
return DispatchFunc<EnumType, allowed...>(
static_cast<EnumType>(values.at(index)), adapter, context_str,
std::forward<Args>(args)...);
}
// (Multi-Dispatch) Recursive Case
template <typename FirstList, typename... RestLists, typename Functor,
typename... Args, auto... items>
auto DispatchFunc(const std::vector<int64_t> &values, size_t index,
Functor &&func, std::string_view context_str, List<items...>,
Args &&...args) {
return MultiDispatchFirstDim<TypePack<RestLists...>>(
values, index, func, context_str, List<items...>{}, FirstList{},
std::forward<Args>(args)...);
}
// -----------------------------------------------------------------------------
// High-Level Specialized Dispatchers
// -----------------------------------------------------------------------------
// These provide cleaner and more convenient APIs for common InfiniOps types.
namespace detail {
// Bridges the generic value dispatch layer to the `DataType`-specific type
// dispatch layer.
template <typename Functor>
struct DataTypeAdapter {
Functor &func;
template <auto dtype, typename... Args>
auto operator()(ValueTag<dtype>, Args &&...args) const {
using T = TypeMapType<static_cast<DataType>(dtype)>;
return func(TypeTag<T>{}, std::forward<Args>(args)...);
}
};
template <typename Functor>
struct DataTypeMultiAdapter {
Functor &func;
template <auto... dtypes, typename... Args>
auto operator()(List<dtypes...>, Args &&...args) const {
return func(TypeTag<TypeMapType<static_cast<DataType>(dtypes)>>{}...,
std::forward<Args>(args)...);
}
};
template <typename Functor>
struct DeviceAdapter {
Functor &func;
template <auto dev, typename... Args>
auto operator()(ValueTag<dev>, Args &&...args) const {
return func(ValueTag<dev>{}, std::forward<Args>(args)...);
}
};
template <typename Functor>
struct DeviceMultiAdapter {
Functor &func;
template <auto... devs, typename... Args>
auto operator()(List<devs...>, Args &&...args) const {
return func(ValueTag<devs>{}..., std::forward<Args>(args)...);
}
};
} // namespace detail
// `DataType` Dispatch
template <DataType... allowed_dtypes, typename Functor, typename... Args>
auto DispatchFunc(DataType dtype, Functor &&func,
std::string_view context_str = "", Args &&...args) {
detail::DataTypeAdapter<std::remove_reference_t<Functor>> adapter{func};
return DispatchFunc<DataType, allowed_dtypes...>(dtype, adapter, context_str,
std::forward<Args>(args)...);
}
// `DataType` Multi-Dispatch
template <typename... Lists, typename Functor, typename... Args>
auto DispatchFunc(std::initializer_list<DataType> dtypes, Functor &&func,
std::string_view context_str = "", Args &&...args) {
std::vector<int64_t> v;
for (auto d : dtypes) v.push_back(static_cast<int64_t>(d));
detail::DataTypeMultiAdapter<std::remove_reference_t<Functor>> adapter{func};
return DispatchFunc<Lists...>(v, 0, adapter, context_str, List<>{},
std::forward<Args>(args)...);
}
// `Device` Dispatch
template <auto... allowed_devices, typename Functor, typename... Args>
auto DispatchFunc(Device::Type device, Functor &&func,
std::string_view context_str = "", Args &&...args) {
detail::DeviceAdapter<std::remove_reference_t<Functor>> adapter{func};
return DispatchFunc<Device::Type,
static_cast<Device::Type>(allowed_devices)...>(
device, adapter, context_str, std::forward<Args>(args)...);
}
// `Device` Multi-Dispatch
template <typename... Lists, typename Functor, typename... Args>
auto DispatchFunc(std::initializer_list<Device::Type> devices, Functor &&func,
std::string_view context_str = "", Args &&...args) {
std::vector<int64_t> v;
for (auto d : devices) v.push_back(static_cast<int64_t>(d));
detail::DeviceMultiAdapter<std::remove_reference_t<Functor>> adapter{func};
return DispatchFunc<Lists...>(v, 0, adapter, context_str, List<>{},
std::forward<Args>(args)...);
}
template <typename ValueType, typename Functor, typename... Args, auto... items>
auto DispatchFuncListAliasImpl(ValueType value, Functor &&func,
std::string_view context_str, List<items...>,
Args &&...args) {
return DispatchFunc<static_cast<std::decay_t<ValueType>>(items)...>(
value, std::forward<Functor>(func), context_str,
std::forward<Args>(args)...);
}
// Interface for Generic `List` Aliases
template <typename ListType, typename ValueType, typename Functor,
typename... Args,
typename = std::enable_if_t<IsListType<ListType>::value>>
auto DispatchFunc(ValueType value, Functor &&func,
std::string_view context_str = "", Args &&...args) {
return DispatchFuncListAliasImpl(value, std::forward<Functor>(func),
context_str, ListType{},
std::forward<Args>(args)...);
}
// Interface for Any `int64_t`-Convertible Types
template <typename... Lists, typename Functor, typename... Args>
auto DispatchFunc(std::initializer_list<int64_t> keys, Functor &&func,
std::string_view context_str = "", Args &&...args) {
std::vector<int64_t> v_keys(keys);
return DispatchFunc<Lists...>(v_keys, 0, std::forward<Functor>(func),
context_str, List<>{},
std::forward<Args>(args)...);
}
} // namespace infini::ops
#endif