|
| 1 | +# RUN: %PYTHON %s | FileCheck %s |
| 2 | + |
| 3 | +from mlir import ir |
| 4 | + |
| 5 | +import lighthouse.dialects as lh_dialects |
| 6 | +from lighthouse.schedule import lower_vector_multi_reductions |
| 7 | + |
| 8 | + |
| 9 | +def run(name: str, payload_str: str): |
| 10 | + print(f"Test: {name}", flush=True) |
| 11 | + with ir.Context(), ir.Location.unknown(): |
| 12 | + lh_dialects.register_and_load() |
| 13 | + payload = ir.Module.parse(payload_str) |
| 14 | + sched = lower_vector_multi_reductions() |
| 15 | + sched.body.operations[0].apply(payload.operation) |
| 16 | + print(payload) |
| 17 | + |
| 18 | + |
| 19 | +# Inner (row) reduction: one horizontal reduction per row, no transpose. |
| 20 | +# CHECK-LABEL: Test: row |
| 21 | +# CHECK-NOT: vector.transpose |
| 22 | +# CHECK-COUNT-2: vector.reduction <add>, %{{.*}}, %{{.*}} : vector<64xf32> into f32 |
| 23 | +# CHECK-NOT: vector.multi_reduction |
| 24 | +run( |
| 25 | + "row", |
| 26 | + """ |
| 27 | +func.func @row(%a: vector<2x64xf32>, %acc: vector<2xf32>) -> vector<2xf32> { |
| 28 | + %0 = vector.multi_reduction <add>, %a, %acc [1] : vector<2x64xf32> to vector<2xf32> |
| 29 | + return %0 : vector<2xf32> |
| 30 | +} |
| 31 | +""", |
| 32 | +) |
| 33 | + |
| 34 | + |
| 35 | +# Outer (column) reduction: lane-wise adds, no transpose. |
| 36 | +# CHECK-LABEL: Test: column |
| 37 | +# CHECK-NOT: vector.transpose |
| 38 | +# CHECK-COUNT-2: arith.addf %{{.*}}, %{{.*}} : vector<32xf32> |
| 39 | +# CHECK-NOT: vector.multi_reduction |
| 40 | +run( |
| 41 | + "column", |
| 42 | + """ |
| 43 | +func.func @column(%a: vector<2x32xf32>, %acc: vector<32xf32>) -> vector<32xf32> { |
| 44 | + %0 = vector.multi_reduction <add>, %a, %acc [0] : vector<2x32xf32> to vector<32xf32> |
| 45 | + return %0 : vector<32xf32> |
| 46 | +} |
| 47 | +""", |
| 48 | +) |
| 49 | + |
| 50 | + |
| 51 | +# 1-D reduction lowers to a single horizontal reduction. |
| 52 | +# CHECK-LABEL: Test: rank1 |
| 53 | +# CHECK: vector.reduction <maximumf>, %{{.*}}, %{{.*}} : vector<128xf32> into f32 |
| 54 | +# CHECK-NOT: vector.multi_reduction |
| 55 | +run( |
| 56 | + "rank1", |
| 57 | + """ |
| 58 | +func.func @rank1(%a: vector<128xf32>, %acc: f32) -> f32 { |
| 59 | + %0 = vector.multi_reduction <maximumf>, %a, %acc [0] : vector<128xf32> to f32 |
| 60 | + return %0 : f32 |
| 61 | +} |
| 62 | +""", |
| 63 | +) |
| 64 | + |
| 65 | + |
| 66 | +# A reduced middle dim is not handled by either strategy directly and falls |
| 67 | +# back to reordering it outward. |
| 68 | +# CHECK-LABEL: Test: middle |
| 69 | +# CHECK: vector.transpose |
| 70 | +# CHECK-NOT: vector.multi_reduction |
| 71 | +# CHECK: return |
| 72 | +run( |
| 73 | + "middle", |
| 74 | + """ |
| 75 | +func.func @middle(%a: vector<2x4x16xf32>, %acc: vector<2x16xf32>) -> vector<2x16xf32> { |
| 76 | + %0 = vector.multi_reduction <add>, %a, %acc [1] : vector<2x4x16xf32> to vector<2x16xf32> |
| 77 | + return %0 : vector<2x16xf32> |
| 78 | +} |
| 79 | +""", |
| 80 | +) |
0 commit comments