[math] Trace float.fromhex (#156976)

Pull Request resolved: https://github.com/pytorch/pytorch/pull/156976
Approved by: https://github.com/zou3519
ghstack dependencies: #156975, #156977
This commit is contained in:
Guilherme Leobas
2025-07-22 14:32:05 -03:00
committed by PyTorch MergeBot
parent f5314f89c8
commit 576253c476
5 changed files with 21 additions and 0 deletions

View File

@ -1271,6 +1271,19 @@ class BuiltinVariable(VariableTracker):
args[1:],
)
if self.fn is float and len(args) == 1 and name in ("fromhex", "hex"):
if isinstance(args[0], ConstantVariable):
try:
fn = getattr(float, name)
res = fn(args[0].as_python_constant())
return variables.ConstantVariable.create(res)
except (OverflowError, ValueError) as e:
raise_observed_exception(
type(e),
tx,
args=list(map(ConstantVariable.create, e.args)),
)
if self.fn is object and name == "__init__":
# object.__init__ is a no-op
return variables.ConstantVariable(None)