|
| 1 | +// |
| 2 | +// NOTE: this test requires gpu-sm80 |
| 3 | +// |
| 4 | +// with RT lib (SoA COO): |
| 5 | +// |
| 6 | +// RUN: mlir-opt %s \ |
| 7 | +// RUN: --sparse-compiler="enable-runtime-library=true enable-gpu-libgen gpu-triple=nvptx64-nvidia-cuda gpu-chip=sm_80 gpu-features=+ptx71" \ |
| 8 | +// RUN: | mlir-cpu-runner \ |
| 9 | +// RUN: --shared-libs=%mlir_cuda_runtime \ |
| 10 | +// RUN: --shared-libs=%mlir_c_runner_utils \ |
| 11 | +// RUN: --e main --entry-point-result=void \ |
| 12 | +// RUN: | FileCheck %s |
| 13 | +// |
| 14 | +// without RT lib (AoS COO): note, may fall back to CPU |
| 15 | +// |
| 16 | +// RUN: mlir-opt %s \ |
| 17 | +// RUN: --sparse-compiler="enable-runtime-library=false enable-gpu-libgen gpu-triple=nvptx64-nvidia-cuda gpu-chip=sm_80 gpu-features=+ptx71" \ |
| 18 | +// RUN: | mlir-cpu-runner \ |
| 19 | +// RUN: --shared-libs=%mlir_cuda_runtime \ |
| 20 | +// RUN: --shared-libs=%mlir_c_runner_utils \ |
| 21 | +// RUN: --e main --entry-point-result=void \ |
| 22 | +// RUN: | FileCheck %s |
| 23 | + |
| 24 | +#SortedCOO = #sparse_tensor.encoding<{ |
| 25 | + lvlTypes = [ "compressed-nu", "singleton" ] |
| 26 | +}> |
| 27 | + |
| 28 | +#CSR = #sparse_tensor.encoding<{ |
| 29 | + lvlTypes = [ "dense", "compressed" ], |
| 30 | + posWidth = 32, |
| 31 | + crdWidth = 32 |
| 32 | +}> |
| 33 | + |
| 34 | +module { |
| 35 | + // Computes C = A x B with A sparse COO. |
| 36 | + func.func @matmulCOO(%A: tensor<8x8xf32, #SortedCOO>, |
| 37 | + %B: tensor<8x8xf32>, |
| 38 | + %C: tensor<8x8xf32>) -> tensor<8x8xf32> { |
| 39 | + %D = linalg.matmul |
| 40 | + ins(%A, %B: tensor<8x8xf32, #SortedCOO>, tensor<8x8xf32>) |
| 41 | + outs(%C: tensor<8x8xf32>) -> tensor<8x8xf32> |
| 42 | + return %D: tensor<8x8xf32> |
| 43 | + } |
| 44 | + |
| 45 | + // Computes C = A x B with A sparse CSR. |
| 46 | + func.func @matmulCSR(%A: tensor<8x8xf32, #CSR>, |
| 47 | + %B: tensor<8x8xf32>, |
| 48 | + %C: tensor<8x8xf32>) -> tensor<8x8xf32> { |
| 49 | + %D = linalg.matmul |
| 50 | + ins(%A, %B: tensor<8x8xf32, #CSR>, tensor<8x8xf32>) |
| 51 | + outs(%C: tensor<8x8xf32>) -> tensor<8x8xf32> |
| 52 | + return %D: tensor<8x8xf32> |
| 53 | + } |
| 54 | + |
| 55 | + func.func @dump(%mat: tensor<8x8xf32>) { |
| 56 | + %f0 = arith.constant 0.0 : f32 |
| 57 | + %c0 = arith.constant 0 : index |
| 58 | + %c1 = arith.constant 1 : index |
| 59 | + %c2 = arith.constant 2 : index |
| 60 | + %c3 = arith.constant 3 : index |
| 61 | + %c4 = arith.constant 4 : index |
| 62 | + %c5 = arith.constant 5 : index |
| 63 | + %c6 = arith.constant 6 : index |
| 64 | + %c7 = arith.constant 7 : index |
| 65 | + %r0 = vector.transfer_read %mat[%c0,%c0], %f0 : tensor<8x8xf32>, vector<8xf32> |
| 66 | + vector.print %r0 : vector<8xf32> |
| 67 | + %r1 = vector.transfer_read %mat[%c1,%c0], %f0 : tensor<8x8xf32>, vector<8xf32> |
| 68 | + vector.print %r1 : vector<8xf32> |
| 69 | + %r2 = vector.transfer_read %mat[%c2,%c0], %f0 : tensor<8x8xf32>, vector<8xf32> |
| 70 | + vector.print %r2 : vector<8xf32> |
| 71 | + %r3 = vector.transfer_read %mat[%c3,%c0], %f0 : tensor<8x8xf32>, vector<8xf32> |
| 72 | + vector.print %r3 : vector<8xf32> |
| 73 | + %r4 = vector.transfer_read %mat[%c4,%c0], %f0 : tensor<8x8xf32>, vector<8xf32> |
| 74 | + vector.print %r4 : vector<8xf32> |
| 75 | + %r5 = vector.transfer_read %mat[%c5,%c0], %f0 : tensor<8x8xf32>, vector<8xf32> |
| 76 | + vector.print %r5 : vector<8xf32> |
| 77 | + %r6 = vector.transfer_read %mat[%c6,%c0], %f0 : tensor<8x8xf32>, vector<8xf32> |
| 78 | + vector.print %r6 : vector<8xf32> |
| 79 | + %r7 = vector.transfer_read %mat[%c7,%c0], %f0 : tensor<8x8xf32>, vector<8xf32> |
| 80 | + vector.print %r7 : vector<8xf32> |
| 81 | + return |
| 82 | + } |
| 83 | + |
| 84 | + // |
| 85 | + // Main driver. |
| 86 | + // |
| 87 | + func.func @main() { |
| 88 | + %f0 = arith.constant 0.0 : f32 |
| 89 | + %f1 = arith.constant 1.0 : f32 |
| 90 | + |
| 91 | + // Stress test with a dense matrix DA. |
| 92 | + %DA = tensor.generate { |
| 93 | + ^bb0(%i: index, %j: index): |
| 94 | + %k = arith.addi %i, %j : index |
| 95 | + %l = arith.index_cast %k : index to i64 |
| 96 | + %f = arith.uitofp %l : i64 to f32 |
| 97 | + tensor.yield %f : f32 |
| 98 | + } : tensor<8x8xf32> |
| 99 | + |
| 100 | + // Convert to a "sparse" matrix A. |
| 101 | + %Acoo = sparse_tensor.convert %DA : tensor<8x8xf32> to tensor<8x8xf32, #SortedCOO> |
| 102 | + %Acsr = sparse_tensor.convert %DA : tensor<8x8xf32> to tensor<8x8xf32, #CSR> |
| 103 | + |
| 104 | + // Initial C matrices. |
| 105 | + %C0 = tensor.generate { |
| 106 | + ^bb0(%i: index, %j: index): |
| 107 | + tensor.yield %f0 : f32 |
| 108 | + } : tensor<8x8xf32> |
| 109 | + %C1 = tensor.generate { |
| 110 | + ^bb0(%i: index, %j: index): |
| 111 | + tensor.yield %f1 : f32 |
| 112 | + } : tensor<8x8xf32> |
| 113 | + |
| 114 | + // Call the kernels. |
| 115 | + %0 = call @matmulCOO(%Acoo, %DA, %C0) : (tensor<8x8xf32, #SortedCOO>, |
| 116 | + tensor<8x8xf32>, |
| 117 | + tensor<8x8xf32>) -> tensor<8x8xf32> |
| 118 | + %1 = call @matmulCSR(%Acsr, %DA, %C0) : (tensor<8x8xf32, #CSR>, |
| 119 | + tensor<8x8xf32>, |
| 120 | + tensor<8x8xf32>) -> tensor<8x8xf32> |
| 121 | + %2 = call @matmulCOO(%Acoo, %DA, %C1) : (tensor<8x8xf32, #SortedCOO>, |
| 122 | + tensor<8x8xf32>, |
| 123 | + tensor<8x8xf32>) -> tensor<8x8xf32> |
| 124 | + %3 = call @matmulCSR(%Acsr, %DA, %C1) : (tensor<8x8xf32, #CSR>, |
| 125 | + tensor<8x8xf32>, |
| 126 | + tensor<8x8xf32>) -> tensor<8x8xf32> |
| 127 | + |
| 128 | + // |
| 129 | + // Sanity check on results. |
| 130 | + // |
| 131 | + // CHECK: ( 140, 168, 196, 224, 252, 280, 308, 336 ) |
| 132 | + // CHECK-NEXT: ( 168, 204, 240, 276, 312, 348, 384, 420 ) |
| 133 | + // CHECK-NEXT: ( 196, 240, 284, 328, 372, 416, 460, 504 ) |
| 134 | + // CHECK-NEXT: ( 224, 276, 328, 380, 432, 484, 536, 588 ) |
| 135 | + // CHECK-NEXT: ( 252, 312, 372, 432, 492, 552, 612, 672 ) |
| 136 | + // CHECK-NEXT: ( 280, 348, 416, 484, 552, 620, 688, 756 ) |
| 137 | + // CHECK-NEXT: ( 308, 384, 460, 536, 612, 688, 764, 840 ) |
| 138 | + // CHECK-NEXT: ( 336, 420, 504, 588, 672, 756, 840, 924 ) |
| 139 | + // |
| 140 | + // CHECK: ( 140, 168, 196, 224, 252, 280, 308, 336 ) |
| 141 | + // CHECK-NEXT: ( 168, 204, 240, 276, 312, 348, 384, 420 ) |
| 142 | + // CHECK-NEXT: ( 196, 240, 284, 328, 372, 416, 460, 504 ) |
| 143 | + // CHECK-NEXT: ( 224, 276, 328, 380, 432, 484, 536, 588 ) |
| 144 | + // CHECK-NEXT: ( 252, 312, 372, 432, 492, 552, 612, 672 ) |
| 145 | + // CHECK-NEXT: ( 280, 348, 416, 484, 552, 620, 688, 756 ) |
| 146 | + // CHECK-NEXT: ( 308, 384, 460, 536, 612, 688, 764, 840 ) |
| 147 | + // CHECK-NEXT: ( 336, 420, 504, 588, 672, 756, 840, 924 ) |
| 148 | + // |
| 149 | + // CHECK: ( 141, 169, 197, 225, 253, 281, 309, 337 ) |
| 150 | + // CHECK-NEXT: ( 169, 205, 241, 277, 313, 349, 385, 421 ) |
| 151 | + // CHECK-NEXT: ( 197, 241, 285, 329, 373, 417, 461, 505 ) |
| 152 | + // CHECK-NEXT: ( 225, 277, 329, 381, 433, 485, 537, 589 ) |
| 153 | + // CHECK-NEXT: ( 253, 313, 373, 433, 493, 553, 613, 673 ) |
| 154 | + // CHECK-NEXT: ( 281, 349, 417, 485, 553, 621, 689, 757 ) |
| 155 | + // CHECK-NEXT: ( 309, 385, 461, 537, 613, 689, 765, 841 ) |
| 156 | + // CHECK-NEXT: ( 337, 421, 505, 589, 673, 757, 841, 925 ) |
| 157 | + // |
| 158 | + // CHECK: ( 141, 169, 197, 225, 253, 281, 309, 337 ) |
| 159 | + // CHECK-NEXT: ( 169, 205, 241, 277, 313, 349, 385, 421 ) |
| 160 | + // CHECK-NEXT: ( 197, 241, 285, 329, 373, 417, 461, 505 ) |
| 161 | + // CHECK-NEXT: ( 225, 277, 329, 381, 433, 485, 537, 589 ) |
| 162 | + // CHECK-NEXT: ( 253, 313, 373, 433, 493, 553, 613, 673 ) |
| 163 | + // CHECK-NEXT: ( 281, 349, 417, 485, 553, 621, 689, 757 ) |
| 164 | + // CHECK-NEXT: ( 309, 385, 461, 537, 613, 689, 765, 841 ) |
| 165 | + // CHECK-NEXT: ( 337, 421, 505, 589, 673, 757, 841, 925 ) |
| 166 | + // |
| 167 | + call @dump(%0) : (tensor<8x8xf32>) -> () |
| 168 | + call @dump(%1) : (tensor<8x8xf32>) -> () |
| 169 | + call @dump(%2) : (tensor<8x8xf32>) -> () |
| 170 | + call @dump(%3) : (tensor<8x8xf32>) -> () |
| 171 | + |
| 172 | + // Release the resources. |
| 173 | + bufferization.dealloc_tensor %Acoo : tensor<8x8xf32, #SortedCOO> |
| 174 | + bufferization.dealloc_tensor %Acsr : tensor<8x8xf32, #CSR> |
| 175 | + |
| 176 | + return |
| 177 | + } |
| 178 | +} |
0 commit comments