[Relax][PyTorch] Cast non-bool inputs to bool in logical_not converter#19645
Open
javierdejesusda wants to merge 1 commit into
Open
[Relax][PyTorch] Cast non-bool inputs to bool in logical_not converter#19645javierdejesusda wants to merge 1 commit into
javierdejesusda wants to merge 1 commit into
Conversation
torch.logical_not returns a bool tensor for any input dtype, but the frontend lowered it with a plain unary op that passes the input dtype through, so a float32 input produced a float32 result instead of bool. Add a shared _logical_not converter in BaseFXGraphImporter that casts non-bool inputs to bool before applying relax.op.logical_not, and wire up both the FX and ExportedProgram frontends. Update the tests to assert the corrected bool output.
Contributor
There was a problem hiding this comment.
Code Review
This pull request updates the PyTorch to TVM Relax frontend translators to correctly handle torch.logical_not by casting non-boolean inputs to boolean before applying the logical NOT operation. This aligns the behavior with PyTorch, which accepts any data type for logical NOT, whereas Relax requires a boolean. The changes are applied to both the FX and Exported Program translators, and corresponding unit tests have been added and updated. No review comments were provided, and there is no further feedback.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
torch.logical_notaccepts an input tensor of any dtype (treating any nonzeroelement as
True) and always returns abooltensor.The PyTorch frontend previously lowered it with
self._unary_op(relax.op.logical_not).relax.op.logical_notis a unary arithmetic op that passes its input dtype through,so a non-bool input (for example
float32) produced afloat32result instead ofthe
boolresult PyTorch returns. This is a dtype mismatch against the referencePyTorch semantics for both the FX and ExportedProgram frontends.
Changes
_logical_notconverter inBaseFXGraphImporterthat casts non-boolinputs to
boolbefore applyingrelax.op.logical_not. Bool inputs are passedthrough unchanged (no redundant cast).
logical_not(FX) andlogical_not.default(ExportedProgram)registrations at the new converter.
test_logical_notto assertthe corrected IR (
astypeto bool, thenlogical_not, producing abooloutput).Notes
The cast to
boollowers to an elementwise nonzero test, so it matches PyTorch's"nonzero is True" semantics for float, integer, and NaN inputs.