Repository navigation
Conversation
347be2e to
bc0ca0e
Compare
|
If you're curious or it helps reviewing this PR:
|
970f1f0 to
50bbb17
Compare
They can be run in any cmake build directory (including Android) with ctest -R matmul These check-tests complete in a few seconds on Android devices, in less than 1 second on a PC, and cover > 200 test cases each. The source .mlir files in the test/ directory only provide helper functions. The actual test code is generated by a iree-opt pass, iree-flow-export-matmul-test-funcs. To integrate that in iree_check_test's, these cmake functions are extended to allow running iree-opt with specified flags as a preprocessing step on the input .mlir file. Covered: * f32 and i8 (accumulating into i32) matmuls. * Dynamic and static shapes (also random mixed shapes). * A wide array of MxKxN shapes. * linalg.matmul lowerings both via mmt4d and direct. * For each shape, the test first tries a very simple case of Identity * Identity + ZeroAccumulator. This maximize the chances that if we have to stare at arrays of numbers to understand a test failures, then it's a very simple array of mostly 0's and a few 1's that's easy to understand. Then gradually the test replaces each of the input matrices by a pseudorandom matrix, finishing with Random*Random+Random. So we should hopefully only rarely have to debug numerical errors in that general case.
50bbb17 to
1894822
Compare
MaheshRavishankar
left a comment
There was a problem hiding this comment.
Did a quick skim. It looks fine to me. Ill let someone else review the CMake parts.
GMNGeoffrey
left a comment
There was a problem hiding this comment.
So overall this is pretty cool, but I'm also a bit scared of it. The detailed comments helped a lot for reviewing this though, so thanks for that! I know this is the approach Ben recommended, but I wasn't totally sold on it then and I'm still not totally sold on it. In particular it scares me that we're running the same pass over both the reference output and the output under test. This seems like a case where a well defined test input spec would be super useful (not that you should be creating one; just another use case in addition to testing a range of frontends)
Please also add lit tests for the individual pass, which may require factoring out the set of matrices it creates from the way it creates them. We definitely want to have high confidence that it does the correct thing, since we're relying pretty heavily on it.
| # Note: --iree-flow-convert-linalg-matmul-to-mmt4d does not mean that all of the | ||
| # testcases are using mmt4d. This pass currently bails in many cases, such as when | ||
| # the matrix shapes are not known at compile time to be multiples of M0,K0,N0. | ||
| # At the moment (09/2021) only 2% (4 out of 238) of the testcases here are actually using mmt4d. |
There was a problem hiding this comment.
ISO8601 please
| # At the moment (09/2021) only 2% (4 out of 238) of the testcases here are actually using mmt4d. | |
| # At the moment (2021-09) only 2% (4 out of 238) of the testcases here are actually using mmt4d. |
| // This relies on linalg.generic NOT being de-generalized (what's the term?) into | ||
| // linalg.matmul. | ||
| // TODO: clarify how we ensure that that is the case? | ||
| func private @reference_matmul(%lhs: tensor<?x?xf32>, %rhs: tensor<?x?xf32>, %acc : tensor<?x?xf32>) -> tensor<?x?xf32> { |
There was a problem hiding this comment.
Hmmm yeah doing exactly that pattern matching is an eventual goal of linalg, so I'm a bit concerned about this approach. In general, doing something that relies on the compiler being insufficiently smart without some kind of guarantee that the compiler cannot be sufficiently smart and maintain correctness (e.g. the do_not_optimize, which declares unbounded side-effects preventing any optimization) is asking for trouble.
| @@ -0,0 +1,512 @@ | |||
| // Copyright 2020 The IREE Authors | |||
There was a problem hiding this comment.
Nit: license year
| // Copyright 2020 The IREE Authors | |
| // Copyright 2021 The IREE Authors |
Yes, it's silly that this has to be manually maintained :-/
| MatrixGenerator lhs; | ||
| MatrixGenerator rhs; | ||
| AccumulatorMatrixGenerator acc; | ||
| ShapeDynamicity dynamicity; |
There was a problem hiding this comment.
I wonder about dynamicity vs dynamism vs dynamicness.
Note sure which is best :-) Helpfully hacker news also doesn't know: https://news.ycombinator.com/item?id=4137596 and this http://rubyhacker.com/coralbook/dynam.html
Yeah IDK. This is an unimportant tangent :-D
| // NOTE: when adding more large sizes here, please be mindful of | ||
| // the impact on test latencies. We might have to add higher-latency test | ||
| // cases when we implement complex logic for dealing with large matmuls. | ||
| // At that point, it might make sense to split such test cases into | ||
| // separate tests. |
There was a problem hiding this comment.
Thank you for leaving this note :-) I love proactively guarding against "organic" growth :-)
| Value mConstOp = doNotOptimize( | ||
| builder.create<ConstantOp>(loc, builder.getIndexAttr(shape.m))); |
There was a problem hiding this comment.
There's UnfoldableConstantOp basically as syntactic sugar for this exact use case, if you want it
| void getDependentDialects(DialectRegistry& registry) const override { | ||
| registry.insert<IREE::Util::UtilDialect>(); | ||
| registry.insert<IREE::Check::CheckDialect>(); | ||
| registry.insert<IREE::Flow::FlowDialect>(); |
There was a problem hiding this comment.
I didn't see you creating flow ops in here. Is it in some helper function?
| // Tests must use fully specified pseudorandom numbers to avoid | ||
| // portability issues, such as test failures when compiled against other | ||
| // C++ standard libraries. | ||
| std::minstd_rand randomEngine; |
There was a problem hiding this comment.
Does it always start with the same seed?
| opt_tool = "//iree/tools:iree-opt", | ||
| opt_flags = [], |
There was a problem hiding this comment.
Hrm I think iree-translate should be able to do everything here. Generally it has all the passes. Now there's some question of whether it should be able to do that, but I think if your pass flags here were before the other flags then they would get run first. Can you try just overriding flags?
| "-mlir-print-op-on-diagnostic=false", | ||
| "-iree-hal-target-backends=%s" % target_backend, | ||
| ] + compiler_flags, |
There was a problem hiding this comment.
Yeah I think here if you moved compiler_flags before the other flags, then this would work without the extra tool
end-to-end matmul tests, meaning: we generate input matrices, perform linalg.matmul's, check that the results are correct. In an earlier attempt (PR #7154) the test was entirely generated code. That approach was abandoned because it potentially amounted to comparing the compiler's output with itself. The new approach here is to have a custom C test runner that has its own C reference implementation of matmul and uses it to compare the output of the generated code, which it drives by means of replay-trace. So now each test is not just a .mlir source, but a (.mlir, .yaml) pair where the yaml file is a trace, determining the runtime values of the inputs of the code being tested. Both .mlir and .yaml files are co-generated by a Python script. A new CMake function iree_trace_runner_test is added. It is a variant of iree_check_test, taking a custom trace runner and trace file, instead of iree_check_test's behavior of just running the module in iree-check-module. A new CMake function iree_generated_trace_runner_test is added to call the Python script to co-generate the .yaml and the .mlirsource and call iree_trace_runner_test on the resulting files. One aspect of PR #7154 is retained here: all the above-mentioned CMake functions are generalized also to allow running iree-opt (or an alternate tool) as a pre-processing step on the source .mlir file, before running iree-translate. That is because by default, new MLIR transformations are not necessarily known to iree-translate. In our case, that's the mmt4d-related transformations. The tests added in this PR share the prefix e2e_matmul (that can be used with ctest -R).
end-to-end linalg.matmul tests
They can be run in any cmake build directory (including Android) with
Bazel is also supported.
These check-tests complete in a few seconds on Android devices, in less
than 1 second on a PC, and cover > 200 test cases each.
The source .mlir files in the test/ directory only provide helper
functions. The actual test code is generated by a iree-opt pass,
iree-flow-export-matmul-test-funcs.
To integrate that in tests,
iree_check_testand a few other related cmake functions areextended to allow running
iree-optwith specified flags as a preprocessing step on the input .mlir file.The flags are propagated down to
iree_bytecode_modulewhich is where the preprocessing is implemented, so this could easily be exposed in any other thing layered aboveiree_bytecode_module. This might help remove some of the motivation for the duplication ofiree-optflags iniree-translate.Covered:
f32 and i8 (accumulating into i32) matmuls.
Dynamic and static shapes (also random mixed shapes).
A wide array of MxKxN shapes.
linalg.matmul lowerings both via mmt4d and direct.
For each shape, the test first tries a very simple case of
Identity * Identity + ZeroAccumulator.
This maximize the chances that if we have to stare at arrays of
numbers to understand a test failures, then it's a very simple
array of mostly 0's and a few 1's that's easy to understand.
Then gradually the test replaces each of the input matrices
by a pseudorandom matrix, finishing with Random*Random+Random.
So we should hopefully only rarely have to debug numerical
errors in that general case.