10 #include <unordered_map> 11 #include <unordered_set> 20 #include "c10/util/Backtrace.h" 21 #include "c10/util/Half.h" 22 #include "c10/macros/Macros.h" 23 #include "c10/util/C++17.h" 24 #include "c10/util/Exception.h" 25 #include "c10/util/IdWrapper.h" 27 #include "c10/util/Type.h" 48 struct is_fundamental<
at::Half> : std::true_type {
65 friend std::ostream& operator<<(
93 return lhs.underlyingId() < rhs.underlyingId();
96 inline std::ostream& operator<<(
99 return stream << typeId.underlyingId();
119 using PlacementNew = void(
void*,
size_t);
120 using Copy = void(
const void*,
void*,
size_t);
121 using PlacementDelete = void(
void*,
size_t);
122 using Delete = void(
void*);
128 PlacementNew* placementNew,
130 PlacementDelete* placementDelete,
133 const char* name) noexcept
134 : itemsize_(itemsize), new_(newFn), placementNew_(placementNew), copy_(copy), placementDelete_(placementDelete), delete_(deleteFn), id_(
id), name_(name) {}
138 PlacementNew* placementNew_;
140 PlacementDelete* placementDelete_;
150 [[noreturn]] C10_API
void _ThrowRuntimeTypeLogicError(
const std::string& msg);
155 template <
typename T>
157 T* typed_ptr =
static_cast<T*
>(ptr);
158 for (
size_t i = 0; i < n; ++i) {
159 new (typed_ptr + i)
T;
163 template <
typename T>
164 inline void _PlacementNewNotDefault(
void* ,
size_t ) {
165 _ThrowRuntimeTypeLogicError(
166 "Type " + std::string(c10::demangle_type<T>()) +
167 " is not default-constructible.");
172 c10::guts::enable_if_t<std::is_default_constructible<T>::value>* =
nullptr>
173 inline constexpr TypeMetaData::PlacementNew* _PickPlacementNew() {
175 (std::is_fundamental<T>::value || std::is_pointer<T>::value)
182 c10::guts::enable_if_t<!std::is_default_constructible<T>::value>* =
nullptr>
183 inline constexpr TypeMetaData::PlacementNew* _PickPlacementNew() {
184 static_assert(!std::is_fundamental<T>::value && !std::is_pointer<T>::value,
"this should have picked the other SFINAE case");
185 return &_PlacementNewNotDefault<T>;
188 template <
typename T>
189 inline void* _New() {
193 template <
typename T>
194 inline void* _NewNotDefault() {
195 _ThrowRuntimeTypeLogicError(
196 "Type " + std::string(c10::demangle_type<T>()) +
197 " is not default-constructible.");
202 c10::guts::enable_if_t<std::is_default_constructible<T>::value>* =
nullptr>
203 inline constexpr TypeMetaData::New* _PickNew() {
209 c10::guts::enable_if_t<!std::is_default_constructible<T>::value>* =
nullptr>
210 inline constexpr TypeMetaData::New* _PickNew() {
211 return &_NewNotDefault<T>;
217 template <
typename T>
218 inline void _Copy(
const void* src,
void* dst,
size_t n) {
219 const T* typed_src =
static_cast<const T*
>(src);
220 T* typed_dst =
static_cast<T*
>(dst);
221 for (
size_t i = 0; i < n; ++i) {
222 typed_dst[i] = typed_src[i];
229 template <
typename T>
231 _ThrowRuntimeTypeLogicError(
232 "Type " + std::string(c10::demangle_type<T>()) +
233 " does not allow assignment.");
238 c10::guts::enable_if_t<std::is_copy_assignable<T>::value>* =
nullptr 240 inline constexpr TypeMetaData::Copy* _PickCopy() {
242 (std::is_fundamental<T>::value || std::is_pointer<T>::value)
249 c10::guts::enable_if_t<!std::is_copy_assignable<T>::value>* =
nullptr 251 inline constexpr TypeMetaData::Copy* _PickCopy() {
252 static_assert(!std::is_fundamental<T>::value && !std::is_pointer<T>::value,
"this should have picked the other SFINAE case");
253 return &_CopyNotAllowed<T>;
259 template <
typename T>
261 T* typed_ptr =
static_cast<T*
>(ptr);
262 for (
size_t i = 0; i < n; ++i) {
267 template <
typename T>
268 inline constexpr TypeMetaData::PlacementDelete* _PickPlacementDelete() {
270 (std::is_fundamental<T>::value || std::is_pointer<T>::value)
272 : &_PlacementDelete<T>;
275 template <
typename T>
276 inline void _Delete(
void* ptr) {
277 T* typed_ptr =
static_cast<T*
>(ptr);
282 inline constexpr TypeMetaData::Delete* _PickDelete() noexcept {
288 const char* _typeName(
const char* literalName) noexcept {
289 std::ignore = literalName;
295 constexpr
const char* _typeName(
const char* literalName) noexcept {
301 inline TypeMetaData _makeTypeMetaDataInstance(
const char* typeName) {
305 _PickPlacementNew<T>(),
307 _PickPlacementDelete<T>(),
309 TypeIdentifier::Get<T>(),
326 using New = detail::TypeMetaData::New;
327 using PlacementNew = detail::TypeMetaData::PlacementNew;
328 using Copy = detail::TypeMetaData::Copy;
329 using PlacementDelete = detail::TypeMetaData::PlacementDelete;
330 using Delete = detail::TypeMetaData::Delete;
366 return data_->itemsize_;
368 constexpr New* newFn()
const noexcept {
375 return data_->placementNew_;
380 constexpr Copy*
copy() const noexcept {
387 return data_->placementDelete_;
389 constexpr Delete* deleteFn()
const noexcept {
390 return data_->delete_;
395 constexpr
const char*
name() const noexcept {
401 template <
typename T>
402 constexpr
bool Match()
const noexcept {
403 return (*
this == Make<T>());
410 return TypeIdentifier::Get<T>();
414 static const char* TypeName() noexcept {
415 return Make<T>().name();
419 static constexpr
size_t ItemSize() noexcept {
426 template <
typename T>
434 # pragma GCC diagnostic push 435 # pragma GCC diagnostic ignored "-Wpragmas" 436 # pragma GCC diagnostic ignored "-Wunknown-warning-option" 437 # pragma GCC diagnostic ignored "-Wundefined-var-template" 439 return TypeMeta(_typeMetaDataInstance<T>());
441 # pragma GCC diagnostic pop 453 C10_EXPORT
const detail::TypeMetaData* TypeMeta::_typeMetaDataInstance<detail::_Uninitialized>() noexcept;
455 inline TypeMeta::TypeMeta() noexcept : data_(_typeMetaDataInstance<
detail::_Uninitialized>()) {}
458 return (lhs.data_ == rhs.data_);
461 return !operator==(lhs, rhs);
464 inline std::ostream& operator<<(
465 std::ostream& stream,
467 return stream << typeMeta.
name();
489 #if defined(_MSC_VER) || defined(__clang__) 490 #define EXPORT_IF_NOT_GCC C10_EXPORT 492 #define EXPORT_IF_NOT_GCC 495 #define _CAFFE_KNOWN_TYPE_DEFINE_TYPEMETADATA_INSTANCE(T, Counter) \ 497 const TypeMetaData MACRO_CONCAT(_typeMetaDataInstance_, Counter) = \ 498 _makeTypeMetaDataInstance<T>(_typeName<T>(#T)); \ 501 EXPORT_IF_NOT_GCC const detail::TypeMetaData* TypeMeta::_typeMetaDataInstance<T>() noexcept { \ 502 return &MACRO_CONCAT(detail::_typeMetaDataInstance_, Counter); \ 504 #define CAFFE_KNOWN_TYPE(T) \ 506 EXPORT_IF_NOT_GCC TypeIdentifier TypeIdentifier::Get<T>() { \ 507 static const TypeIdentifier type_id = TypeIdentifier::createTypeId(); \ 510 _CAFFE_KNOWN_TYPE_DEFINE_TYPEMETADATA_INSTANCE(T, __COUNTER__) 519 #define CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(PreallocatedId, T) \ 521 inline C10_EXPORT TypeIdentifier TypeIdentifier::Get<T>() { \ 522 return TypeIdentifier(PreallocatedId); \ 525 C10_API extern const TypeMetaData \ 526 MACRO_CONCAT(_typeMetaDataInstance_preallocated_, PreallocatedId); \ 528 #define CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(PreallocatedId, T) \ 530 C10_EXPORT const TypeMetaData \ 531 MACRO_CONCAT(_typeMetaDataInstance_preallocated_, PreallocatedId) \ 532 = _makeTypeMetaDataInstance<T>(_typeName<T>(#T)); \ 535 C10_EXPORT const detail::TypeMetaData* TypeMeta::_typeMetaDataInstance<T>() noexcept { \ 536 return &MACRO_CONCAT(detail::_typeMetaDataInstance_preallocated_, PreallocatedId); \ 539 #define CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(PreallocatedId, T) \ 541 inline C10_EXPORT TypeIdentifier TypeIdentifier::Get<T>() { \ 542 return TypeIdentifier(PreallocatedId); \ 545 C10_EXPORT extern const TypeMetaData \ 546 MACRO_CONCAT(_typeMetaDataInstance_preallocated_, PreallocatedId); \ 549 inline const detail::TypeMetaData* TypeMeta::_typeMetaDataInstance<T>() noexcept { \ 550 return &MACRO_CONCAT(detail::_typeMetaDataInstance_preallocated_, PreallocatedId); \ 552 #define CAFFE_DEFINE_PREALLOCATED_KNOWN_TYPE(PreallocatedId, T) \ 554 const TypeMetaData MACRO_CONCAT(_typeMetaDataInstance_preallocated_, PreallocatedId) \ 555 = _makeTypeMetaDataInstance<T>(_typeName<T>(#T)); \ 564 CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(0, uint8_t)
565 CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(1, int8_t)
566 CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(2, int16_t)
567 CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(3,
int)
568 CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(4, int64_t)
569 CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(5,
at::Half)
570 CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(6,
float)
571 CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(7,
double)
573 CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(9, std::complex<float>)
574 CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(10, std::complex<double>)
577 CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(13, std::string)
578 CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(14,
bool)
579 CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(15, uint16_t)
580 CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(16,
char)
581 CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(17, std::unique_ptr<std::mutex>)
582 CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(18, std::unique_ptr<std::atomic<bool>>)
583 CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(19, std::vector<int32_t>)
584 CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(20, std::vector<int64_t>)
585 CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(21, std::vector<unsigned long>)
586 CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(22,
bool*)
587 CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(23,
char*)
588 CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(24,
int*)
603 using _guard_long_unique = c10::guts::conditional_t<
604 std::is_same<long, int32_t>::value || std::is_same<long, int64_t>::value,
608 CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(25, detail::_guard_long_unique<long>)
609 CAFFE_DECLARE_PREALLOCATED_KNOWN_TYPE(
611 detail::_guard_long_unique<std::vector<long>>)
void _Copy(const void *src, void *dst, size_t n)
Typed copy function for classes.
void _PlacementDelete(void *ptr, size_t n)
Destructor for non-fundamental types.
A type id is a unique id for a given C++ type.
A global dictionary that holds information about what Caffe2 modules have been loaded in the current ...
void _PlacementNew(void *ptr, size_t n)
Placement new function for the type.
Flush-To-Zero and Denormals-Are-Zero mode.
std::string demangle(const char *name)
Utility to demangle a C++ symbol name.
This template simplifies generation of simple classes that wrap an id in a typesafe way...
void _CopyNotAllowed(const void *, void *, size_t)
A placeholder function for types that do not allow assignment.