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 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
|
# Owner(s): ["module: dynamo"]
import collections
import dis
import sys
import unittest
import torch
import torch._dynamo.test_case
from torch._dynamo import bytecode_analysis, bytecode_transformation
from torch._dynamo.testing import skipIfNotPy311, skipIfNotPy312
class BytecodeTests(torch._dynamo.test_case.TestCase):
@skipIfNotPy311
def test_linetable_311_writer1(self):
def fn():
a = 10
b = 20
# prevent LOAD_FAST_LOAD_FAST in 3.13 by wrapping b with g()
c = a + g(b)
f = "linetable_writer"
return f"Test if {f} generates correct co_linetable: {c}"
keys = bytecode_transformation.get_code_keys()
code_options = {k: getattr(fn.__code__, k) for k in keys}
result = bytecode_transformation.clean_and_assemble_instructions(
bytecode_transformation.cleaned_instructions(fn.__code__),
keys,
code_options,
)
l1, l2 = list(fn.__code__.co_positions()), list(result[1].co_positions())
self.assertEqual(len(l1), len(l2))
for p1, p2 in zip(l1, l2):
self.assertEqual(p1, p2)
# TODO co_lnotab is deprecated in 3.12 and will be removed in 3.14
# In 3.11+,. it is computed lazily from other linetable attributes (e.g. co_linetable),
# so we do not set this attribute ourselves.
self.assertEqual(fn.__code__.co_lnotab, result[1].co_lnotab)
@skipIfNotPy311
def test_linetable_311_writer2(self):
"""
test large ops (LOAD_METHOD) and EXTENDED_ARGS
fn_str is in the form:
def fn():
...
x0 = 1
x1 = 1
...
l = [x0, x1, ...]
"""
fn_str = f"""\
def fn():
foo.bar(1, 2, 3)
{str(chr(10)).join(' ' * 4 + 'x' + str(i) + ' = 1' for i in range(1 << 9))}
l = [{' '.join('x' + str(i) + ',' for i in range(1 << 9))}]
"""
locals = {}
exec(fn_str, {}, locals)
fn = locals["fn"]
orig_inst_str = "\n".join(list(map(str, dis.get_instructions(fn))))
self.assertIn("EXTENDED_ARG", orig_inst_str)
load_method_str = "LOAD_ATTR" if sys.version_info >= (3, 12) else "LOAD_METHOD"
self.assertIn(load_method_str, orig_inst_str)
keys = bytecode_transformation.get_code_keys()
code_options = {k: getattr(fn.__code__, k) for k in keys}
result = bytecode_transformation.clean_and_assemble_instructions(
bytecode_transformation.cleaned_instructions(fn.__code__),
keys,
code_options,
)
new_inst_str = "\n".join(list(map(str, result[0])))
self.assertIn("EXTENDED_ARG", new_inst_str)
self.assertIn(load_method_str, new_inst_str)
l1, l2 = list(fn.__code__.co_positions()), list(result[1].co_positions())
self.assertEqual(len(l1), len(l2))
for p1, p2 in zip(l1, l2):
self.assertEqual(p1, p2)
self.assertEqual(fn.__code__.co_lnotab, result[1].co_lnotab)
@unittest.skipIf(
sys.version_info < (3, 10) or sys.version_info >= (3, 11),
"linetable test for Python 3.10",
)
def test_linetable_310_writer(self):
def fn():
a = 10
b = 20
c = a + b
f = "linetable_writer"
return f"Test if {f} generates correct co_linetable: {c}"
inst = dis.get_instructions(fn)
result = bytecode_transformation.assemble(inst, fn.__code__.co_firstlineno)
self.assertTrue(result[1] == fn.__code__.co_linetable)
@unittest.skipIf(sys.version_info >= (3, 10), "use lnotab when python < 3.10")
def test_lnotab_writer(self):
def fn():
a = 10
b = 20
c = a + b
f = "lnotab_writer"
return f"Test if {f} generates correct co_lnotab: {c}"
inst = dis.get_instructions(fn)
result = bytecode_transformation.assemble(inst, fn.__code__.co_firstlineno)
self.assertTrue(result[1] == fn.__code__.co_lnotab)
def test_if_tensor_is_none(self):
"""
Python 3.11 adds new jump instructions that check if
TOS is None. We do not support these instructions.
"""
def f(x, y):
z = 1
if x is None:
z *= 2
if y is not None:
z *= 3
return z
opt_f = torch.compile(f, backend="eager", fullgraph=True)
self.assertEqual(opt_f(None, torch.ones(2)), 6)
if sys.version_info >= (3, 11):
insts = bytecode_transformation.cleaned_instructions(f.__code__)
for inst in insts:
self.assertNotIn("_NONE", inst.opname)
@skipIfNotPy311
def test_py311_jump_offset(self):
new_inst = bytecode_transformation.create_instruction
consts = (None, 1, 2, 3, 4)
def create_test_code(jump_opname, target_idx):
targets = [
new_inst("LOAD_CONST", argval=1),
new_inst("LOAD_CONST", argval=3),
]
jump_to_target_inst = new_inst(jump_opname, target=targets[target_idx])
"""
pseudocode of generated bytecode:
def test_py311_fn():
goto target1
target0:
return 1
target1:
goto [target0/target2] (via fwd or bwd jump)
return 2
target2:
return 3
return 4
"""
# test with LOAD_GLOBAL since it has a different instruction size
insts = [
new_inst("RESUME", arg=0),
new_inst("JUMP_FORWARD", target=jump_to_target_inst),
targets[0],
new_inst("LOAD_GLOBAL", arg=0, argval="print"),
new_inst("POP_TOP"),
new_inst("RETURN_VALUE"),
jump_to_target_inst,
new_inst("LOAD_CONST", argval=2),
new_inst("LOAD_GLOBAL", arg=0, argval="print"),
new_inst("POP_TOP"),
new_inst("RETURN_VALUE"),
targets[1],
new_inst("RETURN_VALUE"),
new_inst("LOAD_CONST", argval=4),
new_inst("RETURN_VALUE"),
]
code_options = collections.OrderedDict(
[
("co_argcount", 0),
("co_posonlyargcount", 0),
("co_kwonlyargcount", 0),
("co_nlocals", 0),
("co_stacksize", 2),
("co_flags", 3),
("co_code", b""),
("co_consts", consts),
("co_names", ("print",)),
("co_varnames", ()),
("co_filename", __file__),
("co_name", "test_py311_fn"),
("co_qualname", "test_py311_fn"),
("co_firstlineno", 1),
("co_linetable", b""),
("co_exceptiontable", b""),
("co_freevars", ()),
("co_cellvars", ()),
]
)
return bytecode_transformation.clean_and_assemble_instructions(
insts,
list(code_options.keys()),
code_options,
)
# format: jump_opname, target_idx, expected forward jump, expected return value
test_args = (
("JUMP_FORWARD", 0, False, 1),
("JUMP_FORWARD", 1, True, 3),
("JUMP_BACKWARD", 0, False, 1),
("JUMP_BACKWARD", 1, True, 3),
)
for test in test_args:
insts, code = create_test_code(test[0], test[1])
# check if offset of latest jump instruction is forward/backward
for inst in reversed(insts):
if inst.opname.startswith("JUMP"):
if test[2]:
self.assertIn("FORWARD", inst.opname)
else:
self.assertIn("BACKWARD", inst.opname)
break
# run the code and check result
def dummy_fn():
pass
dummy_fn.__code__ = code
self.assertEqual(dummy_fn(), test[3])
dummy_opt = torch.compile(dummy_fn, backend="eager")
self.assertEqual(dummy_opt(), test[3])
def test_exception_table_encode_varint(self):
# these numbers have no real meaning to them
nums = [
0b111_101010_000000,
0b1100_111000_010101_101010,
]
b = bytecode_transformation.encode_exception_table_varint(
nums[0]
) + bytecode_transformation.encode_exception_table_varint(nums[1])
nums_new = []
b_iter = iter(bytes(b))
while True:
try:
nums_new.append(
bytecode_transformation.decode_exception_table_varint(b_iter)
)
except StopIteration:
break
self.assertEqual(nums, nums_new)
@skipIfNotPy311
def test_exception_table_parsing(self):
def fn():
try:
with a():
b()
c()
except Exception:
d()
finally:
e()
f()
tab = bytecode_transformation.parse_exception_table(
fn.__code__.co_exceptiontable
)
b = bytecode_transformation.assemble_exception_table(tab)
self.assertEqual(b, fn.__code__.co_exceptiontable)
@skipIfNotPy311
def test_exception_table_e2e(self):
def fn():
try:
with a():
b()
c()
except Exception:
d()
finally:
e()
f()
def nothing(*args):
pass
code = bytecode_transformation.transform_code_object(fn.__code__, nothing)
self.assertEqual(code.co_exceptiontable, fn.__code__.co_exceptiontable)
@skipIfNotPy311
def test_exception_table_e2e_2(self):
# last instructions of an exn_table entry is a large instruction
# i.e., LOAD_GLOBAL a
def fn():
try:
return a
except Exception:
pass
def nothing(*args):
pass
code = bytecode_transformation.transform_code_object(fn.__code__, nothing)
self.assertEqual(code.co_exceptiontable, fn.__code__.co_exceptiontable)
@skipIfNotPy311
def test_exception_table_entry_propagation(self):
insts = []
for _ in range(10):
insts.append(bytecode_transformation.create_instruction("NOP"))
insts[8].exn_tab_entry = bytecode_transformation.InstructionExnTabEntry(
insts[0], insts[9], insts[0], 0, True
)
insts[0].exn_tab_entry = bytecode_transformation.InstructionExnTabEntry(
insts[0], insts[0], insts[1], 0, True
)
insts[1].exn_tab_entry = bytecode_transformation.InstructionExnTabEntry(
insts[0], insts[2], insts[2], 0, True
)
insts[5].exn_tab_entry = bytecode_transformation.InstructionExnTabEntry(
insts[4], insts[6], insts[3], 0, True
)
insts[9].exn_tab_entry = bytecode_transformation.InstructionExnTabEntry(
insts[9], insts[9], insts[4], 0, True
)
insts[7].exn_tab_entry = bytecode_transformation.InstructionExnTabEntry(
insts[7], insts[9], insts[5], 0, True
)
bytecode_transformation.propagate_inst_exn_table_entries(insts)
expected = [1, 2, 2, 0, 3, 3, 3, 5, 5, 4]
for inst, exp in zip(insts, expected):
self.assertIsNotNone(inst.exn_tab_entry)
self.assertIs(inst.exn_tab_entry.target, insts[exp])
@skipIfNotPy311
def test_compute_exception_table_nested(self):
insts = []
for _ in range(20):
insts.append(bytecode_transformation.create_instruction("NOP"))
insts[10].exn_tab_entry = bytecode_transformation.InstructionExnTabEntry(
insts[1], insts[10], insts[0], 0, True
)
insts[0].exn_tab_entry = bytecode_transformation.InstructionExnTabEntry(
insts[1], insts[1], insts[1], 0, True
)
insts[1].exn_tab_entry = bytecode_transformation.InstructionExnTabEntry(
insts[1], insts[3], insts[2], 0, True
)
insts[5].exn_tab_entry = bytecode_transformation.InstructionExnTabEntry(
insts[5], insts[7], insts[3], 0, True
)
insts[9].exn_tab_entry = bytecode_transformation.InstructionExnTabEntry(
insts[10], insts[10], insts[4], 0, True
)
insts[7].exn_tab_entry = bytecode_transformation.InstructionExnTabEntry(
insts[8], insts[10], insts[5], 0, True
)
insts[14].exn_tab_entry = bytecode_transformation.InstructionExnTabEntry(
insts[13], insts[17], insts[6], 0, True
)
insts[16].exn_tab_entry = bytecode_transformation.InstructionExnTabEntry(
insts[15], insts[16], insts[7], 0, True
)
bytecode_transformation.update_offsets(insts)
tab = bytecode_transformation.compute_exception_table(insts)
expected = [
(1, 1, 1),
(2, 3, 2),
(4, 4, 0),
(5, 7, 3),
(8, 9, 5),
(10, 10, 4),
(13, 14, 6),
(15, 16, 7),
(17, 17, 6),
]
self.assertEqual(len(tab), len(expected))
for entry, exp in zip(tab, expected):
self.assertEqual(entry.start, exp[0] * 2)
self.assertEqual(entry.end, exp[1] * 2)
self.assertEqual(entry.target, exp[2] * 2)
@skipIfNotPy311
def test_remove_dead_code_with_exn_table_entries(self):
create_instruction = bytecode_transformation.create_instruction
target1 = create_instruction("NOP")
target2 = create_instruction("NOP")
target3 = create_instruction("NOP")
exn_start = create_instruction("NOP")
exn_end = create_instruction("NOP")
insts = [
create_instruction("JUMP_FORWARD", target=target1),
exn_start, # dead
target1,
create_instruction("JUMP_FORWARD", target=target3),
exn_end, # dead
target2,
target3,
]
exn_start.exn_tab_entry = bytecode_transformation.InstructionExnTabEntry(
exn_start, exn_end, target2, 0, True
)
bytecode_transformation.propagate_inst_exn_table_entries(insts)
insts = bytecode_analysis.remove_dead_code(insts)
self.assertEqual(len(insts), 5)
self.assertNotIn(exn_start, insts)
self.assertNotIn(exn_end, insts)
self.assertIn(target2, insts)
self.assertIn(target3, insts)
bytecode_transformation.update_offsets(insts)
tab = bytecode_transformation.compute_exception_table(insts)
self.assertEqual(len(tab), 1)
self.assertEqual(tab[0].start, 2)
self.assertEqual(tab[0].end, 4)
self.assertEqual(tab[0].target, 6)
def test_bytecode_from_template(self):
def fn(d1):
for k, v in d1.items():
d2[k] = v
varname_map = {"d1": "var1", "d2": "var2", "k": "var3", "v": "var4"}
insts = bytecode_transformation.bytecode_from_template(fn, varname_map)
for inst in insts:
self.assertIsNone(inst.starts_line)
if inst.opname.startswith("LOAD"):
self.assertNotIn(inst.argval, varname_map)
if inst.opname not in ("LOAD_GLOBAL", "LOAD_ATTR"):
self.assertIsNone(inst.arg)
self.assertFalse(inst.opname.startswith("RETURN"))
@skipIfNotPy311
def test_bytecode_from_template_noprefix(self):
# Test that 3.11+ prefix instructions are removed
def gen_fn():
cl = None
def fn():
return cl
return fn
fn = gen_fn()
dis_insts = list(dis.get_instructions(fn))
names = {inst.opname for inst in dis_insts}
self.assertIn("RESUME", names)
self.assertIn("COPY_FREE_VARS", names)
insts = bytecode_transformation.bytecode_from_template(fn)
names = {inst.opname for inst in insts}
self.assertNotIn("RESUME", names)
self.assertNotIn("COPY_FREE_VARS", names)
def test_bytecode_from_template_noreturn1(self):
# Test that functions with multiple returns will have their
# returns replaced with jumps to the end
def fn():
if x:
return y
z = 3
return z
dis_insts = list(dis.get_instructions(fn))
dis_returns = list(filter(lambda x: x.opname.startswith("RETURN"), dis_insts))
self.assertGreater(len(dis_returns), 1)
self.assertTrue(dis_insts[-1].opname.startswith("RETURN"))
insts = bytecode_transformation.bytecode_from_template(fn, noprefix=False)
self.assertEqual(insts[-1].opname, "NOP")
self.assertEqual(len(dis_insts), len(insts))
for i0, i1 in zip(dis_insts, insts):
if i0.opname.startswith("RETURN"):
if i1 is insts[-1]:
continue
self.assertIn("JUMP", i1.opname)
self.assertIs(i1.target, insts[-1])
# Should work with 3.10, but testing with 3.11+ is sufficient.
# In 3.8, `fn` ends with a RETURN_VALUE.
@skipIfNotPy311
def test_bytecode_from_template_noreturn2(self):
# Test function that doesn't end with RETURN_VALUE
def fn():
if x:
return x
if x:
return x
raise RuntimeError
dis_insts = list(dis.get_instructions(fn))
self.assertFalse(dis_insts[-1].opname.startswith("RETURN"))
insts = bytecode_transformation.bytecode_from_template(fn, noprefix=False)
self.assertEqual(insts[-1].opname, "NOP")
self.assertEqual(insts[-2].opname, dis_insts[-1].opname)
self.assertEqual(len(dis_insts) + 1, len(insts))
for i0, i1 in zip(dis_insts, insts):
if i0.opname.startswith("RETURN"):
self.assertIn("JUMP", i1.opname)
self.assertIs(i1.target, insts[-1])
@skipIfNotPy312
def test_bytecode_from_template_noreturn_const(self):
# Test 3.12+ RETURN_CONST
def fn():
if x:
return 1
return 0
dis_insts = list(dis.get_instructions(fn))
dis_return_consts = list(
filter(lambda x: x.opname == "RETURN_CONST", dis_insts)
)
self.assertGreater(len(dis_return_consts), 1)
self.assertTrue(dis_insts[-1].opname == "RETURN_CONST")
insts = bytecode_transformation.bytecode_from_template(fn, noprefix=False)
self.assertEqual(insts[-1].opname, "NOP")
insts_i = 0
for i, inst in enumerate(dis_insts):
if inst.opname == "RETURN_CONST":
self.assertEqual(insts[insts_i].opname, "LOAD_CONST")
insts_i += 1
if insts_i != len(insts) - 1:
self.assertIn("JUMP", insts[insts_i].opname)
self.assertIs(insts[insts_i].target, insts[-1])
insts_i += 1
def test_bytecode_analysis_jump_backward_no_interrupt(self):
# bytecode_analysis fails if JUMP_BACKWARD_NO_INTERRUPT is not terminal in 3.13+
@torch.compile(backend="eager")
def fn(x):
# graph break causes bytecode_analysis to analyze the rest of this function
torch._dynamo.graph_break()
with torch.no_grad():
try:
x = x + 1
except NotImplementedError:
x = x + 1
except Exception as e:
x = x + 1
return x
self.assertEqual(fn(torch.ones(3)), torch.ones(3) + 1)
class BytecodeHookTests(torch._dynamo.test_case.TestCase):
def test_bytecode_hook(self):
def fn(a, b):
return a - b * 10
def hook(code, out_code):
print(code)
print(out_code)
return code
torch._dynamo.reset()
handle = torch._dynamo.convert_frame.register_bytecode_hook(hook)
try:
opt_fn = torch.compile(fn)
for i in range(2, 12):
opt_fn(torch.randn(i), torch.randn(i))
finally:
handle.remove()
if __name__ == "__main__":
from torch._dynamo.test_case import run_tests
run_tests()
|