|
1 | 1 | #pragma once
|
2 |
| - |
3 |
| -#include "ATen/ATenGeneral.h" |
4 |
| -#include "ATen/core/ArrayRef.h" |
5 |
| -#include "ATen/core/Half.h" |
6 |
| - |
7 |
| -#include <cstdint> |
8 |
| -#include <iostream> |
9 |
| - |
10 |
| -namespace at { |
11 |
| - |
12 |
| -// NB: Order matters for this macro; it is relied upon in |
13 |
| -// _promoteTypesLookup and the serialization format. |
14 |
| -#define AT_FORALL_SCALAR_TYPES(_) \ |
15 |
| -_(uint8_t,Byte,i) /* 0 */ \ |
16 |
| -_(int8_t,Char,i) /* 1 */ \ |
17 |
| -_(int16_t,Short,i) /* 2 */ \ |
18 |
| -_(int,Int,i) /* 3 */ \ |
19 |
| -_(int64_t,Long,i) /* 4 */ \ |
20 |
| -_(at::Half,Half,d) /* 5 */ \ |
21 |
| -_(float,Float,d) /* 6 */ \ |
22 |
| -_(double,Double,d) /* 7 */ |
23 |
| - |
24 |
| -#define AT_FORALL_SCALAR_TYPES_EXCEPT_HALF(_) \ |
25 |
| -_(uint8_t,Byte,i) \ |
26 |
| -_(int8_t,Char,i) \ |
27 |
| -_(int16_t,Short,i) \ |
28 |
| -_(int,Int,i) \ |
29 |
| -_(int64_t,Long,i) \ |
30 |
| -_(float,Float,d) \ |
31 |
| -_(double,Double,d) |
32 |
| - |
33 |
| -enum class ScalarType { |
34 |
| -#define DEFINE_ENUM(_1,n,_2) \ |
35 |
| - n, |
36 |
| - AT_FORALL_SCALAR_TYPES(DEFINE_ENUM) |
37 |
| -#undef DEFINE_ENUM |
38 |
| - Undefined, // 8 |
39 |
| - NumOptions |
40 |
| -}; |
41 |
| - |
42 |
| -enum class Backend { |
43 |
| - CPU, |
44 |
| - CUDA, |
45 |
| - SparseCPU, |
46 |
| - SparseCUDA, |
47 |
| - Undefined, |
48 |
| - NumOptions |
49 |
| -}; |
50 |
| - |
51 |
| -constexpr Backend kCPU = Backend::CPU; |
52 |
| -constexpr Backend kCUDA = Backend::CUDA; |
53 |
| -constexpr Backend kSparseCPU = Backend::SparseCPU; |
54 |
| -constexpr Backend kSparseCUDA = Backend::SparseCUDA; |
55 |
| - |
56 |
| -static inline Backend toSparse(Backend b) { |
57 |
| - switch (b) { |
58 |
| - case Backend::CPU: return Backend::SparseCPU; |
59 |
| - case Backend::CUDA: return Backend::SparseCUDA; |
60 |
| - case Backend::SparseCPU: return Backend::SparseCPU; |
61 |
| - case Backend::SparseCUDA: return Backend::SparseCUDA; |
62 |
| - default: throw std::runtime_error("Unknown backend"); |
63 |
| - } |
64 |
| -} |
65 |
| - |
66 |
| -static inline Backend toDense(Backend b) { |
67 |
| - switch (b) { |
68 |
| - case Backend::CPU: return Backend::CPU; |
69 |
| - case Backend::CUDA: return Backend::CUDA; |
70 |
| - case Backend::SparseCPU: return Backend::CPU; |
71 |
| - case Backend::SparseCUDA: return Backend::CUDA; |
72 |
| - default: throw std::runtime_error("Unknown backend"); |
73 |
| - } |
74 |
| -} |
75 |
| - |
76 |
| -static inline const char * toString(Backend b) { |
77 |
| - switch(b) { |
78 |
| - case Backend::CPU: return "CPU"; |
79 |
| - case Backend::CUDA: return "CUDA"; |
80 |
| - case Backend::SparseCPU: return "SparseCPU"; |
81 |
| - case Backend::SparseCUDA: return "SparseCUDA"; |
82 |
| - default: return "UNKNOWN_BACKEND"; |
83 |
| - } |
84 |
| -} |
85 |
| - |
86 |
| -#define DEFINE_CONSTANT(_,name,_2) \ |
87 |
| -constexpr ScalarType k##name = ScalarType::name; |
88 |
| - |
89 |
| -AT_FORALL_SCALAR_TYPES(DEFINE_CONSTANT) |
90 |
| -#undef DEFINE_CONSTANT |
91 |
| - |
92 |
| -static inline const char * toString(ScalarType t) { |
93 |
| -#define DEFINE_CASE(_,name,_2) \ |
94 |
| - case ScalarType:: name : return #name; |
95 |
| - |
96 |
| - switch(t) { |
97 |
| - AT_FORALL_SCALAR_TYPES(DEFINE_CASE) |
98 |
| - default: |
99 |
| - return "UNKNOWN_SCALAR"; |
100 |
| - } |
101 |
| -#undef DEFINE_CASE |
102 |
| -} |
103 |
| - |
104 |
| -static inline size_t elementSize(ScalarType t) { |
105 |
| -#define CASE_ELEMENTSIZE_CASE(ctype,name,_2) \ |
106 |
| - case ScalarType:: name : return sizeof(ctype); |
107 |
| - |
108 |
| - switch(t) { |
109 |
| - AT_FORALL_SCALAR_TYPES(CASE_ELEMENTSIZE_CASE) |
110 |
| - default: |
111 |
| - AT_ERROR("Unknown ScalarType"); |
112 |
| - } |
113 |
| -#undef CASE_ELEMENTSIZE_CASE |
114 |
| -} |
115 |
| - |
116 |
| -static inline bool isIntegralType(ScalarType t) { |
117 |
| - return (t == ScalarType::Byte || |
118 |
| - t == ScalarType::Char || |
119 |
| - t == ScalarType::Int || |
120 |
| - t == ScalarType::Long || |
121 |
| - t == ScalarType::Short); |
122 |
| -} |
123 |
| - |
124 |
| -static inline bool isFloatingType(ScalarType t) { |
125 |
| - return (t == ScalarType::Double || |
126 |
| - t == ScalarType::Float || |
127 |
| - t == ScalarType::Half); |
128 |
| -} |
129 |
| - |
130 |
| -static inline ScalarType promoteTypes(ScalarType a, ScalarType b) { |
131 |
| - // This is generated according to NumPy's promote_types |
132 |
| - constexpr auto u1 = ScalarType::Byte; |
133 |
| - constexpr auto i1 = ScalarType::Char; |
134 |
| - constexpr auto i2 = ScalarType::Short; |
135 |
| - constexpr auto i4 = ScalarType::Int; |
136 |
| - constexpr auto i8 = ScalarType::Long; |
137 |
| - constexpr auto f2 = ScalarType::Half; |
138 |
| - constexpr auto f4 = ScalarType::Float; |
139 |
| - constexpr auto f8 = ScalarType::Double; |
140 |
| - constexpr auto ud = ScalarType::Undefined; |
141 |
| - static constexpr ScalarType _promoteTypesLookup |
142 |
| - [static_cast<int>(ScalarType::NumOptions)] |
143 |
| - [static_cast<int>(ScalarType::NumOptions)] = { |
144 |
| - /* u1 i1 i2 i4 i8 f2 f4 f8, ud */ |
145 |
| - /* u1 */ { u1, i2, i2, i4, i8, f2, f4, f8, ud }, |
146 |
| - /* i1 */ { i2, i1, i2, i4, i8, f2, f4, f8, ud }, |
147 |
| - /* i2 */ { i2, i2, i2, i4, i8, f4, f4, f8, ud }, |
148 |
| - /* i4 */ { i4, i4, i4, i4, i8, f8, f4, f8, ud }, |
149 |
| - /* i8 */ { i8, i8, i8, i8, i8, f8, f4, f8, ud }, |
150 |
| - /* f2 */ { f2, f2, f4, f8, f8, f2, f4, f8, ud }, |
151 |
| - /* f4 */ { f4, f4, f4, f4, f4, f4, f4, f8, ud }, |
152 |
| - /* f8 */ { f8, f8, f8, f8, f8, f8, f8, f8, ud }, |
153 |
| - /* ud */ { ud, ud, ud, ud, ud, ud, ud, ud, ud }, |
154 |
| - }; |
155 |
| - return _promoteTypesLookup[static_cast<int>(a)][static_cast<int>(b)]; |
156 |
| -} |
157 |
| - |
158 |
| -struct Tensor; |
159 |
| -typedef ArrayRef<int64_t> IntList; |
160 |
| -typedef ArrayRef<Tensor> TensorList; |
161 |
| - |
162 |
| -} // namespace at |
163 |
| - |
164 |
| -inline std::ostream& operator<<( |
165 |
| - std::ostream& stream, |
166 |
| - at::ScalarType scalar_type) { |
167 |
| - return stream << at::toString(scalar_type); |
168 |
| -} |
| 2 | +#include <ATen/ATenGeneral.h> // for BC reasons |
| 3 | +#include <ATen/Backend.h> |
| 4 | +#include <ATen/core/ScalarType.h> |
0 commit comments