diff --git a/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_tensor_mul.mlir b/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_tensor_mul.mlir new file mode 100644 --- /dev/null +++ b/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_tensor_mul.mlir @@ -0,0 +1,102 @@ +// RUN: mlir-opt %s --sparse-compiler | \ +// RUN: mlir-cpu-runner \ +// RUN: -e entry -entry-point-result=void \ +// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \ +// RUN: FileCheck %s + +#ST = #sparse_tensor.encoding<{dimLevelType = ["compressed", "compressed", "compressed"]}> + +// +// Trait for 3-d tensor element wise multiplication. +// +#trait_mul = { + indexing_maps = [ + affine_map<(i,j,k) -> (i,j,k)>, // A (in) + affine_map<(i,j,k) -> (i,j,k)>, // B (in) + affine_map<(i,j,k) -> (i,j,k)> // X (out) + ], + iterator_types = ["parallel", "parallel", "parallel"], + doc = "X(i,j,k) = A(i,j,k) * B(i,j,k)" +} + +module { + // Multiplies two 3-d sparse tensors element-wise into a new sparse tensor. + func.func @tensor_mul(%arga: tensor, + %argb: tensor) -> tensor { + %c0 = arith.constant 0 : index + %c1 = arith.constant 1 : index + %c2 = arith.constant 2 : index + %d0 = tensor.dim %arga, %c0 : tensor + %d1 = tensor.dim %arga, %c1 : tensor + %d2 = tensor.dim %arga, %c2 : tensor + %xt = bufferization.alloc_tensor(%d0, %d1, %d2) : tensor + %0 = linalg.generic #trait_mul + ins(%arga, %argb: tensor, tensor) + outs(%xt: tensor) { + ^bb(%a: f64, %b: f64, %x: f64): + %1 = arith.mulf %a, %b : f64 + linalg.yield %1 : f64 + } -> tensor + return %0 : tensor + } + + // Driver method to call and verify tensor multiplication kernel. + func.func @entry() { + %c0 = arith.constant 0 : index + %default_val = arith.constant -1.0 : f64 + + // Setup sparse tensor A + %ta = arith.constant dense< + [ [ [1.0, 0.0, 0.0, 0.0, 0.0 ], + [0.0, 0.0, 0.0, 0.0, 0.0 ], + [1.2, 0.0, 3.5, 0.0, 0.0 ] ], + [ [0.0, 0.0, 0.0, 0.0, 0.0 ], + [0.0, 0.0, 0.0, 0.0, 0.0 ], + [0.0, 0.0, 0.0, 0.0, 0.0 ] ], + [ [2.0, 0.0, 0.0, 0.0, 0.0 ], + [0.0, 0.0, 0.0, 0.0, 0.0 ], + [0.0, 0.0, 4.0, 0.0, 0.0 ]] ]> : tensor<3x3x5xf64> + + // Setup sparse tensor B + %tb = arith.constant dense< + [ [ [0.0, 0.0, 0.0, 0.0, 4.0 ], + [0.0, 0.0, 0.0, 0.0, 0.0 ], + [2.0, 0.0, 1.0, 0.0, 0.0 ] ], + [ [0.0, 0.0, 0.0, 0.0, 9.0 ], + [0.0, 0.0, 0.0, 0.0, 0.0 ], + [0.0, 7.0, 0.0, 0.0, 0.0 ] ], + [ [1.0, 0.0, 0.0, 0.0, 0.0 ], + [0.0, 0.0, 0.0, 0.0, 0.0 ], + [0.0, 0.0, 2.0, 0.0, 0.0 ]] ]> : tensor<3x3x5xf64> + + %sta = sparse_tensor.convert %ta : tensor<3x3x5xf64> to tensor + %stb = sparse_tensor.convert %tb : tensor<3x3x5xf64> to tensor + + + // Call sparse tensor multiplication kernel. + %0 = call @tensor_mul(%sta, %stb) + : (tensor, tensor) -> tensor + + // Verify results + // + // CHECK: ( 2.4, 3.5, 2, 8, -1, -1, -1, -1 ) + // CHECK-NEXT: ( ( ( 0, 0, 0, 0, 0 ), ( 0, 0, 0, 0, 0 ), ( 2.4, 0, 3.5, 0, 0 ) ), + // CHECK-SAME: ( ( 0, 0, 0, 0, 0 ), ( 0, 0, 0, 0, 0 ), ( 0, 0, 0, 0, 0 ) ), + // CHECK-SAME: ( ( 2, 0, 0, 0, 0 ), ( 0, 0, 0, 0, 0 ), ( 0, 0, 8, 0, 0 ) ) ) + // + %m1 = sparse_tensor.values %0 : tensor to memref + %v1 = vector.transfer_read %m1[%c0], %default_val: memref, vector<8xf64> + vector.print %v1 : vector<8xf64> + + // Print %0 in dense form. + %dt = sparse_tensor.convert %0 : tensor to tensor + %v2 = vector.transfer_read %dt[%c0, %c0, %c0], %default_val: tensor, vector<3x3x5xf64> + vector.print %v2 : vector<3x3x5xf64> + + // Release the resources. + sparse_tensor.release %sta : tensor + sparse_tensor.release %stb : tensor + sparse_tensor.release %0 : tensor + return + } +}