Fix bug in `check_empty_containers` (#63492)
authorAnsley Ussery <ansley@fb.com>
Wed, 25 Aug 2021 16:01:50 +0000 (09:01 -0700)
committerFacebook GitHub Bot <facebook-github-bot@users.noreply.github.com>
Wed, 25 Aug 2021 16:05:08 +0000 (09:05 -0700)
Summary: Pull Request resolved: https://github.com/pytorch/pytorch/pull/63492

Test Plan: Imported from OSS

Reviewed By: bdhirsh

Differential Revision: D30402749

Pulled By: ansley

fbshipit-source-id: 7de533355fe91ca4f45b2bafc3bfb205a028c1ed

test/jit/test_isinstance.py
test/test_jit.py
torch/_jit_internal.py

index 93b2605..5fd2b87 100644 (file)
@@ -310,3 +310,12 @@ class TestIsinstance(JitTestCase):
             x: int = 2
             fn(x)
             self.assertEqual(len(w), 0)
+
+    def test_empty_container_special_cases(self):
+        # Should not throw "Boolean value of Tensor with no values is
+        # ambiguous" error
+        torch._jit_internal.check_empty_containers(torch.Tensor([]))
+
+        # Should not throw "Boolean value of Tensor with more than
+        # one value is ambiguous" error
+        torch._jit_internal.check_empty_containers(torch.rand(2, 3))
index 06afe65..28de172 100644 (file)
@@ -5944,7 +5944,6 @@ a")
         self.assertEqual(test_bool_arith_not(torch.zeros(3)), 1)
         self.assertTrue(str(test_bool_arith_not.graph).count('if') == 0)
 
-
     def test_conditional_casting(self):
         def test_bool_cast_tensor(x):
             if x:
index bd7b616..418607a 100644 (file)
@@ -1133,7 +1133,7 @@ def check_args_exist(target_type) -> None:
 
 
 def check_empty_containers(obj) -> None:
-    if not obj:
+    if obj == [] or obj == {} or obj == ():
         warnings.warn("The inner type of a container is lost when "
                       "calling torch.jit.isinstance in eager mode. For "
                       "example, List[int] would become list and "