raindrops-0199 opened a new issue, #19743:
URL: https://github.com/apache/tvm/issues/19743

   ### Expected behavior
   
   In PyTorch, torch.logical_and accepts non-bool tensors and interprets 
nonzero values as True, returning a bool tensor:
   
   ```
   >>> torch.logical_and(torch.tensor([1., 0., 2.]), torch.tensor([1., 1., 0.]))
   tensor([ True, False, False])
   ```
   
   Since the model is accepted by both torch.export and 
relax.frontend.torch.from_exported_program, I would expect either:
   
   - the imported Relax program to compile successfully, possibly by converting 
non-bool operands to boolean values before lowering, e.g. via x != 0; or
   - the frontend/importer to report a clearer unsupported-case error earlier.
   
   ### Actual behavior
   
   The model is accepted by `torch.export` and by 
`relax.frontend.torch.from_exported_program`, but `tvm.compile` aborts with an 
internal check failure during legalization:
   
   ```
   InternalError: Check failed: (lhs.dtype().is_bool()) is false:
     Expected boolean argument as LHS of && operator (logical AND), but 
received x[ax0, ax1] of type float32
     File ".../tvm/src/tirx/op/op.cc", line 726, in 
tvm::{anonymous}::type_check_boolean_args(
           const tvm::PrimExpr&, const tvm::PrimExpr&, const char*)
       TVM_FFI_ICHECK(lhs.dtype().is_bool()) << "Expected boolean argument as 
LHS of " << op ...
   ```
   
   From the error message, it looks like Relax logical_and is eventually 
lowered to a TIR && expression directly on the float operands, while TIR && 
expects boolean operands.
   
   Would it be reasonable for the PyTorch frontend or Relax legalization to 
insert a boolean conversion for non-bool inputs in this case? I wonder if 
related operators such as logical_or and logical_xor may need similar handling.
   
   ### Environment
   
   - TVM: 0.25.dev0 (commit `6b4b866d6`)
   - OS: Ubuntu 22.04 (Linux 6.8, x86_64)
   - Python: 3.12
   - PyTorch: 2.12.0
   - Target: `llvm`
   
   ### Steps to reproduce
   
   ```python
   import torch
   import torch.nn as nn
   from torch.export import export
   import tvm
   from tvm import relax
   from tvm.relax.frontend.torch import from_exported_program
   
   
   class M(nn.Module):
       def forward(self, x):
           return torch.logical_and(x, x)   # x is float32 (non-bool)
   
   
   m = M().eval()
   args = (torch.randn(2, 3),)
   ep = export(m, args)
   mod = from_exported_program(ep, keep_params_as_input=True, 
unwrap_unit_return_tuple=True)
   mod, _ = relax.frontend.detach_params(mod)
   tvm.compile(mod, target=tvm.target.Target("llvm"))
   ```
   
   ### Triage
   
   * needs-triage
   


-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to