1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
|
# Owner(s): ["oncall: jit"]
import os
import sys
import torch
from torch.testing import FileCheck
from enum import Enum
from typing import Any, List
# Make the helper files in test/ importable
pytorch_test_dir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
sys.path.append(pytorch_test_dir)
from torch.testing._internal.jit_utils import JitTestCase, make_global
if __name__ == '__main__':
raise RuntimeError("This test file is not meant to be run directly, use:\n\n"
"\tpython test/test_jit.py TESTNAME\n\n"
"instead.")
class TestEnum(JitTestCase):
def test_enum_value_types(self):
class IntEnum(Enum):
FOO = 1
BAR = 2
class FloatEnum(Enum):
FOO = 1.2
BAR = 2.3
class StringEnum(Enum):
FOO = "foo as in foo bar"
BAR = "bar as in foo bar"
make_global(IntEnum, FloatEnum, StringEnum)
@torch.jit.script
def supported_enum_types(a: IntEnum, b: FloatEnum, c: StringEnum):
return (a.name, b.name, c.name)
FileCheck() \
.check("IntEnum") \
.check("FloatEnum") \
.check("StringEnum") \
.run(str(supported_enum_types.graph))
class TensorEnum(Enum):
FOO = torch.tensor(0)
BAR = torch.tensor(1)
make_global(TensorEnum)
def unsupported_enum_types(a: TensorEnum):
return a.name
# TODO: rewrite code so that the highlight is not empty.
with self.assertRaisesRegexWithHighlight(RuntimeError, "Cannot create Enum with value type 'Tensor'", ""):
torch.jit.script(unsupported_enum_types)
def test_enum_comp(self):
class Color(Enum):
RED = 1
GREEN = 2
make_global(Color)
@torch.jit.script
def enum_comp(x: Color, y: Color) -> bool:
return x == y
FileCheck().check("aten::eq").run(str(enum_comp.graph))
self.assertEqual(enum_comp(Color.RED, Color.RED), True)
self.assertEqual(enum_comp(Color.RED, Color.GREEN), False)
def test_enum_comp_diff_classes(self):
class Foo(Enum):
ITEM1 = 1
ITEM2 = 2
class Bar(Enum):
ITEM1 = 1
ITEM2 = 2
make_global(Foo, Bar)
@torch.jit.script
def enum_comp(x: Foo) -> bool:
return x == Bar.ITEM1
FileCheck() \
.check("prim::Constant") \
.check_same("Bar.ITEM1") \
.check("aten::eq") \
.run(str(enum_comp.graph))
self.assertEqual(enum_comp(Foo.ITEM1), False)
def test_heterogenous_value_type_enum_error(self):
class Color(Enum):
RED = 1
GREEN = "green"
make_global(Color)
def enum_comp(x: Color, y: Color) -> bool:
return x == y
# TODO: rewrite code so that the highlight is not empty.
with self.assertRaisesRegexWithHighlight(RuntimeError, "Could not unify type list", ""):
torch.jit.script(enum_comp)
def test_enum_name(self):
class Color(Enum):
RED = 1
GREEN = 2
make_global(Color)
@torch.jit.script
def enum_name(x: Color) -> str:
return x.name
FileCheck() \
.check("Color") \
.check_next("prim::EnumName") \
.check_next("return") \
.run(str(enum_name.graph))
self.assertEqual(enum_name(Color.RED), Color.RED.name)
self.assertEqual(enum_name(Color.GREEN), Color.GREEN.name)
def test_enum_value(self):
class Color(Enum):
RED = 1
GREEN = 2
make_global(Color)
@torch.jit.script
def enum_value(x: Color) -> int:
return x.value
FileCheck() \
.check("Color") \
.check_next("prim::EnumValue") \
.check_next("return") \
.run(str(enum_value.graph))
self.assertEqual(enum_value(Color.RED), Color.RED.value)
self.assertEqual(enum_value(Color.GREEN), Color.GREEN.value)
def test_enum_as_const(self):
class Color(Enum):
RED = 1
GREEN = 2
make_global(Color)
@torch.jit.script
def enum_const(x: Color) -> bool:
return x == Color.RED
FileCheck() \
.check("prim::Constant[value=__torch__.jit.test_enum.Color.RED]") \
.check_next("aten::eq") \
.check_next("return") \
.run(str(enum_const.graph))
self.assertEqual(enum_const(Color.RED), True)
self.assertEqual(enum_const(Color.GREEN), False)
def test_non_existent_enum_value(self):
class Color(Enum):
RED = 1
GREEN = 2
make_global(Color)
def enum_const(x: Color) -> bool:
if x == Color.PURPLE:
return True
else:
return False
with self.assertRaisesRegexWithHighlight(RuntimeError, "has no attribute 'PURPLE'", "Color.PURPLE"):
torch.jit.script(enum_const)
def test_enum_ivalue_type(self):
class Color(Enum):
RED = 1
GREEN = 2
make_global(Color)
@torch.jit.script
def is_color_enum(x: Any):
return isinstance(x, Color)
FileCheck() \
.check("prim::isinstance[types=[Enum<__torch__.jit.test_enum.Color>]]") \
.check_next("return") \
.run(str(is_color_enum.graph))
self.assertEqual(is_color_enum(Color.RED), True)
self.assertEqual(is_color_enum(Color.GREEN), True)
self.assertEqual(is_color_enum(1), False)
def test_closed_over_enum_constant(self):
class Color(Enum):
RED = 1
GREEN = 2
a = Color
@torch.jit.script
def closed_over_aliased_type():
return a.RED.value
FileCheck() \
.check("prim::Constant[value={}]".format(a.RED.value)) \
.check_next("return") \
.run(str(closed_over_aliased_type.graph))
self.assertEqual(closed_over_aliased_type(), Color.RED.value)
b = Color.RED
@torch.jit.script
def closed_over_aliased_value():
return b.value
FileCheck() \
.check("prim::Constant[value={}]".format(b.value)) \
.check_next("return") \
.run(str(closed_over_aliased_value.graph))
self.assertEqual(closed_over_aliased_value(), Color.RED.value)
def test_enum_as_module_attribute(self):
class Color(Enum):
RED = 1
GREEN = 2
class TestModule(torch.nn.Module):
def __init__(self, e: Color):
super(TestModule, self).__init__()
self.e = e
def forward(self):
return self.e.value
m = TestModule(Color.RED)
scripted = torch.jit.script(m)
FileCheck() \
.check("TestModule") \
.check_next("Color") \
.check_same("prim::GetAttr[name=\"e\"]") \
.check_next("prim::EnumValue") \
.check_next("return") \
.run(str(scripted.graph))
self.assertEqual(scripted(), Color.RED.value)
def test_string_enum_as_module_attribute(self):
class Color(Enum):
RED = "red"
GREEN = "green"
class TestModule(torch.nn.Module):
def __init__(self, e: Color):
super(TestModule, self).__init__()
self.e = e
def forward(self):
return (self.e.name, self.e.value)
make_global(Color)
m = TestModule(Color.RED)
scripted = torch.jit.script(m)
self.assertEqual(scripted(), (Color.RED.name, Color.RED.value))
def test_enum_return(self):
class Color(Enum):
RED = 1
GREEN = 2
make_global(Color)
@torch.jit.script
def return_enum(cond: bool):
if cond:
return Color.RED
else:
return Color.GREEN
self.assertEqual(return_enum(True), Color.RED)
self.assertEqual(return_enum(False), Color.GREEN)
def test_enum_module_return(self):
class Color(Enum):
RED = 1
GREEN = 2
class TestModule(torch.nn.Module):
def __init__(self, e: Color):
super(TestModule, self).__init__()
self.e = e
def forward(self):
return self.e
make_global(Color)
m = TestModule(Color.RED)
scripted = torch.jit.script(m)
FileCheck() \
.check("TestModule") \
.check_next("Color") \
.check_same("prim::GetAttr[name=\"e\"]") \
.check_next("return") \
.run(str(scripted.graph))
self.assertEqual(scripted(), Color.RED)
def test_enum_iterate(self):
class Color(Enum):
RED = 1
GREEN = 2
BLUE = 3
def iterate_enum(x: Color):
res: List[int] = []
for e in Color:
if e != x:
res.append(e.value)
return res
make_global(Color)
scripted = torch.jit.script(iterate_enum)
FileCheck() \
.check("Enum<__torch__.jit.test_enum.Color>[]") \
.check_same("Color.RED") \
.check_same("Color.GREEN") \
.check_same("Color.BLUE") \
.run(str(scripted.graph))
# PURPLE always appears last because we follow Python's Enum definition order.
self.assertEqual(scripted(Color.RED), [Color.GREEN.value, Color.BLUE.value])
self.assertEqual(scripted(Color.GREEN), [Color.RED.value, Color.BLUE.value])
# Tests that explicitly and/or repeatedly scripting an Enum class is permitted.
def test_enum_explicit_script(self):
@torch.jit.script
class Color(Enum):
RED = 1
GREEN = 2
torch.jit.script(Color)
|