Skip to content

Instantly share code, notes, and snippets.

@bjacob
Created September 22, 2021 20:40
Show Gist options
  • Save bjacob/5e15a7f09e9e3021b5611fe66be15404 to your computer and use it in GitHub Desktop.
Save bjacob/5e15a7f09e9e3021b5611fe66be15404 to your computer and use it in GitHub Desktop.
#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 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 @random_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>
}
func @matmul_identity_1x1_times_identity_1x1_plus_random() attributes {iree.abi.stub, iree.reflection = {MatmulTest = "entry"}} {
%c1 = constant 1 : index
%c1_0 = constant 1 : index
%c1_1 = constant 1 : index
%0 = call @identity_matrix(%c1, %c1_0) : (index, index) -> tensor<?x?xf32>
%1 = call @identity_matrix(%c1_0, %c1_1) : (index, index) -> tensor<?x?xf32>
%2 = call @random_matrix(%c1, %c1_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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment