File: sparse_reductions_prod.mlir

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (263 lines) | stat: -rw-r--r-- 10,204 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// DEFINE: %{option} = enable-runtime-library=true
// DEFINE: %{command} = mlir-opt %s --sparse-compiler=%{option} | \
// DEFINE: mlir-cpu-runner \
// DEFINE:  -e entry -entry-point-result=void  \
// DEFINE:  -shared-libs=%mlir_c_runner_utils | \
// DEFINE: FileCheck %s
//
// RUN: %{command}
//
// Do the same run, but now with direct IR generation.
// REDEFINE: %{option} = enable-runtime-library=false
// RUN: %{command}
//
// Do the same run, but now with direct IR generation and vectorization.
// REDEFINE: %{option} = "enable-runtime-library=false vl=2 reassociate-fp-reductions=true enable-index-optimizations=true"
// RUN: %{command}

#SV = #sparse_tensor.encoding<{ lvlTypes = [ "compressed" ] }>
#DV = #sparse_tensor.encoding<{ lvlTypes = [ "dense"      ] }>

#trait_reduction = {
  indexing_maps = [
    affine_map<(i) -> (i)>,  // a
    affine_map<(i) -> ()>    // x (scalar out)
  ],
  iterator_types = ["reduction"],
  doc = "x += PROD_CUSTOM_i a(i)"
}

// An example of vector reductions.
module {

  // Custom prod reduction: stored i32 elements only.
  func.func @prod_dreduction_i32(%arga: tensor<32xi32, #DV>,
                                 %argx: tensor<i32>) -> tensor<i32> {
    %c = tensor.extract %argx[] : tensor<i32>
    %0 = linalg.generic #trait_reduction
      ins(%arga: tensor<32xi32, #DV>)
      outs(%argx: tensor<i32>) {
        ^bb(%a: i32, %b: i32):
          %1 = sparse_tensor.reduce %a, %b, %c : i32 {
            ^bb0(%x: i32, %y: i32):
              %2 = arith.muli %x, %y : i32
              sparse_tensor.yield %2 : i32
          }
          linalg.yield %1 : i32
    } -> tensor<i32>
    return %0 : tensor<i32>
  }

  // Custom prod reduction: stored f32 elements only.
  func.func @prod_dreduction_f32(%arga: tensor<32xf32, #DV>,
                                 %argx: tensor<f32>) -> tensor<f32> {
    %c = tensor.extract %argx[] : tensor<f32>
    %0 = linalg.generic #trait_reduction
      ins(%arga: tensor<32xf32, #DV>)
      outs(%argx: tensor<f32>) {
        ^bb(%a: f32, %b: f32):
          %1 = sparse_tensor.reduce %a, %b, %c : f32 {
            ^bb0(%x: f32, %y: f32):
              %2 = arith.mulf %x, %y : f32
              sparse_tensor.yield %2 : f32
          }
          linalg.yield %1 : f32
    } -> tensor<f32>
    return %0 : tensor<f32>
  }

  // Custom prod reduction: stored i32 elements only.
  func.func @prod_sreduction_i32(%arga: tensor<32xi32, #SV>,
                                 %argx: tensor<i32>) -> tensor<i32> {
    %c = tensor.extract %argx[] : tensor<i32>
    %0 = linalg.generic #trait_reduction
      ins(%arga: tensor<32xi32, #SV>)
      outs(%argx: tensor<i32>) {
        ^bb(%a: i32, %b: i32):
          %1 = sparse_tensor.reduce %a, %b, %c : i32 {
            ^bb0(%x: i32, %y: i32):
              %2 = arith.muli %x, %y : i32
              sparse_tensor.yield %2 : i32
          }
          linalg.yield %1 : i32
    } -> tensor<i32>
    return %0 : tensor<i32>
  }

  // Custom prod reduction: stored f32 elements only.
  func.func @prod_sreduction_f32(%arga: tensor<32xf32, #SV>,
                                 %argx: tensor<f32>) -> tensor<f32> {
    %c = tensor.extract %argx[] : tensor<f32>
    %0 = linalg.generic #trait_reduction
      ins(%arga: tensor<32xf32, #SV>)
      outs(%argx: tensor<f32>) {
        ^bb(%a: f32, %b: f32):
          %1 = sparse_tensor.reduce %a, %b, %c : f32 {
            ^bb0(%x: f32, %y: f32):
              %2 = arith.mulf %x, %y : f32
              sparse_tensor.yield %2 : f32
          }
          linalg.yield %1 : f32
    } -> tensor<f32>
    return %0 : tensor<f32>
  }

  // Custom prod reduction: stored i32 elements and implicit zeros.
  //
  // NOTE: this is a somewhat strange operation, since for most sparse
  //       situations the outcome would always be zero; it is added
  //       to test full functionality and illustrate the subtle differences
  //       between the various custom operations; it would make a bit more
  //       sense for e.g. a min/max reductions, although it still would
  //       "densify" the iteration space.
  //
  func.func @prod_xreduction_i32(%arga: tensor<32xi32, #SV>,
                                 %argx: tensor<i32>) -> tensor<i32> {
    %c = tensor.extract %argx[] : tensor<i32>
    %0 = linalg.generic #trait_reduction
      ins(%arga: tensor<32xi32, #SV>)
      outs(%argx: tensor<i32>) {
        ^bb(%a: i32, %b: i32):
           %u = sparse_tensor.unary %a : i32 to i32
           present={
             ^bb0(%x: i32):
             sparse_tensor.yield %x : i32
           } absent={
             ^bb0:
             %c0 = arith.constant 0 : i32
             sparse_tensor.yield %c0 : i32
          }
          %1 = sparse_tensor.reduce %u, %b, %c : i32 {
            ^bb0(%x: i32, %y: i32):
              %2 = arith.muli %x, %y : i32
              sparse_tensor.yield %2 : i32
          }
          linalg.yield %1 : i32
    } -> tensor<i32>
    return %0 : tensor<i32>
  }


  func.func @dump_i32(%arg0 : tensor<i32>) {
    %v = tensor.extract %arg0[] : tensor<i32>
    vector.print %v : i32
    return
  }

  func.func @dump_f32(%arg0 : tensor<f32>) {
    %v = tensor.extract %arg0[] : tensor<f32>
    vector.print %v : f32
    return
  }

  func.func @entry() {
    %ri = arith.constant dense< 7   > : tensor<i32>
    %rf = arith.constant dense< 2.0 > : tensor<f32>

    // Vectors with a few zeros.
    %c_0_i32 = arith.constant dense<[
      1, 1, 7, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1,
      1, 1, 1, 1, 3, 0, 1, 1, 1, 1, 1, 0, 1, 1, 7, 3
    ]> : tensor<32xi32>

    %c_0_f32 = arith.constant dense<[
      1.0, 1.0, 1.0, 3.5, 1.0, 1.0, 1.0, 1.0,
      1.0, 0.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0,
      1.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0, 1.0,
      1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0
    ]> : tensor<32xf32>

    // Vectors with no zeros.
    %c_1_i32 = arith.constant dense<[
      1, 1, 7, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
      1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 7, 3
    ]> : tensor<32xi32>

    %c_1_f32 = arith.constant dense<[
      1.0, 1.0, 1.0, 3.5, 1.0, 1.0, 1.0, 1.0,
      1.0, 1.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0,
      1.0, 1.0, 1.0, 1.0, 3.0, 1.0, 1.0, 1.0,
      1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 4.0
    ]> : tensor<32xf32>

    // Convert constants to annotated tensors. Note that this
    // particular conversion only stores nonzero elements,
    // so we will have no explicit zeros, only implicit zeros.
    %d0_i32 = sparse_tensor.convert %c_0_i32
      : tensor<32xi32> to tensor<32xi32, #DV>
    %d0_f32 = sparse_tensor.convert %c_0_f32
      : tensor<32xf32> to tensor<32xf32, #DV>
    %s0_i32 = sparse_tensor.convert %c_0_i32
      : tensor<32xi32> to tensor<32xi32, #SV>
    %s0_f32 = sparse_tensor.convert %c_0_f32
      : tensor<32xf32> to tensor<32xf32, #SV>
    %d1_i32 = sparse_tensor.convert %c_1_i32
      : tensor<32xi32> to tensor<32xi32, #DV>
    %d1_f32 = sparse_tensor.convert %c_1_f32
      : tensor<32xf32> to tensor<32xf32, #DV>
    %s1_i32 = sparse_tensor.convert %c_1_i32
      : tensor<32xi32> to tensor<32xi32, #SV>
    %s1_f32 = sparse_tensor.convert %c_1_f32
      : tensor<32xf32> to tensor<32xf32, #SV>

    // Special case, construct a sparse vector with an explicit zero.
    %v0 = arith.constant sparse< [ [1] ], [ 0 ] > : tensor<32xi32>
    %s0 = sparse_tensor.convert %v0: tensor<32xi32> to tensor<32xi32, #SV>

    // Call the kernels.
    %0 = call @prod_dreduction_i32(%d0_i32, %ri) : (tensor<32xi32, #DV>, tensor<i32>) -> tensor<i32>
    %1 = call @prod_dreduction_f32(%d0_f32, %rf) : (tensor<32xf32, #DV>, tensor<f32>) -> tensor<f32>
    %2 = call @prod_sreduction_i32(%s0_i32, %ri) : (tensor<32xi32, #SV>, tensor<i32>) -> tensor<i32>
    %3 = call @prod_sreduction_f32(%s0_f32, %rf) : (tensor<32xf32, #SV>, tensor<f32>) -> tensor<f32>
    %4 = call @prod_dreduction_i32(%d1_i32, %ri) : (tensor<32xi32, #DV>, tensor<i32>) -> tensor<i32>
    %5 = call @prod_dreduction_f32(%d1_f32, %rf) : (tensor<32xf32, #DV>, tensor<f32>) -> tensor<f32>
    %6 = call @prod_sreduction_i32(%s1_i32, %ri) : (tensor<32xi32, #SV>, tensor<i32>) -> tensor<i32>
    %7 = call @prod_sreduction_f32(%s1_f32, %rf) : (tensor<32xf32, #SV>, tensor<f32>) -> tensor<f32>
    %8 = call @prod_sreduction_i32(%s0,     %ri) : (tensor<32xi32, #SV>, tensor<i32>) -> tensor<i32>
    %9 = call @prod_xreduction_i32(%s0_i32, %ri) : (tensor<32xi32, #SV>, tensor<i32>) -> tensor<i32>
    %10 = call @prod_xreduction_i32(%s1_i32, %ri) : (tensor<32xi32, #SV>, tensor<i32>) -> tensor<i32>

    // Verify results. Note that the custom reduction gave permission
    // to treat an explicit vs implicit zero differently to compute the
    // full product reduction over stored elements. A "standard" product
    // reduction would have to return 0 for any implicit zero occurrence
    // too. An explicit zero nullifies the product, though, as requested.
    //
    // CHECK: 0
    // CHECK: 0
    // CHECK: 3087
    // CHECK: 14
    // CHECK: 3087
    // CHECK: 168
    // CHECK: 3087
    // CHECK: 168
    // CHECK: 0
    // CHECK: 0
    // CHECK: 3087
    //
    call @dump_i32(%0) : (tensor<i32>) -> ()
    call @dump_f32(%1) : (tensor<f32>) -> ()
    call @dump_i32(%2) : (tensor<i32>) -> ()
    call @dump_f32(%3) : (tensor<f32>) -> ()
    call @dump_i32(%4) : (tensor<i32>) -> ()
    call @dump_f32(%5) : (tensor<f32>) -> ()
    call @dump_i32(%6) : (tensor<i32>) -> ()
    call @dump_f32(%7) : (tensor<f32>) -> ()
    call @dump_i32(%8) : (tensor<i32>) -> ()
    call @dump_i32(%9) : (tensor<i32>) -> ()
    call @dump_i32(%10) : (tensor<i32>) -> ()

    // Release the resources.
    bufferization.dealloc_tensor %d0_i32 : tensor<32xi32, #DV>
    bufferization.dealloc_tensor %d0_f32 : tensor<32xf32, #DV>
    bufferization.dealloc_tensor %s0_i32 : tensor<32xi32, #SV>
    bufferization.dealloc_tensor %s0_f32 : tensor<32xf32, #SV>
    bufferization.dealloc_tensor %d1_i32 : tensor<32xi32, #DV>
    bufferization.dealloc_tensor %d1_f32 : tensor<32xf32, #DV>
    bufferization.dealloc_tensor %s1_i32 : tensor<32xi32, #SV>
    bufferization.dealloc_tensor %s1_f32 : tensor<32xf32, #SV>
    bufferization.dealloc_tensor %s0     : tensor<32xi32, #SV>

    return
  }
}