Skip to content

E2E tests for math ops. - #20169

Merged
bjacob merged 1 commit into
iree-org:mainfrom
bjacob:mathtests
Mar 11, 2025
Merged

bjacob merged 1 commit into
iree-org:mainfrom
bjacob:mathtests

Conversation

@bjacob

@bjacob bjacob commented Mar 6, 2025 •

Copy link
Copy Markdown
Collaborator

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.

@bjacob

bjacob commented Mar 6, 2025

Copy link
Copy Markdown
Collaborator Author

@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.

@bjacob
bjacob marked this pull request as ready for review March 6, 2025 02:54
@ScottTodd

Copy link
Copy Markdown
Member

@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 benvanik 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.

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.

@benvanik

benvanik commented Mar 6, 2025

Copy link
Copy Markdown
Collaborator

(there's also no diffs between the backends - so even more reason for them not to be duplicated)

@ScottTodd ScottTodd 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.

Nice!

Comment on lines +4 to +20
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
}

@ScottTodd ScottTodd Mar 6, 2025 •

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 have a few questions here since this feels like a lot of linalg boilerplate per test case.

  1. 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?

  2. Do we have a more concise way to express that which still exercises codegen?

  3. If the operations themselves support elementwise operation, but that skips codegen, could we teach the compiler to send it through to codegen anyways?

  4. 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)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

Comment thread tests/e2e/math/generate.py Outdated
Comment on lines +213 to +214
# TODO(#20165): uncomment trunc when compilation is fixed on CPU.
# ("trunc", 0, lambda x: True),

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.

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.

Comment on lines +16 to +18
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>

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.

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)

Comment thread tests/e2e/math/BUILD.bazel Outdated
Comment on lines +7 to +11
# 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.

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.

Reword or remove comment. No TOSA dialect here.

Comment thread tests/e2e/math/BUILD.bazel Resolved
Comment thread tests/e2e/math/generate.py Outdated
# Large values would require large tolerance.
lambda x: -2 <= x <= 2,
),
("tanh", 1e-4 if args.type == "f32" else 1e-3, lambda x: True),

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.

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

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

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.

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.

Ah, I mean remove regression/libm_linking.mlir, since these new tests will be providing the same compilation coverage and more exhaustive numerics checks.

@benvanik

benvanik commented Mar 6, 2025

Copy link
Copy Markdown
Collaborator

(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 --gtest_filter= and disabling those specific tests per platform as part of the invocation instead of the source file)

@bjacob

bjacob commented Mar 6, 2025

Copy link
Copy Markdown
Collaborator Author

@benvanik ,

(there's also no diffs between the backends - so even more reason for them not to be duplicated)

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:

git diff --no-index tests/e2e/math/math_ops_f16*

It corresponds to the currently 3 places in generate.py that set the tolerance conditionally on the backend.

@benvanik

benvanik commented Mar 6, 2025 •

Copy link
Copy Markdown
Collaborator

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 - generate.py --backend=rocm --type=f16 --tolerance=sqrt=1e-3 etc. Because currently that generate.py is not maintainable - needing to have conditions for data types and for backends in the table in there does not scale to new data types or backends. At the point you're doing that you should delete the generate scripts and just hand code these - they're copy/paste boilerplate with different tolerances.

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)

@bjacob

bjacob commented Mar 6, 2025

Copy link
Copy Markdown
Collaborator Author

@benvanik

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.

@benvanik

benvanik commented Mar 6, 2025 •

Copy link
Copy Markdown
Collaborator

You can repeat flags, you know :)
(https://stackoverflow.com/questions/63581675/ordered-and-repeated-flags-using-argparse)

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.

@benvanik

benvanik commented Mar 6, 2025 •

Copy link
Copy Markdown
Collaborator

Concretely, instead of burying stuff like this in the python:

"sinh", 1e-4 if args.type == "f32" else 2e-3 if args.backend == "rocm" else 1e-3,

You flip it so that the invocation to generate.py for --type=f16 --backend=rocm passes in -tolerance=sinh=2e-3. If you want to do that via an array you can - just like you have arrays of types and backends in the .sh. So it'll look like

tolerances=(
  "sinh=2e-3"
)

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).

@bjacob

bjacob commented Mar 10, 2025 •

Copy link
Copy Markdown
Collaborator Author

@benvanik , @ScottTodd , PTAL, this new state of this PR should incorporate changes discussed above (and in a call with Ben last week).

  1. .mlir files are now generated, not checked in.
  2. The generate.py script now only contains generic information about each math op.
  3. A minimalistic yet generalist iree_genrule is introduced, to achieve 1. without losing Bazel support (which is neat to avoid handwriting the CMakeLists, because the list comprehensions are useful for this kind of build file).
  4. Testcase-specific information is moved to JSON files.
  5. A further improvement to check.expect_almost_eq caught by these tests: when lhs==rhs==+inf, the naive expression was computing lhs-rhs = NaN and returning false.

@bjacob
bjacob requested review from ScottTodd and benvanik March 10, 2025 20:28

@benvanik benvanik 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.

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

@ScottTodd

Copy link
Copy Markdown
Member

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.

@bjacob

bjacob commented Mar 11, 2025

Copy link
Copy Markdown
Collaborator Author

@benvanik : done. Printing to stderr shows in the terminal output during build, like a build warning.

@ScottTodd, noted.

@bjacob
bjacob requested a review from benvanik March 11, 2025 15:18
@benvanik

Copy link
Copy Markdown
Collaborator

thanks!

Signed-off-by: Benoit Jacob <[email protected]>
@bjacob

bjacob commented Mar 11, 2025

Copy link
Copy Markdown
Collaborator Author

@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 :-)

@ScottTodd

Copy link
Copy Markdown
Member

I can do a quick scan through now.

@bjacob
bjacob merged commit 1193f50 into iree-org:main Mar 11, 2025
@ScottTodd ScottTodd mentioned this pull request Mar 14, 2025
5 of 6 tasks
AWoloszyn pushed a commit that referenced this pull request Dec 1, 2025
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]>
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