You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "javierdejesusda (via GitHub)" <gi...@apache.org> on 2026/06/06 15:44:59 UTC

[PR] [Relax][PyTorch] Cast non-bool inputs to bool in logical_and converter [tvm]

javierdejesusda opened a new pull request, #19679:
URL: https://github.com/apache/tvm/pull/19679

   ### Motivation
   
   `torch.logical_and` accepts input tensors of any dtype (treating any nonzero
   element as `True`) and always returns a `bool` tensor.
   
   The PyTorch frontend did not produce that `bool` result. The ExportedProgram
   frontend lowered `logical_and.default` with
   `self._binary_op(relax.op.logical_and, operator.and_)`, which kept the operand
   dtype and emitted `relax.op.logical_and` on non-bool inputs (for example
   `float32`). `relax.op.logical_and` requires boolean inputs and otherwise fails
   `LegalizeOps` in the TOPI `logical_and`. The FX frontend did not register
   `logical_and` at all, so the op was unsupported there.
   
   ### Changes
   
   - Add a shared `_logical_and` converter in `BaseFXGraphImporter` that casts
     non-bool operands to `bool` before applying `relax.op.logical_and`. Bool
     operands are passed through unchanged (no redundant cast).
   - Point the `logical_and.default` (ExportedProgram) registration at the new
     converter, and add a `logical_and` (FX) registration that was previously
     missing, matching the existing `logical_not` converter.
   - Add a standalone `test_logical_and` to both the FX and ExportedProgram test
     suites asserting the corrected IR (`astype` to bool on each operand, then
     `logical_and`, 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.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [Relax][PyTorch] Cast non-bool inputs to bool in logical_and converter [tvm]

Posted by "tlopex (via GitHub)" <gi...@apache.org>.
tlopex merged PR #19679:
URL: https://github.com/apache/tvm/pull/19679


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [Relax][PyTorch] Cast non-bool inputs to bool in logical_and converter [tvm]

Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #19679:
URL: https://github.com/apache/tvm/pull/19679#discussion_r3367743512


##########
python/tvm/relax/frontend/torch/base_fx_graph_translator.py:
##########
@@ -391,6 +391,17 @@ def _log_softmax(self, node: fx.Node) -> relax.Var:
         dim = node.args[1] if len(node.args) > 1 else node.kwargs.get("dim", -1)
         return self.block_builder.emit(relax.op.nn.log_softmax(x, dim))
 
+    def _logical_and(self, node: fx.Node) -> relax.Var:
+        lhs = self.env[node.args[0]]
+        rhs = self.env[node.args[1]]
+        # torch.logical_and accepts any dtype (treating nonzero as True) and returns bool, but
+        # relax.op.logical_and requires boolean inputs, so cast non-bool inputs to bool first.
+        if lhs.struct_info.dtype != "bool":
+            lhs = self.block_builder.emit(relax.op.astype(lhs, "bool"))
+        if rhs.struct_info.dtype != "bool":
+            rhs = self.block_builder.emit(relax.op.astype(rhs, "bool"))

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   Directly accessing `lhs.struct_info.dtype` can raise an `AttributeError` if `struct_info` is `None` or not an instance of `relax.TensorStructInfo`. To ensure robustness and consistency with other logical operators like `_logical_not`, we should first verify that `struct_info` is indeed a `TensorStructInfo` before accessing its `dtype`.
   
   ```suggestion
           if not isinstance(lhs.struct_info, relax.TensorStructInfo) or lhs.struct_info.dtype != "bool":
               lhs = self.block_builder.emit(relax.op.astype(lhs, "bool"))
           if not isinstance(rhs.struct_info, relax.TensorStructInfo) or rhs.struct_info.dtype != "bool":
               rhs = self.block_builder.emit(relax.op.astype(rhs, "bool"))
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [Relax][PyTorch] Cast non-bool inputs to bool in logical_and converter [tvm]

Posted by "javierdejesusda (via GitHub)" <gi...@apache.org>.
javierdejesusda commented on code in PR #19679:
URL: https://github.com/apache/tvm/pull/19679#discussion_r3368198037


##########
python/tvm/relax/frontend/torch/base_fx_graph_translator.py:
##########
@@ -391,6 +391,17 @@ def _log_softmax(self, node: fx.Node) -> relax.Var:
         dim = node.args[1] if len(node.args) > 1 else node.kwargs.get("dim", -1)
         return self.block_builder.emit(relax.op.nn.log_softmax(x, dim))
 
+    def _logical_and(self, node: fx.Node) -> relax.Var:
+        lhs = self.env[node.args[0]]
+        rhs = self.env[node.args[1]]
+        # torch.logical_and accepts any dtype (treating nonzero as True) and returns bool, but
+        # relax.op.logical_and requires boolean inputs, so cast non-bool inputs to bool first.
+        if lhs.struct_info.dtype != "bool":
+            lhs = self.block_builder.emit(relax.op.astype(lhs, "bool"))
+        if rhs.struct_info.dtype != "bool":
+            rhs = self.block_builder.emit(relax.op.astype(rhs, "bool"))

Review Comment:
   I kept the direct `struct_info.dtype` check to stay
   consistent with the existing `_logical_not` converter just below, which accesses
   `struct_info.dtype` the same way without an `isinstance` check. Both operands come
   from `torch.logical_and`, which only accepts tensor inputs, so their `struct_info`
   is always a `TensorStructInfo` and the access is safe. Adding the guard here would
   make this converter diverge from `_logical_not`, so I left it consistent with the
   surrounding code.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org