Skip to content

[mlir][sparse] migrate tests to sparse_tensor.print #84055

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// DEFINE: %{compile} = mlir-opt %s --sparsifier="%{sparsifier_opts}"
// DEFINE: %{compile_sve} = mlir-opt %s --sparsifier="%{sparsifier_opts_sve}"
// DEFINE: %{run_libs} = -shared-libs=%mlir_c_runner_utils,%mlir_runner_utils
// DEFINE: %{run_opts} = -e entry -entry-point-result=void
// DEFINE: %{run_opts} = -e main -entry-point-result=void
// DEFINE: %{run} = mlir-cpu-runner %{run_opts} %{run_libs}
// DEFINE: %{run_sve} = %mcr_aarch64_cmd --march=aarch64 --mattr="+sve" %{run_opts} %{run_libs}
//
Expand Down Expand Up @@ -51,11 +51,6 @@

module {
func.func private @printMemrefF64(%ptr : tensor<*xf64>)
func.func private @printMemref1dF64(%ptr : memref<?xf64>) attributes { llvm.emit_c_interface }

//
// Tests without permutation.
//

// Concats all sparse matrices (with different encodings) to a sparse matrix.
func.func @concat_sparse_sparse(%arg0: tensor<2x4xf64, #MAT_C_C>, %arg1: tensor<3x4xf64, #MAT_C_D>, %arg2: tensor<4x4xf64, #MAT_D_C>) -> tensor<9x4xf64, #MAT_C_C> {
Expand Down Expand Up @@ -85,30 +80,15 @@ module {
return %0 : tensor<9x4xf64>
}

func.func @dump_mat_9x4(%A: tensor<9x4xf64, #MAT_C_C>) {
%c = sparse_tensor.convert %A : tensor<9x4xf64, #MAT_C_C> to tensor<9x4xf64>
%cu = tensor.cast %c : tensor<9x4xf64> to tensor<*xf64>
call @printMemrefF64(%cu) : (tensor<*xf64>) -> ()

%n = sparse_tensor.number_of_entries %A : tensor<9x4xf64, #MAT_C_C>
vector.print %n : index

%1 = sparse_tensor.values %A : tensor<9x4xf64, #MAT_C_C> to memref<?xf64>
call @printMemref1dF64(%1) : (memref<?xf64>) -> ()

bufferization.dealloc_tensor %c : tensor<9x4xf64>
return
}

// Outputs dense matrix.
func.func @dump_mat_dense_9x4(%A: tensor<9x4xf64>) {
%u = tensor.cast %A : tensor<9x4xf64> to tensor<*xf64>
call @printMemrefF64(%u) : (tensor<*xf64>) -> ()

return
}

// Driver method to call and verify kernels.
func.func @entry() {
func.func @main() {
%m24 = arith.constant dense<
[ [ 1.0, 0.0, 3.0, 0.0],
[ 0.0, 2.0, 0.0, 0.0] ]> : tensor<2x4xf64>
Expand All @@ -126,22 +106,24 @@ module {
%sm34cd = sparse_tensor.convert %m34 : tensor<3x4xf64> to tensor<3x4xf64, #MAT_C_D>
%sm44dc = sparse_tensor.convert %m44 : tensor<4x4xf64> to tensor<4x4xf64, #MAT_D_C>

// CHECK: {{\[}}[1, 0, 3, 0],
// CHECK-NEXT: [0, 2, 0, 0],
// CHECK-NEXT: [1, 0, 1, 1],
// CHECK-NEXT: [0, 0.5, 0, 0],
// CHECK-NEXT: [1, 5, 2, 0],
// CHECK-NEXT: [0, 0, 1.5, 1],
// CHECK-NEXT: [0, 3.5, 0, 0],
// CHECK-NEXT: [1, 5, 2, 0],
// CHECK-NEXT: [1, 0.5, 0, 0]]
// CHECK-NEXT: 18
// CHECK: [1, 3, 2, 1, 1, 1, 0.5, 1, 5, 2, 1.5, 1, 3.5, 1, 5, 2, 1, 0.5
//
// CHECK: ---- Sparse Tensor ----
// CHECK-NEXT: nse = 18
// CHECK-NEXT: dim = ( 9, 4 )
// CHECK-NEXT: lvl = ( 9, 4 )
// CHECK-NEXT: pos[0] : ( 0, 9,
// CHECK-NEXT: crd[0] : ( 0, 1, 2, 3, 4, 5, 6, 7, 8,
// CHECK-NEXT: pos[1] : ( 0, 2, 3, 6, 7, 10, 12, 13, 16, 18,
// CHECK-NEXT: crd[1] : ( 0, 2, 1, 0, 2, 3, 1, 0, 1, 2, 2, 3, 1, 0, 1, 2, 0, 1,
// CHECK-NEXT: values : ( 1, 3, 2, 1, 1, 1, 0.5, 1, 5, 2, 1.5, 1, 3.5, 1, 5, 2, 1, 0.5,
// CHECK-NEXT: ----
//
%0 = call @concat_sparse_sparse(%sm24cc, %sm34cd, %sm44dc)
: (tensor<2x4xf64, #MAT_C_C>, tensor<3x4xf64, #MAT_C_D>, tensor<4x4xf64, #MAT_D_C>) -> tensor<9x4xf64, #MAT_C_C>
call @dump_mat_9x4(%0) : (tensor<9x4xf64, #MAT_C_C>) -> ()
sparse_tensor.print %0 : tensor<9x4xf64, #MAT_C_C>

// CHECK: {{\[}}[1, 0, 3, 0],
//
// CHECK: {{\[}}[1, 0, 3, 0],
// CHECK-NEXT: [0, 2, 0, 0],
// CHECK-NEXT: [1, 0, 1, 1],
// CHECK-NEXT: [0, 0.5, 0, 0],
Expand All @@ -150,26 +132,29 @@ module {
// CHECK-NEXT: [0, 3.5, 0, 0],
// CHECK-NEXT: [1, 5, 2, 0],
// CHECK-NEXT: [1, 0.5, 0, 0]]
//
%1 = call @concat_sparse_dense(%sm24cc, %sm34cd, %sm44dc)
: (tensor<2x4xf64, #MAT_C_C>, tensor<3x4xf64, #MAT_C_D>, tensor<4x4xf64, #MAT_D_C>) -> tensor<9x4xf64>
call @dump_mat_dense_9x4(%1) : (tensor<9x4xf64>) -> ()

// CHECK: {{\[}}[1, 0, 3, 0],
// CHECK-NEXT: [0, 2, 0, 0],
// CHECK-NEXT: [1, 0, 1, 1],
// CHECK-NEXT: [0, 0.5, 0, 0],
// CHECK-NEXT: [1, 5, 2, 0],
// CHECK-NEXT: [0, 0, 1.5, 1],
// CHECK-NEXT: [0, 3.5, 0, 0],
// CHECK-NEXT: [1, 5, 2, 0],
// CHECK-NEXT: [1, 0.5, 0, 0]]
// CHECK-NEXT: 18
// CHECK: [1, 3, 2, 1, 1, 1, 0.5, 1, 5, 2, 1.5, 1, 3.5, 1, 5, 2, 1, 0.5
//
// CHECK: ---- Sparse Tensor ----
// CHECK-NEXT: nse = 18
// CHECK-NEXT: dim = ( 9, 4 )
// CHECK-NEXT: lvl = ( 9, 4 )
// CHECK-NEXT: pos[0] : ( 0, 9,
// CHECK-NEXT: crd[0] : ( 0, 1, 2, 3, 4, 5, 6, 7, 8,
// CHECK-NEXT: pos[1] : ( 0, 2, 3, 6, 7, 10, 12, 13, 16, 18,
// CHECK-NEXT: crd[1] : ( 0, 2, 1, 0, 2, 3, 1, 0, 1, 2, 2, 3, 1, 0, 1, 2, 0, 1,
// CHECK-NEXT: values : ( 1, 3, 2, 1, 1, 1, 0.5, 1, 5, 2, 1.5, 1, 3.5, 1, 5, 2, 1, 0.5,
// CHECK-NEXT: ----
//
%2 = call @concat_mix_sparse(%m24, %sm34cd, %sm44dc)
: (tensor<2x4xf64>, tensor<3x4xf64, #MAT_C_D>, tensor<4x4xf64, #MAT_D_C>) -> tensor<9x4xf64, #MAT_C_C>
call @dump_mat_9x4(%2) : (tensor<9x4xf64, #MAT_C_C>) -> ()
sparse_tensor.print %2 : tensor<9x4xf64, #MAT_C_C>

// CHECK: {{\[}}[1, 0, 3, 0],
//
// CHECK: {{\[}}[1, 0, 3, 0],
// CHECK-NEXT: [0, 2, 0, 0],
// CHECK-NEXT: [1, 0, 1, 1],
// CHECK-NEXT: [0, 0.5, 0, 0],
Expand All @@ -178,11 +163,11 @@ module {
// CHECK-NEXT: [0, 3.5, 0, 0],
// CHECK-NEXT: [1, 5, 2, 0],
// CHECK-NEXT: [1, 0.5, 0, 0]]
//
%3 = call @concat_mix_dense(%m24, %sm34cd, %sm44dc)
: (tensor<2x4xf64>, tensor<3x4xf64, #MAT_C_D>, tensor<4x4xf64, #MAT_D_C>) -> tensor<9x4xf64>
call @dump_mat_dense_9x4(%3) : (tensor<9x4xf64>) -> ()


// Release resources.
bufferization.dealloc_tensor %sm24cc : tensor<2x4xf64, #MAT_C_C>
bufferization.dealloc_tensor %sm34cd : tensor<3x4xf64, #MAT_C_D>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// DEFINE: %{compile} = mlir-opt %s --sparsifier="%{sparsifier_opts}"
// DEFINE: %{compile_sve} = mlir-opt %s --sparsifier="%{sparsifier_opts_sve}"
// DEFINE: %{run_libs} = -shared-libs=%mlir_c_runner_utils,%mlir_runner_utils
// DEFINE: %{run_opts} = -e entry -entry-point-result=void
// DEFINE: %{run_opts} = -e main -entry-point-result=void
// DEFINE: %{run} = mlir-cpu-runner %{run_opts} %{run_libs}
// DEFINE: %{run_sve} = %mcr_aarch64_cmd --march=aarch64 --mattr="+sve" %{run_opts} %{run_libs}
//
Expand Down Expand Up @@ -45,7 +45,6 @@
map = ( i, j ) -> (i : dense, j : compressed)
}>


#BSR = #sparse_tensor.encoding<{
map = ( i, j ) ->
( i floordiv 2 : dense,
Expand All @@ -65,67 +64,66 @@

module {

func.func @mul(%arg0: tensor<4x8xf64>,
%arg1: tensor<4x8xf64, #BSR>) -> tensor<4x4xf64> {
%out = arith.constant dense<0.0> : tensor<4x4xf64>
%0 = linalg.generic #trait_mul
ins(%arg0, %arg1: tensor<4x8xf64>, tensor<4x8xf64, #BSR>)
outs(%out: tensor<4x4xf64>) {
^bb(%x: f64, %y : f64, %z : f64):
%1 = arith.mulf %x, %y : f64
%2 = arith.addf %1, %z : f64
linalg.yield %2 : f64
} -> tensor<4x4xf64>
return %0 : tensor<4x4xf64>
}

func.func @mul_24(%arg0: tensor<4x8xf64>,
%arg1: tensor<4x8xf64, #NV_24>) -> tensor<4x4xf64> {
%out = arith.constant dense<0.0> : tensor<4x4xf64>
%0 = linalg.generic #trait_mul
ins(%arg0, %arg1: tensor<4x8xf64>, tensor<4x8xf64, #NV_24>)
outs(%out: tensor<4x4xf64>) {
^bb(%x: f64, %y : f64, %z : f64):
%1 = arith.mulf %x, %y : f64
%2 = arith.addf %1, %z : f64
linalg.yield %2 : f64
} -> tensor<4x4xf64>
return %0 : tensor<4x4xf64>
}
func.func @mul(%arg0: tensor<4x8xf64>,
%arg1: tensor<4x8xf64, #BSR>) -> tensor<4x4xf64> {
%out = arith.constant dense<0.0> : tensor<4x4xf64>
%0 = linalg.generic #trait_mul
ins(%arg0, %arg1: tensor<4x8xf64>, tensor<4x8xf64, #BSR>)
outs(%out: tensor<4x4xf64>) {
^bb(%x: f64, %y : f64, %z : f64):
%1 = arith.mulf %x, %y : f64
%2 = arith.addf %1, %z : f64
linalg.yield %2 : f64
} -> tensor<4x4xf64>
return %0 : tensor<4x4xf64>
}

func.func @mul_csr_bsr(%arg0: tensor<4x8xf64, #CSR>,
%arg1: tensor<4x8xf64, #BSR>) -> tensor<4x4xf64> {
%out = arith.constant dense<0.0> : tensor<4x4xf64>
%0 = linalg.generic #trait_mul
ins(%arg0, %arg1: tensor<4x8xf64, #CSR>, tensor<4x8xf64, #BSR>)
outs(%out: tensor<4x4xf64>) {
^bb(%x: f64, %y : f64, %z : f64):
%1 = arith.mulf %x, %y : f64
%2 = arith.addf %1, %z : f64
linalg.yield %2 : f64
} -> tensor<4x4xf64>
return %0 : tensor<4x4xf64>
}
func.func @mul_24(%arg0: tensor<4x8xf64>,
%arg1: tensor<4x8xf64, #NV_24>) -> tensor<4x4xf64> {
%out = arith.constant dense<0.0> : tensor<4x4xf64>
%0 = linalg.generic #trait_mul
ins(%arg0, %arg1: tensor<4x8xf64>, tensor<4x8xf64, #NV_24>)
outs(%out: tensor<4x4xf64>) {
^bb(%x: f64, %y : f64, %z : f64):
%1 = arith.mulf %x, %y : f64
%2 = arith.addf %1, %z : f64
linalg.yield %2 : f64
} -> tensor<4x4xf64>
return %0 : tensor<4x4xf64>
}

func.func @mul_dense(%arg0: tensor<4x8xf64>,
%arg1: tensor<4x8xf64>) -> tensor<4x4xf64> {
%out = arith.constant dense<0.0> : tensor<4x4xf64>
%0 = linalg.generic #trait_mul
ins(%arg0, %arg1: tensor<4x8xf64>, tensor<4x8xf64>)
outs(%out: tensor<4x4xf64>) {
^bb(%x: f64, %y : f64, %z : f64):
%1 = arith.mulf %x, %y : f64
%2 = arith.addf %1, %z : f64
linalg.yield %2 : f64
} -> tensor<4x4xf64>
return %0 : tensor<4x4xf64>
}
func.func @mul_csr_bsr(%arg0: tensor<4x8xf64, #CSR>,
%arg1: tensor<4x8xf64, #BSR>) -> tensor<4x4xf64> {
%out = arith.constant dense<0.0> : tensor<4x4xf64>
%0 = linalg.generic #trait_mul
ins(%arg0, %arg1: tensor<4x8xf64, #CSR>, tensor<4x8xf64, #BSR>)
outs(%out: tensor<4x4xf64>) {
^bb(%x: f64, %y : f64, %z : f64):
%1 = arith.mulf %x, %y : f64
%2 = arith.addf %1, %z : f64
linalg.yield %2 : f64
} -> tensor<4x4xf64>
return %0 : tensor<4x4xf64>
}

func.func @mul_dense(%arg0: tensor<4x8xf64>,
%arg1: tensor<4x8xf64>) -> tensor<4x4xf64> {
%out = arith.constant dense<0.0> : tensor<4x4xf64>
%0 = linalg.generic #trait_mul
ins(%arg0, %arg1: tensor<4x8xf64>, tensor<4x8xf64>)
outs(%out: tensor<4x4xf64>) {
^bb(%x: f64, %y : f64, %z : f64):
%1 = arith.mulf %x, %y : f64
%2 = arith.addf %1, %z : f64
linalg.yield %2 : f64
} -> tensor<4x4xf64>
return %0 : tensor<4x4xf64>
}

//
// Output utilities.
// Output utility.
//
func.func @dumpf64(%arg0: tensor<4x4xf64>) {
func.func @dump_dense_f64(%arg0: tensor<4x4xf64>) {
%c0 = arith.constant 0 : index
%d0 = arith.constant -1.0 : f64
%0 = vector.transfer_read %arg0[%c0, %c0], %d0: tensor<4x4xf64>, vector<4x4xf64>
Expand All @@ -136,36 +134,32 @@ func.func @mul_dense(%arg0: tensor<4x8xf64>,
//
// Main driver.
//
func.func @entry() {
func.func @main() {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%c2 = arith.constant 2 : index


%td = arith.constant dense<[[ 1.0, 2.0, 0.0, 0.0, 0.0, 0.0, 4.0, 5.0],
[ 6.0, 7.0, 0.0, 0.0, 0.0, 0.0, 10.0, 11.0],
[ 0.0, 0.0, 12.0, 13.0, 16.0, 17.0, 0.0, 0.0],
[ 0.0, 0.0, 18.0, 19.0, 22.0, 23.0, 0.0, 0.0]]> : tensor<4x8xf64>


%2 = sparse_tensor.convert %td : tensor<4x8xf64> to tensor<4x8xf64, #BSR>
%3 = sparse_tensor.convert %td : tensor<4x8xf64> to tensor<4x8xf64, #NV_24>
%4 = sparse_tensor.convert %td : tensor<4x8xf64> to tensor<4x8xf64, #CSR>
%a = sparse_tensor.convert %td : tensor<4x8xf64> to tensor<4x8xf64, #BSR>
%b = sparse_tensor.convert %td : tensor<4x8xf64> to tensor<4x8xf64, #NV_24>
%c = sparse_tensor.convert %td : tensor<4x8xf64> to tensor<4x8xf64, #CSR>

%d = call @mul_dense(%td, %td)
: (tensor<4x8xf64>, tensor<4x8xf64>) -> tensor<4x4xf64>
%s = call @mul(%td, %2)
%s = call @mul(%td, %a)
: (tensor<4x8xf64>, tensor<4x8xf64, #BSR>) -> tensor<4x4xf64>
%s24 = call @mul_24(%td, %3)
%s24 = call @mul_24(%td, %b)
: (tensor<4x8xf64>, tensor<4x8xf64, #NV_24>) -> tensor<4x4xf64>
%scsr = call @mul_csr_bsr(%4, %2)
%scsr = call @mul_csr_bsr(%c, %a)
: (tensor<4x8xf64, #CSR>, tensor<4x8xf64, #BSR>) -> tensor<4x4xf64>

// CHECK-COUNT-4: ( ( 46, 115, 0, 0 ), ( 115, 306, 0, 0 ), ( 0, 0, 858, 1206 ), ( 0, 0, 1206, 1698 ) )
call @dumpf64(%d) : (tensor<4x4xf64>) -> ()
call @dumpf64(%s) : (tensor<4x4xf64>) -> ()
call @dumpf64(%s24) : (tensor<4x4xf64>) -> ()
call @dumpf64(%scsr) : (tensor<4x4xf64>) -> ()
call @dump_dense_f64(%d) : (tensor<4x4xf64>) -> ()
call @dump_dense_f64(%s) : (tensor<4x4xf64>) -> ()
call @dump_dense_f64(%s24) : (tensor<4x4xf64>) -> ()
call @dump_dense_f64(%scsr) : (tensor<4x4xf64>) -> ()

return
}
Expand Down
Loading