|
| 1 | +#include <cstdlib> |
| 2 | +#include <iostream> |
| 3 | +#include <string> |
| 4 | +#include <type_traits> |
| 5 | + |
| 6 | +#include "glog/logging.h" |
| 7 | + |
| 8 | +#include "infini_train/include/datatype.h" |
| 9 | +#include "infini_train/include/dtype_dispatch.h" |
| 10 | + |
| 11 | +#include "infini_train/src/core/runtime/cpu/cpu_dispatch.h" |
| 12 | + |
| 13 | +using namespace infini_train; |
| 14 | + |
| 15 | +// ============================================================================ |
| 16 | +// Test 1: HasMappedType_v intercepts backends missing FP16 / BF16 |
| 17 | +// ============================================================================ |
| 18 | + |
| 19 | +// A backend TypeMap that only registers kFLOAT32 — FP16 / BF16 are absent. |
| 20 | +template <DataType DType> struct LowPrecisionAbsentTypeMap; |
| 21 | + |
| 22 | +template <> struct LowPrecisionAbsentTypeMap<DataType::kFLOAT32> { |
| 23 | + using type = float; |
| 24 | +}; |
| 25 | + |
| 26 | +static_assert(HasMappedType_v<LowPrecisionAbsentTypeMap, DataType::kFLOAT32>, |
| 27 | + "sanity: registered dtype must be detected as present"); |
| 28 | +static_assert(!HasMappedType_v<LowPrecisionAbsentTypeMap, DataType::kFLOAT16>, |
| 29 | + "unregistered kFLOAT16 must be intercepted by HasMappedType_v"); |
| 30 | +static_assert(!HasMappedType_v<LowPrecisionAbsentTypeMap, DataType::kBFLOAT16>, |
| 31 | + "unregistered kBFLOAT16 must be intercepted by HasMappedType_v"); |
| 32 | + |
| 33 | +// ============================================================================ |
| 34 | +// Test 2: CpuTypeMap resolves FP16 / BF16 to framework scalar types |
| 35 | +// ============================================================================ |
| 36 | + |
| 37 | +static_assert(std::is_same_v<MappedType_t<core::cpu::CpuTypeMap, DataType::kFLOAT16>, FP16>, |
| 38 | + "CpuTypeMap<kFLOAT16> must resolve to framework FP16"); |
| 39 | +static_assert(std::is_same_v<MappedType_t<core::cpu::CpuTypeMap, DataType::kBFLOAT16>, BF16>, |
| 40 | + "CpuTypeMap<kBFLOAT16> must resolve to framework BF16"); |
| 41 | + |
| 42 | +// ============================================================================ |
| 43 | +// Test 3: Runtime dispatch of kFLOAT16 / kBFLOAT16 |
| 44 | +// ============================================================================ |
| 45 | + |
| 46 | +void TestRuntimeDispatchLowPrecision() { |
| 47 | + std::cout << "\n=== Test 3: Runtime dispatch of kFLOAT16 / kBFLOAT16 ===" << std::endl; |
| 48 | + |
| 49 | + // kFLOAT16 must dispatch to framework FP16 |
| 50 | + bool called_fp16 = false; |
| 51 | + core::cpu::DispatchCpuFunc<DataType::kFLOAT16, DataType::kBFLOAT16>( |
| 52 | + DataType::kFLOAT16, |
| 53 | + [&called_fp16]<typename T>() { |
| 54 | + if constexpr (std::is_same_v<T, FP16>) { |
| 55 | + called_fp16 = true; |
| 56 | + } |
| 57 | + }, |
| 58 | + "dispatch kFLOAT16"); |
| 59 | + CHECK(called_fp16) << "DispatchCpuFunc did not invoke functor for kFLOAT16"; |
| 60 | + |
| 61 | + // kBFLOAT16 must dispatch to framework BF16 |
| 62 | + bool called_bf16 = false; |
| 63 | + core::cpu::DispatchCpuFunc<DataType::kFLOAT16, DataType::kBFLOAT16>( |
| 64 | + DataType::kBFLOAT16, |
| 65 | + [&called_bf16]<typename T>() { |
| 66 | + if constexpr (std::is_same_v<T, BF16>) { |
| 67 | + called_bf16 = true; |
| 68 | + } |
| 69 | + }, |
| 70 | + "dispatch kBFLOAT16"); |
| 71 | + CHECK(called_bf16) << "DispatchCpuFunc did not invoke functor for kBFLOAT16"; |
| 72 | + |
| 73 | + std::cout << "Low-precision dispatch OK." << std::endl; |
| 74 | +} |
| 75 | + |
| 76 | +// ============================================================================ |
| 77 | +// Test 4: Runtime dispatch of a low-precision dtype outside AllowedDTypes |
| 78 | +// must fatal |
| 79 | +// ============================================================================ |
| 80 | + |
| 81 | +// Sub-process entry: tries to dispatch kFLOAT16 with only kFLOAT32 allowed. |
| 82 | +void TriggerRuntimeUnsupportedLowPrecisionFatal() { |
| 83 | + core::cpu::DispatchCpuFunc<DataType::kFLOAT32>( |
| 84 | + DataType::kFLOAT16, |
| 85 | + []<typename T>() { (void)sizeof(T); }, |
| 86 | + "intercept kFLOAT16 when only kFLOAT32 is allowed"); |
| 87 | +} |
| 88 | + |
| 89 | +void TestRuntimeInterceptLowPrecision(const char *argv0) { |
| 90 | + std::cout << "\n=== Test 4: Runtime intercept of kFLOAT16 outside AllowedDTypes ===" << std::endl; |
| 91 | + const std::string cmd = std::string(argv0) + " --expect-runtime-fatal > /dev/null 2>&1"; |
| 92 | + const int status = std::system(cmd.c_str()); |
| 93 | + CHECK_NE(status, 0) << "Expected non-zero exit when dispatching an unallowed low-precision dtype"; |
| 94 | + std::cout << "Low-precision runtime intercept OK." << std::endl; |
| 95 | +} |
| 96 | + |
| 97 | +// ============================================================================ |
| 98 | +// Main |
| 99 | +// ============================================================================ |
| 100 | + |
| 101 | +int main(int argc, char *argv[]) { |
| 102 | + google::InitGoogleLogging(argv[0]); |
| 103 | + |
| 104 | + if (argc > 1 && std::string(argv[1]) == "--expect-runtime-fatal") { |
| 105 | + TriggerRuntimeUnsupportedLowPrecisionFatal(); |
| 106 | + return 0; |
| 107 | + } |
| 108 | + |
| 109 | + std::cout << "========================================" << std::endl; |
| 110 | + std::cout << " Low-precision Dtype Dispatch Test Suite" << std::endl; |
| 111 | + std::cout << "========================================" << std::endl; |
| 112 | + |
| 113 | + std::cout << "Compile-time checks: PASSED" << std::endl; |
| 114 | + |
| 115 | + TestRuntimeDispatchLowPrecision(); |
| 116 | + TestRuntimeInterceptLowPrecision(argv[0]); |
| 117 | + |
| 118 | + std::cout << "\nAll low-precision dtype dispatch tests passed." << std::endl; |
| 119 | + return 0; |
| 120 | +} |
0 commit comments