1 | #include <c10/util/Exception.h> |
2 | #include <c10/util/typeid.h> |
3 | |
4 | #include <algorithm> |
5 | #include <atomic> |
6 | |
7 | namespace caffe2 { |
8 | namespace detail { |
9 | C10_EXPORT void _ThrowRuntimeTypeLogicError(const std::string& msg) { |
10 | // In earlier versions it used to be std::abort() but it's a bit hard-core |
11 | // for a library |
12 | TORCH_CHECK(false, msg); |
13 | } |
14 | } // namespace detail |
15 | |
16 | [[noreturn]] void TypeMeta::error_unsupported_typemeta(caffe2::TypeMeta dtype) { |
17 | TORCH_CHECK( |
18 | false, |
19 | "Unsupported TypeMeta in ATen: " , |
20 | dtype, |
21 | " (please report this error)" ); |
22 | } |
23 | |
24 | // see TypeMeta::addTypeMetaData |
25 | std::mutex TypeMeta::typeMetaDatasLock; |
26 | uint16_t TypeMeta::nextTypeIndex(NumScalarTypes); |
27 | |
28 | // fixed length array of TypeMetaData instances |
29 | detail::TypeMetaData* TypeMeta::typeMetaDatas() { |
30 | // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays) |
31 | static detail::TypeMetaData instances[MaxTypeIndex + 1] = { |
32 | #define SCALAR_TYPE_META(T, name) \ |
33 | /* ScalarType::name */ \ |
34 | detail::TypeMetaData( \ |
35 | sizeof(T), \ |
36 | detail::_PickNew<T>(), \ |
37 | detail::_PickPlacementNew<T>(), \ |
38 | detail::_PickCopy<T>(), \ |
39 | detail::_PickPlacementDelete<T>(), \ |
40 | detail::_PickDelete<T>(), \ |
41 | TypeIdentifier::Get<T>(), \ |
42 | c10::util::get_fully_qualified_type_name<T>()), |
43 | AT_FORALL_SCALAR_TYPES_WITH_COMPLEX_AND_QINTS(SCALAR_TYPE_META) |
44 | #undef SCALAR_TYPE_META |
45 | // The remainder of the array is padded with TypeMetaData blanks. |
46 | // The first of these is the entry for ScalarType::Undefined. |
47 | // The rest are consumed by CAFFE_KNOWN_TYPE entries. |
48 | }; |
49 | return instances; |
50 | } |
51 | |
52 | uint16_t TypeMeta::existingMetaDataIndexForType(TypeIdentifier identifier) { |
53 | auto* metaDatas = typeMetaDatas(); |
54 | const auto end = metaDatas + nextTypeIndex; |
55 | // MaxTypeIndex is not very large; linear search should be fine. |
56 | auto it = std::find_if(metaDatas, end, [identifier](const auto& metaData) { |
57 | return metaData.id_ == identifier; |
58 | }); |
59 | if (it == end) { |
60 | return MaxTypeIndex; |
61 | } |
62 | return static_cast<uint16_t>(it - metaDatas); |
63 | } |
64 | |
65 | CAFFE_DEFINE_KNOWN_TYPE(std::string) |
66 | CAFFE_DEFINE_KNOWN_TYPE(uint16_t) |
67 | CAFFE_DEFINE_KNOWN_TYPE(char) |
68 | CAFFE_DEFINE_KNOWN_TYPE(std::unique_ptr<std::mutex>) |
69 | CAFFE_DEFINE_KNOWN_TYPE(std::unique_ptr<std::atomic<bool>>) |
70 | CAFFE_DEFINE_KNOWN_TYPE(std::vector<int32_t>) |
71 | CAFFE_DEFINE_KNOWN_TYPE(std::vector<int64_t>) |
72 | CAFFE_DEFINE_KNOWN_TYPE(std::vector<unsigned long>) |
73 | CAFFE_DEFINE_KNOWN_TYPE(bool*) |
74 | CAFFE_DEFINE_KNOWN_TYPE(char*) |
75 | CAFFE_DEFINE_KNOWN_TYPE(int*) |
76 | |
77 | CAFFE_DEFINE_KNOWN_TYPE(detail::_guard_long_unique<long>); |
78 | CAFFE_DEFINE_KNOWN_TYPE(detail::_guard_long_unique<std::vector<long>>) |
79 | |
80 | CAFFE_DEFINE_KNOWN_TYPE(float*) |
81 | CAFFE_DEFINE_KNOWN_TYPE(at::Half*) |
82 | |
83 | } // namespace caffe2 |
84 | |