1# RUN: %PYTHON %s | FileCheck %s 2 3from mlir.ir import * 4from mlir.dialects import builtin 5from mlir.dialects import linalg 6from mlir.dialects import std 7 8from mlir.dialects.linalg.opdsl.lang import * 9 10T1 = TV.T1 11T2 = TV.T2 12 13 14@linalg_structured_op 15def pooling_max_poly( 16 I=TensorDef(T1, S.N, S.H, S.W, S.C), 17 K=TensorDef(T2, S.KH, S.KW, index_dims=[D.kh, D.kw]), 18 O=TensorDef(U, S.N, S.OH, S.OW, S.C, output=True), 19 strides=IndexAttrDef(S.SH, S.SW, default=[1, 1]), 20 dilations=IndexAttrDef(S.DH, S.DW, default=[1, 1])): 21 domain(D.n, D.oh, D.ow, D.kh, D.kw, D.c) 22 O[D.n, D.oh, D.ow, D.c] = ReduceFn.max[D.kh, D.kw]( 23 TypeFn.cast( 24 U, I[D.n, D.oh * S.SH + D.kh * S.DH, D.ow * S.SW + D.kw * S.DW, D.c])) 25 26 27@linalg_structured_op 28def pooling_max_unsigned_poly( 29 I=TensorDef(T1, S.N, S.H, S.W, S.C), 30 K=TensorDef(T2, S.KH, S.KW, index_dims=[D.kh, D.kw]), 31 O=TensorDef(U, S.N, S.OH, S.OW, S.C, output=True), 32 strides=IndexAttrDef(S.SH, S.SW, default=[1, 1]), 33 dilations=IndexAttrDef(S.DH, S.DW, default=[1, 1])): 34 domain(D.n, D.oh, D.ow, D.kh, D.kw, D.c) 35 O[D.n, D.oh, D.ow, D.c] = ReduceFn.max_unsigned[D.kh, D.kw]( 36 TypeFn.cast_unsigned( 37 U, I[D.n, D.oh * S.SH + D.kh * S.DH, D.ow * S.SW + D.kw * S.DW, D.c])) 38 39 40@linalg_structured_op 41def pooling_min_poly( 42 I=TensorDef(T1, S.N, S.H, S.W, S.C), 43 K=TensorDef(T2, S.KH, S.KW, index_dims=[D.kh, D.kw]), 44 O=TensorDef(U, S.N, S.OH, S.OW, S.C, output=True), 45 strides=IndexAttrDef(S.SH, S.SW, default=[1, 1]), 46 dilations=IndexAttrDef(S.DH, S.DW, default=[1, 1])): 47 domain(D.n, D.oh, D.ow, D.kh, D.kw, D.c) 48 O[D.n, D.oh, D.ow, D.c] = ReduceFn.min[D.kh, D.kw]( 49 TypeFn.cast( 50 U, I[D.n, D.oh * S.SH + D.kh * S.DH, D.ow * S.SW + D.kw * S.DW, D.c])) 51 52 53@linalg_structured_op 54def pooling_min_unsigned_poly( 55 I=TensorDef(T1, S.N, S.H, S.W, S.C), 56 K=TensorDef(T2, S.KH, S.KW, index_dims=[D.kh, D.kw]), 57 O=TensorDef(U, S.N, S.OH, S.OW, S.C, output=True), 58 strides=IndexAttrDef(S.SH, S.SW, default=[1, 1]), 59 dilations=IndexAttrDef(S.DH, S.DW, default=[1, 1])): 60 domain(D.n, D.oh, D.ow, D.kh, D.kw, D.c) 61 O[D.n, D.oh, D.ow, D.c] = ReduceFn.min_unsigned[D.kh, D.kw]( 62 TypeFn.cast_unsigned( 63 U, I[D.n, D.oh * S.SH + D.kh * S.DH, D.ow * S.SW + D.kw * S.DW, D.c])) 64 65 66with Context() as ctx, Location.unknown(): 67 module = Module.create() 68 f32 = F32Type.get() 69 i32 = IntegerType.get_signless(32) 70 with InsertionPoint(module.body): 71 72 # Pooling indexing maps. 73 # CHECK: #[[$POOL_MAP_I:.+]] = affine_map<(d0, d1, d2, d3, d4, d5) -> (d0, d1 * 2 + d3, d2 * 4 + d4 * 2, d5)> 74 # CHECK: #[[$POOL_MAP_K:.+]] = affine_map<(d0, d1, d2, d3, d4, d5) -> (d3, d4)> 75 # CHECK: #[[$POOL_MAP_O:.+]] = affine_map<(d0, d1, d2, d3, d4, d5) -> (d0, d1, d2, d5)> 76 77 # CHECK-LABEL: @test_f32i32_max_pooling 78 # CHECK: linalg.generic 79 # CHECK-SAME: indexing_maps = [#[[$POOL_MAP_I]], #[[$POOL_MAP_K]], #[[$POOL_MAP_O]]] 80 # CHECK-SAME: iterator_types = ["parallel", "parallel", "parallel", "reduction", "reduction", "parallel"] 81 # CHECK: ^{{.*}}(%[[IN:.+]]: f32, %[[SHAPE:.+]]: f32, %[[OUT:.+]]: i32) 82 # CHECK-NEXT: %[[IN_CAST:.+]] = arith.fptosi %[[IN:.+]] : f32 to i32 83 # CHECK-NEXT: %[[MAX:.+]] = arith.maxsi %[[OUT]], %[[IN_CAST:.+]] : i32 84 # CHECK-NEXT: linalg.yield %[[MAX]] : i32 85 # CHECK-NEXT: -> tensor<1x2x4x1xi32> 86 @builtin.FuncOp.from_py_func( 87 RankedTensorType.get((1, 4, 16, 1), f32), 88 RankedTensorType.get((2, 2), f32), 89 RankedTensorType.get((1, 2, 4, 1), i32)) 90 def test_f32i32_max_pooling(input, shape, init_result): 91 return pooling_max_poly( 92 input, shape, outs=[init_result], strides=[2, 4], dilations=[1, 2]) 93 94 # CHECK-LABEL: @test_f32i32_max_unsigned_pooling 95 # CHECK: = arith.fptoui 96 # CHECK: = arith.maxui 97 @builtin.FuncOp.from_py_func( 98 RankedTensorType.get((1, 4, 16, 1), f32), 99 RankedTensorType.get((2, 2), f32), 100 RankedTensorType.get((1, 2, 4, 1), i32)) 101 def test_f32i32_max_unsigned_pooling(input, shape, init_result): 102 return pooling_max_unsigned_poly( 103 input, shape, outs=[init_result], strides=[2, 4], dilations=[1, 2]) 104 105 # CHECK-LABEL: @test_f32f32_max_pooling 106 # CHECK: linalg.generic 107 # CHECK-SAME: indexing_maps = [#[[$POOL_MAP_I]], #[[$POOL_MAP_K]], #[[$POOL_MAP_O]]] 108 # CHECK-SAME: iterator_types = ["parallel", "parallel", "parallel", "reduction", "reduction", "parallel"] 109 # CHECK: ^{{.*}}(%[[IN:.+]]: f32, %[[SHAPE:.+]]: f32, %[[OUT:.+]]: f32) 110 # CHECK-NEXT: %[[MAX:.+]] = arith.maxf %[[OUT]], %[[IN:.+]] : f32 111 # CHECK-NEXT: linalg.yield %[[MAX]] : f32 112 # CHECK-NEXT: -> tensor<1x2x4x1xf32> 113 @builtin.FuncOp.from_py_func( 114 RankedTensorType.get((1, 4, 16, 1), f32), 115 RankedTensorType.get((2, 2), f32), 116 RankedTensorType.get((1, 2, 4, 1), f32)) 117 def test_f32f32_max_pooling(input, shape, init_result): 118 return pooling_max_poly( 119 input, shape, outs=[init_result], strides=[2, 4], dilations=[1, 2]) 120 121 # CHECK-LABEL: @test_f32i32_min_pooling 122 # CHECK: = arith.fptosi 123 # CHECK: = arith.minsi 124 @builtin.FuncOp.from_py_func( 125 RankedTensorType.get((1, 4, 16, 1), f32), 126 RankedTensorType.get((2, 2), f32), 127 RankedTensorType.get((1, 2, 4, 1), i32)) 128 def test_f32i32_min_pooling(input, shape, init_result): 129 return pooling_min_poly( 130 input, shape, outs=[init_result], strides=[2, 4], dilations=[1, 2]) 131 132 # CHECK-LABEL: @test_f32i32_min_unsigned_pooling 133 # CHECK: = arith.fptoui 134 # CHECK: = arith.minui 135 @builtin.FuncOp.from_py_func( 136 RankedTensorType.get((1, 4, 16, 1), f32), 137 RankedTensorType.get((2, 2), f32), 138 RankedTensorType.get((1, 2, 4, 1), i32)) 139 def test_f32i32_min_unsigned_pooling(input, shape, init_result): 140 return pooling_min_unsigned_poly( 141 input, shape, outs=[init_result], strides=[2, 4], dilations=[1, 2]) 142 143 # CHECK-LABEL: @test_f32f32_min_pooling 144 # CHECK: = arith.minf 145 @builtin.FuncOp.from_py_func( 146 RankedTensorType.get((1, 4, 16, 1), f32), 147 RankedTensorType.get((2, 2), f32), 148 RankedTensorType.get((1, 2, 4, 1), f32)) 149 def test_f32f32_min_pooling(input, shape, init_result): 150 return pooling_min_poly( 151 input, shape, outs=[init_result], strides=[2, 4], dilations=[1, 2]) 152 153 154print(module) 155