MCPcopy Create free account
hub / github.com/pytorch/pytorch / test_conditional_casting

Method test_conditional_casting

test/test_jit.py:6145–6201  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

6143 self.assertTrue(str(test_bool_arith_not.graph).count('if') == 0)
6144
6145 def test_conditional_casting(self):
6146 def test_bool_cast_tensor(x):
6147 if x:
6148 return 1
6149 else:
6150 return 0
6151
6152 for make_one_dim in [True, False]:
6153 for inp_val in [0.1, 0.0, -0.0, -0.1, -1, 0, 1]:
6154 inp_val = [inp_val] if make_one_dim else inp_val
6155 self.checkScript(test_bool_cast_tensor, (torch.tensor(inp_val),))
6156
6157 self.checkScriptRaisesRegex(test_bool_cast_tensor, (torch.tensor([1, 1]),), Exception,
6158 "Boolean value of Tensor with more than one value")
6159
6160 def test_not_cast(x):
6161 if not x:
6162 return 1
6163 else:
6164 return 0
6165
6166 self.checkScript(test_not_cast, (torch.tensor(1),))
6167 self.checkScript(test_not_cast, (torch.tensor(0),))
6168
6169 with self.assertRaisesRegex(RuntimeError, r"Could not cast value of type Tuple\[Tensor, Tensor\]"): # noqa: W605
6170 @torch.jit.script
6171 def test_mult(x, y):
6172 return not(x, y)
6173
6174 def test_cast_int(x):
6175 # type: (int) -> int
6176 if x:
6177 return 1
6178 else:
6179 return 0
6180 self.checkScript(test_cast_int, (1,))
6181 self.checkScript(test_cast_int, (0,))
6182 self.checkScript(test_cast_int, (-1,))
6183
6184 def test_cast_float(x):
6185 # type: (float) -> int
6186 if x:
6187 return 1
6188 else:
6189 return 0
6190 self.checkScript(test_cast_float, (1.,))
6191 self.checkScript(test_cast_float, (0.,))
6192 self.checkScript(test_cast_float, (-1.,))
6193
6194 with self.assertRaisesRegex(RuntimeError, r"Could not cast value of type Tuple\[int, int\] to bool"): # noqa: W605
6195
6196 @torch.jit.script
6197 def test_bad_conditional(x):
6198 if (1, 2): # noqa: F634
6199 return
6200 else:
6201 return 0
6202

Callers

nothing calls this directly

Calls 4

checkScriptMethod · 0.80
assertRaisesRegexMethod · 0.80
tensorMethod · 0.45

Tested by

no test coverage detected