xref: /llvm-project/mlir/test/Dialect/SparseTensor/sparse_relu.mlir (revision 70e227a404e51f9248c7ad5d79953805b2afacb4)
1// RUN: mlir-opt %s --sparsification-and-bufferization | FileCheck %s
2
3#map = affine_map<(d0, d1, d2) -> (d0, d1, d2)>
4
5#sparse = #sparse_tensor.encoding<{
6    map = (d0, d1, d2) -> (d0 : dense, d1 : dense, d2 : compressed)
7}>
8
9//
10// Make sure a simple ReLU passes the sparsifier
11//
12// CHECK-LABEL: func.func @relu
13// CHECK:       scf.for
14// CHECK:         scf.for
15// CHECK:           scf.for
16// CHECK:             arith.cmpf ugt
17// CHECK:             arith.select
18//
19func.func @relu(%arg0: tensor<10x20x30xf64, #sparse>) -> tensor<10x20x30xf64, #sparse> {
20  %cst = arith.constant 0.000000e+00 : f64
21  %0 = tensor.empty() : tensor<10x20x30xf64>
22  %1 = linalg.generic {
23      indexing_maps = [#map, #map],
24      iterator_types = ["parallel", "parallel", "parallel"]}
25      ins(%arg0 : tensor<10x20x30xf64, #sparse>)
26      outs(%0 : tensor<10x20x30xf64>) {
27  ^bb0(%in: f64, %out: f64):
28      %2 = arith.cmpf ugt, %in, %cst : f64
29      %3 = arith.select %2, %in, %cst : f64
30      linalg.yield %3 : f64
31  } -> tensor<10x20x30xf64>
32  %cast = tensor.cast %1 : tensor<10x20x30xf64> to tensor<10x20x30xf64, #sparse>
33  return %cast : tensor<10x20x30xf64, #sparse>
34}
35