forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interned_strings.h
483 lines (452 loc) · 18.3 KB
/
interned_strings.h
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#pragma once
#include <vector>
#include <cstdint>
#include <string>
#include <unordered_map>
#include <algorithm>
#include <c10/macros/Macros.h>
#if !defined(C10_MOBILE) || defined(FEATURE_TORCH_MOBILE)
#include <ATen/core/aten_interned_strings.h>
#endif
namespace c10 {
#if !defined(C10_MOBILE) || defined(FEATURE_TORCH_MOBILE)
#define FORALL_NS_SYMBOLS(_) \
_(namespaces, prim) \
_(namespaces, aten) \
_(namespaces, onnx) \
_(namespaces, attr) \
_(namespaces, scope) \
_(namespaces, user) \
_(namespaces, _caffe2) \
_(namespaces, dimname) \
_(namespaces, namespaces) \
_(prim, Assign) \
_(prim, BroadcastingChunk) \
_(prim, BroadcastSizes) \
_(prim, Constant) \
_(prim, ChunkSizes) \
_(prim, Drop) \
_(prim, Eval) \
_(prim, Expand) /* onnx */ \
_(prim, FusionGroup) \
_(prim, CudaFusionGroup) \
_(prim, FunctionalGraph) \
_(prim, DifferentiableGraph) \
_(prim, If) \
_(prim, Jump) /* debug */ \
_(prim, JumpNZ) /* debug */ \
_(prim, JumpZ) /* debug */ \
_(prim, Load) \
_(prim, Loop) \
_(prim, Param) \
_(prim, PackPadded) /* onnx */ \
_(prim, PadPacked) /* onnx */ \
_(prim, Placeholder) /* debug */ \
_(prim, Print) \
_(prim, PythonOp) \
_(prim, IgnoredPythonOp) \
_(prim, Reverse) \
_(prim, Return) \
_(prim, ReturnStmt) \
_(prim, BreakStmt) \
_(prim, ContinueStmt) \
_(prim, LocalVariableScope) \
_(prim, Store) \
_(prim, AutogradZero) \
_(prim, AutogradAnyNonZero) \
_(prim, Starred) \
_(prim, TupleConstruct) \
_(prim, TupleUnpack) \
_(prim, TupleIndex) \
_(prim, TupleSlice) \
_(prim, ListConstruct) \
_(prim, ListUnpack) \
_(prim, DictConstruct) \
_(prim, EnumName) \
_(prim, EnumValue) \
_(prim, StringIndex) \
_(prim, NumToTensor) \
_(prim, Uninitialized) \
_(prim, With) \
_(prim, Enter) \
_(prim, Exit) \
_(aten, Bool) \
_(aten, Int) \
_(aten, FloatImplicit) \
_(aten, IntImplicit) \
_(aten, ScalarImplicit) \
_(aten, Float) \
_(aten, str) \
_(aten, Delete) \
_(prim, device) \
_(prim, dtype) \
_(prim, layout) \
_(prim, id) \
_(prim, requires_grad) \
_(prim, MakeTestTensor) /* test */ \
_(prim, AutogradAdd) \
_(prim, GradOf) \
_(aten, grad) \
_(aten, backward) \
_(prim, Guard) \
_(prim, BailOut) \
_(prim, TypeCheck) \
_(prim, FusedConcat) \
_(prim, ConstantChunk) \
_(prim, MMTreeReduce) \
_(prim, MMBatchSide) \
_(prim, min) \
_(prim, max) \
_(prim, abs) \
_(aten, divmod) \
_(prim, zip) \
_(prim, enumerate) \
_(prim, range) \
_(prim, rangelist) \
_(prim, isinstance) \
_(prim, tolist) \
_(prim, unchecked_cast) \
_(aten, _grad_sum_to_size) \
_(aten, _size_if_not_equal) \
_(aten, _ncf_unsqueeze) \
_(aten, warn) \
_(aten, sorted) \
_(aten, floordiv) \
_(aten, __range_length) \
_(aten, __derive_index) \
_(aten, __round_to_zero_floordiv) \
_(aten, is_scripting) \
_(aten, _unwrap_optional) \
_(prim, fork) \
_(prim, forkClosure) \
_(prim, RaiseException) \
_(prim, Function) \
_(prim, CreateObject) \
_(prim, SetAttr) \
_(prim, GetAttr) \
_(prim, HasAttr) \
_(prim, profile) \
_(prim, AddStatValue) \
_(prim, TimePoint) \
_(prim, CallFunction) \
_(prim, CallMethod) \
_(prim, LoopContinuation) \
_(prim, annotate) \
_(prim, TracedModuleForward) \
_(prim, TracedFork) \
_(prim, TracedAttr) \
_(prim, rpc_async) \
_(prim, is_cuda) \
_(aten, abs_) \
_(aten, absolute) \
_(aten, absolute_) \
_(aten, acos) \
_(aten, acos_) \
_(aten, arccos) \
_(aten, arccos_) \
_(aten, acosh) \
_(aten, acosh_) \
_(aten, arccosh) \
_(aten, arccosh_) \
_(aten, asin) \
_(aten, asin_) \
_(aten, arcsin) \
_(aten, arcsin_) \
_(aten, atan) \
_(aten, atan_) \
_(aten, arctan) \
_(aten, arctan_) \
_(aten, clamp) \
_(aten, clamp_) \
_(aten, clip) \
_(aten, clip_) \
_(aten, det) \
_(aten, linalg_det) \
_(aten, append) \
_(aten, item) \
_(aten, format) \
_(aten, __not__) \
_(aten, __is__) \
_(aten, __isnot__) \
_(aten, copy) \
_(aten, copy_) \
_(aten, t_) \
_(aten, addbmm_) \
_(aten, addcdiv_) \
_(aten, addcmul_) \
_(aten, addmv_) \
_(aten, addr_) \
_(aten, baddbmm_) \
_(aten, ge_) \
_(aten, gt_) \
_(aten, le_) \
_(aten, lerp_) \
_(aten, lt_) \
_(aten, ne_) \
_(aten, _ger) \
_(aten, ger) \
_(aten, outer) \
_(aten, transpose_) \
_(aten, unsqueeze_) \
_(aten, __getitem__) \
_(aten, _set_item) \
_(aten, manual_seed) \
_(aten, set_) \
_(aten, index_put_) \
_(aten, device) \
_(aten, hash) \
_(aten, len) \
_(aten, list) \
_(aten, wait) \
_(aten, save) \
_(aten, keys) \
_(aten, ord) \
_(aten, chr) \
_(aten, hex) \
_(aten, oct) \
_(aten, clear) \
_(aten, trunc) \
_(aten, trunc_) \
_(aten, fix) \
_(aten, fix_) \
_(aten, neg) \
_(aten, neg_) \
_(aten, negative) \
_(aten, negative_) \
_(aten, setdefault) \
_(aten, bin) \
_(aten, pop) \
_(aten, insert) \
_(prim, unchecked_unwrap_optional) \
_(aten, __contains__) \
_(prim, BailoutTemplate) \
_(prim, grad) \
_(aten, zero_) \
_(aten, fill_) \
FORALL_ATEN_BASE_SYMBOLS(_) \
_(onnx, Add) \
_(onnx, Concat) \
_(onnx, Constant) \
_(onnx, ConstantFill) \
_(onnx, Div) \
_(onnx, GRU) \
_(onnx, Gather) \
_(onnx, Gemm) \
_(onnx, LSTM) \
_(onnx, Mul) \
_(onnx, Pow) \
_(onnx, RNN) \
_(onnx, Shape) \
_(onnx, Size) \
_(onnx, Slice) \
_(onnx, Squeeze) \
_(onnx, Sub) \
_(onnx, Transpose) \
_(onnx, Unsqueeze) \
_(onnx, Loop) \
_(onnx, If) \
_(onnx, Reshape) \
_(onnx, Expand) \
_(onnx, Equal) \
_(onnx, Greater) \
_(onnx, GreaterOrEqual) \
_(onnx, Less) \
_(onnx, LessOrEqual) \
_(onnx, Not) \
_(onnx, ATen) \
_(onnx, Split) \
_(onnx, ConstantOfShape) \
_(onnx, Cast) \
_(onnx, Mod) \
_(onnx, Sqrt) \
_(onnx, SplitToSequence) \
_(onnx, SequenceAt) \
_(onnx, SequenceConstruct) \
_(onnx, SequenceEmpty) \
_(onnx, SequenceInsert) \
_(onnx, ConcatFromSequence) \
_(onnx, Identity) \
_(onnx, SoftmaxCrossEntropyLoss) \
_(onnx, NegativeLogLikelihoodLoss) \
_(onnx, LogSoftmax) \
_(onnx, ReduceL1) \
_(onnx, ReduceL2) \
_(onnx, Conv) \
_(onnx, BatchNormalization) \
FORALL_ATTR_BASE_SYMBOLS(_) \
_(attr, Subgraph) \
_(attr, ReverseSubgraph) \
_(attr, f_real_outputs) \
_(attr, df_input_vjps) \
_(attr, df_input_captured_inputs) \
_(attr, df_input_captured_outputs) \
_(attr, df_output_vjps) \
_(attr, axes) \
_(attr, axis) \
_(attr, broadcast) \
_(attr, direction) \
_(attr, ends) \
_(attr, inplace) \
_(attr, input_as_shape) \
_(attr, is_zero) \
_(attr, perm) \
_(attr, sizes) \
_(attr, starts) \
_(attr, profiled_type) \
_(attr, transA) \
_(attr, transB) \
_(attr, name) \
_(attr, a) \
_(attr, b) \
_(attr, beg) \
_(attr, idx) \
_(attr, split) \
_(attr, slot) \
_(attr, kinds) \
_(attr, types) \
_(attr, scope) \
_(attr, keepdims) \
_(attr, cache_id) \
_(attr, new_axis)
#else
#define FORALL_NS_SYMBOLS(_) \
_(namespaces, prim) \
_(namespaces, aten) \
_(namespaces, onnx) \
_(namespaces, attr) \
_(namespaces, scope) \
_(namespaces, user) \
_(namespaces, _caffe2) \
_(namespaces, dimname) \
_(namespaces, namespaces)
#endif
// 'prim' symbols are synthetic operators that occur only in the IR
// and don't have corresponding implementations in ATen.
// 'onnx' symbols correspond to ONNX operators. Their semantics
// are defined in https://github.com/onnx/onnx/blob/master/docs/Operators.md
// The particular version we are targeting is specified by '_onnx_opset_version'
// in torch.onnx.symbolic_helper
//
// In general, most ONNX operators won't get an entry here, because they
// are handled from the Python end. However, you may occasionally need
// to intern an ONNX symbol here so that you can conveniently write an
// optimization on ONNX operations.
// 'attr' symbols are attribute keys. They are shared between both ONNX and ATen
// operators (you disambiguate their meaning by looking at the operator itself).
// In general, you only need to define attribute keys that are used by
// onnx or prim; ATen attributes are automatically generated in FORALL_ATTR_BASE_SYMBOLS.
// Note [Symbol allocation]
// ~~~~~~~~~~~~~~~~~~~~~~~~
//
// 1. Symbol namespace is split up into namespaces.
//
// 2. The intended access pattern for built-in symbols is onnx::MatMul
// in the c10 namespace (this is a Symbol).
//
// Built-in constant definition strategy:
// - Enum is the most convenient way to generate a contiguous sequence
// of numbers for an identifier.
// - However, an enum gives you a fresh type. We want onnx::MatMul to
// be type Symbol, not some random enum type!
// - Therefore, after using enums to generate the sequence of integers,
// we then declare constexpr Symbols to get everything the actual Symbol
// type we want. Symbols must be constexpr to be valid to be "case"ed on.
using unique_t = uint32_t;
const std::string& domain_prefix();
// A Symbol is like an interned string, but with a little extra
// structure; it is namespaced via SymbolNamespace and the resulting
// intern pointers support efficient namespace testing.
struct CAFFE2_API Symbol {
explicit constexpr Symbol() : value(0) {};
explicit constexpr Symbol(unique_t uniq)
: value(uniq) {}
// Get a Symbol for a qualified string like "attr::bar"
static Symbol fromQualString(const std::string & s);
// Get a Symbol from a domain and an unqualified string like "org.pytorch.attr" and "bar"
static Symbol fromDomainAndUnqualString(const std::string & d, const std::string & s);
// Constructors for our various namespaced strings. This will construct
// the appropriate namespaced string, e.g., "attr::foo" for the
// argument "foo", and then attempt to intern it. DO NOT USE THIS
// with a string literal; attr::foo should be available in that case
// (and if it's not, you should add it to the built-ins list above.)
static Symbol attr(const std::string & s);
static Symbol aten(const std::string & s);
static Symbol onnx(const std::string & s);
static Symbol prim(const std::string & s);
static Symbol user(const std::string & s);
static Symbol caffe2(const std::string & s);
static Symbol dimname(const std::string & s);
// TODO: eliminate me
static Symbol scope(const std::string & s);
bool is_attr() const;
bool is_aten() const;
bool is_prim() const;
bool is_onnx() const;
bool is_user() const;
bool is_caffe2() const;
bool is_dimname() const;
// So we can switch on this
constexpr operator unique_t() const {
return value;
}
Symbol ns() const;
// Give a string corresponding to the unqualified version of this name, e.g.,
// "mm". Use this in a context where the intended namespace of the string is
// obvious; this is a *lossy* conversion.
const char * toUnqualString() const;
// Give a string corresponding to the qualified version of this name,
// e.g., "aten::mm". This string format is made available to Python bindings
// (so we know how to parse it.)
const char * toQualString() const;
// This describes a symbol in a case where humans read it. At the moment it's
// the same as toQualString. This has to be a const char* returned because
// a lot of printf style macros use it.
const char * toDisplayString() const;
// Give a string corresponding to the domain name for the symbol,
// e.g., "org.pytorch.aten".
std::string domainString() const;
private:
explicit Symbol(Symbol ns, const std::string & s);
unique_t value;
};
static inline bool operator==(Symbol lhs, Symbol rhs) {
return static_cast<unique_t>(lhs) == static_cast<unique_t>(rhs);
}
enum class _keys : unique_t {
#define DEFINE_KEY(ns, s) ns##_##s,
FORALL_NS_SYMBOLS(DEFINE_KEY)
#undef DEFINE_KEY
num_symbols
};
#define DEFINE_SYMBOL(s) \
constexpr Symbol s(static_cast<unique_t>(_keys::s));
#undef DEFINE_SYMBOL
#define DEFINE_SYMBOL(ns, s) \
namespace ns { constexpr Symbol s(static_cast<unique_t>(_keys::ns##_##s)); }
FORALL_NS_SYMBOLS(DEFINE_SYMBOL)
#undef DEFINE_SYMBOL
inline Symbol Symbol::attr(const std::string & s) { return Symbol::fromQualString("attr::" + s); }
inline Symbol Symbol::aten(const std::string & s) { return Symbol::fromQualString("aten::" + s); }
inline Symbol Symbol::onnx(const std::string & s) { return Symbol::fromQualString("onnx::" + s); }
inline Symbol Symbol::prim(const std::string & s) { return Symbol::fromQualString("prim::" + s); }
inline Symbol Symbol::scope(const std::string & s) { return Symbol::fromQualString("scope::" + s); }
inline Symbol Symbol::user(const std::string & s) { return Symbol::fromQualString("user::" + s); }
inline Symbol Symbol::caffe2(const std::string & s) { return Symbol::fromQualString("_caffe2::" + s); }
inline Symbol Symbol::dimname(const std::string & s) { return Symbol::fromQualString("dimname::" + s); }
inline bool Symbol::is_attr() const { return ns() == namespaces::attr; }
inline bool Symbol::is_aten() const { return ns() == namespaces::aten; }
inline bool Symbol::is_prim() const { return ns() == namespaces::prim; }
inline bool Symbol::is_onnx() const { return ns() == namespaces::onnx; }
inline bool Symbol::is_user() const { return ns() == namespaces::user; }
inline bool Symbol::is_caffe2() const { return ns() == namespaces::_caffe2; }
inline bool Symbol::is_dimname() const { return ns() == namespaces::dimname; }
} // namespace c10
// make symbol behave like an integer in hash tables
namespace std {
template <>
struct hash<c10::Symbol> {
size_t operator()(c10::Symbol s) const {
return std::hash<uint32_t>()(static_cast<uint32_t>(s));
}
};
}