[dynamo, 3.14] fix BUILD_TUPLE with 0 args (#163818)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/163818
Approved by: https://github.com/anijain2305
ghstack dependencies: #161838, #161555, #161839, #163009, #163109, #163110, #163191, #163292, #163796
This commit is contained in:
William Wen
2025-09-26 14:58:20 -07:00
committed by PyTorch MergeBot
parent d4b785a6a7
commit 4ead8ebf70
5 changed files with 26 additions and 21 deletions

View File

@ -556,6 +556,12 @@ def create_binary_subscr() -> Instruction:
return create_instruction("BINARY_OP", arg=26)
def create_build_tuple(n: int) -> Instruction:
if sys.version_info >= (3, 14) and n == 0:
return create_load_const(())
return create_instruction("BUILD_TUPLE", arg=n)
def linetable_writer(
first_lineno: int,
) -> tuple[list[int], Callable[[int, int], None], Callable[[int], None]]:

View File

@ -27,6 +27,7 @@ from .bytecode_transformation import (
add_push_null,
add_push_null_call_function_ex,
create_binary_subscr,
create_build_tuple,
create_call_function,
create_call_function_ex,
create_call_method,
@ -515,7 +516,7 @@ class PyCodegen:
except AttributeError:
# desired rotate bytecode doesn't exist, generate equivalent bytecode
return [
create_instruction("BUILD_TUPLE", arg=n),
create_build_tuple(n),
self.create_load_const_unchecked(rot_n_helper(n)),
*create_rot_n(2),
*create_call_function_ex(False),
@ -563,7 +564,7 @@ class PyCodegen:
assert var in tx.cell_and_freevars()
assert tx.post_prune_cell_and_freevars
self(tx.post_prune_cell_and_freevars[var])
output.append(create_instruction("BUILD_TUPLE", arg=len(freevars)))
output.append(create_build_tuple(len(freevars)))
output.append(self.create_load_const(code))
if sys.version_info < (3, 11):
output.append(self.create_load_const(fn_name))

View File

@ -27,8 +27,8 @@ from torch._guards import ChainedSource, Guard, GuardSource, Source
from . import utils
from .bytecode_transformation import (
create_binary_subscr,
create_build_tuple,
create_call_function,
create_instruction,
)
@ -972,7 +972,7 @@ class TorchSource(Source):
codegen.extend_output(
[
codegen.create_load_const(0), # level
create_instruction("BUILD_TUPLE", arg=0), # fromlist
create_build_tuple(0), # fromlist
codegen.create_import_name("torch"),
]
)

View File

@ -20,6 +20,7 @@ from typing import TYPE_CHECKING, Union
from .. import graph_break_hints, polyfills, variables
from ..bytecode_transformation import (
create_build_tuple,
create_call_function,
create_call_function_ex,
create_instruction,
@ -433,9 +434,7 @@ class ZipVariable(IteratorVariable):
if isinstance(it, list):
remaining_items = it[self.index :]
codegen.foreach(remaining_items)
codegen.append_output(
create_instruction("BUILD_TUPLE", arg=len(remaining_items))
)
codegen.append_output(create_build_tuple(len(remaining_items)))
else:
codegen(it)
@ -444,9 +443,7 @@ class ZipVariable(IteratorVariable):
lambda: codegen.load_import_from("builtins", "zip"), call_function_ex=True
)
self.reconstruct_items(codegen)
codegen.append_output(
create_instruction("BUILD_TUPLE", arg=len(self.iterables))
)
codegen.append_output(create_build_tuple(len(self.iterables)))
codegen.extend_output(
[
codegen.create_load_const("strict"),
@ -489,7 +486,7 @@ class MapVariable(ZipVariable):
self.reconstruct_items(codegen)
codegen.extend_output(
[
create_instruction("BUILD_TUPLE", arg=len(self.iterables) + 1),
create_build_tuple(len(self.iterables) + 1),
*create_call_function_ex(False),
]
)
@ -562,9 +559,7 @@ class FilterVariable(IteratorVariable):
if isinstance(self.iterable, list):
remaining_items = self.iterable[self.index :]
codegen.foreach(remaining_items)
codegen.append_output(
create_instruction("BUILD_TUPLE", arg=len(remaining_items))
)
codegen.append_output(create_build_tuple(len(remaining_items)))
else:
codegen(self.iterable)

View File

@ -27,6 +27,7 @@ import torch.fx
from .. import graph_break_hints, polyfills, variables
from ..bytecode_transformation import (
create_build_tuple,
create_call_function,
create_instruction,
create_rot_n,
@ -622,9 +623,11 @@ class CommonListMethodsVariable(BaseListVariable):
else:
items = slice(
*[
s.evaluate_expr()
if isinstance(s, SymNodeVariable)
else s.as_python_constant()
(
s.evaluate_expr()
if isinstance(s, SymNodeVariable)
else s.as_python_constant()
)
for s in key.items
]
)
@ -966,7 +969,7 @@ class TupleVariable(BaseListVariable):
def reconstruct(self, codegen: "PyCodegen") -> None:
codegen.foreach(self.items)
codegen.append_output(create_instruction("BUILD_TUPLE", arg=len(self.items)))
codegen.append_output(create_build_tuple(len(self.items)))
def call_method(
self,
@ -1069,7 +1072,7 @@ class SizeVariable(TupleVariable):
codegen.add_push_null(lambda: codegen.load_import_from("torch", "Size"))
codegen.foreach(self.items)
build_torch_size = [
create_instruction("BUILD_TUPLE", arg=len(self.items)),
create_build_tuple(len(self.items)),
] + create_call_function(1, False)
codegen.extend_output(build_torch_size)
@ -1219,7 +1222,7 @@ class NamedTupleVariable(TupleVariable):
codegen.foreach(self.items)
codegen.extend_output(
[
create_instruction("BUILD_TUPLE", arg=len(self.items)),
create_build_tuple(len(self.items)),
]
+ create_call_function(1, False)
)
@ -1499,7 +1502,7 @@ class ListIteratorVariable(IteratorVariable):
codegen.foreach(remaining_items)
codegen.extend_output(
[
create_instruction("BUILD_TUPLE", arg=len(remaining_items)),
create_build_tuple(len(remaining_items)),
create_instruction("GET_ITER"),
]
)