[dynamo] Support method calls on complex ConstantVariables (#161122)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/161122
Approved by: https://github.com/mlazos, https://github.com/guilhermeleobas
This commit is contained in:
Rob Timpe
2025-08-22 17:46:34 +00:00
committed by PyTorch MergeBot
parent 9d882fd9ff
commit 4c36c8a994
8 changed files with 42 additions and 23 deletions

View File

@ -308,6 +308,7 @@ class BuiltinVariable(VariableTracker):
bool,
callable,
chr,
complex,
divmod,
float,
getattr,
@ -1478,21 +1479,6 @@ class BuiltinVariable(VariableTracker):
call_int = _call_int_float
call_float = _call_int_float
def call_complex(self, tx: "InstructionTranslator", *args, **kwargs):
if self.constant_args(*args, **kwargs):
try:
c = complex(
*(arg.as_python_constant() for arg in args),
**{k: kwargs[k].as_python_constant() for k in kwargs},
)
except (TypeError, ValueError) as exc:
raise_observed_exception(
type(exc),
tx,
args=list(map(ConstantVariable.create, exc.args)),
)
return ConstantVariable(c)
def call_bool(self, tx: "InstructionTranslator", arg):
# Emulate `PyBool_Type.tp_vectorcall` which boils down to `PyObject_IsTrue`.
# https://github.com/python/cpython/blob/3.12/Objects/object.c#L1674-L1697