MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
TensorDType.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "GpuDataFormat.hpp"
4
5namespace MayaFlux::Kakshya {
6
7/**
8 * @enum TensorDTypeKind
9 * @brief Element family of a tensor. Values equal the DLPack type codes.
10 */
32
33namespace detail {
34
35 template <typename T>
36 struct is_std_complex : std::false_type { };
37
38 template <typename T>
39 struct is_std_complex<std::complex<T>> : std::bool_constant<std::is_floating_point_v<T>> { };
40
41}
42
43/** @brief A C++ scalar or std::complex that maps to a TensorDType. */
44template <typename T>
45concept TensorElement = (std::is_arithmetic_v<T>
46 && (!std::is_floating_point_v<T> || sizeof(T) == 4 || sizeof(T) == 8))
48
49/**
50 * @struct TensorDType
51 * @brief Element type as kind, bit width and lane count.
52 *
53 * Mirrors the DLPack data type, so half, bfloat16, float8, int4 and every
54 * integer width are first class and nothing is widened. Sub-byte types are
55 * packed; their storage is rounded up to whole bytes for the tensor.
56 *
57 * Usage:
58 * @code
59 * auto f = TensorDType::of<float>();
60 * auto h = TensorDType::float16();
61 * bool same = f.is<float>();
62 * @endcode
63 */
64struct MAYAFLUX_API TensorDType {
66 uint8_t bits { 32 };
67 uint16_t lanes { 1 };
68
69 constexpr bool operator==(const TensorDType&) const noexcept = default;
70
71 /** @brief Type for a C++ scalar or complex element. */
72 template <TensorElement T>
73 [[nodiscard]] static constexpr TensorDType of() noexcept
74 {
75 constexpr auto width = static_cast<uint8_t>(sizeof(T) * 8);
76 if constexpr (std::same_as<T, bool>) {
77 return { .kind = TensorDTypeKind::BOOL, .bits = 8, .lanes = 1 };
78 } else if constexpr (std::is_floating_point_v<T>) {
79 return { .kind = TensorDTypeKind::FLOAT, .bits = width, .lanes = 1 };
80 } else if constexpr (std::is_integral_v<T>) {
81 return { .kind = std::is_signed_v<T> ? TensorDTypeKind::INT : TensorDTypeKind::UINT, .bits = width, .lanes = 1 };
82 } else {
83 return { .kind = TensorDTypeKind::COMPLEX, .bits = width, .lanes = 1 };
84 }
85 }
86
87 /** @brief IEEE half precision. */
88 [[nodiscard]] static constexpr TensorDType float16() noexcept { return { .kind = TensorDTypeKind::FLOAT, .bits = 16, .lanes = 1 }; }
89 /** @brief Brain floating point, 16 bits. */
90 [[nodiscard]] static constexpr TensorDType bfloat16() noexcept { return { .kind = TensorDTypeKind::BFLOAT, .bits = 16, .lanes = 1 }; }
91
92 /** @brief Whether this type is exactly the C++ element type T. */
93 template <TensorElement T>
94 [[nodiscard]] constexpr bool is() const noexcept { return *this == of<T>(); }
95
96 /** @brief Bits of one element, lanes included. */
97 [[nodiscard]] constexpr size_t element_bits() const noexcept { return static_cast<size_t>(bits) * lanes; }
98 /** @brief Whether one element is not a whole number of bytes. */
99 [[nodiscard]] constexpr bool is_sub_byte() const noexcept { return element_bits() % 8 != 0; }
100 /** @brief Bytes of one element, zero for a sub-byte type. */
101 [[nodiscard]] constexpr size_t element_bytes() const noexcept { return is_sub_byte() ? 0 : element_bits() / 8; }
102
103 /**
104 * @brief Packed bytes needed for @p count elements.
105 * @return Byte count, or std::nullopt on overflow or a zero-width type.
106 */
107 [[nodiscard]] std::optional<size_t> storage_bytes(uint64_t count) const noexcept;
108
109 /** @brief Short readable name such as f32, bf16, i64, u8. */
110 [[nodiscard]] std::string name() const;
111
112 /**
113 * @brief GpuDataFormat holding exactly this element, when one exists.
114 *
115 * Half precision maps to UINT16, the raw half-float storage format.
116 */
117 [[nodiscard]] std::optional<GpuDataFormat> gpu_format() const noexcept;
118};
119
120}
A C++ scalar or std::complex that maps to a TensorDType.
TensorDTypeKind
Element family of a tensor.
@ COMPLEX
2 rows → complex (row 0 = real, row 1 = imag)
constexpr size_t element_bits() const noexcept
Bits of one element, lanes included.
static constexpr TensorDType float16() noexcept
IEEE half precision.
constexpr bool operator==(const TensorDType &) const noexcept=default
constexpr size_t element_bytes() const noexcept
Bytes of one element, zero for a sub-byte type.
static constexpr TensorDType bfloat16() noexcept
Brain floating point, 16 bits.
constexpr bool is() const noexcept
Whether this type is exactly the C++ element type T.
constexpr bool is_sub_byte() const noexcept
Whether one element is not a whole number of bytes.
static constexpr TensorDType of() noexcept
Type for a C++ scalar or complex element.
Element type as kind, bit width and lane count.