Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 0 additions & 24 deletions test/expect/TestScript.test_if_is_none_dispatch.expect

This file was deleted.

85 changes: 60 additions & 25 deletions test/test_jit.py
Original file line number Diff line number Diff line change
Expand Up @@ -4203,36 +4203,71 @@ def func(a, b):
self.checkScript(func, inputs, optimize=True)

def test_if_is_none_dispatch(self):
class Test(torch.jit.ScriptModule):
__constants__ = ['b']

def __init__(self, b=None):
super(Test, self).__init__()
self.b = b
@torch.jit.script
def test_lhs_none_rhs_none():
# LHS, RHS both alwaysNone, dispatch always_none_branch
# only emit one prim::Constant
if None is None:
return 1
elif None is not None:
return 2
else:
return 3

@torch.jit.script_method
def forward(self, input, opt=None):
# type: (Tensor, Optional[Tensor]) -> Tensor
x = input
if self.b is not None:
x = self.b(input)
self.assertTrue(str(test_lhs_none_rhs_none.graph).count(': int = prim::Constant') == 1)

if self.b is None:
x = input + 2
@torch.jit.script
def test_lhs_opt_rhs_none(lhs=None):
# type: (Optional[Tensor]) -> int
# LHS maybeNone: emit normal if stmt that contains 3 constants
if lhs is not None:
return 2
elif lhs is None:
return 1
else:
return 3

if opt is not None:
opt = torch.jit._unwrap_optional(opt)
x = opt + x
self.assertTrue(str(test_lhs_opt_rhs_none.graph).count(': int = prim::Constant') == 3)

if opt is None:
x = x + 4
@torch.jit.script
def test_lhs_none_rhs_opt(rhs=None):
# type: (Optional[Tensor]) -> int
# RHS maybeNone, emit normal if stmt that contains 3 constants
if None is rhs:
return 1
elif None is not rhs:
return 2
else:
return 3

return x
self.assertTrue(str(test_lhs_opt_rhs_none.graph).count(': int = prim::Constant') == 3)

@torch.jit.script
def test_lhs_never_rhs_none(lhs):
# LHS neverNone, RHS alwaysNone dispatch never_none_branch
# only emit one prim::Constant
if lhs is None:
return 1
elif lhs is not None:
return 2
else:
return 3

self.assertTrue(str(test_lhs_never_rhs_none.graph).count(': int = prim::Constant') == 1)

@torch.jit.script
def test_lhs_none_rhs_never(rhs):
# LHS alwaysNone, RHS neverNone dispatch never_none_branch
# only emit one prim::Constant
if None is rhs:
return 1
elif None is not rhs:
return 2
else:
return 3

inputs = torch.zeros(1, 2)
self.assertExpectedGraph(Test().graph)
out = Test()(inputs)
self.assertEqual(out, inputs + 6)
self.assertTrue(str(test_lhs_none_rhs_never.graph).count(': int = prim::Constant') == 1)

def test_explicit_bool_cast(self):
with self.assertRaisesRegex(RuntimeError, "expected a boolean"):
Expand Down Expand Up @@ -4507,7 +4542,7 @@ def test_script_optional_tensor_none(x=None):
if x is None:
res = res + 1
else:
res = torch.jit._unwrap_optional(x)
res = x
return res

fn = test_script_optional_tensor_none
Expand All @@ -4522,7 +4557,7 @@ def test_script_optional_other_none(x=None):
if x is None:
res = res + 1.0
else:
res = torch.jit._unwrap_optional(x)
res = x
return res

fn = test_script_optional_other_none
Expand Down