[Relax][Frontend][ONNX] Lower ThresholdedRelu with where in the input dtype (#20861)

fixes #20851

the ThresholdedRelu converter computed `(x > alpha).astype("float32") *
x`. that gives nan for nan and -inf inputs (nan * 0 and -inf * 0 are
nan) where onnx expects 0, and the float32 mask doesn't match float16 or
float64 inputs, so those failed to convert.

this lowers it as `where(x > alpha, x, 0)` with alpha and zero built in
the input dtype, which matches the Where-based function body in the onnx
spec.

tests:
- new `test_thresholded_relu_nonfinite_and_dtypes` runs `[nan, -inf, -2,
0, 0.5, 1, 2, inf]` in float32, float16 and float64 and checks the exact
output and dtype; it fails before the change and passes after.
- updated the expected IR in `test_thresholded_relu`.

note: i couldn't build libtvm from source on this machine, so i ran the
changed converter and these tests (plus the other relu tests, 9 passed)
against the 0.27.0.post1 wheel's runtime; the converter code is
identical there. ruff format and check are clean.

---------

Signed-off-by: Dundy Pasupuleti <dundysm@gmail.com>
2 files changed
tree: 78db787b851ba999b1f5392206e20341f9c875ce
  1. .agents/
  2. .github/
  3. 3rdparty/
  4. apps/
  5. ci/
  6. cmake/
  7. docker/
  8. docs/
  9. include/
  10. jvm/
  11. licenses/
  12. python/
  13. src/
  14. tests/
  15. web/
  16. .asf.yaml
  17. .clang-format
  18. .gitattributes
  19. .gitignore
  20. .gitmodules
  21. .markdownlint-cli2.yaml
  22. .pre-commit-config.yaml
  23. .yamllint.yaml
  24. AGENTS.md
  25. CMakeLists.txt
  26. CONTRIBUTORS.md
  27. KEYS
  28. LICENSE
  29. NOTICE
  30. pyproject.toml
  31. README.md
README.md

Open Machine Learning Compiler Framework

Documentation | Contributors | Community | Release Notes

Apache TVM is an open machine learning compilation framework, following the following principles:

  • Python-first development that enables quick customization of machine learning compiler pipelines.
  • Universal deployment to bring models into minimum deployable modules.

License

TVM is licensed under the Apache-2.0 license.

Getting Started

Check out the TVM Documentation site for installation instructions, tutorials, examples, and more. The Getting Started with TVM tutorial is a great place to start.

Contribute to TVM

TVM adopts the Apache committer model. We aim to create an open-source project maintained and owned by the community. Check out the Contributor Guide.

History and Acknowledgement

TVM started as a research project for deep learning compilation. The first version of the project benefited a lot from the following projects:

  • Halide: Part of TVM's TIR and arithmetic simplification module originates from Halide. We also learned and adapted some parts of the lowering pipeline from Halide.
  • Loopy: use of integer set analysis and its loop transformation primitives.
  • Theano: the design inspiration of symbolic scan operator for recurrence.

Since then, the project has gone through several rounds of redesigns. The current design is also drastically different from the initial design, following the development trend of the ML compiler community.

The most recent version focuses on a cross-level design with TensorIR as the tensor-level representation and Relax as the graph-level representation and Python-first transformations. The project's current design goal is to make the ML compiler accessible by enabling most transformations to be customizable in Python and bringing a cross-level representation that can jointly optimize computational graphs, tensor programs, and libraries. The project is also a foundation infra for building Python-first vertical compilers for domains, such as LLMs.