Load.h 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. #pragma once
  3. #include <c10/macros/Macros.h>
  4. #include <cstring>
  5. namespace c10 {
  6. namespace detail {
  7. template <typename T>
  8. struct LoadImpl {
  9. C10_HOST_DEVICE static T apply(const void* src) {
  10. return *reinterpret_cast<const T*>(src);
  11. }
  12. };
  13. template <>
  14. struct LoadImpl<bool> {
  15. C10_HOST_DEVICE static bool apply(const void* src) {
  16. static_assert(sizeof(bool) == sizeof(char));
  17. // NOTE: [Loading boolean values]
  18. // Protect against invalid boolean values by loading as a byte
  19. // first, then converting to bool (see gh-54789).
  20. return *reinterpret_cast<const unsigned char*>(src);
  21. }
  22. };
  23. } // namespace detail
  24. template <typename T>
  25. C10_HOST_DEVICE constexpr T load(const void* src) {
  26. return c10::detail::LoadImpl<T>::apply(src);
  27. }
  28. template <typename scalar_t>
  29. C10_HOST_DEVICE constexpr scalar_t load(const scalar_t* src) {
  30. return c10::detail::LoadImpl<scalar_t>::apply(src);
  31. }
  32. } // namespace c10
  33. #else
  34. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  35. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)