Created
September 22, 2021 19:16
-
-
Save bjacob/535ac59fdd43aec01479ac950eb46b6f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #map0 = affine_map<(d0, d1, d2) -> (d0, d1)> | |
| #map1 = affine_map<(d0, d1, d2) -> (d1, d2)> | |
| #map2 = affine_map<(d0, d1, d2) -> (d0, d2)> | |
| #map3 = affine_map<(d0, d1) -> (d0, d1)> | |
| module { | |
| func private @actual_matmul(%arg0: tensor<?x?xf32>, %arg1: tensor<?x?xf32>, %arg2: tensor<?x?xf32>) -> tensor<?x?xf32> { | |
| %0 = linalg.matmul ins(%arg0, %arg1 : tensor<?x?xf32>, tensor<?x?xf32>) outs(%arg2 : tensor<?x?xf32>) -> tensor<?x?xf32> | |
| return %0 : tensor<?x?xf32> | |
| } | |
| func private @reference_matmul(%arg0: tensor<?x?xf32>, %arg1: tensor<?x?xf32>, %arg2: tensor<?x?xf32>) -> tensor<?x?xf32> { | |
| %0 = linalg.generic {indexing_maps = [#map0, #map1, #map2], iterator_types = ["parallel", "reduction", "parallel"]} ins(%arg0, %arg1 : tensor<?x?xf32>, tensor<?x?xf32>) outs(%arg2 : tensor<?x?xf32>) { | |
| ^bb0(%arg3: f32, %arg4: f32, %arg5: f32): // no predecessors | |
| %1 = mulf %arg3, %arg4 : f32 | |
| %2 = addf %1, %arg5 : f32 | |
| linalg.yield %2 : f32 | |
| } -> tensor<?x?xf32> | |
| return %0 : tensor<?x?xf32> | |
| } | |
| func @matmul_test() attributes {iree.abi.stub, iree.reflection = {MatmulTest = "entry"}} { | |
| %c10 = constant 10 : index | |
| %c10_0 = constant 10 : index | |
| %c10_1 = constant 10 : index | |
| %0 = call @identity_matrix(%c10, %c10_0) : (index, index) -> tensor<?x?xf32> | |
| %1 = call @identity_matrix(%c10_0, %c10_1) : (index, index) -> tensor<?x?xf32> | |
| %2 = call @zero_matrix(%c10, %c10_1) : (index, index) -> tensor<?x?xf32> | |
| %3 = call @actual_matmul(%0, %1, %2) : (tensor<?x?xf32>, tensor<?x?xf32>, tensor<?x?xf32>) -> tensor<?x?xf32> | |
| %4 = call @reference_matmul(%0, %1, %2) : (tensor<?x?xf32>, tensor<?x?xf32>, tensor<?x?xf32>) -> tensor<?x?xf32> | |
| check.expect_almost_eq(%3, %4) : tensor<?x?xf32> | |
| return | |
| } | |
| func private @zero_matrix(%arg0: index, %arg1: index) -> tensor<?x?xf32> { | |
| %0 = linalg.init_tensor [%arg0, %arg1] : tensor<?x?xf32> | |
| %cst = constant 0.000000e+00 : f32 | |
| %1 = linalg.fill(%cst, %0) : f32, tensor<?x?xf32> -> tensor<?x?xf32> | |
| return %1 : tensor<?x?xf32> | |
| } | |
| func private @pseudorandom_matrix(%arg0: index, %arg1: index) -> tensor<?x?xf32> { | |
| %0 = linalg.init_tensor [%arg0, %arg1] : tensor<?x?xf32> | |
| %cst = constant -1.000000e+00 : f64 | |
| %cst_0 = constant 1.000000e+00 : f64 | |
| %c12345_i32 = constant 12345 : i32 | |
| %1 = linalg.fill_rng_2d ins(%cst, %cst_0, %c12345_i32 : f64, f64, i32) outs(%0 : tensor<?x?xf32>) -> tensor<?x?xf32> | |
| return %1 : tensor<?x?xf32> | |
| } | |
| func private @identity_matrix(%arg0: index, %arg1: index) -> tensor<?x?xf32> { | |
| %0 = linalg.init_tensor [%arg0, %arg1] : tensor<?x?xf32> | |
| %1 = linalg.generic {indexing_maps = [#map3, #map3], iterator_types = ["parallel", "parallel"]} ins(%0 : tensor<?x?xf32>) outs(%0 : tensor<?x?xf32>) { | |
| ^bb0(%arg2: f32, %arg3: f32): // no predecessors | |
| %2 = linalg.index 0 : index | |
| %3 = linalg.index 1 : index | |
| %4 = cmpi eq, %2, %3 : index | |
| %cst = constant 0.000000e+00 : f32 | |
| %cst_0 = constant 1.000000e+00 : f32 | |
| %5 = select %4, %cst_0, %cst : f32 | |
| linalg.yield %5 : f32 | |
| } -> tensor<?x?xf32> | |
| return %1 : tensor<?x?xf32> | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment