/
usr
/
local
/
lib64
/
python3.6
/
site-packages
/
torch
/
include
/
c10
/
util
/
/usr/local/lib64/python3.6/site-packages/torch/include/c10/util
mkdir
upload
Name
Size
Mode
Actions
accumulate.h
4205
0644
edit
dl
rm
AlignOf.h
4835
0644
edit
dl
rm
Array.h
11354
0644
edit
dl
rm
ArrayRef.h
9058
0644
edit
dl
rm
Backtrace.h
364
0644
edit
dl
rm
BFloat16-inl.h
9222
0644
edit
dl
rm
BFloat16-math.h
5217
0644
edit
dl
rm
BFloat16.h
2372
0644
edit
dl
rm
Bitset.h
3414
0644
edit
dl
rm
C++17.h
13329
0644
edit
dl
rm
complex.h
17542
0644
edit
dl
rm
complex_math.h
10902
0644
edit
dl
rm
complex_utils.h
958
0644
edit
dl
rm
ConstexprCrc.h
6633
0644
edit
dl
rm
copysign.h
866
0644
edit
dl
rm
DeadlockDetection.h
1920
0644
edit
dl
rm
Deprecated.h
3579
0644
edit
dl
rm
either.h
6423
0644
edit
dl
rm
env.h
835
0644
edit
dl
rm
Exception.h
24723
0644
edit
dl
rm
ExclusivelyOwned.h
4759
0644
edit
dl
rm
Flags.h
10056
0644
edit
dl
rm
flat_hash_map.h
61655
0644
edit
dl
rm
FunctionRef.h
2301
0644
edit
dl
rm
Half-inl.h
8674
0644
edit
dl
rm
Half.h
18965
0644
edit
dl
rm
hash.h
5084
0644
edit
dl
rm
IdWrapper.h
2348
0644
edit
dl
rm
intrusive_ptr.h
35563
0644
edit
dl
rm
in_place.h
350
0644
edit
dl
rm
irange.h
2679
0644
edit
dl
rm
LeftRight.h
6016
0644
edit
dl
rm
llvmMathExtras.h
29168
0644
edit
dl
rm
Logging.h
11256
0644
edit
dl
rm
logging_is_google_glog.h
2031
0644
edit
dl
rm
logging_is_not_google_glog.h
8271
0644
edit
dl
rm
MathConstants.h
858
0644
edit
dl
rm
math_compat.h
7296
0644
edit
dl
rm
MaybeOwned.h
6689
0644
edit
dl
rm
Metaprogramming.h
15288
0644
edit
dl
rm
numa.h
696
0644
edit
dl
rm
Optional.h
35592
0644
edit
dl
rm
order_preserving_flat_hash_map.h
65482
0644
edit
dl
rm
overloaded.h
709
0644
edit
dl
rm
python_stub.h
56
0644
edit
dl
rm
qint8.h
472
0644
edit
dl
rm
qint32.h
319
0644
edit
dl
rm
quint4x2.h
366
0644
edit
dl
rm
quint8.h
320
0644
edit
dl
rm
Registry.h
12242
0644
edit
dl
rm
reverse_iterator.h
8796
0644
edit
dl
rm
ScopeExit.h
1345
0644
edit
dl
rm
signal_handler.h
3154
0644
edit
dl
rm
SmallBuffer.h
1243
0644
edit
dl
rm
SmallVector.h
34456
0644
edit
dl
rm
sparse_bitset.h
26511
0644
edit
dl
rm
StringUtil.h
4538
0644
edit
dl
rm
string_utils.h
3989
0644
edit
dl
rm
string_view.h
20189
0644
edit
dl
rm
tempfile.h
6029
0644
edit
dl
rm
ThreadLocal.h
3883
0644
edit
dl
rm
ThreadLocalDebugInfo.h
2603
0644
edit
dl
rm
thread_name.h
148
0644
edit
dl
rm
Type.h
607
0644
edit
dl
rm
TypeCast.h
6972
0644
edit
dl
rm
typeid.h
18793
0644
edit
dl
rm
TypeIndex.h
5251
0644
edit
dl
rm
TypeList.h
16901
0644
edit
dl
rm
TypeTraits.h
5368
0644
edit
dl
rm
Unicode.h
295
0644
edit
dl
rm
UniqueVoidPtr.h
4117
0644
edit
dl
rm
Unroll.h
667
0644
edit
dl
rm
variant.h
95182
0644
edit
dl
rm
win32-headers.h
858
0644
edit
dl
rm
Edit:
/usr/local/lib64/python3.6/site-packages/torch/include/c10/util/TypeCast.h
(6972B)
#pragma once #include <c10/core/ScalarType.h> #include <c10/macros/Macros.h> #include <c10/util/BFloat16.h> #include <c10/util/Half.h> #include <type_traits> namespace c10 { template <typename dest_t, typename src_t> struct needs_real { constexpr static bool value = (is_complex<src_t>::value && !is_complex<dest_t>::value); }; template <bool, typename src_t> struct maybe_real { C10_HOST_DEVICE static inline src_t apply(src_t src) { return src; } }; template <typename src_t> struct maybe_real<true, src_t> { C10_HOST_DEVICE static inline decltype(auto) apply(src_t src) { return src.real(); } }; // Note: deliberately ignores undefined behavior, consistent with NumPy. // PyTorch's type conversions can cause a variety of undefined behavior, // including float to integral overflow and signed to unsigned integer overflow. // Some of this undefined behavior is addressed below. template <typename dest_t, typename src_t> struct static_cast_with_inter_type { C10_HOST_DEVICE __ubsan_ignore_undefined__ static inline dest_t apply( src_t src) { constexpr bool real = needs_real<dest_t, src_t>::value; return static_cast<dest_t>(maybe_real<real, src_t>::apply(src)); } }; // Partial template instantiation for casting to uint8. // Note: Converting from negative float values to unsigned integer types is // undefined behavior in C++, and current CPU and GPU compilers exhibit // divergent behavior. Casting from negative float values to signed // integer types and then to unsigned integer types is not undefined, // however, so this cast improves the consistency of type conversions // to uint8 across compilers. // Further note: Type conversions across compilers still have other undefined // and divergent behavior. template <typename src_t> struct static_cast_with_inter_type<uint8_t, src_t> { C10_HOST_DEVICE __ubsan_ignore_undefined__ static inline uint8_t apply( src_t src) { constexpr bool real = needs_real<uint8_t, src_t>::value; return static_cast<uint8_t>( static_cast<int64_t>(maybe_real<real, src_t>::apply(src))); } }; // Dynamic type casting utils: // - fetch_and_cast // - cast_and_store // // fetch_and_cast fetch a value with dynamic type specified by a ScalarType // from a void pointer and cast it to a static type. // // cast_and_store casts a static typed value into dynamic type specified // by a ScalarType, and store it into a void pointer. // // NOTE: // // Dynamic casting allows us to support type promotion without blowing up // the combination space: For example, without dynamic cast, in order to // implement `add_` with type promotion, we would need something like // // AT_DISPATCH_ALL_TYPES(output.dtype(), // AT_DISPATCH_ALL_TYPES(input1.dtype(), // AT_DISPATCH_ALL_TYPES(input2.dtype(), // [](arg0_t a, arg1_t b) -> out_t { return a + b; } // ) // ) // ) // // If we support N dtypes, the above code would generate the a+b kernel for // all the N * N * N different supported types, the compilation time and // binary size would become horrible. // // Dynamic casting might sounds like a bad idea in terms of performance. // Especially if you ever do it in a loop, you are going to do a billion tests. // But in practice it is not as bad as it might look: // // - on CPU, this is a branch that always has the same outcome, therefore // hopefully the branch predictor could do the job pretty well // - on GPU, these branches will not diverge, so we could still have the same // warp executing the same line of code // - Most kernels, like `add`, are bandwidth bound, adding a few clock cycles to // check an integer does not hurt the performance much because the ALUs would // wait for load instructions anyway. // // For the discussion and benchmark, refer to: // - https://github.com/pytorch/pytorch/pull/28343 // - https://github.com/pytorch/pytorch/pull/28344 // - https://github.com/pytorch/pytorch/pull/28345 // #ifdef C10_HOST_DEVICE #define ERROR_UNSUPPORTED_CAST CUDA_KERNEL_ASSERT(false); #else #define ERROR_UNSUPPORTED_CAST TORCH_CHECK(false, "Unexpected scalar type"); #endif // Fetch a value with dynamic type src_type from ptr, and cast it to static type // dest_t. #define FETCH_AND_CAST_CASE(type, scalartype) \ case ScalarType::scalartype: \ return static_cast_with_inter_type<dest_t, type>::apply(*(const type*)ptr); template <typename dest_t> C10_HOST_DEVICE inline dest_t fetch_and_cast( const ScalarType src_type, const void* ptr) { switch (src_type) { AT_FORALL_SCALAR_TYPES_WITH_COMPLEX_EXCEPT_COMPLEX_HALF(FETCH_AND_CAST_CASE) default: ERROR_UNSUPPORTED_CAST } return dest_t(0); // just to avoid compiler warning } // Cast a value with static type src_t into dynamic dest_type, and store it to // ptr. #define CAST_AND_STORE_CASE(type, scalartype) \ case ScalarType::scalartype: \ *(type*)ptr = static_cast_with_inter_type<type, src_t>::apply(value); \ return; template <typename src_t> C10_HOST_DEVICE inline void cast_and_store( const ScalarType dest_type, void* ptr, src_t value) { switch (dest_type) { AT_FORALL_SCALAR_TYPES_WITH_COMPLEX_EXCEPT_COMPLEX_HALF(CAST_AND_STORE_CASE) default:; } ERROR_UNSUPPORTED_CAST } #define DEFINE_UNCASTABLE(T, scalartype_) \ template <> \ C10_HOST_DEVICE inline T fetch_and_cast<T>( \ const ScalarType src_type, const void* ptr) { \ CUDA_KERNEL_ASSERT(ScalarType::scalartype_ == src_type); \ return *(const T*)ptr; \ } \ template <> \ C10_HOST_DEVICE inline void cast_and_store<T>( \ const ScalarType dest_type, void* ptr, T value) { \ CUDA_KERNEL_ASSERT(ScalarType::scalartype_ == dest_type); \ *(T*)ptr = value; \ } AT_FORALL_QINT_TYPES(DEFINE_UNCASTABLE) #undef FETCH_AND_CAST_CASE #undef CAST_AND_STORE_CASE #undef DEFINE_UNCASTABLE #undef ERROR_UNSUPPORTED_CAST template <typename To, typename From> To convert(From f) { return static_cast_with_inter_type<To, From>::apply(f); } template <typename To, typename From> To checked_convert(From f, const char* name) { // Converting to bool can't overflow so we exclude this case from checking. if (!std::is_same<To, bool>::value && overflows<To, From>(f)) { std::ostringstream oss; oss << "value cannot be converted to type " << name << " without overflow: " << f; throw std::runtime_error( oss.str()); // rather than domain_error (issue 33562) } return convert<To, From>(f); } } // namespace c10 // Trigger tests for D25440771. TODO: Remove this line any time you want.
Save
cmd:
run