Merged
feat(pypto_pro): Upload for vf api for fp8 and fp4 - add function and check problem #5000
liuyu205Add on Jul 22
feat(pypto_pro): Upload for vf api for fp8 and fp4 - add function and check problem #5000
Merged
Merged fromliuyu205/pypto_mastermaster_subintocann/pyptomaster
Total: 19 files changed+981-126
| @@ -39,6 +39,9 @@ | |||
| 39 | #define IR_KW_TYPE_BF16 "bfloat16_t" | 39 | #define IR_KW_TYPE_BF16 "bfloat16_t" |
| 40 | #define IR_KW_TYPE_FP8E4M3FN "float8_e4m3_t" | 40 | #define IR_KW_TYPE_FP8E4M3FN "float8_e4m3_t" |
| 41 | #define IR_KW_TYPE_FP8E5M2 "float8_e5m2_t" | 41 | #define IR_KW_TYPE_FP8E5M2 "float8_e5m2_t" |
| 42 | +#define IR_KW_TYPE_FP8E8M0 "float8_e8m0_t" | ||
| 43 | +#define IR_KW_TYPE_FP4E2M1 "float4_e2m1x2_t" | ||
| 44 | +#define IR_KW_TYPE_FP4E1M2 "float4_e1m2x2_t" | ||
| 42 | #define IR_KW_TYPE_HF4 "hifloat4_t" | 45 | #define IR_KW_TYPE_HF4 "hifloat4_t" |
| 43 | #define IR_KW_TYPE_HF8 "hifloat8_t" | 46 | #define IR_KW_TYPE_HF8 "hifloat8_t" |
| 44 | #define IR_KW_TYPE_UNKNOWN "unknown" | 47 | #define IR_KW_TYPE_UNKNOWN "unknown" |
| @@ -53,7 +56,7 @@ namespace ir { | |||
| 53 | * It includes: | 56 | * It includes: |
| 54 | * - Signed integers: INT4, INT8, INT16, INT32, INT64 | 57 | * - Signed integers: INT4, INT8, INT16, INT32, INT64 |
| 55 | * - Unsigned integers: UINT4, UINT8, UINT16, UINT32, UINT64 | 58 | * - Unsigned integers: UINT4, UINT8, UINT16, UINT32, UINT64 |
| 56 | - * - Floating point: FP4, FP8, FP16, FP32 | 59 | + * - Floating point: FP4, FP4E2M1, FP4E1M2, FP8, FP8E4M3FN, FP8E5M2, FP8E8M0, FP16, FP32 |
| 57 | * - Brain floating point: BF16 | 60 | * - Brain floating point: BF16 |
| 58 | * - Hisilicon float formats: HF4, HF8 | 61 | * - Hisilicon float formats: HF4, HF8 |
| 59 | * - Boolean: BOOL | 62 | * - Boolean: BOOL |
| @@ -93,11 +96,14 @@ public: | |||
| 93 | static constexpr uint8_t kFp8e4m3fnCode = 0x31; | 96 | static constexpr uint8_t kFp8e4m3fnCode = 0x31; |
| 94 | static constexpr uint8_t kFp8e5m2Code = 0x32; | 97 | static constexpr uint8_t kFp8e5m2Code = 0x32; |
| 95 | static constexpr uint8_t kFp8Code = kFp8e4m3fnCode; // Backward compatibility alias | 98 | static constexpr uint8_t kFp8Code = kFp8e4m3fnCode; // Backward compatibility alias |
| 99 | + static constexpr uint8_t kFp8e8m0Code = 0x36; | ||
| 100 | + static constexpr uint8_t kFp4e2m1Code = 0x37; | ||
| 101 | + static constexpr uint8_t kFp4e1m2Code = 0x38; | ||
| 96 | static constexpr uint8_t kFp16Code = 0x33; | 102 | static constexpr uint8_t kFp16Code = 0x33; |
| 97 | static constexpr uint8_t kFp32Code = 0x34; | 103 | static constexpr uint8_t kFp32Code = 0x34; |
| 98 | static constexpr uint8_t kFp64Code = 0x35; // Reserved for future FP64 support | 104 | static constexpr uint8_t kFp64Code = 0x35; // Reserved for future FP64 support |
| 99 | static constexpr uint8_t kIeeeFloatRangeEnd = 0x3F; | 105 | static constexpr uint8_t kIeeeFloatRangeEnd = 0x3F; |
| 100 | - // 0x36-0x3F reserved for future IEEE float types | 106 | + // 0x39-0x3F reserved for future IEEE float types |
| 101 | 107 | ||
| 102 | // Brain/Hisilicon float types: 0x40-0x4F (16 slots reserved) | 108 | // Brain/Hisilicon float types: 0x40-0x4F (16 slots reserved) |
| 103 | static constexpr uint8_t kBrainFloatRangeStart = 0x40; | 109 | static constexpr uint8_t kBrainFloatRangeStart = 0x40; |
| @@ -123,6 +129,9 @@ public: | |||
| 123 | static const DataType FP8E4M3FN; // 8-bit floating point (IEEE 754 e4m3fn format) | 129 | static const DataType FP8E4M3FN; // 8-bit floating point (IEEE 754 e4m3fn format) |
| 124 | static const DataType FP8E5M2; // 8-bit floating point (IEEE 754 e5m2 format) | 130 | static const DataType FP8E5M2; // 8-bit floating point (IEEE 754 e5m2 format) |
| 125 | static const DataType FP8; // 8-bit floating point (backward compatibility alias) | 131 | static const DataType FP8; // 8-bit floating point (backward compatibility alias) |
| 132 | + static const DataType FP8E8M0; // 8-bit floating point (8-bit exponent, 0-bit mantissa) | ||
| 133 | + static const DataType FP4E2M1; // 4-bit floating point (2-bit exponent, 1-bit mantissa) | ||
| 134 | + static const DataType FP4E1M2; // 4-bit floating point (1-bit exponent, 2-bit mantissa) | ||
| 126 | static const DataType FP16; // 16-bit floating point (IEEE 754 half precision) | 135 | static const DataType FP16; // 16-bit floating point (IEEE 754 half precision) |
| 127 | static const DataType FP32; // 32-bit floating point (IEEE 754 single precision) | 136 | static const DataType FP32; // 32-bit floating point (IEEE 754 single precision) |
| 128 | static const DataType FP64; // 64-bit floating point (IEEE 754 double precision) | 137 | static const DataType FP64; // 64-bit floating point (IEEE 754 double precision) |
| @@ -157,12 +166,15 @@ public: | |||
| 157 | return 8; // These dtypes occupy 8 bits. | 166 | return 8; // These dtypes occupy 8 bits. |
| 158 | case kHf4Code: | 167 | case kHf4Code: |
| 159 | case kFp4Code: | 168 | case kFp4Code: |
| 169 | + case kFp4e2m1Code: | ||
| 170 | + case kFp4e1m2Code: | ||
| 160 | case kUInt4Code: | 171 | case kUInt4Code: |
| 161 | case kInt4Code: | 172 | case kInt4Code: |
| 162 | return 4; // These dtypes occupy 4 bits. | 173 | return 4; // These dtypes occupy 4 bits. |
| 163 | case kHf8Code: | 174 | case kHf8Code: |
| 164 | case kFp8e4m3fnCode: | 175 | case kFp8e4m3fnCode: |
| 165 | case kFp8e5m2Code: | 176 | case kFp8e5m2Code: |
| 177 | + case kFp8e8m0Code: | ||
| 166 | case kUInt8Code: | 178 | case kUInt8Code: |
| 167 | case kInt8Code: | 179 | case kInt8Code: |
| 168 | return 8; // These dtypes occupy 8 bits. | 180 | return 8; // These dtypes occupy 8 bits. |
| @@ -221,6 +233,12 @@ public: | |||
| 221 | return "fp8e4m3fn"; | 233 | return "fp8e4m3fn"; |
| 222 | case kFp8e5m2Code: | 234 | case kFp8e5m2Code: |
| 223 | return "fp8e5m2"; | 235 | return "fp8e5m2"; |
| 236 | + case kFp8e8m0Code: | ||
| 237 | + return "fp8e8m0"; | ||
| 238 | + case kFp4e2m1Code: | ||
| 239 | + return "fp4e2m1"; | ||
| 240 | + case kFp4e1m2Code: | ||
| 241 | + return "fp4e1m2"; | ||
| 224 | case kFp16Code: | 242 | case kFp16Code: |
| 225 | return "fp16"; | 243 | return "fp16"; |
| 226 | case kFp32Code: | 244 | case kFp32Code: |
| @@ -265,6 +283,9 @@ public: | |||
| 265 | {kBf16Code, IR_KW_TYPE_BF16}, | 283 | {kBf16Code, IR_KW_TYPE_BF16}, |
| 266 | {kFp8e4m3fnCode, IR_KW_TYPE_FP8E4M3FN}, | 284 | {kFp8e4m3fnCode, IR_KW_TYPE_FP8E4M3FN}, |
| 267 | {kFp8e5m2Code, IR_KW_TYPE_FP8E5M2}, | 285 | {kFp8e5m2Code, IR_KW_TYPE_FP8E5M2}, |
| 286 | + {kFp8e8m0Code, IR_KW_TYPE_FP8E8M0}, | ||
| 287 | + {kFp4e2m1Code, IR_KW_TYPE_FP4E2M1}, | ||
| 288 | + {kFp4e1m2Code, IR_KW_TYPE_FP4E1M2}, | ||
| 268 | {kHf4Code, IR_KW_TYPE_UNKNOWN}, | 289 | {kHf4Code, IR_KW_TYPE_UNKNOWN}, |
| 269 | {kHf8Code, IR_KW_TYPE_HF8}, | 290 | {kHf8Code, IR_KW_TYPE_HF8}, |
| 270 | }}; | 291 | }}; |
| @@ -288,7 +309,7 @@ public: | |||
| 288 | /** | 309 | /** |
| 289 | * \brief Check if this data type is a floating point type | 310 | * \brief Check if this data type is a floating point type |
| 290 | * | 311 | * |
| 291 | - * \return true if this is FP4, FP8, FP16, FP32, BF16, HF4, or HF8 | 312 | + * \return true if this is FP4, FP4E2M1, FP4E1M2, FP8, FP8E4M3FN, FP8E5M2, FP8E8M0, FP16, FP32, BF16, HF4, or HF8 |
| 292 | */ | 313 | */ |
| 293 | [[nodiscard]] bool IsFloat() const | 314 | [[nodiscard]] bool IsFloat() const |
| 294 | { | 315 | { |
| @@ -364,6 +385,9 @@ inline constexpr DataType DataType::FP4 = DataType(kFp4Code); | |||
| 364 | inline constexpr DataType DataType::FP8E4M3FN = DataType(kFp8e4m3fnCode); | 385 | inline constexpr DataType DataType::FP8E4M3FN = DataType(kFp8e4m3fnCode); |
| 365 | inline constexpr DataType DataType::FP8E5M2 = DataType(kFp8e5m2Code); | 386 | inline constexpr DataType DataType::FP8E5M2 = DataType(kFp8e5m2Code); |
| 366 | inline constexpr DataType DataType::FP8 = DataType(kFp8Code); | 387 | inline constexpr DataType DataType::FP8 = DataType(kFp8Code); |
| 388 | +inline constexpr DataType DataType::FP8E8M0 = DataType(kFp8e8m0Code); | ||
| 389 | +inline constexpr DataType DataType::FP4E2M1 = DataType(kFp4e2m1Code); | ||
| 390 | +inline constexpr DataType DataType::FP4E1M2 = DataType(kFp4e1m2Code); | ||
| 367 | inline constexpr DataType DataType::FP16 = DataType(kFp16Code); | 391 | inline constexpr DataType DataType::FP16 = DataType(kFp16Code); |
| 368 | inline constexpr DataType DataType::FP32 = DataType(kFp32Code); | 392 | inline constexpr DataType DataType::FP32 = DataType(kFp32Code); |
| 369 | inline constexpr DataType DataType::FP64 = DataType(kFp64Code); | 393 | inline constexpr DataType DataType::FP64 = DataType(kFp64Code); |
| @@ -417,6 +441,12 @@ inline std::string DTypeToString(const DataType& dtype) | |||
| 417 | return "FP8E4M3FN"; | 441 | return "FP8E4M3FN"; |
| 418 | if (dtype == DataType::FP8E5M2) | 442 | if (dtype == DataType::FP8E5M2) |
| 419 | return "FP8E5M2"; | 443 | return "FP8E5M2"; |
| 444 | + if (dtype == DataType::FP8E8M0) | ||
| 445 | + return "FP8E8M0"; | ||
| 446 | + if (dtype == DataType::FP4E2M1) | ||
| 447 | + return "FP4E2M1"; | ||
| 448 | + if (dtype == DataType::FP4E1M2) | ||
| 449 | + return "FP4E1M2"; | ||
| 420 | if (dtype == DataType::FP16) | 450 | if (dtype == DataType::FP16) |
| 421 | return "FP16"; | 451 | return "FP16"; |
| 422 | if (dtype == DataType::FP32) | 452 | if (dtype == DataType::FP32) |
| @@ -447,6 +447,14 @@ REGISTER_OP("vf.copy") | |||
| 447 | .set_attr<int>("mode") | 447 | .set_attr<int>("mode") |
| 448 | .f_deduce_type(DeduceVFFromDstArg); | 448 | .f_deduce_type(DeduceVFFromDstArg); |
| 449 | 449 | ||
| 450 | +REGISTER_OP("vf.bit_cast") | ||
| 451 | + .set_op_category("VFOp") | ||
| 452 | + .set_description("Bitwise type reinterpretation (no value conversion)") | ||
| 453 | + .add_argument("dst", "Destination register") | ||
| 454 | + .add_argument("src", "Source register") | ||
| 455 | + .set_attr<DataType>("dtype") | ||
| 456 | + .f_deduce_type(DeduceVFScalarType); | ||
| 457 | + | ||
| 450 | REGISTER_OP("vf.mul_dst_add") | 458 | REGISTER_OP("vf.mul_dst_add") |
| 451 | .set_op_category("VFOp") | 459 | .set_op_category("VFOp") |
| 452 | .set_description("Multiply-dst-add: dst = dst + src0 * src1 (vmadd)") | 460 | .set_description("Multiply-dst-add: dst = dst + src0 * src1 (vmadd)") |
| @@ -150,7 +150,15 @@ void NoNestedCallVerifier::VisitExpr_(const CallPtr& op) | |||
| 150 | { | 150 | { |
| 151 | // Check each argument of the call | 151 | // Check each argument of the call |
| 152 | for (const auto& arg : op->args_) { | 152 | for (const auto& arg : op->args_) { |
| 153 | - if (As<Call>(arg)) { | 153 | + auto nested = As<Call>(arg); |
| 154 | + if (nested) { | ||
| 155 | + // Allow vf.bit_cast as a nested call — it is a type annotation, | ||
| 156 | + // not a real compute op, and is designed to be used as an | ||
| 157 | + // argument inside other vf.xxx calls. | ||
| 158 | + if (nested->name_ == "vf.bit_cast") { | ||
| 159 | + VisitExpr(arg); | ||
| 160 | + continue; | ||
| 161 | + } | ||
| 154 | std::ostringstream msg; | 162 | std::ostringstream msg; |
| 155 | msg << "Call expression has nested call in arguments"; | 163 | msg << "Call expression has nested call in arguments"; |
| 156 | RecordError(nested_call::ErrorType::CALL_IN_CALL_ARGS, msg.str(), arg->span_); | 164 | RecordError(nested_call::ErrorType::CALL_IN_CALL_ARGS, msg.str(), arg->span_); |
| @@ -1911,6 +1911,7 @@ void CCECodegen::VisitExpr_(const ir::CallPtr& op) | |||
| 1911 | "vf.mask_gen_with_reg_tensor", | 1911 | "vf.mask_gen_with_reg_tensor", |
| 1912 | "vf.compare", | 1912 | "vf.compare", |
| 1913 | "vf.compares", | 1913 | "vf.compares", |
| 1914 | + "vf.bit_cast", | ||
| 1914 | }; | 1915 | }; |
| 1915 | if (skip_ops.count(op->name_) == 0) { | 1916 | if (skip_ops.count(op->name_) == 0) { |
| 1916 | static const std::set<std::string> dual_dst_ops = { | 1917 | static const std::set<std::string> dual_dst_ops = { |
| @@ -109,5 +109,29 @@ TEST_F(VFOpsTest, Add_WithArgs_ReturnsDstArgType) | |||
| 109 | EXPECT_EQ(rt->dtype_, DataType::FP32); | 109 | EXPECT_EQ(rt->dtype_, DataType::FP32); |
| 110 | } | 110 | } |
| 111 | 111 | ||
| 112 | +// ============================================================================ | ||
| 113 | +// DeduceVFScalarType: vf.bit_cast | ||
| 114 | +// ============================================================================ | ||
| 115 | + | ||
| 116 | +TEST_F(VFOpsTest, BitCast_WithDtypeKwarg_ReturnsTargetScalarType) | ||
| 117 | +{ | ||
| 118 | + auto& reg = OpRegistry::GetInstance(); | ||
| 119 | + auto src = MakeScalarVar("src", DataType::FP8E4M3FN); | ||
| 120 | + auto call = reg.Create("vf.bit_cast", {src}, {{"dtype", DataType::FP32}}, Sp()); | ||
| 121 | + auto rt = As<ScalarType>(call->GetType()); | ||
| 122 | + ASSERT_NE(rt, nullptr); | ||
| 123 | + EXPECT_EQ(rt->dtype_, DataType::FP32); | ||
| 124 | +} | ||
| 125 | + | ||
| 126 | +TEST_F(VFOpsTest, BitCast_NoDtypeKwarg_DefaultsToFP32) | ||
| 127 | +{ | ||
| 128 | + auto& reg = OpRegistry::GetInstance(); | ||
| 129 | + auto src = MakeScalarVar("src", DataType::UINT16); | ||
| 130 | + auto call = reg.Create("vf.bit_cast", {src}, Sp()); | ||
| 131 | + auto rt = As<ScalarType>(call->GetType()); | ||
| 132 | + ASSERT_NE(rt, nullptr); | ||
| 133 | + EXPECT_EQ(rt->dtype_, DataType::FP32); | ||
| 134 | +} | ||
| 135 | + | ||
| 112 | } // namespace ir | 136 | } // namespace ir |
| 113 | } // namespace pypto | 137 | } // namespace pypto |
| @@ -676,6 +676,19 @@ TEST_F(VerifierNoNestedCallTest, NoNesting_Clean) | |||
| 676 | EXPECT_TRUE(diags.empty()); | 676 | EXPECT_TRUE(diags.empty()); |
| 677 | } | 677 | } |
| 678 | 678 | ||
| 679 | +TEST_F(VerifierNoNestedCallTest, BitCastAsNestedCall_NoError) | ||
| 680 | +{ | ||
| 681 | + IRVerifier v; | ||
| 682 | + v.AddRule(CreateNoNestedCallPropertyVerifier()); | ||
| 683 | + | ||
| 684 | + auto bit_cast_call = std::make_shared<Call>("vf.bit_cast", std::vector<ExprPtr>{}, Sp()); | ||
| 685 | + auto outer_call = std::make_shared<Call>("vf.xor", std::vector<ExprPtr>{bit_cast_call}, Sp()); | ||
| 686 | + auto eval = std::make_shared<EvalStmt>(outer_call, Sp()); | ||
| 687 | + auto program = MakeProgram("f", eval); | ||
| 688 | + auto diags = v.Verify(program); | ||
| 689 | + EXPECT_TRUE(diags.empty()); | ||
| 690 | +} | ||
| 691 | + | ||
| 679 | TEST_F(VerifierNoNestedCallTest, ErrorTypeToString_ReturnsCorrectString) | 692 | TEST_F(VerifierNoNestedCallTest, ErrorTypeToString_ReturnsCorrectString) |
| 680 | { | 693 | { |
| 681 | using namespace nested_call; | 694 | using namespace nested_call; |
| @@ -252,6 +252,10 @@ TEST(BackendCCEVFOpsTest, EmitsDeclarationsMasksBroadcastsAndMoves) | |||
| 252 | {{"dtype", ir::DataType::FP16}}, "mask2"); | 252 | {{"dtype", ir::DataType::FP16}}, "mask2"); |
| 253 | ExpectInvoke(codegen, "vf.create_mask", {"MaskReg mask_default;", "mask_default = pset_b32(PAT_ALL);"}, {}, {}, | 253 | ExpectInvoke(codegen, "vf.create_mask", {"MaskReg mask_default;", "mask_default = pset_b32(PAT_ALL);"}, {}, {}, |
| 254 | "mask_default"); | 254 | "mask_default"); |
| 255 | + ExpectInvoke(codegen, "vf.create_mask", {"pset_b8("}, {}, | ||
| 256 | + {{"pattern", EnumValue(ir::MaskPattern::ALL)}, {"dtype", ir::DataType::FP8E4M3FN}}, "mask_fp8"); | ||
| 257 | + ExpectInvoke(codegen, "vf.create_mask", {"pset_b8("}, {}, | ||
| 258 | + {{"pattern", EnumValue(ir::MaskPattern::ALL)}, {"dtype", ir::DataType::FP4E2M1}}, "mask_fp4"); | ||
| 255 | EXPECT_TRUE(codegen.IsMaskRegVar("mask")); | 259 | EXPECT_TRUE(codegen.IsMaskRegVar("mask")); |
| 256 | 260 | ||
| 257 | ExpectInvoke(codegen, "vf.full", {"vbr(fp, 2.500000);"}, {fp, Float(2.5)}); | 261 | ExpectInvoke(codegen, "vf.full", {"vbr(fp, 2.500000);"}, {fp, Float(2.5)}); |
| @@ -264,8 +268,8 @@ TEST(BackendCCEVFOpsTest, EmitsDeclarationsMasksBroadcastsAndMoves) | |||
| 264 | ExpectInvoke(codegen, "vf.create_addr_reg", {"AddrReg addr = vag_b32((4) * 2, (8) * 2);"}, | 268 | ExpectInvoke(codegen, "vf.create_addr_reg", {"AddrReg addr = vag_b32((4) * 2, (8) * 2);"}, |
| 265 | {Int(0), Int(4), Int(1), Int(8)}, {{"dtype", ir::DataType::INT64}}, "addr"); | 269 | {Int(0), Int(4), Int(1), Int(8)}, {{"dtype", ir::DataType::INT64}}, "addr"); |
| 266 | EXPECT_TRUE(codegen.IsAddrRegVar("addr")); | 270 | EXPECT_TRUE(codegen.IsAddrRegVar("addr")); |
| 267 | - ExpectInvoke(codegen, "vf.move", {"vmov(fp, i32_reg);"}, {fp, i32_reg}); | 271 | + ExpectInvoke(codegen, "vf.move", {"vmov(fp, fp);"}, {fp, fp}); |
| 268 | - ExpectInvoke(codegen, "vf.move", {"vmov(fp, i32_reg, mask, MODE_MERGING);"}, {fp, i32_reg, mask}); | 272 | + ExpectInvoke(codegen, "vf.move", {"vmov(fp, fp, mask, MODE_MERGING);"}, {fp, fp, mask}); |
| 269 | ExpectInvoke(codegen, "vf.move", {"pmov(mask2, mask);"}, {mask2, mask}); | 273 | ExpectInvoke(codegen, "vf.move", {"pmov(mask2, mask);"}, {mask2, mask}); |
| 270 | ExpectInvoke(codegen, "vf.move", {"pmov(mask2, mask, mask);"}, {mask2, mask, mask}); | 274 | ExpectInvoke(codegen, "vf.move", {"pmov(mask2, mask, mask);"}, {mask2, mask, mask}); |
| 271 | } | 275 | } |
| @@ -311,6 +315,7 @@ TEST(BackendCCEVFOpsTest, EmitsArithmeticIntrinsics) | |||
| 311 | expect_unary("vf.abs", {"vabs("}); | 315 | expect_unary("vf.abs", {"vabs("}); |
| 312 | expect_unary("vf.not_", {"vnot("}); | 316 | expect_unary("vf.not_", {"vnot("}); |
| 313 | expect_unary("vf.sqrt", {"vsqrt("}); | 317 | expect_unary("vf.sqrt", {"vsqrt("}); |
| 318 | + ExpectInvoke(codegen, "vf.sqrt", {"vsqrt<float, &"}, {dst, src0, mask}, {{"precision", true}}); | ||
| 314 | expect_unary("vf.relu", {"vrelu("}); | 319 | expect_unary("vf.relu", {"vrelu("}); |
| 315 | expect_unary("vf.neg", {"vneg("}); | 320 | expect_unary("vf.neg", {"vneg("}); |
| 316 | expect_unary("vf.log2", {"vln(", "1.4426950408889634f"}); | 321 | expect_unary("vf.log2", {"vln(", "1.4426950408889634f"}); |
| @@ -331,6 +336,10 @@ TEST(BackendCCEVFOpsTest, EmitsArithmeticIntrinsics) | |||
| 331 | codegen.RegisterRegTensorVar("fp16_dst"); | 336 | codegen.RegisterRegTensorVar("fp16_dst"); |
| 332 | ExpectInvoke(codegen, "vf.muls_cast", {"vmulscvt("}, {fp16_dst, fp32_src, Float(0.5), mask}, | 337 | ExpectInvoke(codegen, "vf.muls_cast", {"vmulscvt("}, {fp16_dst, fp32_src, Float(0.5), mask}, |
| 333 | {{"mode", EnumValue(ir::MergeMode::MERGING)}, {"dtype", ir::DataType::FP16}}); | 338 | {{"mode", EnumValue(ir::MergeMode::MERGING)}, {"dtype", ir::DataType::FP16}}); |
| 339 | + auto f8_src = MakeVar("f8_src", ir::DataType::FP8E4M3FN); | ||
| 340 | + codegen.RegisterRegTensorVar("f8_src"); | ||
| 341 | + ExpectInvoke(codegen, "vf.full", {"vdup("}, {dst, f8_src, mask}, | ||
| 342 | + {{"mode", EnumValue(ir::MergeMode::MERGING)}, {"pos", EnumValue(ir::DuplicatePos::LOWEST)}}); | ||
| 334 | } | 343 | } |
| 335 | 344 | ||
| 336 | TEST(BackendCCEVFOpsTest, EmitsReductionAndPermutationIntrinsics) | 345 | TEST(BackendCCEVFOpsTest, EmitsReductionAndPermutationIntrinsics) |
| @@ -374,14 +383,20 @@ TEST(BackendCCEVFOpsTest, EmitsPackAndCastIntrinsics) | |||
| 374 | CapturingCCECodegen codegen(ir::SectionKind::Vector); | 383 | CapturingCCECodegen codegen(ir::SectionKind::Vector); |
| 375 | auto fp32 = MakeVar("fp32", ir::DataType::FP32); | 384 | auto fp32 = MakeVar("fp32", ir::DataType::FP32); |
| 376 | auto fp16 = MakeVar("fp16", ir::DataType::FP16); | 385 | auto fp16 = MakeVar("fp16", ir::DataType::FP16); |
| 386 | + auto bf16 = MakeVar("bf16", ir::DataType::BF16); | ||
| 377 | auto i64 = MakeVar("i64", ir::DataType::INT64); | 387 | auto i64 = MakeVar("i64", ir::DataType::INT64); |
| 378 | auto i32 = MakeVar("i32", ir::DataType::INT32); | 388 | auto i32 = MakeVar("i32", ir::DataType::INT32); |
| 379 | auto i16 = MakeVar("i16", ir::DataType::INT16); | 389 | auto i16 = MakeVar("i16", ir::DataType::INT16); |
| 380 | auto u16 = MakeVar("u16", ir::DataType::UINT16); | 390 | auto u16 = MakeVar("u16", ir::DataType::UINT16); |
| 381 | auto u32 = MakeVar("u32", ir::DataType::UINT32); | 391 | auto u32 = MakeVar("u32", ir::DataType::UINT32); |
| 382 | auto s4 = MakeVar("s4", ir::DataType::INT4); | 392 | auto s4 = MakeVar("s4", ir::DataType::INT4); |
| 393 | + auto f8e4m3 = MakeVar("f8e4m3", ir::DataType::FP8E4M3FN); | ||
| 394 | + auto f8e5m2 = MakeVar("f8e5m2", ir::DataType::FP8E5M2); | ||
| 395 | + auto hf8 = MakeVar("hf8", ir::DataType::HF8); | ||
| 396 | + auto f4e2m1 = MakeVar("f4e2m1", ir::DataType::FP4E2M1); | ||
| 397 | + auto f4e1m2 = MakeVar("f4e1m2", ir::DataType::FP4E1M2); | ||
| 383 | auto mask = MakeVar("mask", ir::DataType::UINT32); | 398 | auto mask = MakeVar("mask", ir::DataType::UINT32); |
| 384 | - for (const auto& var : {fp32, fp16, i64, i32, i16, u16, u32, s4}) { | 399 | + for (const auto& var : {fp32, fp16, bf16, i64, i32, i16, u16, u32, s4, f8e4m3, f8e5m2, hf8, f4e2m1, f4e1m2}) { |
| 385 | codegen.RegisterRegTensorVar(var->name_); | 400 | codegen.RegisterRegTensorVar(var->name_); |
| 386 | } | 401 | } |
| 387 | 402 | ||
| @@ -400,22 +415,224 @@ TEST(BackendCCEVFOpsTest, EmitsPackAndCastIntrinsics) | |||
| 400 | result.emplace_back("dtype", dt); | 415 | result.emplace_back("dtype", dt); |
| 401 | return result; | 416 | return result; |
| 402 | }; | 417 | }; |
| 418 | + // FP16→FP32 (2x widening): no round_mode/saturate | ||
| 403 | ExpectInvoke(codegen, "vf.astype", {"vcvt(fp32, fp16, mask, PART_ODD, MODE_ZEROING);"}, {fp32, fp16, mask}, | 419 | ExpectInvoke(codegen, "vf.astype", {"vcvt(fp32, fp16, mask, PART_ODD, MODE_ZEROING);"}, {fp32, fp16, mask}, |
| 404 | - with_dtype(cast_options, ir::DataType::FP32)); | 420 | + {{"layout", EnumValue(ir::CastLayout::ONE)}, {"dtype", ir::DataType::FP32}}); |
| 421 | + // INT32→FP32 (same-width int→float): layout=ZERO, no saturate | ||
| 405 | ExpectInvoke(codegen, "vf.astype", {"vcvt(fp32, i32, mask, ROUND_F, MODE_ZEROING);"}, {fp32, i32, mask}, | 422 | ExpectInvoke(codegen, "vf.astype", {"vcvt(fp32, i32, mask, ROUND_F, MODE_ZEROING);"}, {fp32, i32, mask}, |
| 406 | - with_dtype(cast_options, ir::DataType::FP32)); | 423 | + {{"layout", EnumValue(ir::CastLayout::ZERO)}, |
| 424 | + {"round_mode", EnumValue(ir::VFRoundMode::CAST_FLOOR)}, | ||
| 425 | + {"dtype", ir::DataType::FP32}}); | ||
| 426 | + // FP32→INT32 (same-width float→int): layout=ZERO | ||
| 407 | ExpectInvoke(codegen, "vf.astype", {"vcvt(i32, fp32, mask, ROUND_F, RS_ENABLE, MODE_ZEROING);"}, {i32, fp32, mask}, | 427 | ExpectInvoke(codegen, "vf.astype", {"vcvt(i32, fp32, mask, ROUND_F, RS_ENABLE, MODE_ZEROING);"}, {i32, fp32, mask}, |
| 408 | - with_dtype(cast_options, ir::DataType::INT32)); | 428 | + {{"layout", EnumValue(ir::CastLayout::ZERO)}, |
| 429 | + {"round_mode", EnumValue(ir::VFRoundMode::CAST_FLOOR)}, | ||
| 430 | + {"saturate", EnumValue(ir::SaturateMode::ON)}, | ||
| 431 | + {"dtype", ir::DataType::INT32}}); | ||
| 432 | + // FP16→INT32 (wider int): uses ROUND + PART | ||
| 409 | ExpectInvoke(codegen, "vf.astype", {"vcvt(i32, fp16, mask, ROUND_F, PART_ODD, MODE_ZEROING);"}, {i32, fp16, mask}, | 433 | ExpectInvoke(codegen, "vf.astype", {"vcvt(i32, fp16, mask, ROUND_F, PART_ODD, MODE_ZEROING);"}, {i32, fp16, mask}, |
| 410 | with_dtype(cast_options, ir::DataType::INT32)); | 434 | with_dtype(cast_options, ir::DataType::INT32)); |
| 435 | + // INT32→FP16 (cross-width): uses ROUND + PART | ||
| 411 | ExpectInvoke(codegen, "vf.astype", {"vcvt(fp16, i32, mask, ROUND_F, PART_ODD, MODE_ZEROING);"}, {fp16, i32, mask}, | 436 | ExpectInvoke(codegen, "vf.astype", {"vcvt(fp16, i32, mask, ROUND_F, PART_ODD, MODE_ZEROING);"}, {fp16, i32, mask}, |
| 412 | with_dtype(cast_options, ir::DataType::FP16)); | 437 | with_dtype(cast_options, ir::DataType::FP16)); |
| 438 | + // INT32→INT16 (int narrowing): no round_mode, uses RS + PART | ||
| 413 | ExpectInvoke(codegen, "vf.astype", {"vcvt(i16, i32, mask, RS_ENABLE, PART_ODD, MODE_ZEROING);"}, {i16, i32, mask}, | 439 | ExpectInvoke(codegen, "vf.astype", {"vcvt(i16, i32, mask, RS_ENABLE, PART_ODD, MODE_ZEROING);"}, {i16, i32, mask}, |
| 414 | - with_dtype(cast_options, ir::DataType::INT16)); | 440 | + {{"layout", EnumValue(ir::CastLayout::ONE)}, |
| 441 | + {"saturate", EnumValue(ir::SaturateMode::ON)}, | ||
| 442 | + {"dtype", ir::DataType::INT16}}); | ||
| 443 | + // INT4→FP16 (s4 widening): no round_mode/saturate | ||
| 415 | ExpectInvoke(codegen, "vf.astype", {"vcvt_s42f16(fp16, s4, mask, PART_P1, MODE_ZEROING);"}, {fp16, s4, mask}, | 444 | ExpectInvoke(codegen, "vf.astype", {"vcvt_s42f16(fp16, s4, mask, PART_P1, MODE_ZEROING);"}, {fp16, s4, mask}, |
| 416 | - with_dtype(cast_options, ir::DataType::FP16)); | 445 | + {{"layout", EnumValue(ir::CastLayout::ONE)}, {"dtype", ir::DataType::FP16}}); |
| 417 | ExpectInvoke(codegen, "vf.astype", {"vcvt_f162s4(s4, fp16, mask, ROUND_F, RS_ENABLE, PART_P1, MODE_ZEROING);"}, | 446 | ExpectInvoke(codegen, "vf.astype", {"vcvt_f162s4(s4, fp16, mask, ROUND_F, RS_ENABLE, PART_P1, MODE_ZEROING);"}, |
| 418 | {s4, fp16, mask}, with_dtype(cast_options, ir::DataType::INT4)); | 447 | {s4, fp16, mask}, with_dtype(cast_options, ir::DataType::INT4)); |
| 448 | + // FP8/FP4 low-precision conversions | ||
| 449 | + // 4x widening PP (5-arg): FP8→FP32, FP4→BF16 → vcvt(dst,src,mask,PART_PP,MODE) | ||
| 450 | + // Widening paths: round_mode and saturate are not applicable | ||
| 451 | + const Kwargs widen_options = {{"layout", EnumValue(ir::CastLayout::ONE)}}; | ||
| 452 | + ExpectInvoke(codegen, "vf.astype", {"vcvt(fp32, f8e4m3, mask, PART_P1, MODE_ZEROING);"}, {fp32, f8e4m3, mask}, | ||
| 453 | + widen_options); | ||
| 454 | + ExpectInvoke(codegen, "vf.astype", {"vcvt(fp32, f8e5m2, mask, PART_P1, MODE_ZEROING);"}, {fp32, f8e5m2, mask}, | ||
| 455 | + widen_options); | ||
| 456 | + ExpectInvoke(codegen, "vf.astype", {"vcvt(bf16, f4e2m1, mask, PART_P1, MODE_ZEROING);"}, {bf16, f4e2m1, mask}, | ||
| 457 | + widen_options); | ||
| 458 | + // 2x widening PART (5-arg): HF8→FP16 → vcvt(dst,src,mask,PART,MODE) | ||
| 459 | + ExpectInvoke(codegen, "vf.astype", {"vcvt(fp16, hf8, mask, PART_ODD, MODE_ZEROING);"}, {fp16, hf8, mask}, | ||
| 460 | + widen_options); | ||
| 461 | + // 4x narrowing RND_SAT_PP (7-arg): FP32→FP8 → vcvt(dst,src,mask,ROUND,SAT,PART_PP,MODE) | ||
| 462 | + // FP32→FP8E4M3FN: round_mode must be CAST_RINT | ||
| 463 | + ExpectInvoke(codegen, "vf.astype", {"vcvt(f8e4m3, fp32, mask, ROUND_R, RS_ENABLE, PART_P1, MODE_ZEROING);"}, | ||
| 464 | + {f8e4m3, fp32, mask}, | ||
| 465 | + {{"layout", EnumValue(ir::CastLayout::ONE)}, | ||
| 466 | + {"round_mode", EnumValue(ir::VFRoundMode::CAST_RINT)}, | ||
| 467 | + {"saturate", EnumValue(ir::SaturateMode::ON)}}); | ||
| 468 | + // 4x narrowing RND_PP (6-arg): BF16→FP4 → vcvt(dst,src,mask,ROUND,PART_PP,MODE) | ||
| 469 | + // BF16→FP4: saturate not applicable | ||
| 470 | + ExpectInvoke(codegen, "vf.astype", {"vcvt(f4e2m1, bf16, mask, ROUND_F, PART_P1, MODE_ZEROING);"}, | ||
| 471 | + {f4e2m1, bf16, mask}, | ||
| 472 | + {{"layout", EnumValue(ir::CastLayout::ONE)}, {"round_mode", EnumValue(ir::VFRoundMode::CAST_FLOOR)}}); | ||
| 473 | + // 2x narrowing RND_SAT_PART (7-arg): FP16→HF8 → vcvt(dst,src,mask,ROUND,SAT,PART,MODE) | ||
| 474 | + // FP16→HF8: round_mode must be CAST_ROUND/CAST_HYBRID | ||
| 475 | + ExpectInvoke(codegen, "vf.astype", {"vcvt(hf8, fp16, mask, ROUND_A, RS_ENABLE, PART_ODD, MODE_ZEROING);"}, | ||
| 476 | + {hf8, fp16, mask}, | ||
| 477 | + {{"layout", EnumValue(ir::CastLayout::ONE)}, | ||
| 478 | + {"round_mode", EnumValue(ir::VFRoundMode::CAST_ROUND)}, | ||
| 479 | + {"saturate", EnumValue(ir::SaturateMode::ON)}}); | ||
| 480 | + // FP8E5M2 / FP4E1M2 narrowing casts with dtype | ||
| 481 | + // FP32→FP8E5M2: round_mode must be CAST_RINT | ||
| 482 | + ExpectInvoke(codegen, "vf.astype", {"vcvt(f8e5m2, fp32, mask, ROUND_R, RS_ENABLE, PART_P1, MODE_ZEROING);"}, | ||
| 483 | + {f8e5m2, fp32, mask}, | ||
| 484 | + {{"layout", EnumValue(ir::CastLayout::ONE)}, | ||
| 485 | + {"round_mode", EnumValue(ir::VFRoundMode::CAST_RINT)}, | ||
| 486 | + {"saturate", EnumValue(ir::SaturateMode::ON)}, | ||
| 487 | + {"dtype", ir::DataType::FP8E5M2}}); | ||
| 488 | + // BF16→FP4E1M2: saturate not applicable | ||
| 489 | + ExpectInvoke(codegen, "vf.astype", {"vcvt(f4e1m2, bf16, mask, ROUND_F, PART_P1, MODE_ZEROING);"}, | ||
| 490 | + {f4e1m2, bf16, mask}, | ||
| 491 | + {{"layout", EnumValue(ir::CastLayout::ONE)}, | ||
| 492 | + {"round_mode", EnumValue(ir::VFRoundMode::CAST_FLOOR)}, | ||
| 493 | + {"dtype", ir::DataType::FP4E1M2}}); | ||
| 494 | + // layout TWO/THREE → PART_P2/PART_P3 | ||
| 495 | + const Kwargs layout_two = {{"layout", EnumValue(ir::CastLayout::TWO)}, | ||
| 496 | + {"round_mode", EnumValue(ir::VFRoundMode::CAST_FLOOR)}, | ||
| 497 | + {"saturate", EnumValue(ir::SaturateMode::ON)}, | ||
| 498 | + {"dtype", ir::DataType::FP16}}; | ||
| 499 | + ExpectInvoke(codegen, "vf.astype", {"PART_TWO"}, {fp16, fp32, mask}, layout_two); | ||
| 500 | + const Kwargs layout_three = {{"layout", EnumValue(ir::CastLayout::THREE)}, | ||
| 501 | + {"round_mode", EnumValue(ir::VFRoundMode::CAST_FLOOR)}, | ||
| 502 | + {"saturate", EnumValue(ir::SaturateMode::ON)}, | ||
| 503 | + {"dtype", ir::DataType::FP16}}; | ||
| 504 | + ExpectInvoke(codegen, "vf.astype", {"PART_THREE"}, {fp16, fp32, mask}, layout_three); | ||
| 505 | +} | ||
| 506 | + | ||
| 507 | +// ============================================================================ | ||
| 508 | +// Tests for new validation CHECKs added for data type consistency and | ||
| 509 | +// parameter combination constraints. | ||
| 510 | +// ============================================================================ | ||
| 511 | + | ||
| 512 | +TEST(BackendCCEVFOpsTest, RejectsMismatchedSrcDstTypes) | ||
| 513 | +{ | ||
| 514 | + CapturingCCECodegen codegen(ir::SectionKind::Vector); | ||
| 515 | + auto fp32_dst = MakeVar("fp32_dst", ir::DataType::FP32); | ||
| 516 | + auto fp16_src = MakeVar("fp16_src", ir::DataType::FP16); | ||
| 517 | + auto mask = MakeVar("mask", ir::DataType::UINT32); | ||
| 518 | + | ||
| 519 | + // vf.add: dst=FP32, src0=FP16, src1=FP16 → should reject | ||
| 520 | + EXPECT_ANY_THROW(Invoke(codegen, "vf.add", {fp32_dst, fp16_src, fp16_src, mask})); | ||
| 521 | + | ||
| 522 | + // vf.move: dst=FP32, src=FP16 → should reject | ||
| 523 | + EXPECT_ANY_THROW(Invoke(codegen, "vf.move", {fp32_dst, fp16_src, mask})); | ||
| 524 | + | ||
| 525 | + // vf.abs: dst=FP32, src=FP16 → should reject | ||
| 526 | + EXPECT_ANY_THROW(Invoke(codegen, "vf.abs", {fp32_dst, fp16_src, mask})); | ||
| 527 | + | ||
| 528 | + // vf.truncate: dst=INT32, src=FP32 → should reject | ||
| 529 | + auto i32_dst = MakeVar("i32_dst", ir::DataType::INT32); | ||
| 530 | + auto fp32_src = MakeVar("fp32_src", ir::DataType::FP32); | ||
| 531 | + EXPECT_ANY_THROW(Invoke(codegen, "vf.truncate", {i32_dst, fp32_src, mask})); | ||
| 532 | + | ||
| 533 | + // vf.xor: dst=32-bit, src0=16-bit → should reject (bit width mismatch) | ||
| 534 | + auto fp16_dst = MakeVar("fp16_dst", ir::DataType::FP16); | ||
| 535 | + EXPECT_ANY_THROW(Invoke(codegen, "vf.xor", {fp32_dst, fp16_src, fp16_dst, mask})); | ||
| 536 | +} | ||
| 537 | + | ||
| 538 | +TEST(BackendCCEVFOpsTest, RejectsInvalidParameterCombinations) | ||
| 539 | +{ | ||
| 540 | + CapturingCCECodegen codegen(ir::SectionKind::Vector); | ||
| 541 | + auto tile = MakeTile("tile", ir::DataType::FP16); | ||
| 542 | + auto tile8 = MakeTile("tile8", ir::DataType::UINT8); | ||
| 543 | + auto fp16 = MakeVar("fp16", ir::DataType::FP16); | ||
| 544 | + auto mask = MakeVar("mask", ir::DataType::UINT32); | ||
| 545 | + auto addr = MakeVar("addr", ir::DataType::INT64); | ||
| 546 | + codegen.RegisterAddrRegVar("addr"); | ||
| 547 | + | ||
| 548 | + // load_align 2-arg with data_copy_mode → should reject | ||
| 549 | + EXPECT_ANY_THROW(Invoke(codegen, "vf.load_align", {fp16, tile}, | ||
| 550 | + {{"data_copy_mode", EnumValue(ir::DataCopyMode::DATA_BLOCK_COPY)}})); | ||
| 551 | + | ||
| 552 | + // load_align 4-arg (de-interleave) with data_copy_mode → should reject | ||
| 553 | + auto fp16b = MakeVar("fp16b", ir::DataType::FP16); | ||
| 554 | + EXPECT_ANY_THROW(Invoke(codegen, "vf.load_align", {fp16, fp16b, tile, Int(0)}, | ||
| 555 | + {{"data_copy_mode", EnumValue(ir::DataCopyMode::DATA_BLOCK_COPY)}})); | ||
| 556 | + | ||
| 557 | + // store_align MaskReg with data_copy_mode → should reject | ||
| 558 | + codegen.RegisterMaskRegVar("mask"); | ||
| 559 | + EXPECT_ANY_THROW(Invoke(codegen, "vf.store_align", {tile, mask}, | ||
| 560 | + {{"data_copy_mode", EnumValue(ir::DataCopyMode::DATA_BLOCK_COPY)}})); | ||
| 561 | + | ||
| 562 | + // store_align INTLV with data_copy_mode → should reject | ||
| 563 | + auto fp16c = MakeVar("fp16c", ir::DataType::FP16); | ||
| 564 | + EXPECT_ANY_THROW(Invoke( | ||
| 565 | + codegen, "vf.store_align", {tile, fp16, fp16c, mask}, | ||
| 566 | + {{"dist", EnumValue(ir::StoreDist::INTLV)}, {"data_copy_mode", EnumValue(ir::DataCopyMode::DATA_BLOCK_COPY)}})); | ||
| 567 | + | ||
| 568 | + // gather reg→reg with 4 args (mask) → should reject | ||
| 569 | + auto u16_idx = MakeVar("u16_idx", ir::DataType::UINT16); | ||
| 570 | + EXPECT_ANY_THROW(Invoke(codegen, "vf.gather", {fp16, fp16b, u16_idx, mask})); | ||
| 571 | + | ||
| 572 | + // gather reg→reg with data_copy_mode → should reject | ||
| 573 | + EXPECT_ANY_THROW(Invoke(codegen, "vf.gather", {fp16, fp16b, u16_idx}, | ||
| 574 | + {{"data_copy_mode", EnumValue(ir::DataCopyMode::DATA_BLOCK_LOAD)}})); | ||
| 575 | +} | ||
| 576 | + | ||
| 577 | +TEST(BackendCCEVFOpsTest, RejectsInvalidAstypeRoundMode) | ||
| 578 | +{ | ||
| 579 | + CapturingCCECodegen codegen(ir::SectionKind::Vector); | ||
| 580 | + auto fp16 = MakeVar("fp16", ir::DataType::FP16); | ||
| 581 | + auto fp32 = MakeVar("fp32", ir::DataType::FP32); | ||
| 582 | + auto mask = MakeVar("mask", ir::DataType::UINT32); | ||
| 583 | + | ||
| 584 | + // FP16→FP32 widening with round_mode=CAST_ROUND → should reject | ||
| 585 | + EXPECT_ANY_THROW(Invoke(codegen, "vf.astype", {fp32, fp16, mask}, | ||
| 586 | + {{"layout", EnumValue(ir::CastLayout::ZERO)}, | ||
| 587 | + {"round_mode", EnumValue(ir::VFRoundMode::CAST_ROUND)}, | ||
| 588 | + {"saturate", EnumValue(ir::SaturateMode::ON)}, | ||
| 589 | + {"dtype", ir::DataType::FP32}})); | ||
| 590 | + | ||
| 591 | + // FP32→FP8E4M3FN with round_mode=CAST_ROUND (only CAST_RINT allowed) → should reject | ||
| 592 | + auto f8_dst = MakeVar("f8_dst", ir::DataType::FP8E4M3FN); | ||
| 593 | + EXPECT_ANY_THROW(Invoke(codegen, "vf.astype", {f8_dst, fp32, mask}, | ||
| 594 | + {{"layout", EnumValue(ir::CastLayout::ZERO)}, | ||
| 595 | + {"round_mode", EnumValue(ir::VFRoundMode::CAST_ROUND)}, | ||
| 596 | + {"saturate", EnumValue(ir::SaturateMode::ON)}, | ||
| 597 | + {"dtype", ir::DataType::FP8E4M3FN}})); | ||
| 598 | + | ||
| 599 | + // INT32→FP32 (is_int_to_float) with round_mode=CAST_ODD → should reject | ||
| 600 | + auto i32 = MakeVar("i32", ir::DataType::INT32); | ||
| 601 | + EXPECT_ANY_THROW(Invoke(codegen, "vf.astype", {fp32, i32, mask}, | ||
| 602 | + {{"layout", EnumValue(ir::CastLayout::ZERO)}, | ||
| 603 | + {"round_mode", EnumValue(ir::VFRoundMode::CAST_ODD)}, | ||
| 604 | + {"dtype", ir::DataType::FP32}})); | ||
| 605 | +} | ||
| 606 | + | ||
| 607 | +TEST(BackendCCEVFOpsTest, RejectsInvalidAstypeSaturateAndLayout) | ||
| 608 | +{ | ||
| 609 | + CapturingCCECodegen codegen(ir::SectionKind::Vector); | ||
| 610 | + auto fp16 = MakeVar("fp16", ir::DataType::FP16); | ||
| 611 | + auto fp32 = MakeVar("fp32", ir::DataType::FP32); | ||
| 612 | + auto bf16 = MakeVar("bf16", ir::DataType::BF16); | ||
| 613 | + auto mask = MakeVar("mask", ir::DataType::UINT32); | ||
| 614 | + | ||
| 615 | + // FP32→FP8 widening path with saturate=ON → should reject (not applicable for narrowing) | ||
| 616 | + // Actually FP32→FP8 is narrowing RND_SAT_PP, saturate must be ON. | ||
| 617 | + // Test the reverse: FP8→FP32 widening with saturate=ON → should reject | ||
| 618 | + auto f8_src = MakeVar("f8_src", ir::DataType::FP8E4M3FN); | ||
| 619 | + EXPECT_ANY_THROW(Invoke(codegen, "vf.astype", {fp32, f8_src, mask}, | ||
| 620 | + {{"layout", EnumValue(ir::CastLayout::ZERO)}, | ||
| 621 | + {"saturate", EnumValue(ir::SaturateMode::ON)}, | ||
| 622 | + {"dtype", ir::DataType::FP32}})); | ||
| 623 | + | ||
| 624 | + // FP32→INT32 (is_float_to_same_int) with layout=ONE → should reject (layout not applicable) | ||
| 625 | + auto i32_dst = MakeVar("i32_dst", ir::DataType::INT32); | ||
| 626 | + EXPECT_ANY_THROW(Invoke(codegen, "vf.astype", {i32_dst, fp32, mask}, | ||
| 627 | + {{"layout", EnumValue(ir::CastLayout::ONE)}, | ||
| 628 | + {"round_mode", EnumValue(ir::VFRoundMode::CAST_RINT)}, | ||
| 629 | + {"saturate", EnumValue(ir::SaturateMode::ON)}, | ||
| 630 | + {"dtype", ir::DataType::INT32}})); | ||
| 631 | + | ||
| 632 | + // HF8→FP16 widening with layout=TWO → should reject (only ZERO/ONE for 2x widening) | ||
| 633 | + auto hf8 = MakeVar("hf8", ir::DataType::HF8); | ||
| 634 | + EXPECT_ANY_THROW(Invoke(codegen, "vf.astype", {fp16, hf8, mask}, | ||
| 635 | + {{"layout", EnumValue(ir::CastLayout::TWO)}, {"dtype", ir::DataType::FP16}})); | ||
| 419 | } | 636 | } |
| 420 | 637 | ||
| 421 | TEST(BackendCCEVFOpsTest, EmitsCompareHistogramAndMaskConversions) | 638 | TEST(BackendCCEVFOpsTest, EmitsCompareHistogramAndMaskConversions) |
| @@ -451,7 +668,7 @@ TEST(BackendCCEVFOpsTest, EmitsCompareHistogramAndMaskConversions) | |||
| 451 | {{"index_order", EnumValue(ir::IndexOrder::DECREASE_ORDER)}, {"dtype", ir::DataType::INT32}}); | 668 | {{"index_order", EnumValue(ir::IndexOrder::DECREASE_ORDER)}, {"dtype", ir::DataType::INT32}}); |
| 452 | ExpectInvoke(codegen, "vf.arange", {"vci(i64_b64_lo_"}, {i64, Int(5)}, {{"dtype", ir::DataType::INT64}}); | 669 | ExpectInvoke(codegen, "vf.arange", {"vci(i64_b64_lo_"}, {i64, Int(5)}, {{"dtype", ir::DataType::INT64}}); |
| 453 | ExpectInvoke(codegen, "vf.unsqueeze", {"vusqz("}, {i32, mask}); | 670 | ExpectInvoke(codegen, "vf.unsqueeze", {"vusqz("}, {i32, mask}); |
| 454 | - ExpectInvoke(codegen, "vf.truncate", {"vtrc(i32, fp32, ROUND_C, mask, MODE_MERGING)"}, {i32, fp32, mask}, | 671 | + ExpectInvoke(codegen, "vf.truncate", {"vtrc(fp32, fp32, ROUND_C, mask, MODE_MERGING)"}, {fp32, fp32, mask}, |
| 455 | {{"round_mode", EnumValue(ir::VFRoundMode::CAST_CEIL)}, {"mode", EnumValue(ir::MergeMode::MERGING)}}); | 672 | {{"round_mode", EnumValue(ir::VFRoundMode::CAST_CEIL)}, {"mode", EnumValue(ir::MergeMode::MERGING)}}); |
| 456 | } | 673 | } |
| 457 | 674 | ||
| @@ -467,8 +684,10 @@ TEST(BackendCCEVFOpsTest, EmitsAlignedDataMovement) | |||
| 467 | auto i64 = MakeVar("i64", ir::DataType::INT64); | 684 | auto i64 = MakeVar("i64", ir::DataType::INT64); |
| 468 | auto mask = MakeVar("mask", ir::DataType::UINT32); | 685 | auto mask = MakeVar("mask", ir::DataType::UINT32); |
| 469 | auto addr = MakeVar("addr", ir::DataType::INT64); | 686 | auto addr = MakeVar("addr", ir::DataType::INT64); |
| 687 | + auto f8 = MakeVar("f8", ir::DataType::FP8E4M3FN); | ||
| 470 | codegen.RegisterAddrRegVar("addr"); | 688 | codegen.RegisterAddrRegVar("addr"); |
| 471 | codegen.RegisterMaskRegVar("mask"); | 689 | codegen.RegisterMaskRegVar("mask"); |
| 690 | + codegen.RegisterRegTensorVar("f8"); | ||
| 472 | 691 | ||
| 473 | ExpectInvoke(codegen, "vf.load_align", {"vlds(fp16"}, {fp16, tile, Int(0)}); | 692 | ExpectInvoke(codegen, "vf.load_align", {"vlds(fp16"}, {fp16, tile, Int(0)}); |
| 474 | ExpectInvoke(codegen, "vf.load_align", {"BRC_B8"}, {u8, tile8, Int(1)}, {{"dist", EnumValue(ir::LoadDist::BRC)}}); | 693 | ExpectInvoke(codegen, "vf.load_align", {"BRC_B8"}, {u8, tile8, Int(1)}, {{"dist", EnumValue(ir::LoadDist::BRC)}}); |
| @@ -483,6 +702,7 @@ TEST(BackendCCEVFOpsTest, EmitsAlignedDataMovement) | |||
| 483 | {"block_stride", 2}, | 702 | {"block_stride", 2}, |
| 484 | {"repeat_stride", 3}, | 703 | {"repeat_stride", 3}, |
| 485 | {"post_update", true}}); | 704 | {"post_update", true}}); |
| 705 | + ExpectInvoke(codegen, "vf.load_align", {"BRC_B8"}, {f8, tile8, Int(1)}, {{"dist", EnumValue(ir::LoadDist::BRC)}}); | ||
| 486 | 706 | ||
| 487 | ExpectInvoke(codegen, "vf.store_align", {"vsts("}, {tile, fp16, mask}); | 707 | ExpectInvoke(codegen, "vf.store_align", {"vsts("}, {tile, fp16, mask}); |
| 488 | ExpectInvoke(codegen, "vf.store_align", {"ONEPT_B8"}, {tile8, u8, mask}, | 708 | ExpectInvoke(codegen, "vf.store_align", {"ONEPT_B8"}, {tile8, u8, mask}, |
| @@ -492,6 +712,7 @@ TEST(BackendCCEVFOpsTest, EmitsAlignedDataMovement) | |||
| 492 | ExpectInvoke(codegen, "vf.store_align", {"vsstb(", "POST_UPDATE"}, {tile, fp16, mask, Int(2), Int(3)}, | 712 | ExpectInvoke(codegen, "vf.store_align", {"vsstb(", "POST_UPDATE"}, {tile, fp16, mask, Int(2), Int(3)}, |
| 493 | {{"data_copy_mode", EnumValue(ir::DataCopyMode::DATA_BLOCK_COPY)}, {"post_update", true}}); | 713 | {{"data_copy_mode", EnumValue(ir::DataCopyMode::DATA_BLOCK_COPY)}, {"post_update", true}}); |
| 494 | ExpectInvoke(codegen, "vf.store_align", {"vst(fp16"}, {tile, fp16, mask, addr}); | 714 | ExpectInvoke(codegen, "vf.store_align", {"vst(fp16"}, {tile, fp16, mask, addr}); |
| 715 | + ExpectInvoke(codegen, "vf.store_align", {"NORM_B8"}, {tile8, f8, mask}); | ||
| 495 | } | 716 | } |
| 496 | 717 | ||
| 497 | TEST(BackendCCEVFOpsTest, EmitsGatherAndUnalignedDataMovement) | 718 | TEST(BackendCCEVFOpsTest, EmitsGatherAndUnalignedDataMovement) |
| @@ -589,6 +810,31 @@ TEST(BackendCCEVFOpsTest, EmitsMaskMemoryAndSpecialRegisterOperations) | |||
| 589 | ExpectInvoke(codegen, "vf.get_mask_spr", {"movp_b32()"}, {}, {}, "spr_mask32"); | 810 | ExpectInvoke(codegen, "vf.get_mask_spr", {"movp_b32()"}, {}, {}, "spr_mask32"); |
| 590 | } | 811 | } |
| 591 | 812 | ||
| 813 | +TEST(BackendCCEVFOpsTest, BitCastEmitInlineReferenceCast) | ||
| 814 | +{ | ||
| 815 | + CapturingCCECodegen codegen(ir::SectionKind::Vector); | ||
| 816 | + auto src = MakeVar("src", ir::DataType::FP8E4M3FN); | ||
| 817 | + | ||
| 818 | + // Create a vf.bit_cast Call with proper type so GetExprDtype returns FP32 | ||
| 819 | + auto fp32_type = std::make_shared<const ir::ScalarType>(ir::DataType::FP32); | ||
| 820 | + auto bit_cast_call = std::make_shared<const ir::Call>( | ||
| 821 | + "vf.bit_cast", std::vector<ir::ExprPtr>{src}, | ||
| 822 | + std::vector<std::pair<std::string, std::any>>{{"dtype", ir::DataType::FP32}}, fp32_type, ir::Span::Unknown()); | ||
| 823 | + | ||
| 824 | + // GetExprAsCode on bit_cast should return the cast expression directly | ||
| 825 | + EXPECT_EQ(codegen.GetExprAsCode(bit_cast_call), "(RegTensor<float> &)src"); | ||
| 826 | + | ||
| 827 | + // vf.xor with both args as bit_cast: vxor(dst, (RegTensor<float>&)src, ...) | ||
| 828 | + auto dst = MakeVar("dst", ir::DataType::FP32); | ||
| 829 | + auto mask = MakeVar("mask", ir::DataType::UINT32); | ||
| 830 | + ExpectInvoke(codegen, "vf.xor", {"vxor(", "(RegTensor<float> &)src", "(RegTensor<float> &)src"}, | ||
| 831 | + {dst, bit_cast_call, bit_cast_call, mask}); | ||
| 832 | + | ||
| 833 | + // vf.xor with one bit_cast arg and one plain FP32 arg (src2 must match bit_cast's 32-bit width) | ||
| 834 | + auto src2 = MakeVar("src2", ir::DataType::FP32); | ||
| 835 | + ExpectInvoke(codegen, "vf.xor", {"vxor(", "(RegTensor<float> &)src", "src2"}, {dst, bit_cast_call, src2, mask}); | ||
| 836 | +} | ||
| 837 | + | ||
| 592 | } // namespace | 838 | } // namespace |
| 593 | } // namespace backend | 839 | } // namespace backend |
| 594 | } // namespace pypto | 840 | } // namespace pypto |
| @@ -264,6 +264,9 @@ UINT64 = DataType.UINT64 | |||
| 264 | FP4 = DataType.FP4 | 264 | FP4 = DataType.FP4 |
| 265 | FP8E4M3FN = DataType.FP8E4M3FN | 265 | FP8E4M3FN = DataType.FP8E4M3FN |
| 266 | FP8E5M2 = DataType.FP8E5M2 | 266 | FP8E5M2 = DataType.FP8E5M2 |
| 267 | +FP8E8M0 = DataType.FP8E8M0 | ||
| 268 | +FP4E2M1 = DataType.FP4E2M1 | ||
| 269 | +FP4E1M2 = DataType.FP4E1M2 | ||
| 267 | FP16 = DataType.FP16 | 270 | FP16 = DataType.FP16 |
| 268 | FP32 = DataType.FP32 | 271 | FP32 = DataType.FP32 |
| 269 | FP64 = DataType.FP64 | 272 | FP64 = DataType.FP64 |
| @@ -30,6 +30,9 @@ class DataType: | |||
| 30 | FP4: DataType # 4-bit floating point | 30 | FP4: DataType # 4-bit floating point |
| 31 | FP8E4M3FN: DataType # 8-bit floating point (IEEE 754 e4m3fn format) | 31 | FP8E4M3FN: DataType # 8-bit floating point (IEEE 754 e4m3fn format) |
| 32 | FP8E5M2: DataType # 8-bit floating point (IEEE 754 e5m2 format) | 32 | FP8E5M2: DataType # 8-bit floating point (IEEE 754 e5m2 format) |
| 33 | + FP8E8M0: DataType # 8-bit floating point (8-bit exponent, 0-bit mantissa) | ||
| 34 | + FP4E2M1: DataType # 4-bit floating point (2-bit exponent, 1-bit mantissa) | ||
| 35 | + FP4E1M2: DataType # 4-bit floating point (1-bit exponent, 2-bit mantissa) | ||
| 33 | FP16: DataType # 16-bit floating point (IEEE 754 half precision) | 36 | FP16: DataType # 16-bit floating point (IEEE 754 half precision) |
| 34 | FP32: DataType # 32-bit floating point (IEEE 754 single precision) | 37 | FP32: DataType # 32-bit floating point (IEEE 754 single precision) |
| 35 | FP64: DataType # 64-bit floating point (IEEE 754 double precision) | 38 | FP64: DataType # 64-bit floating point (IEEE 754 double precision) |
| @@ -58,7 +61,7 @@ class DataType: | |||
| 58 | 61 | ||
| 59 | def is_float(self) -> bool: | 62 | def is_float(self) -> bool: |
| 60 | """ | 63 | """ |
| 61 | - Check if this data type is a floating point type (FP4, FP8, FP16, FP32, BF16, HF4, HF8). | 64 | + Check if this data type is a floating point type (FP4, FP4E2M1, FP4E1M2, FP8, FP8E4M3FN, FP8E5M2, FP8E8M0, FP16, FP32, BF16, HF4, HF8). |
| 62 | 65 | ||
| 63 | Returns: | 66 | Returns: |
| 64 | True if the data type is a floating point type, False otherwise | 67 | True if the data type is a floating point type, False otherwise |
| @@ -62,6 +62,9 @@ __all__ = [ | |||
| 62 | "DT_FP4", | 62 | "DT_FP4", |
| 63 | "DT_FP8E4M3FN", | 63 | "DT_FP8E4M3FN", |
| 64 | "DT_FP8E5M2", | 64 | "DT_FP8E5M2", |
| 65 | + "DT_FP8E8M0", | ||
| 66 | + "DT_FP4E2M1", | ||
| 67 | + "DT_FP4E1M2", | ||
| 65 | "DT_FP16", | 68 | "DT_FP16", |
| 66 | "DT_FP32", | 69 | "DT_FP32", |
| 67 | "DT_BF16", | 70 | "DT_BF16", |
| @@ -367,6 +370,9 @@ mutex = SimpleNamespace(mutex_lock=_mutex_lock, mutex_unlock=_mutex_unlock) | |||
| 367 | DT_FP4 = DataType.FP4 | 370 | DT_FP4 = DataType.FP4 |
| 368 | DT_FP8E4M3FN = DataType.FP8E4M3FN | 371 | DT_FP8E4M3FN = DataType.FP8E4M3FN |
| 369 | DT_FP8E5M2 = DataType.FP8E5M2 | 372 | DT_FP8E5M2 = DataType.FP8E5M2 |
| 373 | +DT_FP8E8M0 = DataType.FP8E8M0 | ||
| 374 | +DT_FP4E2M1 = DataType.FP4E2M1 | ||
| 375 | +DT_FP4E1M2 = DataType.FP4E1M2 | ||
| 370 | DT_FP16 = DataType.FP16 | 376 | DT_FP16 = DataType.FP16 |
| 371 | DT_FP32 = DataType.FP32 | 377 | DT_FP32 = DataType.FP32 |
| 372 | DT_BF16 = DataType.BF16 | 378 | DT_BF16 = DataType.BF16 |
| @@ -299,7 +299,8 @@ def ssbuf_store(struct_var: Any, offset: int) -> None: | |||
| 299 | # Constraints (apply to all ops in this section): | 299 | # Constraints (apply to all ops in this section): |
| 300 | # - No broadcast: ``out``, ``lhs``, ``rhs`` (when Tile) must have identical shape. | 300 | # - No broadcast: ``out``, ``lhs``, ``rhs`` (when Tile) must have identical shape. |
| 301 | # - No implicit type promotion: all operands must have the same dtype. | 301 | # - No implicit type promotion: all operands must have the same dtype. |
| 302 | -# - Supported dtypes: FP16, FP32, BF16 (op-dependent; FP8 not supported). | 302 | +# - Supported dtypes: FP16, FP32, BF16 (op-dependent; FP8/FP4 are storage-only |
| 303 | +# — use vf.astype to convert to/from FP32/BF16/FP16 for computation). | ||
| 303 | 304 | ||
| 304 | 305 | ||
| 305 | @_api_decl | 306 | @_api_decl |
| @@ -74,7 +74,8 @@ class Vf: | |||
| 74 | pattern: Mask pattern, ``pl.MaskPattern.ALL`` (default) or other | 74 | pattern: Mask pattern, ``pl.MaskPattern.ALL`` (default) or other |
| 75 | ``MaskPattern`` enum value (VL1..VL128, M3, M4, H, Q, ALLF) | 75 | ``MaskPattern`` enum value (VL1..VL128, M3, M4, H, Q, ALLF) |
| 76 | dtype: Data type that determines mask granularity (default FP32; | 76 | dtype: Data type that determines mask granularity (default FP32; |
| 77 | - FP16/UINT8 etc.) | 77 | + FP16/BF16/UINT8/INT8/FP8E4M3FN/FP8E5M2/FP8E8M0/HF8/FP4E2M1/FP4E1M2 etc. |
| 78 | + — all b8/b4 types are treated as b8 mask width) | ||
| 78 | 79 | ||
| 79 | Returns: | 80 | Returns: |
| 80 | mask_reg: Initialized mask register | 81 | mask_reg: Initialized mask register |
| @@ -157,7 +158,10 @@ class Vf: | |||
| 157 | list/tuple (linear offset = ``row * shape[1] + col``) | 158 | list/tuple (linear offset = ``row * shape[1] + col``) |
| 158 | 159 | ||
| 159 | Kwargs: | 160 | Kwargs: |
| 160 | - dtype: Data type for type-specific variants (e.g. ``pl.DT_UINT32``) | 161 | + dtype: Data type for type-specific variants (e.g. ``pl.DT_UINT32``). |
| 162 | + Supports all b8 types including FP8 (``DT_FP8E4M3FN``/``DT_FP8E5M2``/ | ||
| 163 | + ``DT_FP8E8M0``/``DT_HF8``) and b4-packed FP4 (``DT_FP4E2M1``/ | ||
| 164 | + ``DT_FP4E1M2``). | ||
| 161 | dist: ``pl.LoadDist`` value selecting load distribution pattern | 165 | dist: ``pl.LoadDist`` value selecting load distribution pattern |
| 162 | (e.g. ``pl.LoadDist.BRC``, ``pl.LoadDist.US``, ``pl.LoadDist.BRC_B32``) | 166 | (e.g. ``pl.LoadDist.BRC``, ``pl.LoadDist.US``, ``pl.LoadDist.BRC_B32``) |
| 163 | data_copy_mode: ``pl.DataCopyMode.DATA_BLOCK_COPY`` (AscendC's name for | 167 | data_copy_mode: ``pl.DataCopyMode.DATA_BLOCK_COPY`` (AscendC's name for |
| @@ -662,9 +666,22 @@ class Vf: | |||
| 662 | each lane ``i`` where ``mask[i]`` is active, the converted value is | 666 | each lane ``i`` where ``mask[i]`` is active, the converted value is |
| 663 | written to ``dst[i]``. Supports same-width and cross-width | 667 | written to ``dst[i]``. Supports same-width and cross-width |
| 664 | conversions: float->int, int->int narrowing/widening, float precision | 668 | conversions: float->int, int->int narrowing/widening, float precision |
| 665 | - changes. | 669 | + changes, and low-precision FP8/FP4 conversions. |
| 666 | 670 | ||
| 667 | - .. math:: dstReg_i = \text{cast}_{dtype}(srcReg_i) | 671 | + **FP8/FP4 conversion support** (Ascend 950PR/950DT only): |
| 672 | + | ||
| 673 | + ================================== ================================== | ||
| 674 | + Source type Target type | ||
| 675 | + ================================== ================================== | ||
| 676 | + ``DT_HF8`` ``DT_FP16``, ``DT_FP32`` | ||
| 677 | + ``DT_FP8E4M3FN`` ``DT_FP32`` | ||
| 678 | + ``DT_FP8E5M2`` ``DT_FP32`` | ||
| 679 | + ``DT_FP4E2M1`` ``DT_BF16`` | ||
| 680 | + ``DT_FP4E1M2`` ``DT_BF16`` | ||
| 681 | + ``DT_FP16`` ``DT_HF8`` | ||
| 682 | + ``DT_FP32`` ``DT_HF8``, ``DT_FP8E4M3FN``, ``DT_FP8E5M2`` | ||
| 683 | + ``DT_BF16`` ``DT_FP4E2M1``, ``DT_FP4E1M2`` | ||
| 684 | + ================================== ================================== | ||
| 668 | 685 | ||
| 669 | Args: | 686 | Args: |
| 670 | src: Source register (source type) | 687 | src: Source register (source type) |
| @@ -1148,7 +1165,7 @@ class Vf: | |||
| 1148 | @staticmethod | 1165 | @staticmethod |
| 1149 | @_api_decl | 1166 | @_api_decl |
| 1150 | def sqrt(src, mask, mode: Optional[MergeMode] = None, | 1167 | def sqrt(src, mask, mode: Optional[MergeMode] = None, |
| 1151 | - precision: Optional[str] = None): | 1168 | + precision: Optional[bool] = None): |
| 1152 | r"""Square root of each element. | 1169 | r"""Square root of each element. |
| 1153 | 1170 | ||
| 1154 | For each lane ``i`` where ``mask[i]`` is active, computes the square | 1171 | For each lane ``i`` where ``mask[i]`` is active, computes the square |
| @@ -1164,6 +1181,12 @@ class Vf: | |||
| 1164 | 1181 | ||
| 1165 | Kwargs: | 1182 | Kwargs: |
| 1166 | mode: ``pl.MergeMode.ZEROING`` (default) or ``pl.MergeMode.MERGING`` | 1183 | mode: ``pl.MergeMode.ZEROING`` (default) or ``pl.MergeMode.MERGING`` |
| 1184 | + precision: When ``True``, enables high-precision mode using the | ||
| 1185 | + fast-inverse algorithm. The maximum precision error is 0 ulp | ||
| 1186 | + for input values in [0, 85070596800837026223494223584045301760]. | ||
| 1187 | + Input values exceeding this range output 0. Only effective for | ||
| 1188 | + ``DT_FP32`` source type. Default ``False`` (standard mode, | ||
| 1189 | + 1 ulp precision error). | ||
| 1167 | 1190 | ||
| 1168 | Returns: | 1191 | Returns: |
| 1169 | Destination register (``RegTensor``) holding the square | 1192 | Destination register (``RegTensor``) holding the square |
| @@ -1455,6 +1478,43 @@ class Vf: | |||
| 1455 | elements from ``src``. | 1478 | elements from ``src``. |
| 1456 | """ | 1479 | """ |
| 1457 | 1480 | ||
| 1481 | + @staticmethod | ||
| 1482 | + @_api_decl | ||
| 1483 | + def bit_cast(src, *, dtype: DType): | ||
| 1484 | + r"""Bitwise type reinterpretation of a register. | ||
| 1485 | + | ||
| 1486 | + Returns a view of ``src`` reinterpreted as the target ``dtype``, | ||
| 1487 | + without converting any element values. The bit pattern is preserved | ||
| 1488 | + exactly; only the C++ type used for code generation changes. | ||
| 1489 | + | ||
| 1490 | + This is primarily used as an argument inside other ``vf.xxx`` calls | ||
| 1491 | + to satisfy AscendC instruction type requirements. For example, to | ||
| 1492 | + perform ``vf.xor`` on FP8E4M3 registers: | ||
| 1493 | + | ||
| 1494 | + .. code-block:: python | ||
| 1495 | + | ||
| 1496 | + reg_c = vf.xor(vf.bit_cast(reg_a, dtype=pl.DT_FP32), | ||
| 1497 | + vf.bit_cast(reg_b, dtype=pl.DT_FP32), preg) | ||
| 1498 | + | ||
| 1499 | + The generated C++ code applies ``RegTensor<T>&`` reference casts: | ||
| 1500 | + | ||
| 1501 | + .. code-block:: cpp | ||
| 1502 | + | ||
| 1503 | + RegTensor<float> reg_c; | ||
| 1504 | + vxor(reg_c, (RegTensor<float>&)reg_a, | ||
| 1505 | + (RegTensor<float>&)reg_b, preg, MODE_ZEROING); | ||
| 1506 | + | ||
| 1507 | + Args: | ||
| 1508 | + src: Source register (``RegTensor``) | ||
| 1509 | + | ||
| 1510 | + Kwargs: | ||
| 1511 | + dtype: Target data type (e.g. ``pl.DT_FP32``, ``pl.DT_UINT16``) | ||
| 1512 | + | ||
| 1513 | + Returns: | ||
| 1514 | + A type-annotated reference to ``src``, for use as an argument | ||
| 1515 | + in other ``vf.xxx`` calls. | ||
| 1516 | + """ | ||
| 1517 | + | ||
| 1458 | @staticmethod | 1518 | @staticmethod |
| 1459 | @_api_decl | 1519 | @_api_decl |
| 1460 | def mul_dst_add(src0, src1, mask, mode: Optional[MergeMode] = None): | 1520 | def mul_dst_add(src0, src1, mask, mode: Optional[MergeMode] = None): |
| @@ -108,7 +108,7 @@ class AssignmentParserMixin: | |||
| 108 | ) | 108 | ) |
| 109 | 109 | ||
| 110 | # Ops where dst type differs from src type — dtype kwarg is mandatory. | 110 | # Ops where dst type differs from src type — dtype kwarg is mandatory. |
| 111 | - _TYPE_CHANGING_OPS = frozenset({"astype", "muls_cast", "pack", "unpack"}) | 111 | + _TYPE_CHANGING_OPS = frozenset({"astype", "muls_cast", "pack", "unpack", "bit_cast"}) |
| 112 | 112 | ||
| 113 | def _parse_vf_assignment( | 113 | def _parse_vf_assignment( |
| 114 | self, | 114 | self, |
| @@ -339,6 +339,7 @@ class CallParserMixin: | |||
| 339 | "le": 1, | 339 | "le": 1, |
| 340 | "ge": 1, | 340 | "ge": 1, |
| 341 | "histograms": 1, | 341 | "histograms": 1, |
| 342 | + "bit_cast": 1, | ||
| 342 | # 2 dsts | 343 | # 2 dsts |
| 343 | "interleave": 2, | 344 | "interleave": 2, |
| 344 | "de_interleave": 2, | 345 | "de_interleave": 2, |
| @@ -1138,7 +1139,7 @@ class CallParserMixin: | |||
| 1138 | # (The assignment form is handled in _assignment_parser, which never routes | 1139 | # (The assignment form is handled in _assignment_parser, which never routes |
| 1139 | # through here — so reaching this point means it's a statement-form call.) | 1140 | # through here — so reaching this point means it's a statement-form call.) |
| 1140 | dst_count = self._VF_OP_DST_COUNT.get(op_name) | 1141 | dst_count = self._VF_OP_DST_COUNT.get(op_name) |
| 1141 | - if dst_count is not None and dst_count > 0: | 1142 | + if dst_count is not None and dst_count > 0 and op_name != "bit_cast": |
这样适配显然拦截了xxx = bit_cast的写法啊,codegen还做了假适配 ![]() ![]() | |||
| 1142 | if dst_count == 1: | 1143 | if dst_count == 1: |
| 1143 | correct = f"dst = vf.{op_name}(...)" | 1144 | correct = f"dst = vf.{op_name}(...)" |
| 1144 | else: | 1145 | else: |
| @@ -406,7 +406,7 @@ class ExpressionParserMixin: | |||
| 406 | # Every other operator on a pointer (-, *, /, //, %) is forbidden. | 406 | # Every other operator on a pointer (-, *, /, //, %) is forbidden. |
| 407 | if isinstance(left.type, ir.PtrType): | 407 | if isinstance(left.type, ir.PtrType): |
| 408 | if isinstance(binop.op, ast.Add): | 408 | if isinstance(binop.op, ast.Add): |
| 409 | - # Sub-byte element types (INT4/UINT4/FP4/HF4) pack two elements per | 409 | + # Sub-byte element types (INT4/UINT4/FP4/FP4E2M1/FP4E1M2/HF4) pack two elements per |
| 410 | # byte, so an element offset cannot address a half-byte and there is | 410 | # byte, so an element offset cannot address a half-byte and there is |
| 411 | # no valid C element type to lower to. Forbid `ptr + offset` on them. | 411 | # no valid C element type to lower to. Forbid `ptr + offset` on them. |
| 412 | if left.type.dtype.get_bit() < 8: | 412 | if left.type.dtype.get_bit() < 8: |
| @@ -42,6 +42,9 @@ class TypeResolver: | |||
| 42 | "DT_FP4": DataType.FP4, | 42 | "DT_FP4": DataType.FP4, |
| 43 | "DT_FP8E4M3FN": DataType.FP8E4M3FN, | 43 | "DT_FP8E4M3FN": DataType.FP8E4M3FN, |
| 44 | "DT_FP8E5M2": DataType.FP8E5M2, | 44 | "DT_FP8E5M2": DataType.FP8E5M2, |
| 45 | + "DT_FP8E8M0": DataType.FP8E8M0, | ||
| 46 | + "DT_FP4E2M1": DataType.FP4E2M1, | ||
| 47 | + "DT_FP4E1M2": DataType.FP4E1M2, | ||
| 45 | "DT_FP16": DataType.FP16, | 48 | "DT_FP16": DataType.FP16, |
| 46 | "DT_FP32": DataType.FP32, | 49 | "DT_FP32": DataType.FP32, |
| 47 | "DT_BF16": DataType.BF16, | 50 | "DT_BF16": DataType.BF16, |
| @@ -65,6 +68,9 @@ class TypeResolver: | |||
| 65 | "DT_FP4", | 68 | "DT_FP4", |
| 66 | "DT_FP8E4M3FN", | 69 | "DT_FP8E4M3FN", |
| 67 | "DT_FP8E5M2", | 70 | "DT_FP8E5M2", |
| 71 | + "DT_FP8E8M0", | ||
| 72 | + "DT_FP4E2M1", | ||
| 73 | + "DT_FP4E1M2", | ||
| 68 | "DT_INT4", | 74 | "DT_INT4", |
| 69 | "DT_UINT4", | 75 | "DT_UINT4", |
| 70 | "DT_HF4", | 76 | "DT_HF4", |
| @@ -488,7 +494,8 @@ class TypeResolver: | |||
| 488 | if dtype_name in self._SCALAR_UNSUPPORTED_DTYPE_NAMES: | 494 | if dtype_name in self._SCALAR_UNSUPPORTED_DTYPE_NAMES: |
| 489 | raise ParserTypeError( | 495 | raise ParserTypeError( |
| 490 | f"Scalar type does not support dtype {dtype_name}; " | 496 | f"Scalar type does not support dtype {dtype_name}; " |
| 491 | - "low-precision types (FP4/FP8/INT4/UINT4/HF4/HF8) are storage-only " | 497 | + "low-precision types (FP4/FP4E2M1/FP4E1M2/FP8/FP8E4M3FN/FP8E5M2/" |
| 498 | + "FP8E8M0/INT4/UINT4/HF4/HF8) are storage-only " | ||
| 492 | "and cannot be used in scalar expressions", | 499 | "and cannot be used in scalar expressions", |
| 493 | span=self._get_span(type_node), | 500 | span=self._get_span(type_node), |
| 494 | hint="Use a supported scalar dtype: DT_BOOL, DT_INT8, DT_INT16, DT_INT32, " | 501 | hint="Use a supported scalar dtype: DT_BOOL, DT_INT8, DT_INT16, DT_INT32, " |
| @@ -254,6 +254,9 @@ void BindDType(py::module_& ir) | |||
| 254 | .def_readonly_static("FP4", &DataType::FP4, "4-bit floating point") | 254 | .def_readonly_static("FP4", &DataType::FP4, "4-bit floating point") |
| 255 | .def_readonly_static("FP8E4M3FN", &DataType::FP8E4M3FN, "8-bit floating point (E4M3FN format)") | 255 | .def_readonly_static("FP8E4M3FN", &DataType::FP8E4M3FN, "8-bit floating point (E4M3FN format)") |
| 256 | .def_readonly_static("FP8E5M2", &DataType::FP8E5M2, "8-bit floating point (E5M2 format)") | 256 | .def_readonly_static("FP8E5M2", &DataType::FP8E5M2, "8-bit floating point (E5M2 format)") |
| 257 | + .def_readonly_static("FP8E8M0", &DataType::FP8E8M0, "8-bit floating point (E8M0 format)") | ||
| 258 | + .def_readonly_static("FP4E2M1", &DataType::FP4E2M1, "4-bit floating point (E2M1 format)") | ||
| 259 | + .def_readonly_static("FP4E1M2", &DataType::FP4E1M2, "4-bit floating point (E1M2 format)") | ||
| 257 | .def_readonly_static("FP16", &DataType::FP16, "16-bit floating point (IEEE 754 half precision)") | 260 | .def_readonly_static("FP16", &DataType::FP16, "16-bit floating point (IEEE 754 half precision)") |
| 258 | .def_readonly_static("FP32", &DataType::FP32, "32-bit floating point (IEEE 754 single precision)") | 261 | .def_readonly_static("FP32", &DataType::FP32, "32-bit floating point (IEEE 754 single precision)") |
| 259 | .def_readonly_static("BF16", &DataType::BF16, "16-bit brain floating point") | 262 | .def_readonly_static("BF16", &DataType::BF16, "16-bit brain floating point") |
| @@ -2733,7 +2733,7 @@ def _vf_kernel_71_cast_cross_width_0(in_a, t_f0, t_f1): | |||
| 2733 | reg_f16 = vf.astype(reg_fp32_tmp, preg_f32, round_mode=pl.VFRoundMode.CAST_ROUND, dtype=pl.DT_FP16) | 2733 | reg_f16 = vf.astype(reg_fp32_tmp, preg_f32, round_mode=pl.VFRoundMode.CAST_ROUND, dtype=pl.DT_FP16) |
| 2734 | # Test S16→FP32: cast FP32→S16 first, then S16→FP32 (cross-width widening) | 2734 | # Test S16→FP32: cast FP32→S16 first, then S16→FP32 (cross-width widening) |
| 2735 | reg_s16 = vf.astype(reg_a, preg_f32, round_mode=pl.VFRoundMode.CAST_TRUNC, dtype=pl.DT_INT16) | 2735 | reg_s16 = vf.astype(reg_a, preg_f32, round_mode=pl.VFRoundMode.CAST_TRUNC, dtype=pl.DT_INT16) |
| 2736 | - reg_dst = vf.astype(reg_s16, preg_f32, round_mode=pl.VFRoundMode.CAST_ROUND, dtype=pl.DT_FP32) | 2736 | + reg_dst = vf.astype(reg_s16, preg_f32, dtype=pl.DT_FP32) |
| 2737 | vf.store_align(t_f0, reg_dst, preg_f32) | 2737 | vf.store_align(t_f0, reg_dst, preg_f32) |
| 2738 | # Test FP16→BF16: cast FP32→FP16 then FP16→BF16 then BF16→FP32 roundtrip | 2738 | # Test FP16→BF16: cast FP32→FP16 then FP16→BF16 then BF16→FP32 roundtrip |
| 2739 | reg_f16 = vf.astype(reg_a, preg_f32, round_mode=pl.VFRoundMode.CAST_ROUND, dtype=pl.DT_FP16) | 2739 | reg_f16 = vf.astype(reg_a, preg_f32, round_mode=pl.VFRoundMode.CAST_ROUND, dtype=pl.DT_FP16) |
| @@ -3242,7 +3242,7 @@ def kernel_88_maskdist( | |||
| 3242 | def _vf_kernel_89_cast_odd_0(in_a, t_f0): | 3242 | def _vf_kernel_89_cast_odd_0(in_a, t_f0): |
| 3243 | preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) | 3243 | preg = vf.create_mask(pattern=pl.MaskPattern.ALL, dtype=pl.DT_FP32) |
| 3244 | reg_a = vf.load_align(in_a, 0) | 3244 | reg_a = vf.load_align(in_a, 0) |
| 3245 | - reg_i = vf.astype(reg_a, preg, dtype=pl.DT_INT32, round_mode=pl.VFRoundMode.CAST_ODD) | 3245 | + reg_i = vf.astype(reg_a, preg, dtype=pl.DT_INT32, round_mode=pl.VFRoundMode.CAST_TRUNC) |
| 3246 | reg_f = vf.astype(reg_i, preg, dtype=pl.DT_FP32) | 3246 | reg_f = vf.astype(reg_i, preg, dtype=pl.DT_FP32) |
| 3247 | vf.store_align(t_f0, reg_f, preg) | 3247 | vf.store_align(t_f0, reg_f, preg) |
| 3248 | 3248 | ||


这个pass能走进来? 如果是很久没维护的pass,就不要适配了