Repository: tinygrad/tinygrad

Issue:
Tensor.__eq__() with two bool tensors raises error on Torch backend
This was introduced from #1493

To reproduce:
```
In [24]: (Tensor([1], dtype=dtypes.bool, device="TORCH") == Tensor([1], dtype=dtypes.bool, device="TORCH")).realize()
RuntimeError: Subtraction, the `-` operator, with a bool tensor is not supported. If you are trying to invert a mask, use the `~` or `logical_not()` operator instead.
```
RuntimeError is from pytorch




Discussion:

Resolving patch:
diff --git a/tinygrad/runtime/ops_torch.py b/tinygrad/runtime/ops_torch.py
--- a/tinygrad/runtime/ops_torch.py
+++ b/tinygrad/runtime/ops_torch.py
@@ -18,7 +18,7 @@ def as_strided(x, arg):
 torch_fxn_for_op: Dict[Op, Callable] = {**base_fxn_for_op, **{
   UnaryOps.NOOP: lambda x: x.contiguous(), UnaryOps.SQRT: lambda x: x.sqrt(), UnaryOps.EXP2: lambda x: x.exp2(), UnaryOps.LOG2: lambda x: x.log2(), UnaryOps.SIN: torch.sin,
   UnaryOps.CAST: lambda x,y: (x.view if y[1] else x.type)(next(k for k,v in type_map.items() if v==y[0])),
-  BinaryOps.MAX: torch.maximum, BinaryOps.CMPLT: lambda x,y: (x<y).type(torch.promote_types(x.dtype, y.dtype)),
+  BinaryOps.MAX: torch.maximum, BinaryOps.CMPLT: lambda x,y: (x<y).type(torch.promote_types(x.dtype, y.dtype)), BinaryOps.SUB: lambda x,y: torch.logical_xor(x, y) if y.dtype is torch.bool else torch.sub(x, y),
   MovementOps.PAD: lambda x, padding: torch.nn.functional.pad(x, [item for sublist in padding[::-1] for item in sublist]),
   TernaryOps.MULACC: einsum_mulacc(lambda s,a,b: torch.einsum(s, a.float(), b.float()).type(torch.promote_types(a.dtype, b.dtype)), lambda x: x.stride(), lambda x,s: x.expand(s)),
   TernaryOps.WHERE: lambda x, y, z: torch.where(x != 0, y, z),


Test patch:
diff --git a/test/external/external_test_onnx_backend.py b/test/external/external_test_onnx_backend.py
--- a/test/external/external_test_onnx_backend.py
+++ b/test/external/external_test_onnx_backend.py
@@ -160,42 +160,6 @@ def supports_device(cls, device: str) -> bool:
   backend_test.exclude('test_dequantizelinear_axis_cpu')
   backend_test.exclude('test_dequantizelinear_cpu')
 
-if getenv("TORCH"): # 1562
-  backend_test.exclude('test_and2d_cpu')
-  backend_test.exclude('test_and3d_cpu')
-  backend_test.exclude('test_and4d_cpu')
-  backend_test.exclude('test_and_bcast3v1d_cpu')
-  backend_test.exclude('test_and_bcast3v2d_cpu')
-  backend_test.exclude('test_and_bcast4v2d_cpu')
-  backend_test.exclude('test_and_bcast4v3d_cpu')
-  backend_test.exclude('test_and_bcast4v4d_cpu')
-  backend_test.exclude('test_dequantizelinear_axis_cpu')
-  backend_test.exclude('test_dequantizelinear_cpu')
-  backend_test.exclude('test_greater_equal_bcast_expanded_cpu')
-  backend_test.exclude('test_greater_equal_expanded_cpu')
-  backend_test.exclude('test_isinf_cpu')
-  backend_test.exclude('test_isinf_negative_cpu')
-  backend_test.exclude('test_isinf_positive_cpu')
-  backend_test.exclude('test_isnan_cpu')
-  backend_test.exclude('test_less_equal_bcast_expanded_cpu')
-  backend_test.exclude('test_less_equal_expanded_cpu')
-  backend_test.exclude('test_or2d_cpu')
-  backend_test.exclude('test_or3d_cpu')
-  backend_test.exclude('test_or4d_cpu')
-  backend_test.exclude('test_or_bcast3v1d_cpu')
-  backend_test.exclude('test_or_bcast3v2d_cpu')
-  backend_test.exclude('test_or_bcast4v2d_cpu')
-  backend_test.exclude('test_or_bcast4v3d_cpu')
-  backend_test.exclude('test_or_bcast4v4d_cpu')
-  backend_test.exclude('test_xor2d_cpu')
-  backend_test.exclude('test_xor3d_cpu')
-  backend_test.exclude('test_xor4d_cpu')
-  backend_test.exclude('test_xor_bcast3v1d_cpu')
-  backend_test.exclude('test_xor_bcast3v2d_cpu')
-  backend_test.exclude('test_xor_bcast4v2d_cpu')
-  backend_test.exclude('test_xor_bcast4v3d_cpu')
-  backend_test.exclude('test_xor_bcast4v4d_cpu')
-
 if getenv('LLVM') or getenv('GPU') or getenv('CLANG') or getenv('METAL') or getenv('MPS'):
   # compiled backends cannot reshape to 0 or from 0
   backend_test.exclude('test_slice_start_out_of_bounds_cpu')
diff --git a/test/models/test_bert.py b/test/models/test_bert.py
--- a/test/models/test_bert.py
+++ b/test/models/test_bert.py
@@ -22,7 +22,6 @@ def set_equal_weights(mdl, torch_mdl):
   torch_mdl.eval()
 
 class TestBert(unittest.TestCase):
-  @unittest.skipIf(Device.DEFAULT == "TORCH", "torch doesn't support '-' operator with a bool tensor")  # TODO
   def test_questions(self):
     from models.bert import BertForQuestionAnswering
     from transformers import BertForQuestionAnswering as TorchBertForQuestionAnswering
diff --git a/test/test_ops.py b/test/test_ops.py
--- a/test/test_ops.py
+++ b/test/test_ops.py
@@ -160,6 +160,7 @@ def _test_cmp(self, fxn, reverse=True):
       helper_test_op(shps, fxn, fxn, forward_only=True)
     helper_test_op(None, fxn, fxn, forward_only=True, vals=[[0.,1,2], [2.,1,0]])
     helper_test_op(None, lambda x,y: fxn(x,2), lambda x,y: fxn(x,2), forward_only=True, vals=[[0.,1,2], [2.,1,0]])
+    helper_test_op(None, fxn, fxn, forward_only=True, vals=[[True, True, False], [False,True,False]])
     if reverse: helper_test_op(None, lambda x,y: fxn(2,y), lambda x,y: fxn(2,y), forward_only=True, vals=[[0.,1,2], [2.,1,0]])
 
   def test_cmp_eq(self): self._test_cmp(lambda x,y: x==y, reverse=False)