Skip to content

[mlir][sparse] migrate integration tests to sparse_tensor.print #83357

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 1 commit into from
Feb 29, 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
45 changes: 23 additions & 22 deletions mlir/test/Integration/Dialect/SparseTensor/CPU/block.mlir
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 @@ -82,38 +82,39 @@ module {
return %0 : tensor<?x?xf64, #BSR>
}

func.func @entry() {
func.func @main() {
%c0 = arith.constant 0 : index
%f0 = arith.constant 0.0 : f64

%fileName = call @getTensorFilename(%c0) : (index) -> (!Filename)
%A = sparse_tensor.new %fileName : !Filename to tensor<?x?xf64, #BSR>

// CHECK: ( 0, 2, 3 )
// CHECK-NEXT: ( 0, 2, 1 )
// CHECK-NEXT: ( 1, 2, 0, 3, 4, 0, 0, 5, 6, 7, 8, 0 )
%pos = sparse_tensor.positions %A {level = 1 : index } : tensor<?x?xf64, #BSR> to memref<?xindex>
%vecp = vector.transfer_read %pos[%c0], %c0 : memref<?xindex>, vector<3xindex>
vector.print %vecp : vector<3xindex>
%crd = sparse_tensor.coordinates %A {level = 1 : index } : tensor<?x?xf64, #BSR> to memref<?xindex>
%vecc = vector.transfer_read %crd[%c0], %c0 : memref<?xindex>, vector<3xindex>
vector.print %vecc : vector<3xindex>
%val = sparse_tensor.values %A : tensor<?x?xf64, #BSR> to memref<?xf64>
%vecv = vector.transfer_read %val[%c0], %f0 : memref<?xf64>, vector<12xf64>
vector.print %vecv : vector<12xf64>
// CHECK: ---- Sparse Tensor ----
// CHECK-NEXT: nse = 12
// CHECK-NEXT: pos[1] : ( 0, 2, 3,
// CHECK-NEXT: crd[1] : ( 0, 2, 1,
// CHECK-NEXT: values : ( 1, 2, 0, 3, 4, 0, 0, 5, 6, 7, 8, 0,
// CHECK-NEXT: ----
sparse_tensor.print %A : tensor<?x?xf64, #BSR>

// CHECK-NEXT: ( 1, 2, 0, 3, 4, 0, 0, 5, 6, 7, 8, 0 )
// CHECK-NEXT: ---- Sparse Tensor ----
// CHECK-NEXT: nse = 12
// CHECK-NEXT: pos[1] : ( 0, 2, 3,
// CHECK-NEXT: crd[1] : ( 0, 2, 1
// CHECK-NEXT: values : ( 1, 2, 0, 3, 4, 0, 0, 5, 6, 7, 8, 0,
// CHECK-NEXT: ----
%t1 = sparse_tensor.reinterpret_map %A : tensor<?x?xf64, #BSR>
to tensor<?x?x2x2xf64, #DSDD>
%vdsdd = sparse_tensor.values %t1 : tensor<?x?x2x2xf64, #DSDD> to memref<?xf64>
%vecdsdd = vector.transfer_read %vdsdd[%c0], %f0 : memref<?xf64>, vector<12xf64>
vector.print %vecdsdd : vector<12xf64>
sparse_tensor.print %t1 : tensor<?x?x2x2xf64, #DSDD>

// CHECK-NEXT: ( 3, 6, 0, 9, 12, 0, 0, 15, 18, 21, 24, 0 )
// CHECK-NEXT: ---- Sparse Tensor ----
// CHECK-NEXT: nse = 12
// CHECK-NEXT: pos[1] : ( 0, 2, 3,
// CHECK-NEXT: crd[1] : ( 0, 2, 1,
// CHECK-NEXT: values : ( 3, 6, 0, 9, 12, 0, 0, 15, 18, 21, 24, 0,
// CHECK-NEXT: ----
%As = call @scale(%A) : (tensor<?x?xf64, #BSR>) -> (tensor<?x?xf64, #BSR>)
%vals = sparse_tensor.values %As : tensor<?x?xf64, #BSR> to memref<?xf64>
%vecs = vector.transfer_read %vals[%c0], %f0 : memref<?xf64>, vector<12xf64>
vector.print %vecs : vector<12xf64>
sparse_tensor.print %As : tensor<?x?xf64, #BSR>

// Release the resources.
bufferization.dealloc_tensor %A: tensor<?x?xf64, #BSR>
Expand Down
85 changes: 37 additions & 48 deletions mlir/test/Integration/Dialect/SparseTensor/CPU/block_majors.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,15 @@
//
module {

// CHECK: ( 0, 1, 2 )
// CHECK-NEXT: ( 0, 2 )
// CHECK-NEXT: ( 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7 )

//
// CHECK: ---- Sparse Tensor ----
// CHECK-NEXT: nse = 24
// CHECK-NEXT: pos[1] : ( 0, 1, 2,
// CHECK-NEXT: crd[1] : ( 0, 2,
// CHECK-NEXT: values : ( 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7,
// CHECK-NEXT: ----
//
func.func @foo1() {
// Build.
%c0 = arith.constant 0 : index
Expand All @@ -115,23 +121,20 @@ module {
> : tensor<6x16xf64>
%s1 = sparse_tensor.convert %m : tensor<6x16xf64> to tensor<?x?xf64, #BSR_row_rowmajor>
// Test.
%pos1 = sparse_tensor.positions %s1 {level = 1 : index } : tensor<?x?xf64, #BSR_row_rowmajor> to memref<?xindex>
%vecp1 = vector.transfer_read %pos1[%c0], %c0 : memref<?xindex>, vector<3xindex>
vector.print %vecp1 : vector<3xindex>
%crd1 = sparse_tensor.coordinates %s1 {level = 1 : index } : tensor<?x?xf64, #BSR_row_rowmajor> to memref<?xindex>
%vecc1 = vector.transfer_read %crd1[%c0], %c0 : memref<?xindex>, vector<2xindex>
vector.print %vecc1 : vector<2xindex>
%val1 = sparse_tensor.values %s1 : tensor<?x?xf64, #BSR_row_rowmajor> to memref<?xf64>
%vecv1 = vector.transfer_read %val1[%c0], %f0 : memref<?xf64>, vector<24xf64>
vector.print %vecv1 : vector<24xf64>
sparse_tensor.print %s1 : tensor<?x?xf64, #BSR_row_rowmajor>
// Release.
bufferization.dealloc_tensor %s1: tensor<?x?xf64, #BSR_row_rowmajor>
return
}

// CHECK-NEXT: ( 0, 1, 2 )
// CHECK-NEXT: ( 0, 2 )
// CHECK-NEXT: ( 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 4, 0, 0, 5, 0, 0, 0, 0, 6, 0, 0, 7 )
//
// CHECK-NEXT: ---- Sparse Tensor ----
// CHECK-NEXT: nse = 24
// CHECK-NEXT: pos[1] : ( 0, 1, 2,
// CHECK-NEXT: crd[1] : ( 0, 2,
// CHECK-NEXT: values : ( 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 4, 0, 0, 5, 0, 0, 0, 0, 6, 0, 0, 7,
// CHECK-NEXT: ----
//
func.func @foo2() {
// Build.
%c0 = arith.constant 0 : index
Expand All @@ -142,23 +145,20 @@ module {
> : tensor<6x16xf64>
%s2 = sparse_tensor.convert %m : tensor<6x16xf64> to tensor<?x?xf64, #BSR_row_colmajor>
// Test.
%pos2 = sparse_tensor.positions %s2 {level = 1 : index } : tensor<?x?xf64, #BSR_row_colmajor> to memref<?xindex>
%vecp2 = vector.transfer_read %pos2[%c0], %c0 : memref<?xindex>, vector<3xindex>
vector.print %vecp2 : vector<3xindex>
%crd2 = sparse_tensor.coordinates %s2 {level = 1 : index } : tensor<?x?xf64, #BSR_row_colmajor> to memref<?xindex>
%vecc2 = vector.transfer_read %crd2[%c0], %c0 : memref<?xindex>, vector<2xindex>
vector.print %vecc2 : vector<2xindex>
%val2 = sparse_tensor.values %s2 : tensor<?x?xf64, #BSR_row_colmajor> to memref<?xf64>
%vecv2 = vector.transfer_read %val2[%c0], %f0 : memref<?xf64>, vector<24xf64>
vector.print %vecv2 : vector<24xf64>
sparse_tensor.print %s2 : tensor<?x?xf64, #BSR_row_colmajor>
// Release.
bufferization.dealloc_tensor %s2: tensor<?x?xf64, #BSR_row_colmajor>
return
}

// CHECK-NEXT: ( 0, 1, 1, 2, 2 )
// CHECK-NEXT: ( 0, 1 )
// CHECK-NEXT: ( 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7 )
//
// CHECK-NEXT: ---- Sparse Tensor ----
// CHECK-NEXT: nse = 24
// CHECK-NEXT: pos[1] : ( 0, 1, 1, 2, 2,
// CHECK-NEXT: crd[1] : ( 0, 1,
// CHECK-NEXT: values : ( 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 6, 7,
// CHECK-NEXT: ----
//
func.func @foo3() {
// Build.
%c0 = arith.constant 0 : index
Expand All @@ -169,23 +169,20 @@ module {
> : tensor<6x16xf64>
%s3 = sparse_tensor.convert %m : tensor<6x16xf64> to tensor<?x?xf64, #BSR_col_rowmajor>
// Test.
%pos3 = sparse_tensor.positions %s3 {level = 1 : index } : tensor<?x?xf64, #BSR_col_rowmajor> to memref<?xindex>
%vecp3 = vector.transfer_read %pos3[%c0], %c0 : memref<?xindex>, vector<5xindex>
vector.print %vecp3 : vector<5xindex>
%crd3 = sparse_tensor.coordinates %s3 {level = 1 : index } : tensor<?x?xf64, #BSR_col_rowmajor> to memref<?xindex>
%vecc3 = vector.transfer_read %crd3[%c0], %c0 : memref<?xindex>, vector<2xindex>
vector.print %vecc3 : vector<2xindex>
%val3 = sparse_tensor.values %s3 : tensor<?x?xf64, #BSR_col_rowmajor> to memref<?xf64>
%vecv3 = vector.transfer_read %val3[%c0], %f0 : memref<?xf64>, vector<24xf64>
vector.print %vecv3 : vector<24xf64>
sparse_tensor.print %s3 : tensor<?x?xf64, #BSR_col_rowmajor>
// Release.
bufferization.dealloc_tensor %s3: tensor<?x?xf64, #BSR_col_rowmajor>
return
}

// CHECK-NEXT: ( 0, 1, 1, 2, 2 )
// CHECK-NEXT: ( 0, 1 )
// CHECK-NEXT: ( 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 4, 0, 0, 5, 0, 0, 0, 0, 6, 0, 0, 7 )
//
// CHECK-NEXT: ---- Sparse Tensor ----
// CHECK-NEXT: nse = 24
// CHECK-NEXT: pos[1] : ( 0, 1, 1, 2, 2,
// CHECK-NEXT: crd[1] : ( 0, 1,
// CHECK-NEXT: values : ( 1, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 4, 0, 0, 5, 0, 0, 0, 0, 6, 0, 0, 7,
// CHECK-NEXT: ----
//
func.func @foo4() {
// Build.
%c0 = arith.constant 0 : index
Expand All @@ -196,15 +193,7 @@ module {
> : tensor<6x16xf64>
%s4 = sparse_tensor.convert %m : tensor<6x16xf64> to tensor<?x?xf64, #BSR_col_colmajor>
// Test.
%pos4 = sparse_tensor.positions %s4 {level = 1 : index } : tensor<?x?xf64, #BSR_col_colmajor> to memref<?xindex>
%vecp4 = vector.transfer_read %pos4[%c0], %c0 : memref<?xindex>, vector<5xindex>
vector.print %vecp4 : vector<5xindex>
%crd4 = sparse_tensor.coordinates %s4 {level = 1 : index } : tensor<?x?xf64, #BSR_col_colmajor> to memref<?xindex>
%vecc4 = vector.transfer_read %crd4[%c0], %c0 : memref<?xindex>, vector<2xindex>
vector.print %vecc4 : vector<2xindex>
%val4 = sparse_tensor.values %s4 : tensor<?x?xf64, #BSR_col_colmajor> to memref<?xf64>
%vecv4 = vector.transfer_read %val4[%c0], %f0 : memref<?xf64>, vector<24xf64>
vector.print %vecv4 : vector<24xf64>
sparse_tensor.print %s4 : tensor<?x?xf64, #BSR_col_colmajor>
// Release.
bufferization.dealloc_tensor %s4: tensor<?x?xf64, #BSR_col_colmajor>
return
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 @@ -83,12 +83,11 @@ module {
}

func.func private @getTensorFilename(index) -> (!Filename)
func.func private @printMemref1dF64(%ptr : memref<?xf64>) attributes { llvm.emit_c_interface }

//
// Main driver that reads matrix from file and calls the kernel.
//
func.func @entry() {
func.func @main() {
%d0 = arith.constant 0.0 : f64
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
Expand All @@ -104,14 +103,13 @@ module {

//
// Print the linearized 5x5 result for verification.
// CHECK: 25
// CHECK: [2, 0, 0, 2.8, 0, 0, 4, 0, 0, 5, 0, 0, 6, 0, 0, 8.2, 0, 0, 8, 0, 0, 10.4, 0, 0, 10
//
%n = sparse_tensor.number_of_entries %0 : tensor<?x?xf64, #DenseMatrix>
vector.print %n : index
%m = sparse_tensor.values %0
: tensor<?x?xf64, #DenseMatrix> to memref<?xf64>
call @printMemref1dF64(%m) : (memref<?xf64>) -> ()
// CHECK: ---- Sparse Tensor ----
// CHECK-NEXT: nse = 25
// CHECK-NEXT: values : ( 2, 0, 0, 2.8, 0, 0, 4, 0, 0, 5, 0, 0, 6, 0, 0, 8.2, 0, 0, 8, 0, 0, 10.4, 0, 0, 10,
// CHECK-NEXT: ----
//
sparse_tensor.print %0 : tensor<?x?xf64, #DenseMatrix>

// Release the resources.
bufferization.dealloc_tensor %a : tensor<?x?xf64, #SparseMatrix>
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 @@ -67,20 +67,8 @@ module {
return %0 : tensor<?xbf16, #DenseVector>
}

// Dumps a dense vector of type bf16.
func.func @dump_vec(%arg0: tensor<?xbf16, #DenseVector>) {
// Dump the values array to verify only sparse contents are stored.
%c0 = arith.constant 0 : index
%d0 = arith.constant -1.0 : bf16
%0 = sparse_tensor.values %arg0 : tensor<?xbf16, #DenseVector> to memref<?xbf16>
%1 = vector.transfer_read %0[%c0], %d0: memref<?xbf16>, vector<32xbf16>
%f1 = arith.extf %1: vector<32xbf16> to vector<32xf32>
vector.print %f1 : vector<32xf32>
return
}

// Driver method to call and verify the kernel.
func.func @entry() {
func.func @main() {
%c0 = arith.constant 0 : index

// Setup sparse vectors.
Expand All @@ -103,8 +91,12 @@ module {
//
// Verify the result.
//
// CHECK: ( 1, 11, 0, 2, 13, 0, 0, 0, 0, 0, 14, 3, 0, 0, 0, 0, 15, 4, 16, 0, 5, 6, 0, 0, 0, 0, 0, 0, 7, 8, 0, 9 )
call @dump_vec(%0) : (tensor<?xbf16, #DenseVector>) -> ()
// CHECK: ---- Sparse Tensor ----
// CHECK-NEXT: nse = 32
// CHECK-NEXT: values : ( 1, 11, 0, 2, 13, 0, 0, 0, 0, 0, 14, 3, 0, 0, 0, 0, 15, 4, 16, 0, 5, 6, 0, 0, 0, 0, 0, 0, 7, 8, 0, 9,
// CHECK-NEXT: ----
//
sparse_tensor.print %0 : tensor<?xbf16, #DenseVector>

// Release the resources.
bufferization.dealloc_tensor %sv1 : tensor<?xbf16, #SparseVector>
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 @@ -68,20 +68,8 @@ module {
return %0 : tensor<?xf16, #DenseVector>
}

// Dumps a dense vector of type f16.
func.func @dump_vec(%arg0: tensor<?xf16, #DenseVector>) {
// Dump the values array to verify only sparse contents are stored.
%c0 = arith.constant 0 : index
%d0 = arith.constant -1.0 : f16
%0 = sparse_tensor.values %arg0 : tensor<?xf16, #DenseVector> to memref<?xf16>
%1 = vector.transfer_read %0[%c0], %d0: memref<?xf16>, vector<32xf16>
%f1 = arith.extf %1: vector<32xf16> to vector<32xf32>
vector.print %f1 : vector<32xf32>
return
}

// Driver method to call and verify the kernel.
func.func @entry() {
func.func @main() {
%c0 = arith.constant 0 : index

// Setup sparse vectors.
Expand All @@ -104,8 +92,12 @@ module {
//
// Verify the result.
//
// CHECK: ( 1, 11, 0, 2, 13, 0, 0, 0, 0, 0, 14, 3, 0, 0, 0, 0, 15, 4, 16, 0, 5, 6, 0, 0, 0, 0, 0, 0, 7, 8, 0, 9 )
call @dump_vec(%0) : (tensor<?xf16, #DenseVector>) -> ()
// CHECK: ---- Sparse Tensor ----
// CHECK-NEXT: nse = 32
// CHECK-NEXT: values : ( 1, 11, 0, 2, 13, 0, 0, 0, 0, 0, 14, 3, 0, 0, 0, 0, 15, 4, 16, 0, 5, 6, 0, 0, 0, 0, 0, 0, 7, 8, 0, 9,
// CHECK-NEXT: ----
//
sparse_tensor.print %0 : tensor<?xf16, #DenseVector>

// Release the resources.
bufferization.dealloc_tensor %sv1 : tensor<?xf16, #SparseVector>
Expand Down
Loading