|
| 1 | +# RUN: %PYTHON %s |
| 2 | + |
| 3 | +from mlir.dialects import arith, func, linalg |
| 4 | +from mlir.dialects.linalg.opdsl.lang import * |
| 5 | +from mlir.ir import * |
| 6 | + |
| 7 | + |
| 8 | +def run(f): |
| 9 | + print("\nTEST:", f.__name__) |
| 10 | + f() |
| 11 | + return f |
| 12 | + |
| 13 | + |
| 14 | +@run |
| 15 | +def test_infer_contraction_dimensions_from_ops(): |
| 16 | + with Context(), Location.unknown(): |
| 17 | + module = Module.create() |
| 18 | + f32 = F32Type.get() |
| 19 | + with InsertionPoint(module.body): |
| 20 | + # === Static shapes === |
| 21 | + m, n, k = 4, 4, 4 |
| 22 | + a_type = RankedTensorType.get((m, k), f32) |
| 23 | + b_type = RankedTensorType.get((k, n), f32) |
| 24 | + c_type = RankedTensorType.get((m, n), f32) |
| 25 | + |
| 26 | + @func.FuncOp.from_py_func(a_type, b_type, c_type) |
| 27 | + def contraction_fn(a, b, c): |
| 28 | + zero = arith.ConstantOp(value=FloatAttr.get(f32, 0.0), result=f32) |
| 29 | + filled = linalg.fill(zero, outs=[c]) |
| 30 | + fill_op = filled.owner |
| 31 | + |
| 32 | + assert not linalg.isa_contraction_op(zero.operation) |
| 33 | + assert not linalg.isa_contraction_op(fill_op) |
| 34 | + assert linalg.infer_contraction_dimensions(fill_op) is None |
| 35 | + |
| 36 | + dim_m = AffineDimExpr.get(0) |
| 37 | + dim_n = AffineDimExpr.get(1) |
| 38 | + dim_k = AffineDimExpr.get(2) |
| 39 | + |
| 40 | + a_map = AffineMap.get(3, 0, [dim_m, dim_k]) |
| 41 | + b_map = AffineMap.get(3, 0, [dim_k, dim_n]) |
| 42 | + c_map = AffineMap.get(3, 0, [dim_m, dim_n]) |
| 43 | + result = linalg.contract( |
| 44 | + a, |
| 45 | + b, |
| 46 | + outs=(filled,), |
| 47 | + indexing_maps=[a_map, b_map, c_map], |
| 48 | + ) |
| 49 | + contraction_op = result.owner |
| 50 | + |
| 51 | + assert linalg.isa_contraction_op(contraction_op) |
| 52 | + dims = linalg.infer_contraction_dimensions(contraction_op) |
| 53 | + assert dims is not None |
| 54 | + |
| 55 | + # Expect m=[0], n=[1], k=[2] as per standard matmul |
| 56 | + assert list(dims.m) == [0], f"Expected m=[0], got {list(dims.m)}" |
| 57 | + assert list(dims.n) == [1], f"Expected n=[1], got {list(dims.n)}" |
| 58 | + assert list(dims.k) == [2], f"Expected k=[2], got {list(dims.k)}" |
| 59 | + assert ( |
| 60 | + list(dims.batch) == [] |
| 61 | + ), f"Expected batch=[], got {list(dims.batch)}" |
| 62 | + |
| 63 | + # === Dynamic shape case === |
| 64 | + dyn = ShapedType.get_dynamic_size() |
| 65 | + a_dyn_type = RankedTensorType.get((4, dyn), f32) |
| 66 | + b_dyn_type = RankedTensorType.get((dyn, 4), f32) |
| 67 | + c_type = RankedTensorType.get((4, 4), f32) |
| 68 | + |
| 69 | + @func.FuncOp.from_py_func(a_dyn_type, b_dyn_type, c_type) |
| 70 | + def dynamic_contraction_fn(a, b, c): |
| 71 | + zero = arith.ConstantOp(value=FloatAttr.get(f32, 0.0), result=f32) |
| 72 | + filled = linalg.fill(zero, outs=[c]) |
| 73 | + dim_m = AffineDimExpr.get(0) |
| 74 | + dim_n = AffineDimExpr.get(1) |
| 75 | + dim_k = AffineDimExpr.get(2) |
| 76 | + |
| 77 | + a_map = AffineMap.get(3, 0, [dim_m, dim_k]) |
| 78 | + b_map = AffineMap.get(3, 0, [dim_k, dim_n]) |
| 79 | + c_map = AffineMap.get(3, 0, [dim_m, dim_n]) |
| 80 | + |
| 81 | + result = linalg.contract( |
| 82 | + a, |
| 83 | + b, |
| 84 | + outs=(filled,), |
| 85 | + indexing_maps=[a_map, b_map, c_map], |
| 86 | + ) |
| 87 | + contraction_op = result.owner |
| 88 | + |
| 89 | + assert linalg.isa_contraction_op(contraction_op) |
| 90 | + dims = linalg.infer_contraction_dimensions(contraction_op) |
| 91 | + assert dims is not None |
| 92 | + assert list(dims.m) == [0], f"Expected m=[0], got {list(dims.m)}" |
| 93 | + assert list(dims.n) == [1], f"Expected n=[1], got {list(dims.n)}" |
| 94 | + assert list(dims.k) == [2], f"Expected k=[2], got {list(dims.k)}" |
| 95 | + assert ( |
| 96 | + list(dims.batch) == [] |
| 97 | + ), f"Expected batch=[], got {list(dims.batch)}" |
0 commit comments