Skip to content

[Relax][PyTorch] Cast non-bool inputs to bool in logical_not converter#19645

Open
javierdejesusda wants to merge 1 commit into
apache:mainfrom
javierdejesusda:relax-torch-logical-not-bool
Open

[Relax][PyTorch] Cast non-bool inputs to bool in logical_not converter#19645
javierdejesusda wants to merge 1 commit into
apache:mainfrom
javierdejesusda:relax-torch-logical-not-bool

Conversation

@javierdejesusda
Copy link
Copy Markdown
Contributor

Motivation

torch.logical_not accepts an input tensor of any dtype (treating any nonzero
element as True) and always returns a bool tensor.

The PyTorch frontend previously lowered it with self._unary_op(relax.op.logical_not).
relax.op.logical_not is a unary arithmetic op that passes its input dtype through,
so a non-bool input (for example float32) produced a float32 result instead of
the bool result PyTorch returns. This is a dtype mismatch against the reference
PyTorch semantics for both the FX and ExportedProgram frontends.

Changes

  • Add a shared _logical_not converter in BaseFXGraphImporter that casts non-bool
    inputs to bool before applying relax.op.logical_not. Bool inputs are passed
    through unchanged (no redundant cast).
  • Point the logical_not (FX) and logical_not.default (ExportedProgram)
    registrations at the new converter.
  • Update the FX test and add a standalone ExportedProgram test_logical_not to assert
    the corrected IR (astype to bool, then logical_not, producing a bool output).

Notes

The cast to bool lowers to an elementwise nonzero test, so it matches PyTorch's
"nonzero is True" semantics for float, integer, and NaN inputs.

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.
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

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.

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.

1 participant