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 {
_ThrowRuntimeTypeLogicError(const std::string & msg)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
error_unsupported_typemeta(caffe2::TypeMeta dtype)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
getTypeMetaDatasLock()24 std::mutex& TypeMeta::getTypeMetaDatasLock() {
25 static std::mutex lock;
26 return lock;
27 }
28
29 uint16_t TypeMeta::nextTypeIndex(NumScalarTypes);
30
31 // fixed length array of TypeMetaData instances
typeMetaDatas()32 detail::TypeMetaData* TypeMeta::typeMetaDatas() {
33 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
34 static detail::TypeMetaData instances[MaxTypeIndex + 1] = {
35 #define SCALAR_TYPE_META(T, name) \
36 /* ScalarType::name */ \
37 detail::TypeMetaData( \
38 sizeof(T), \
39 detail::_PickNew<T>(), \
40 detail::_PickPlacementNew<T>(), \
41 detail::_PickCopy<T>(), \
42 detail::_PickPlacementDelete<T>(), \
43 detail::_PickDelete<T>(), \
44 TypeIdentifier::Get<T>(), \
45 c10::util::get_fully_qualified_type_name<T>()),
46 AT_FORALL_SCALAR_TYPES_WITH_COMPLEX_AND_QINTS(SCALAR_TYPE_META)
47 #undef SCALAR_TYPE_META
48 // The remainder of the array is padded with TypeMetaData blanks.
49 // The first of these is the entry for ScalarType::Undefined.
50 // The rest are consumed by CAFFE_KNOWN_TYPE entries.
51 };
52 return instances;
53 }
54
existingMetaDataIndexForType(TypeIdentifier identifier)55 uint16_t TypeMeta::existingMetaDataIndexForType(TypeIdentifier identifier) {
56 auto* metaDatas = typeMetaDatas();
57 const auto end = metaDatas + nextTypeIndex;
58 // MaxTypeIndex is not very large; linear search should be fine.
59 auto it = std::find_if(metaDatas, end, [identifier](const auto& metaData) {
60 return metaData.id_ == identifier;
61 });
62 if (it == end) {
63 return MaxTypeIndex;
64 }
65 return static_cast<uint16_t>(it - metaDatas);
66 }
67
68 CAFFE_DEFINE_KNOWN_TYPE(std::string, std_string)
69 CAFFE_DEFINE_KNOWN_TYPE(uint16_t, uint16_t)
70 CAFFE_DEFINE_KNOWN_TYPE(char, char)
71 CAFFE_DEFINE_KNOWN_TYPE(std::unique_ptr<std::mutex>, std_unique_ptr_std_mutex)
72 CAFFE_DEFINE_KNOWN_TYPE(
73 std::unique_ptr<std::atomic<bool>>,
74 std_unique_ptr_std_atomic_bool)
75 CAFFE_DEFINE_KNOWN_TYPE(std::vector<int32_t>, std_vector_int32_t)
76 CAFFE_DEFINE_KNOWN_TYPE(std::vector<int64_t>, std_vector_int64_t)
77 CAFFE_DEFINE_KNOWN_TYPE(std::vector<unsigned long>, std_vector_unsigned_long)
78 CAFFE_DEFINE_KNOWN_TYPE(bool*, bool_ptr)
79 CAFFE_DEFINE_KNOWN_TYPE(char*, char_ptr)
80 CAFFE_DEFINE_KNOWN_TYPE(int*, int_ptr)
81
82 CAFFE_DEFINE_KNOWN_TYPE(
83 detail::_guard_long_unique<long>,
84 detail_guard_long_unique_long);
85 CAFFE_DEFINE_KNOWN_TYPE(
86 detail::_guard_long_unique<std::vector<long>>,
87 detail_guard_long_unique_std_vector_long)
88
89 CAFFE_DEFINE_KNOWN_TYPE(float*, float_ptr)
90 CAFFE_DEFINE_KNOWN_TYPE(at::Half*, at_Half)
91
92 } // namespace caffe2
93