|
| 1 | +#ifndef INFINI_CCL_CASTER_H_ |
| 2 | +#define INFINI_CCL_CASTER_H_ |
| 3 | + |
| 4 | +#include <type_traits> |
| 5 | +#include <typeinfo> |
| 6 | +#include <utility> |
| 7 | + |
| 8 | +#include "device.h" |
| 9 | + |
| 10 | +namespace infini::ccl { |
| 11 | + |
| 12 | +// Check if a type is complete (i.e. fully defined) at compile-time. |
| 13 | +template <typename T, typename = void> |
| 14 | +struct IsComplete : std::false_type {}; |
| 15 | + |
| 16 | +template <typename T> |
| 17 | +struct IsComplete<T, std::void_t<decltype(sizeof(T))>> : std::true_type {}; |
| 18 | + |
| 19 | +template <Device::Type kDev, typename Dst, typename Src> |
| 20 | +struct HardwareCastImpl; |
| 21 | + |
| 22 | +template <Device::Type kDev, typename Dst, typename Src> |
| 23 | +inline constexpr bool HasHardwareCastV = |
| 24 | + IsComplete<HardwareCastImpl<kDev, Dst, Src>>::value; |
| 25 | + |
| 26 | +// Intermediate type used for fallback conversions, usually `float`. |
| 27 | +template <Device::Type kDev> |
| 28 | +struct CastBridge { |
| 29 | + using Type = float; |
| 30 | +}; |
| 31 | + |
| 32 | +template <Device::Type kDev> |
| 33 | +struct Caster { |
| 34 | + template <typename Dst, typename Src> |
| 35 | + static constexpr Dst Cast(Src&& x) { |
| 36 | + using PureSrc = std::remove_cv_t<std::remove_reference_t<Src>>; |
| 37 | + using PureDst = std::remove_cv_t<std::remove_reference_t<Dst>>; |
| 38 | + using Bridge = typename CastBridge<kDev>::Type; |
| 39 | + |
| 40 | + if constexpr (std::is_same_v<PureSrc, PureDst>) { |
| 41 | + return std::forward<Src>(x); |
| 42 | + } else if constexpr (HasHardwareCastV<kDev, PureDst, PureSrc>) { |
| 43 | + return HardwareCastImpl<kDev, PureDst, PureSrc>::Apply( |
| 44 | + std::forward<Src>(x)); |
| 45 | + } else if constexpr (!std::is_same_v<PureSrc, Bridge> && |
| 46 | + !std::is_same_v<PureDst, Bridge> && |
| 47 | + HasHardwareCastV<kDev, Bridge, PureSrc> && |
| 48 | + (HasHardwareCastV<kDev, PureDst, Bridge> || |
| 49 | + std::is_arithmetic_v<PureDst>)) { |
| 50 | + Bridge tmp = Cast<Bridge>(std::forward<Src>(x)); |
| 51 | + return Cast<PureDst>(tmp); |
| 52 | + } else if constexpr (!std::is_same_v<PureDst, Bridge> && |
| 53 | + std::is_arithmetic_v<PureSrc> && |
| 54 | + HasHardwareCastV<kDev, PureDst, Bridge>) { |
| 55 | + Bridge tmp = static_cast<Bridge>(std::forward<Src>(x)); |
| 56 | + return Cast<PureDst>(tmp); |
| 57 | + } else if constexpr (std::is_arithmetic_v<PureSrc> && |
| 58 | + std::is_arithmetic_v<PureDst>) { |
| 59 | + return static_cast<PureDst>(std::forward<Src>(x)); |
| 60 | + } else { |
| 61 | + static_assert(HasHardwareCastV<kDev, PureDst, PureSrc>, |
| 62 | + "no cast path available. " |
| 63 | + "Need to provide `HardwareCastImpl` specialization."); |
| 64 | + } |
| 65 | + } |
| 66 | +}; |
| 67 | + |
| 68 | +// Convenience wrapper for casting between two types. |
| 69 | +// Otherwise, need to write `Caster<kDev>::template Cast<Target>(val)` every |
| 70 | +// time. |
| 71 | +template <Device::Type kDev, typename Target, typename Source> |
| 72 | +inline Target CastTo(Source&& val) { |
| 73 | + return Caster<kDev>::template Cast<Target>(std::forward<Source>(val)); |
| 74 | +} |
| 75 | + |
| 76 | +template <Device::Type kDev, typename Source> |
| 77 | +inline float ToFloat(Source&& val) { |
| 78 | + return Caster<kDev>::template Cast<float>(std::forward<Source>(val)); |
| 79 | +} |
| 80 | + |
| 81 | +} // namespace infini::ccl |
| 82 | + |
| 83 | +#endif // INFINI_CCL_CASTER_H_ |
0 commit comments