Merged
fix(pypto_pro): relax MX matmul physical shape checks #6670
fix(pypto_pro): relax MX matmul physical shape checks #6670
Merged
yzfAdd on Sep 23
Total: 7 files changed+88-24
@@ -773,6 +773,9 @@ bool StructuralEqualImpl<AssertMode>::EqualType(const TypePtr& lhs, const TypePt
773 if (lhs_hw.pad != rhs_hw.pad) {773 if (lhs_hw.pad != rhs_hw.pad) {
774 return IRAssert({}, "HardwareInfo pad mismatch");774 return IRAssert({}, "HardwareInfo pad mismatch");
775 }775 }
776+ if (lhs_hw.compact != rhs_hw.compact) {
777+ return IRAssert({}, "HardwareInfo compact mismatch");
778+ }
776 }779 }
777 return true;780 return true;
778 } else if (auto lhs_tuple = As<TupleType>(lhs)) {781 } else if (auto lhs_tuple = As<TupleType>(lhs)) {
@@ -402,6 +402,7 @@ StructuralHasher::ResultType StructuralHasher::HashType(const TypePtr& type)
402 h = HashCombine(h, static_cast<ResultType>(hw.slayout));402 h = HashCombine(h, static_cast<ResultType>(hw.slayout));
403 h = HashCombine(h, static_cast<ResultType>(hw.fractal));403 h = HashCombine(h, static_cast<ResultType>(hw.fractal));
404 h = HashCombine(h, static_cast<ResultType>(hw.pad));404 h = HashCombine(h, static_cast<ResultType>(hw.pad));
405+ h = HashCombine(h, static_cast<ResultType>(hw.compact));
405 } else {406 } else {
406 h = HashCombine(h, static_cast<ResultType>(0));407 h = HashCombine(h, static_cast<ResultType>(0));
407 }408 }
@@ -222,6 +222,14 @@ TEST_F(IRStructEqTypeTest, TestTensorAndTileTypeEqual)
222 EXPECT_TRUE(structural_equal(tl1, tl2));222 EXPECT_TRUE(structural_equal(tl1, tl2));
223 EXPECT_FALSE(structural_equal(tl1, std::make_shared<TileType>(std::vector<ExprPtr>{d16a}, DataType::FP16)));223 EXPECT_FALSE(structural_equal(tl1, std::make_shared<TileType>(std::vector<ExprPtr>{d16a}, DataType::FP16)));
224 EXPECT_FALSE(structural_equal(tl1, std::make_shared<TileType>(std::vector<ExprPtr>{d16a, d16a}, DataType::FP32)));224 EXPECT_FALSE(structural_equal(tl1, std::make_shared<TileType>(std::vector<ExprPtr>{d16a, d16a}, DataType::FP32)));
225+ 
226+ HardwareInfo compactNull(TileLayout::row_major, TileLayout::none_box, 512, TilePad::null, CompactMode::null);
227+ HardwareInfo compactNormal(TileLayout::row_major, TileLayout::none_box, 512, TilePad::null, CompactMode::normal);
228+ auto compactNullTile = std::make_shared<TileType>(std::vector<ExprPtr>{d16a}, DataType::FP32, std::nullopt,
229+ std::nullopt, compactNull);
230+ auto compactNormalTile = std::make_shared<TileType>(std::vector<ExprPtr>{d16a}, DataType::FP32, std::nullopt,
231+ std::nullopt, compactNormal);
232+ EXPECT_FALSE(structural_equal(compactNullTile, compactNormalTile));
225}233}
226 234 
227TEST_F(IRStructEqTypeTest, TestTupleAndOtherTypes)235TEST_F(IRStructEqTypeTest, TestTupleAndOtherTypes)
@@ -410,12 +410,15 @@ TEST_F(IRStructHashTest, TestHashTileTypeWithHardwareInfo)
410 HardwareInfo hw1(TileLayout::row_major, TileLayout::none_box, 512, TilePad::null);410 HardwareInfo hw1(TileLayout::row_major, TileLayout::none_box, 512, TilePad::null);
411 HardwareInfo hw2(TileLayout::row_major, TileLayout::none_box, 512, TilePad::null);411 HardwareInfo hw2(TileLayout::row_major, TileLayout::none_box, 512, TilePad::null);
412 HardwareInfo hw3(TileLayout::col_major, TileLayout::none_box, 512, TilePad::null);412 HardwareInfo hw3(TileLayout::col_major, TileLayout::none_box, 512, TilePad::null);
413+ HardwareInfo hw4(TileLayout::row_major, TileLayout::none_box, 512, TilePad::null, CompactMode::normal);
413 414 
414 auto t1 = std::make_shared<TileType>(std::vector<ExprPtr>{d16}, DataType::FP32, std::nullopt, std::nullopt, hw1);415 auto t1 = std::make_shared<TileType>(std::vector<ExprPtr>{d16}, DataType::FP32, std::nullopt, std::nullopt, hw1);
415 auto t2 = std::make_shared<TileType>(std::vector<ExprPtr>{d16}, DataType::FP32, std::nullopt, std::nullopt, hw2);416 auto t2 = std::make_shared<TileType>(std::vector<ExprPtr>{d16}, DataType::FP32, std::nullopt, std::nullopt, hw2);
416 auto t3 = std::make_shared<TileType>(std::vector<ExprPtr>{d16}, DataType::FP32, std::nullopt, std::nullopt, hw3);417 auto t3 = std::make_shared<TileType>(std::vector<ExprPtr>{d16}, DataType::FP32, std::nullopt, std::nullopt, hw3);
418+ auto t4 = std::make_shared<TileType>(std::vector<ExprPtr>{d16}, DataType::FP32, std::nullopt, std::nullopt, hw4);
417 EXPECT_EQ(structural_hash(t1), structural_hash(t2));419 EXPECT_EQ(structural_hash(t1), structural_hash(t2));
418 EXPECT_NE(structural_hash(t1), structural_hash(t3));420 EXPECT_NE(structural_hash(t1), structural_hash(t3));
421+ EXPECT_NE(structural_hash(t1), structural_hash(t4));
419}422}
420 423 
421TEST_F(IRStructHashTest, TestHashTupleType)424TEST_F(IRStructHashTest, TestHashTupleType)
@@ -4049,6 +4049,8 @@ def _ir_matmul_acc(
4049 _check_tile_memory_space("matmul_acc", "lhs_tile", lhs, MemorySpace.Left, "L0A (Left)")4049 _check_tile_memory_space("matmul_acc", "lhs_tile", lhs, MemorySpace.Left, "L0A (Left)")
4050 _check_tile_memory_space("matmul_acc", "rhs_tile", rhs, MemorySpace.Right, "L0B (Right)")4050 _check_tile_memory_space("matmul_acc", "rhs_tile", rhs, MemorySpace.Right, "L0B (Right)")
4051 _check_matmul_dtype("matmul_acc", dst, lhs, rhs, acc=acc)4051 _check_matmul_dtype("matmul_acc", dst, lhs, rhs, acc=acc)
4052+ if not _ir_core.structural_equal(dst.type, acc.type, enable_auto_mapping=False):
4053+ raise InvalidType("matmul_acc: acc_tile type must match dst_tile type")
4052 kwargs: dict[str, Any] = {}4054 kwargs: dict[str, Any] = {}
4053 if phase is not None:4055 if phase is not None:
4054 kwargs["phase"] = phase4056 kwargs["phase"] = phase
@@ -4179,36 +4181,22 @@ def _check_mx_operands(
4179 f"got ({a_dtype},{b_dtype},{dst_dtype})."4181 f"got ({a_dtype},{b_dtype},{dst_dtype})."
4180 )4182 )
4181 4183 
4182- dst_shape = _tile_shape_ints(dst.type)
4183 lhs_shape = _tile_shape_ints(lhs.type)4184 lhs_shape = _tile_shape_ints(lhs.type)
4184 rhs_shape = _tile_shape_ints(rhs.type)4185 rhs_shape = _tile_shape_ints(rhs.type)
4185 lhs_k = lhs_shape[1] if lhs_shape is not None else None4186 lhs_k = lhs_shape[1] if lhs_shape is not None else None
4186 rhs_k = rhs_shape[0] if rhs_shape is not None else None4187 rhs_k = rhs_shape[0] if rhs_shape is not None else None
4187 4188 
4188- if lhs_k is not None and rhs_k is not None and lhs_k != rhs_k:
4189- raise InvalidShape(f"{op_name}: lhs and rhs K dimensions must match, got lhs K={lhs_k}, rhs K={rhs_k}.")
4190 for name, k_value in (("lhs", lhs_k), ("rhs", rhs_k)):4189 for name, k_value in (("lhs", lhs_k), ("rhs", rhs_k)):
4191 if k_value is not None and k_value % 64 != 0:4190 if k_value is not None and k_value % 64 != 0:
4192 raise InvalidShape(4191 raise InvalidShape(
4193 f"{op_name}: K dimension must be a multiple of 64 for MX matmul, got {name} K={k_value}."4192 f"{op_name}: K dimension must be a multiple of 64 for MX matmul, got {name} K={k_value}."
4194 )4193 )
4195 4194 
4196- if dst_shape is not None and lhs_shape is not None and rhs_shape is not None:
4197- expected_shape = [lhs_shape[0], rhs_shape[1]]
4198- if dst_shape != expected_shape:
4199- raise InvalidType(
4200- f"{op_name}: dst_tile shape must be [lhs M, rhs N], "
4201- f"expected {expected_shape}, got {dst_shape}."
4202- )
4203 if acc is not None:4195 if acc is not None:
4204- acc_shape = _tile_shape_ints(acc.type)
4205- if dst_shape is not None and acc_shape is not None and acc_shape != dst_shape:
4206- raise InvalidShape(
4207- f"{op_name}: acc_tile shape must match dst_tile shape, "
4208- f"got acc_tile={acc_shape}, dst_tile={dst_shape}."
4209- )
4210 if acc.type.dtype != DataType.FP32:4196 if acc.type.dtype != DataType.FP32:
4211 raise InvalidType(f"{op_name}: acc_tile must use FP32 dtype, got {acc.type.dtype}.")4197 raise InvalidType(f"{op_name}: acc_tile must use FP32 dtype, got {acc.type.dtype}.")
4198+ if not _ir_core.structural_equal(dst.type, acc.type, enable_auto_mapping=False):
4199+ raise InvalidType(f"{op_name}: acc_tile type must match dst_tile type")
4212 4200 
4213 _check_mx_scale_tile(op_name, scale_a, lhs, is_left=True)4201 _check_mx_scale_tile(op_name, scale_a, lhs, is_left=True)
4214 _check_mx_scale_tile(op_name, scale_b, rhs, is_left=False)4202 _check_mx_scale_tile(op_name, scale_b, rhs, is_left=False)
@@ -29,6 +29,46 @@ def test_coordinate_apis_use_sequence_parameters():
29 assert list(inspect.signature(pl.set_validshape).parameters) == ["tile", "shape"]29 assert list(inspect.signature(pl.set_validshape).parameters) == ["tile", "shape"]
30 30 
31 31 
32+def test_matmul_acc_rejects_mismatched_acc_shape():
33+ with pytest.raises(InvalidType, match=r"acc_tile type must match dst_tile type"):
34+ 
35+ @pl.jit(auto_mutex=False)
36+ def main(_jit_entry: pl.DT_INT64):
37+ dst_type = pl.TileType(
38+ shape=[64, 64],
39+ dtype=pl.DT_FP32,
40+ target_memory=pl.MemorySpace.Acc,
41+ layout=pl.NZ,
42+ fractal=1024,
43+ )
44+ acc_type = pl.TileType(
45+ shape=[32, 64],
46+ dtype=pl.DT_FP32,
47+ target_memory=pl.MemorySpace.Acc,
48+ layout=pl.NZ,
49+ fractal=1024,
50+ )
51+ lhs_type = pl.TileType(
52+ shape=[64, 64],
53+ dtype=pl.DT_FP16,
54+ target_memory=pl.MemorySpace.Left,
55+ layout=pl.NZ,
56+ )
57+ rhs_type = pl.TileType(
58+ shape=[64, 64],
59+ dtype=pl.DT_FP16,
60+ target_memory=pl.MemorySpace.Right,
61+ layout=pl.ZN,
62+ )
63+ dst = pl.make_tile(dst_type, addr=0x0000)
64+ acc = pl.make_tile(acc_type, addr=0x0000)
65+ lhs = pl.make_tile(lhs_type, addr=0x0000)
66+ rhs = pl.make_tile(rhs_type, addr=0x0000)
67+ pl.matmul_acc(dst, acc, lhs, rhs)
68+ 
69+ main.to_kernel_def().parse_target_program(ir.SectionKind.Cube)
70+ 
71+ 
32def test_manual_add():72def test_manual_add():
33 @pl.jit(auto_mutex=False)73 @pl.jit(auto_mutex=False)
34 def main(74 def main(
@@ -115,26 +115,47 @@ def test_matmul_mx_checks_dtype_before_shape():
115 _ir_matmul_mx(t["dst"], t["lhs"], t["rhs"], t["scale_a"], t["scale_b"])115 _ir_matmul_mx(t["dst"], t["lhs"], t["rhs"], t["scale_a"], t["scale_b"])
116 116 
117 117 
118-def test_matmul_mx_rejects_mismatched_k():118+def test_matmul_mx_allows_physical_k_selected_by_valid_shape():
119 t = _mx_tiles(k=64)119 t = _mx_tiles(k=64)
120 t["rhs"] = _tile("rhs", [128, 64], DataType.FP8E5M2, ir.MemorySpace.Right)120 t["rhs"] = _tile("rhs", [128, 64], DataType.FP8E5M2, ir.MemorySpace.Right)
121- with pytest.raises(ValueError, match=r"K dimensions must match, got lhs K=64, rhs K=128"):121+ t["scale_b"] = _tile("scale_b", [4, 64], DataType.FP8E8M0, ir.MemorySpace.ScaleRight)
122- _ir_matmul_mx(t["dst"], t["lhs"], t["rhs"], t["scale_a"], t["scale_b"])122+ 
123+ call = _ir_matmul_mx(t["dst"], t["lhs"], t["rhs"], t["scale_a"], t["scale_b"])
124+ 
125+ assert call.name == "block.matmul_mx"
123 126 
124 127 
125-def test_matmul_mx_rejects_mismatched_output_shape():128+def test_matmul_mx_allows_output_shape_selected_by_valid_shape():
126 t = _mx_tiles()129 t = _mx_tiles()
127- t["dst"] = _tile("dst", [32, 64], DataType.FP32, ir.MemorySpace.Acc)130+ # set_validshape updates descriptor state rather than TileType.shape, so the
131+ # builder must not infer the logical output window from the input physical shapes.
132+ t["dst"] = _tile("dst", [32, 16], DataType.FP32, ir.MemorySpace.Acc)
128 133 
129- with pytest.raises(InvalidType, match=r"dst_tile shape must be \[lhs M, rhs N\]"):134+ call = _ir_matmul_mx(t["dst"], t["lhs"], t["rhs"], t["scale_a"], t["scale_b"])
130- _ir_matmul_mx(t["dst"], t["lhs"], t["rhs"], t["scale_a"], t["scale_b"])135+ 
136+ assert call.name == "block.matmul_mx"
131 137 
132 138 
133def test_matmul_mx_acc_rejects_mismatched_acc_shape():139def test_matmul_mx_acc_rejects_mismatched_acc_shape():
134 t = _mx_tiles()140 t = _mx_tiles()
135 t["acc"] = _tile("acc", [64, 32], DataType.FP32, ir.MemorySpace.Acc)141 t["acc"] = _tile("acc", [64, 32], DataType.FP32, ir.MemorySpace.Acc)
136 142 
137- with pytest.raises(ValueError, match=r"acc_tile shape must match dst_tile shape"):143+ with pytest.raises(InvalidType, match=r"acc_tile type must match dst_tile type"):
144+ _ir_matmul_mx_acc(t["dst"], t["acc"], t["lhs"], t["rhs"], t["scale_a"], t["scale_b"])
145+ 
146+ 
147+def test_matmul_mx_acc_rejects_mismatched_acc_fractal():
148+ t = _mx_tiles()
149+ t["dst"] = _tile(
150+ "dst", [64, 64], DataType.FP32, ir.MemorySpace.Acc,
151+ layout=ir.TensorLayout.NZ, fractal=1024,
152+ )
153+ t["acc"] = _tile(
154+ "acc", [64, 64], DataType.FP32, ir.MemorySpace.Acc,
155+ layout=ir.TensorLayout.NZ, fractal=512,
156+ )
157+ 
158+ with pytest.raises(InvalidType, match=r"acc_tile type must match dst_tile type"):
138 _ir_matmul_mx_acc(t["dst"], t["acc"], t["lhs"], t["rhs"], t["scale_a"], t["scale_b"])159 _ir_matmul_mx_acc(t["dst"], t["acc"], t["lhs"], t["rhs"], t["scale_a"], t["scale_b"])
139 160 
140 161