|
| 1 | +#ifndef INFINI_OPS_COMMON_CAMBRICON_CAST_H_ |
| 2 | +#define INFINI_OPS_COMMON_CAMBRICON_CAST_H_ |
| 3 | + |
| 4 | +#include "bang_fp16.h" |
| 5 | +#include "bang_bf16.h" |
| 6 | + |
| 7 | +#include "data_type.h" |
| 8 | + |
| 9 | +namespace infini::ops { |
| 10 | + |
| 11 | +namespace detail { |
| 12 | + |
| 13 | +template <typename T> |
| 14 | +using PureType = std::remove_cv_t<std::remove_reference_t<T>>; |
| 15 | + |
| 16 | +template <typename T> |
| 17 | +__host__ __device__ constexpr float ToFloatHelper(T&& x) { |
| 18 | + using PureSrc = PureType<T>; |
| 19 | + if constexpr (IsBFloat16<PureSrc>) { |
| 20 | + return __bfloat162float__(x); |
| 21 | + } else if constexpr (IsFP16<PureSrc>) { |
| 22 | + return __half2float(x); |
| 23 | + } else { |
| 24 | + return static_cast<float>(std::forward<T>(x)); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +template <typename Dst> |
| 29 | +__host__ __device__ constexpr Dst FromFloatHelper(float f) { |
| 30 | + using PureDst = PureType<Dst>; |
| 31 | + if constexpr (IsBFloat16<PureDst>) { |
| 32 | + return __float2bfloat16__(f); |
| 33 | + } else if constexpr (IsFP16<PureDst>) { |
| 34 | + return __float2half__(f); |
| 35 | + } else { |
| 36 | + return static_cast<Dst>(f); |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +// Priority tags for overload resolution. |
| 41 | +struct PriorityLow {}; |
| 42 | + |
| 43 | +struct PriorityHigh : PriorityLow {}; |
| 44 | + |
| 45 | +// Fallback: lowest priority. This always matches if nothing else does. |
| 46 | +template <typename Dst, typename Src> |
| 47 | +__host__ __device__ constexpr Dst HardwareCast(Src&& x, PriorityLow) { |
| 48 | + return FromFloatHelper<Dst>(ToFloatHelper(std::forward<Src>(x))); |
| 49 | +} |
| 50 | + |
| 51 | +// Usage: `DEFINE_DIRECT_CAST(INTRINSIC, CONDITION)`. |
| 52 | +#define DEFINE_DIRECT_CAST(INTRINSIC, ...) \ |
| 53 | + template <typename Dst, typename Src> \ |
| 54 | + __host__ __device__ auto HardwareCast(Src x, PriorityHigh) \ |
| 55 | + ->std::enable_if_t<(__VA_ARGS__), \ |
| 56 | + decltype(INTRINSIC(std::declval<Src>()))> { \ |
| 57 | + return INTRINSIC(x); \ |
| 58 | + } |
| 59 | + |
| 60 | +DEFINE_DIRECT_CAST( |
| 61 | + __bfloat162int_rz__, |
| 62 | + std::is_same_v<PureType<Dst>, int>&& IsBFloat16<PureType<Src>>) |
| 63 | +DEFINE_DIRECT_CAST( |
| 64 | + __bfloat162short_rz__, |
| 65 | + std::is_same_v<PureType<Dst>, short>&& IsBFloat16<PureType<Src>>) |
| 66 | +DEFINE_DIRECT_CAST( |
| 67 | + __int2bfloat16_rn__, |
| 68 | + IsBFloat16<PureType<Dst>>&& std::is_same_v<PureType<Src>, int>) |
| 69 | +DEFINE_DIRECT_CAST(__int2half_rn__, |
| 70 | + IsFP16<PureType<Dst>>&& std::is_same_v<PureType<Src>, int>) |
| 71 | +DEFINE_DIRECT_CAST( |
| 72 | + __float2bfloat16__, |
| 73 | + IsBFloat16<PureType<Dst>>&& std::is_same_v<PureType<Src>, double>) |
| 74 | +DEFINE_DIRECT_CAST( |
| 75 | + __float2half__, |
| 76 | + IsFP16<PureType<Dst>>&& std::is_same_v<PureType<Src>, double>) |
| 77 | +DEFINE_DIRECT_CAST(__half, IsFP16<PureType<Dst>>&& IsBFloat16<PureType<Src>>) |
| 78 | +#undef DEFINE_DIRECT_CAST |
| 79 | + |
| 80 | +} // namespace detail |
| 81 | + |
| 82 | +template <typename Dst, typename Src> |
| 83 | +__host__ __device__ Dst Cast(Src&& x) { |
| 84 | + static_assert(!std::is_reference_v<Dst>, |
| 85 | + "`Cast` cannot return reference types"); |
| 86 | + |
| 87 | + using PureSrc = std::remove_cv_t<std::remove_reference_t<Src>>; |
| 88 | + using PureDst = std::remove_cv_t<std::remove_reference_t<Dst>>; |
| 89 | + |
| 90 | + if constexpr (std::is_same_v<PureSrc, PureDst>) { |
| 91 | + return std::forward<Src>(x); |
| 92 | + } else { |
| 93 | + return detail::HardwareCast<PureDst>(std::forward<Src>(x), |
| 94 | + detail::PriorityHigh{}); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +} // namespace infini::ops |
| 99 | + |
| 100 | +#endif |
0 commit comments