[TOPI] Reject non-float inputs for inverse unary math ops#18880
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the robustness of the "topi.acos" and "topi.asin" operations by introducing strict input type validation. It ensures that these trigonometric functions only accept floating-point tensors, preventing potential runtime errors or unexpected behavior when non-float types are passed. The changes include a new utility function for type checking and corresponding tests to verify both rejection of invalid inputs and continued functionality for valid ones. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request introduces a validation check to ensure that topi.acos and topi.asin only accept floating-point inputs, improving robustness and error messaging. No security vulnerabilities were found. For further consistency and test coverage, consider applying this float check to other inverse functions like acosh, asinh, and atanh, and extend tests to cover bfloat16.
| def _require_float_tensor(op_name, x): | ||
| if DataType(x.dtype).type_code not in (DataTypeCode.FLOAT, DataTypeCode.BFLOAT): | ||
| raise TypeError(f"topi.{op_name} only supports floating-point inputs, but got {x.dtype}") | ||
| return x |
There was a problem hiding this comment.
This is a useful helper function for enforcing float-only inputs. For consistency and to prevent future issues, consider applying this check to other inverse trigonometric and hyperbolic functions that currently lack input type validation, such as acosh, asinh, and atanh. This would ensure they also handle non-floating-point types gracefully by raising a clear error.
For example, acosh could be updated as follows:
@tvm.te.tag_scope(tag=tag.ELEMWISE)
def acosh(x):
"""Take arc cosh of input x.
...
"""
x = _require_float_tensor("acosh", x)
return te.compute(x.shape, lambda *i: te.acosh(x(*i)))| @pytest.mark.parametrize("op_name", ["acos", "asin"]) | ||
| def test_topi_float_unary_accepts_float_input(op_name): | ||
| x = te.placeholder((1, 8), dtype="float32", name="x") | ||
| op = getattr(topi, op_name) | ||
| out = op(x) | ||
|
|
||
| func = te.create_prim_func([x, out]).with_attr("target", tvm.target.Target("llvm")) | ||
| mod = tvm.IRModule({"main": func}) | ||
| compiled = tvm.build(mod, target="llvm") | ||
|
|
||
| assert compiled is not None |
There was a problem hiding this comment.
The _require_float_tensor function supports both FLOAT and BFLOAT types. To ensure full coverage, it would be beneficial to extend this test to also validate bfloat16 inputs. You can achieve this by parameterizing the dtype.
You may need to add logic to skip the bfloat16 test on platforms where it's not supported.
| @pytest.mark.parametrize("op_name", ["acos", "asin"]) | |
| def test_topi_float_unary_accepts_float_input(op_name): | |
| x = te.placeholder((1, 8), dtype="float32", name="x") | |
| op = getattr(topi, op_name) | |
| out = op(x) | |
| func = te.create_prim_func([x, out]).with_attr("target", tvm.target.Target("llvm")) | |
| mod = tvm.IRModule({"main": func}) | |
| compiled = tvm.build(mod, target="llvm") | |
| assert compiled is not None | |
| @pytest.mark.parametrize("op_name", ["acos", "asin"]) | |
| @pytest.mark.parametrize("dtype", ["float32", "bfloat16"]) | |
| def test_topi_float_unary_accepts_float_input(op_name, dtype): | |
| x = te.placeholder((1, 8), dtype=dtype, name="x") | |
| op = getattr(topi, op_name) | |
| out = op(x) | |
| func = te.create_prim_func([x, out]).with_attr("target", tvm.target.Target("llvm")) | |
| mod = tvm.IRModule({"main": func}) | |
| compiled = tvm.build(mod, target="llvm") | |
| assert compiled is not None |
|
I extended the same float-only validation to |
Summary
Reject non-float inputs for inverse trigonometric and hyperbolic unary ops in TOPI.
Changes
topi.acos,topi.acosh,topi.asin,topi.asinh, andtopi.atanhfloat32andbfloat16Validation
tests/python/te/test_te_create_primfunc.py -k 'topi_float_unary'TypeErrorfor integer inputsfloat32andbfloat16paths still compile with LLVMIssue
Fixes #18729