File: emit_pooling.py

package info (click to toggle)
llvm-toolchain-14 1%3A14.0.6-20
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,496,436 kB
  • sloc: cpp: 5,593,990; ansic: 986,873; asm: 585,869; python: 184,223; objc: 72,530; lisp: 31,119; f90: 27,793; javascript: 9,780; pascal: 9,762; sh: 9,482; perl: 7,468; ml: 5,432; awk: 3,523; makefile: 2,547; xml: 953; cs: 573; fortran: 567
file content (154 lines) | stat: -rw-r--r-- 6,302 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# RUN: %PYTHON %s | FileCheck %s

from mlir.ir import *
from mlir.dialects import builtin
from mlir.dialects import linalg
from mlir.dialects import std

from mlir.dialects.linalg.opdsl.lang import *

T1 = TV.T1
T2 = TV.T2


@linalg_structured_op
def pooling_max_poly(
    I=TensorDef(T1, S.N, S.H, S.W, S.C),
    K=TensorDef(T2, S.KH, S.KW, index_dims=[D.kh, D.kw]),
    O=TensorDef(U, S.N, S.OH, S.OW, S.C, output=True),
    strides=IndexAttrDef(S.SH, S.SW),
    dilations=IndexAttrDef(S.DH, S.DW)):
  domain(D.n, D.oh, D.ow, D.kh, D.kw, D.c)
  O[D.n, D.oh, D.ow, D.c] = ReduceFn.max[D.kh, D.kw](
      TypeFn.cast(
          U, I[D.n, D.oh * S.SH + D.kh * S.DH, D.ow * S.SW + D.kw * S.DW, D.c]))


@linalg_structured_op
def pooling_max_unsigned_poly(
    I=TensorDef(T1, S.N, S.H, S.W, S.C),
    K=TensorDef(T2, S.KH, S.KW, index_dims=[D.kh, D.kw]),
    O=TensorDef(U, S.N, S.OH, S.OW, S.C, output=True),
    strides=IndexAttrDef(S.SH, S.SW),
    dilations=IndexAttrDef(S.DH, S.DW)):
  domain(D.n, D.oh, D.ow, D.kh, D.kw, D.c)
  O[D.n, D.oh, D.ow, D.c] = ReduceFn.max_unsigned[D.kh, D.kw](
      TypeFn.cast_unsigned(
          U, I[D.n, D.oh * S.SH + D.kh * S.DH, D.ow * S.SW + D.kw * S.DW, D.c]))


@linalg_structured_op
def pooling_min_poly(
    I=TensorDef(T1, S.N, S.H, S.W, S.C),
    K=TensorDef(T2, S.KH, S.KW, index_dims=[D.kh, D.kw]),
    O=TensorDef(U, S.N, S.OH, S.OW, S.C, output=True),
    strides=IndexAttrDef(S.SH, S.SW),
    dilations=IndexAttrDef(S.DH, S.DW)):
  domain(D.n, D.oh, D.ow, D.kh, D.kw, D.c)
  O[D.n, D.oh, D.ow, D.c] = ReduceFn.min[D.kh, D.kw](
      TypeFn.cast(
          U, I[D.n, D.oh * S.SH + D.kh * S.DH, D.ow * S.SW + D.kw * S.DW, D.c]))


@linalg_structured_op
def pooling_min_unsigned_poly(
    I=TensorDef(T1, S.N, S.H, S.W, S.C),
    K=TensorDef(T2, S.KH, S.KW, index_dims=[D.kh, D.kw]),
    O=TensorDef(U, S.N, S.OH, S.OW, S.C, output=True),
    strides=IndexAttrDef(S.SH, S.SW),
    dilations=IndexAttrDef(S.DH, S.DW)):
  domain(D.n, D.oh, D.ow, D.kh, D.kw, D.c)
  O[D.n, D.oh, D.ow, D.c] = ReduceFn.min_unsigned[D.kh, D.kw](
      TypeFn.cast_unsigned(
          U, I[D.n, D.oh * S.SH + D.kh * S.DH, D.ow * S.SW + D.kw * S.DW, D.c]))


with Context() as ctx, Location.unknown():
  module = Module.create()
  f32 = F32Type.get()
  i32 = IntegerType.get_signless(32)
  with InsertionPoint(module.body):

    # Pooling indexing maps.
    # CHECK: #[[$POOL_MAP_I:.+]] = affine_map<(d0, d1, d2, d3, d4, d5) -> (d0, d1 * 2 + d3, d2 * 4 + d4 * 2, d5)>
    # CHECK: #[[$POOL_MAP_K:.+]] = affine_map<(d0, d1, d2, d3, d4, d5) -> (d3, d4)>
    # CHECK: #[[$POOL_MAP_O:.+]] = affine_map<(d0, d1, d2, d3, d4, d5) -> (d0, d1, d2, d5)>

    # CHECK-LABEL: @test_f32i32_max_pooling
    # CHECK: linalg.generic
    # CHECK-SAME: indexing_maps = [#[[$POOL_MAP_I]], #[[$POOL_MAP_K]], #[[$POOL_MAP_O]]]
    # CHECK-SAME: iterator_types = ["parallel", "parallel", "parallel", "reduction", "reduction", "parallel"]
    # CHECK:      ^{{.*}}(%[[IN:.+]]: f32, %[[SHAPE:.+]]: f32, %[[OUT:.+]]: i32)
    # CHECK-NEXT:   %[[IN_CAST:.+]] = arith.fptosi %[[IN:.+]] : f32 to i32
    # CHECK-NEXT:   %[[MAX:.+]] = arith.maxsi %[[OUT]], %[[IN_CAST:.+]] : i32
    # CHECK-NEXT:   linalg.yield %[[MAX]] : i32
    # CHECK-NEXT: -> tensor<1x2x4x1xi32>
    @builtin.FuncOp.from_py_func(
        RankedTensorType.get((1, 4, 16, 1), f32),
        RankedTensorType.get((2, 2), f32),
        RankedTensorType.get((1, 2, 4, 1), i32))
    def test_f32i32_max_pooling(input, shape, init_result):
      return pooling_max_poly(
          input, shape, outs=[init_result], strides=[2, 4], dilations=[1, 2])

    # CHECK-LABEL: @test_f32i32_max_unsigned_pooling
    # CHECK:   = arith.fptoui
    # CHECK:   = arith.maxui
    @builtin.FuncOp.from_py_func(
        RankedTensorType.get((1, 4, 16, 1), f32),
        RankedTensorType.get((2, 2), f32),
        RankedTensorType.get((1, 2, 4, 1), i32))
    def test_f32i32_max_unsigned_pooling(input, shape, init_result):
      return pooling_max_unsigned_poly(
          input, shape, outs=[init_result], strides=[2, 4], dilations=[1, 2])

    # CHECK-LABEL: @test_f32f32_max_pooling
    # CHECK: linalg.generic
    # CHECK-SAME: indexing_maps = [#[[$POOL_MAP_I]], #[[$POOL_MAP_K]], #[[$POOL_MAP_O]]]
    # CHECK-SAME: iterator_types = ["parallel", "parallel", "parallel", "reduction", "reduction", "parallel"]
    # CHECK:      ^{{.*}}(%[[IN:.+]]: f32, %[[SHAPE:.+]]: f32, %[[OUT:.+]]: f32)
    # CHECK-NEXT:   %[[MAX:.+]] = arith.maxf %[[OUT]], %[[IN:.+]] : f32
    # CHECK-NEXT:   linalg.yield %[[MAX]] : f32
    # CHECK-NEXT: -> tensor<1x2x4x1xf32>
    @builtin.FuncOp.from_py_func(
        RankedTensorType.get((1, 4, 16, 1), f32),
        RankedTensorType.get((2, 2), f32),
        RankedTensorType.get((1, 2, 4, 1), f32))
    def test_f32f32_max_pooling(input, shape, init_result):
      return pooling_max_poly(
          input, shape, outs=[init_result], strides=[2, 4], dilations=[1, 2])

    # CHECK-LABEL: @test_f32i32_min_pooling
    # CHECK:   = arith.fptosi
    # CHECK:   = arith.minsi
    @builtin.FuncOp.from_py_func(
        RankedTensorType.get((1, 4, 16, 1), f32),
        RankedTensorType.get((2, 2), f32),
        RankedTensorType.get((1, 2, 4, 1), i32))
    def test_f32i32_min_pooling(input, shape, init_result):
      return pooling_min_poly(
          input, shape, outs=[init_result], strides=[2, 4], dilations=[1, 2])

    # CHECK-LABEL: @test_f32i32_min_unsigned_pooling
    # CHECK:   = arith.fptoui
    # CHECK:   = arith.minui
    @builtin.FuncOp.from_py_func(
        RankedTensorType.get((1, 4, 16, 1), f32),
        RankedTensorType.get((2, 2), f32),
        RankedTensorType.get((1, 2, 4, 1), i32))
    def test_f32i32_min_unsigned_pooling(input, shape, init_result):
      return pooling_min_unsigned_poly(
          input, shape, outs=[init_result], strides=[2, 4], dilations=[1, 2])

    # CHECK-LABEL: @test_f32f32_min_pooling
    # CHECK:   = arith.minf
    @builtin.FuncOp.from_py_func(
        RankedTensorType.get((1, 4, 16, 1), f32),
        RankedTensorType.get((2, 2), f32),
        RankedTensorType.get((1, 2, 4, 1), f32))
    def test_f32f32_min_pooling(input, shape, init_result):
      return pooling_min_poly(
          input, shape, outs=[init_result], strides=[2, 4], dilations=[1, 2])


print(module)