NumericLimits.cuh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #if !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)
  2. #pragma once
  3. #include <limits>
  4. // at::numeric_limits is a historical artifact which was needed for ROCm HIP
  5. // because std::numeric_limits functions are not marked __device__ and did not
  6. // work with ROCm. This is no longer the case according to the discussion on
  7. // #50902 and #52058.
  8. //
  9. // This header cannot be removed because lower_bound/upper_bound functions are
  10. // not present in std::numeric_limits.
  11. //
  12. // The lower_bound and upper_bound constants are same as lowest and max for
  13. // integral types, but are -inf and +inf for floating point types. They are
  14. // useful in implementing min, max, etc.
  15. namespace at {
  16. template <typename T>
  17. struct numeric_limits {
  18. static inline __host__ __device__ T lowest() {
  19. return std::numeric_limits<T>::lowest();
  20. }
  21. static inline __host__ __device__ T max() {
  22. return std::numeric_limits<T>::max();
  23. }
  24. static inline __host__ __device__ T lower_bound() {
  25. if constexpr (std::numeric_limits<T>::has_infinity) {
  26. return -std::numeric_limits<T>::infinity();
  27. } else {
  28. return std::numeric_limits<T>::lowest();
  29. }
  30. }
  31. static inline __host__ __device__ T upper_bound() {
  32. if constexpr (std::numeric_limits<T>::has_infinity) {
  33. return std::numeric_limits<T>::infinity();
  34. } else {
  35. return std::numeric_limits<T>::max();
  36. }
  37. }
  38. };
  39. } // namespace at
  40. #else
  41. #error "This file should not be included when either TORCH_STABLE_ONLY or TORCH_TARGET_VERSION is defined."
  42. #endif // !defined(TORCH_STABLE_ONLY) && !defined(TORCH_TARGET_VERSION)