Fix Dynamo resume after f-string logging graph breaks (#186500)
TorchDynamo eagerly applies FORMAT_VALUE conversion flags by tracing str, repr, or ascii. Debug f-strings such as f"{a=}" therefore raise Unsupported while trying to evaluate repr(tensor), which requires runtime values. Because the conversion bytecodes were not handled by the graph-break speculation machinery, Dynamo abandoned the frame instead of compiling the prefix and resumed remainder.
Wrap FORMAT_VALUE and Python 3.13+'s split CONVERT_VALUE opcode with break_graph_if_unsupported. Dynamo now compiles the prefix, executes the conversion eagerly at the correct runtime bytecode position, and resumes tracing afterward. This preserves formatting order across later tensor mutations while allowing the code after a logging graph break to compile.
Deferring tensor conversion through StringFormatVariable was rejected because a later in-place mutation could silently change the formatted string. Restricting that deferral to apparent logging calls is also unsafe when later f-string components or call arguments mutate the tensor.
Fixes #120375
Generated by my agent
Test Plan:
- python test/dynamo/test_reorder_logs.py
- python test/dynamo/test_str_protocol.py
- python test/dynamo/test_functions.py -k fstrings
- python test/dynamo/test_misc.py -k fstring
- python test/dynamo/test_nested_graph_breaks.py -k fstring_graph_break_in_custom_str
- lintrunner -a
Pull Request resolved: https://github.com/pytorch/pytorch/pull/186500
Approved by: https://github.com/bobrenjc93 J
Jason Ansel committed
03943929e0350ce6333b1658faf239c879a1c130
Parent: 2557cb8
Committed by PyTorch MergeBot <pytorchmergebot@users.noreply.github.com>
on 8/23/2026, 9:53:30 PM