Repository navigation
E2E tests for math ops. - #20169
E2E tests for math ops.#20169
Conversation
|
@ScottTodd FYI. Please don't suggest to move this to the external tests repository at least for now. We are iterating fast on math functions codegen and code changes will need to be paired with test changes. Having the tests in-tree is very valuable. |
Putting these tests in this repository makes sense. They are closer to unit tests than integration tests in size (dozens of elements instead of thousands, relatively simple generator). What moving to an external repository would provide is a guarantee that testing is done in a black-box style, without tight coupling to the core IREE build systems. I do think there is a place for that style of test-driven development where we first add all the test cases we want to pass then we do the work to get the tests passing, but that can be harder to do if we're breaking new ground with data types and operations. |
benvanik
left a comment
There was a problem hiding this comment.
Agreed with them being good unit tests and that they belong here. Don't like that they are replicated per backend and am not sure I see why that was done - given that they are large files and we have a lot of backends we really don't want to be having them stamped out per data type and per backend.
|
(there's also no diffs between the backends - so even more reason for them not to be duplicated) |
| func.func @test_acos_f16() -> () { | ||
| %input = util.unfoldable_constant dense<[-1.0, -0.75, -0.5, -0.25, 0.0, 0.25, 0.5, 0.75, 1.0]> : tensor<9xf16> | ||
| %result_empty = tensor.empty() : tensor<9xf16> | ||
| %result = linalg.generic {indexing_maps = [ | ||
| affine_map<(d0) -> (d0)>, | ||
| affine_map<(d0) -> (d0)> | ||
| ], iterator_types = ["parallel"]} | ||
| ins(%input : tensor<9xf16>) outs(%result_empty : tensor<9xf16>) { | ||
| ^bb0(%in: f16, %out: f16): | ||
| %result = math.acos %in : f16 | ||
| linalg.yield %result : f16 | ||
| } -> tensor<9xf16> | ||
| check.expect_almost_eq_const(%result, | ||
| dense<[3.141592653589793, 2.4188584057763776, 2.0943951023931957, 1.8234765819369754, 1.5707963267948966, 1.318116071652818, 1.0471975511965979, 0.7227342478134157, 0.0]> : tensor<9xf16>, | ||
| tolerance 0.01) : tensor<9xf16> | ||
| return | ||
| } |
There was a problem hiding this comment.
I have a few questions here since this feels like a lot of linalg boilerplate per test case.
-
How load bearing is the
linalg.generic? If I'm reading this correctly (and the fact that it takes effort is what I'd like to highlight), this is wrapping the inner operation in an elementwise compute loop? -
Do we have a more concise way to express that which still exercises codegen?
-
If the operations themselves support elementwise operation, but that skips codegen, could we teach the compiler to send it through to codegen anyways?
-
Maybe we could define a helper "elementwise" function in the test file for each test case to call with its inner loop? At least that's how I might write such a test file in plain C/C++/Python, e.g.
test_math_function = math.acos input = [-1.0, -0.75, -0.5, -0.25, 0.0, 0.25, 0.5, 0.75, 1.0] expected_output = [3.14159, 2.41885, ...] output = run_test(test_math_function, input) assert_near(expected_output, output)
There was a problem hiding this comment.
I am not aware of another legal way to apply an arbitrary math dialect op elementwise to a tensor. Linalg has a few named ops for specific cases, such as linalg.powf. There is also some tolerance for passing tensors directly to math ops, but the last time I did it (a couple years ago) I was told that that was only accidentally working.
| # TODO(#20165): uncomment trunc when compilation is fixed on CPU. | ||
| # ("trunc", 0, lambda x: True), |
There was a problem hiding this comment.
This is one spot having support in the test runner for XFAIL or conditional skipping would be useful. Hard to get that in Bazel and CMake but easier to get it in pytest.
It's easier to ignore failing tests when they are disabled in the generator vs included but skipped vs included but xfailed.
| check.expect_almost_eq_const(%result, | ||
| dense<[3.141592653589793, 2.4188584057763776, 2.0943951023931957, 1.8234765819369754, 1.5707963267948966, 1.318116071652818, 1.0471975511965979, 0.7227342478134157, 0.0]> : tensor<9xf16>, | ||
| tolerance 0.01) : tensor<9xf16> |
There was a problem hiding this comment.
If the tolerance is to only two decimal places, would it make more sense to round the expected values to a similar level of precision?
Some ideas:
- Some fixed number of digits regardless of precision, like 5-8 digits
- Tolerance + some fixed number of digits extra, like 5 digits for 0.01 tolerance or 6 digits for 0.001 tolerance (could also go even tighter, but it's easier for people to look at the source and think through the math for 3.14159 compared to just 3.14)
| # Tests of end-to-end IREE support for individual ops in the TOSA dialect. | ||
| # Each test file should have a name matching the corresponding TOSA op and test only the | ||
| # functionality of that op (though may make use of other ops where necessary). Tests should be | ||
| # written using the IREE Check framework. | ||
| # See https://iree.dev/developers/general/testing-guide/#iree-core-end-to-end-e2e-tests. |
There was a problem hiding this comment.
Reword or remove comment. No TOSA dialect here.
| # Large values would require large tolerance. | ||
| lambda x: -2 <= x <= 2, | ||
| ), | ||
| ("tanh", 1e-4 if args.type == "f32" else 1e-3, lambda x: True), |
There was a problem hiding this comment.
Maybe we can remove this single test once these new tests are up and running: https://github.com/iree-org/iree/blob/main/tests/e2e/regression/libm_linking.mlir. Could check that the provide the same coverage
There was a problem hiding this comment.
I didn't understand that. What part of this test can be removed when the linked tests is available? The linked test, as the file name suggests, does not seem to be verifying numerics, unlike the test here.
There was a problem hiding this comment.
Ah, I mean remove regression/libm_linking.mlir, since these new tests will be providing the same compilation coverage and more exhaustive numerics checks.
|
(if the expectation is that some tests fail on some platforms and you want to ignore those, then making the test suite rule pass along |
This is to allow setting the tolerance values separately for each backend. At the moment, this happens not to be used on f32, but that's only because the default tolerance 1e-4 is too lax (it allows for substantial numerical errors to slip in unnoticed). There is a diff at the moment on f16, as reported by: It corresponds to the currently 3 places in |
|
Ok meta comment then: having a python script that hardcodes a bunch of if-else assumptions about data types and backends to generate files and then checking in the generated files per backend is not a good solution :) Having the conditions inside the script disallows out of tree backends from reusing the same scripts - if you pulled out those special cases into flags that could be driven in the script instead that'd be better - As for checking in the generated files, if they really are per-backend then you'll need to flip to generating them as part of the build process - there's no value in having them checked in as they are so tightly coupled to the generation script and the invocation of that script. (still saying these tests are useful and would be great to have - just not in this form) |
|
The tolerance is per-op. So it can't be a flag to the script, unless one is going to call the script separately for each op (which then moves the hardcoding of tolerances up to the parent script, generate.sh). Fine to have this generated at build-time into the build dir. |
|
You can repeat flags, you know :) You've got to have the tolerance overrides somewhere - having them at the leaf (those invoking the script) is the best - the python should not be aware of the types and backends it is being called with as then there's no reason to make it have args: it should just generate all the outputs it needs. That's bad, though, because again out of tree backends don't work. The generic script should generically generate the math ops with default tolerances for a particular type and backend. Any backend-specific tolerance changes should be passed as overrides for those default tolerances as flags. Then your build rules for generating the mlir files are hermetic: they contain the data type, backend, and any tolerance overrides required to make it pass. |
|
Concretely, instead of burying stuff like this in the python: You flip it so that the invocation to generate.py for --type=f16 --backend=rocm passes in so it should make everything strictly more readable - requiring a human to go build out that if chain is not good, especially if we throw in half a dozen fp8 types and expand to run on all backends. Or, if you have a build rule for it you can have it right in the cmakelists instead of having a shell script (what I'd do). |
|
@benvanik , @ScottTodd , PTAL, this new state of this PR should incorporate changes discussed above (and in a call with Ben last week).
|
There was a problem hiding this comment.
nice! looks good!
one question: what happens if an op is not present in the json? could you warn on generation if an op listed in generate.py had no matching test case? (trying to manage integration - if a new op gets added and someone failed to update a json file it'll never get tested). if you made the math op info a table so get_math_op_info just returned the entry you could enumerate the table to do the check
|
FYI I'm not sure when I'll have bandwidth to review this again. Need to focus my attention on some other projects and can't take context switches. |
|
@benvanik : done. Printing to stderr shows in the terminal output during build, like a build warning. @ScottTodd, noted. |
|
thanks! |
Signed-off-by: Benoit Jacob <[email protected]>
|
@ScottTodd , do you want to review this PR before it's merged? it blocks a chain of dependent PRs on math functions codegen, so if that's OK with you, I would like to merge it, but then I promise I would apply any post review comments :-) |
|
I can do a quick scan through now. |
This is thought to be needed before going ahead with a batch of math ops codegen changes (#19970 (review)) and this also discovered a few bugs: #20163 #20164 #20165. Signed-off-by: Benoit Jacob <[email protected]>
This is thought to be needed before going ahead with a batch of math ops codegen changes (#19970 (review)) and this also discovered a few bugs: #20163 #20164 #20165.