|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <cstdint> |
| 4 | +#include <cstring> |
| 5 | +#include <type_traits> |
| 6 | + |
| 7 | +#include "infini_train/include/datatype.h" |
| 8 | +#include "infini_train/include/device.h" |
| 9 | + |
| 10 | +namespace infini_train::core { |
| 11 | + |
| 12 | +/** |
| 13 | + * Dtype bridge |
| 14 | + * |
| 15 | + * Purpose: |
| 16 | + * - Define the backend-agnostic mapping protocol from framework scalar types |
| 17 | + * (e.g. infini_train::FP16/BF16) to backend-native scalar types |
| 18 | + * (e.g. __half / __nv_bfloat16 / vendor fp16/bf16 types). |
| 19 | + * |
| 20 | + * Design notes: |
| 21 | + * - This header MUST remain backend-agnostic. |
| 22 | + * - Framework public code should only depend on infini_train::FP16/BF16. |
| 23 | + * - Backend code provides specializations of NativeScalar<Dev, Scalar>. |
| 24 | + * - ScalarConvert provides optional value-level conversion helpers. |
| 25 | + */ |
| 26 | + |
| 27 | +// ----------------------------------------------------------------------------- |
| 28 | +// NativeScalar: framework scalar -> backend native scalar mapping |
| 29 | +// ----------------------------------------------------------------------------- |
| 30 | +// Primary template intentionally undefined. |
| 31 | +// Each backend specializes the scalar types it supports. |
| 32 | +template <Device::DeviceType Dev, typename Scalar> struct NativeScalar; |
| 33 | + |
| 34 | +template <Device::DeviceType Dev, typename Scalar> using NativeScalar_t = typename NativeScalar<Dev, Scalar>::type; |
| 35 | + |
| 36 | +// Optional convenience alias for CUDA call sites. |
| 37 | +// Keep only one copy here; backend files should NOT redefine it. |
| 38 | +template <typename Scalar> using NativeScalarCUDA_t = NativeScalar_t<Device::DeviceType::kCUDA, Scalar>; |
| 39 | + |
| 40 | +// ----------------------------------------------------------------------------- |
| 41 | +// Bitcast utilities |
| 42 | +// ----------------------------------------------------------------------------- |
| 43 | +template <typename To, typename From> inline To Bitcast(const From &from) noexcept { |
| 44 | + static_assert(sizeof(To) == sizeof(From), "Bitcast requires same size"); |
| 45 | + static_assert(std::is_trivially_copyable_v<To>, "Bitcast To must be trivially copyable"); |
| 46 | + static_assert(std::is_trivially_copyable_v<From>, "Bitcast From must be trivially copyable"); |
| 47 | + |
| 48 | + To to{}; |
| 49 | + std::memcpy(&to, &from, sizeof(To)); |
| 50 | + return to; |
| 51 | +} |
| 52 | + |
| 53 | +// ----------------------------------------------------------------------------- |
| 54 | +// HasNativeScalar: detect whether a NativeScalar specialization exists |
| 55 | +// ----------------------------------------------------------------------------- |
| 56 | +template <Device::DeviceType Dev, typename Scalar, typename = void> struct HasNativeScalar : std::false_type {}; |
| 57 | + |
| 58 | +template <Device::DeviceType Dev, typename Scalar> |
| 59 | +struct HasNativeScalar<Dev, Scalar, std::void_t<typename NativeScalar<Dev, Scalar>::type>> : std::true_type {}; |
| 60 | + |
| 61 | +template <Device::DeviceType Dev, typename Scalar> |
| 62 | +inline constexpr bool HasNativeScalar_v = HasNativeScalar<Dev, Scalar>::value; |
| 63 | + |
| 64 | +// ----------------------------------------------------------------------------- |
| 65 | +// ScalarConvert: framework scalar <-> backend native scalar conversion glue |
| 66 | +// ----------------------------------------------------------------------------- |
| 67 | +// Primary template intentionally undefined by default. |
| 68 | +// Backends may specialize this if simple bitcast is insufficient. |
| 69 | +template <Device::DeviceType Dev, typename Scalar, typename Enable = void> struct ScalarConvert; |
| 70 | + |
| 71 | +// Default FP16 conversion: preserve raw 16-bit bit pattern. |
| 72 | +template <Device::DeviceType Dev> struct ScalarConvert<Dev, infini_train::FP16, void> { |
| 73 | + static_assert(HasNativeScalar_v<Dev, infini_train::FP16>, |
| 74 | + "Missing NativeScalar specialization for FP16 on this backend"); |
| 75 | + |
| 76 | + using Native = NativeScalar_t<Dev, infini_train::FP16>; |
| 77 | + |
| 78 | + static inline Native ToNative(infini_train::FP16 v) noexcept { |
| 79 | + static_assert(sizeof(Native) == sizeof(uint16_t), "Native FP16 must be 16-bit"); |
| 80 | + return Bitcast<Native>(v.x); |
| 81 | + } |
| 82 | + |
| 83 | + static inline infini_train::FP16 FromNative(Native v) noexcept { |
| 84 | + infini_train::FP16 out{}; |
| 85 | + static_assert(sizeof(Native) == sizeof(uint16_t), "Native FP16 must be 16-bit"); |
| 86 | + out.x = Bitcast<uint16_t>(v); |
| 87 | + return out; |
| 88 | + } |
| 89 | +}; |
| 90 | + |
| 91 | +// Default BF16 conversion: preserve raw 16-bit bit pattern. |
| 92 | +template <Device::DeviceType Dev> struct ScalarConvert<Dev, infini_train::BF16, void> { |
| 93 | + static_assert(HasNativeScalar_v<Dev, infini_train::BF16>, |
| 94 | + "Missing NativeScalar specialization for BF16 on this backend"); |
| 95 | + |
| 96 | + using Native = NativeScalar_t<Dev, infini_train::BF16>; |
| 97 | + |
| 98 | + static inline Native ToNative(infini_train::BF16 v) noexcept { |
| 99 | + static_assert(sizeof(Native) == sizeof(uint16_t), "Native BF16 must be 16-bit"); |
| 100 | + return Bitcast<Native>(v.x); |
| 101 | + } |
| 102 | + |
| 103 | + static inline infini_train::BF16 FromNative(Native v) noexcept { |
| 104 | + infini_train::BF16 out{}; |
| 105 | + static_assert(sizeof(Native) == sizeof(uint16_t), "Native BF16 must be 16-bit"); |
| 106 | + out.x = Bitcast<uint16_t>(v); |
| 107 | + return out; |
| 108 | + } |
| 109 | +}; |
| 110 | + |
| 111 | +// ----------------------------------------------------------------------------- |
| 112 | +// Convenience wrappers |
| 113 | +// ----------------------------------------------------------------------------- |
| 114 | +template <Device::DeviceType Dev, typename Scalar> inline NativeScalar_t<Dev, Scalar> ToNative(Scalar v) noexcept { |
| 115 | + return ScalarConvert<Dev, Scalar>::ToNative(v); |
| 116 | +} |
| 117 | + |
| 118 | +template <Device::DeviceType Dev, typename Scalar> inline Scalar FromNative(NativeScalar_t<Dev, Scalar> v) noexcept { |
| 119 | + return ScalarConvert<Dev, Scalar>::FromNative(v); |
| 120 | +} |
| 121 | + |
| 122 | +} // namespace infini_train::core |
0 commit comments