ScalarType
See c10/core/ScalarType.h. and https://github.com/pytorch/pytorch/blob/master/torch/csrc/api/include/torch/types.h.
ScalarType
is an enum class
, i.e., enum class ScalarType : int8_t { ... }
.
Members
It has the following members:
./code/scalar-type/members.cc
1#define AT_FORALL_SCALAR_TYPES_WITH_COMPLEX_EXCEPT_COMPLEX_HALF(_) \
2 _(uint8_t, Byte) \
3 _(int8_t, Char) \
4 _(int16_t, Short) \
5 _(int, Int) \
6 _(int64_t, Long) \
7 _(at::Half, Half) \
8 _(float, Float) \
9 _(double, Double) \
10 _(c10::complex<float>, ComplexFloat) \
11 _(c10::complex<double>, ComplexDouble) \
12 _(bool, Bool) \
13 _(at::BFloat16, BFloat16)
Some aliases
./code/scalar-type/main.cc (alias)
1static void TestAlias() {
2 static_assert(c10::ScalarType::Int == c10::kInt, "");
3 static_assert(c10::ScalarType::Byte == c10::kByte, "");
4}
./code/scalar-type/alias.cc
1// See torch/csrc/api/include/torch/types.h
2using Dtype = at::ScalarType;
3
4/// Fixed width dtypes.
5constexpr auto kUInt8 = at::kByte;
6constexpr auto kInt8 = at::kChar;
7constexpr auto kInt16 = at::kShort;
8constexpr auto kInt32 = at::kInt;
9constexpr auto kInt64 = at::kLong;
10constexpr auto kFloat16 = at::kHalf;
11constexpr auto kFloat32 = at::kFloat;
12constexpr auto kFloat64 = at::kDouble;
13
14/// Rust-style short dtypes.
15constexpr auto kU8 = kUInt8;
16constexpr auto kI8 = kInt8;
17constexpr auto kI16 = kInt16;
18constexpr auto kI32 = kInt32;
19constexpr auto kI64 = kInt64;
20constexpr auto kF16 = kFloat16;
21constexpr auto kF32 = kFloat32;
22constexpr auto kF64 = kFloat64;
ScalarType to CPP type
./code/scalar-type/main.cc
1static void TestScalarTypeToCppType() {
2 static_assert(
3 std::is_same<
4 int32_t, //
5 c10::impl::ScalarTypeToCPPType<c10 ::ScalarType::Int>::type>::value,
6 "");
7}
CPP type to ScalarType
./code/scalar-type/main.cc
1static void TestCppTypeToScalarType() {
2 static_assert(
3 c10::CppTypeToScalarType<float>::value == c10::ScalarType::Float, "");
4}
Note
It is c10::impl::ScalarTypeToCPPType
, but it is c10::CppTypeToScalarType
.