Skip to content

(Superseded by PR #7347) end-to-end linalg.matmul tests - #7154

Closed
bjacob wants to merge 1 commit into
iree-org:mainfrom
bjacob:matmul-tests
Closed

bjacob wants to merge 1 commit into
iree-org:mainfrom
bjacob:matmul-tests

Conversation

@bjacob

@bjacob bjacob commented Sep 23, 2021 •

Copy link
Copy Markdown
Collaborator

end-to-end linalg.matmul tests

They can be run in any cmake build directory (including Android) with

    ctest -R matmul

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_test and a few other related cmake functions are
extended to allow running iree-opt with specified flags as a preprocessing step on the input .mlir file.
The flags are propagated down to iree_bytecode_module which is where the preprocessing is implemented, so this could easily be exposed in any other thing layered above iree_bytecode_module. This might help remove some of the motivation for the duplication of iree-opt flags in iree-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.

@bjacob
bjacob force-pushed the matmul-tests branch 4 times, most recently from 347be2e to bc0ca0e Compare September 24, 2021 19:36
@bjacob
bjacob marked this pull request as ready for review September 24, 2021 19:41
@bjacob

bjacob commented Sep 24, 2021

Copy link
Copy Markdown
Collaborator Author

If you're curious or it helps reviewing this PR:

  • output of one of the tests
  • command line to run this test: ctest -R check_regression_matmul_f32_mmt4d_8x1x8_dylib-llvm-aot_matmul_f32.mlir (add --verbose to see the test output)
  • MLIR generated code of that test.

@bjacob
bjacob force-pushed the matmul-tests branch 2 times, most recently from 970f1f0 to 50bbb17 Compare September 27, 2021 14:43
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.

@MaheshRavishankar MaheshRavishankar left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did a quick skim. It looks fine to me. Ill let someone else review the CMake parts.

@GMNGeoffrey GMNGeoffrey left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ISO8601 please

Suggested change
# 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.

Comment on lines +15 to +18
// 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> {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: license year

Suggested change
// 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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +205 to +209
// 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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for leaving this note :-) I love proactively guarding against "organic" growth :-)

Comment on lines +419 to +420
Value mConstOp = doNotOptimize(
builder.create<ConstantOp>(loc, builder.getIndexAttr(shape.m)));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it always start with the same seed?

Comment on lines +18 to +19
opt_tool = "//iree/tools:iree-opt",
opt_flags = [],

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment on lines 56 to 58
"-mlir-print-op-on-diagnostic=false",
"-iree-hal-target-backends=%s" % target_backend,
] + compiler_flags,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think here if you moved compiler_flags before the other flags, then this would work without the extra tool

@bjacob bjacob mentioned this pull request Oct 15, 2021
@bjacob bjacob changed the title end-to-end linalg.matmul tests (Superseded by PR #7347) end-to-end linalg.matmul tests Oct 21, 2021
@bjacob bjacob closed this Oct 21, 2021
bjacob added a commit that referenced this pull request Oct 21, 2021
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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants