From 4e420415e84dcd0a59f935568dd73bd405033be2 Mon Sep 17 00:00:00 2001 From: Guilherme Leobas Date: Mon, 13 Oct 2025 09:48:27 -0300 Subject: [PATCH] Avoids calling builtin `iter` if object is a generator (#162521) The `iter(gen)` call will return the given `gen` object. So, we just avoid this call and shaves off a few ms of tracing time Pull Request resolved: https://github.com/pytorch/pytorch/pull/162521 Approved by: https://github.com/mlazos --- torch/_dynamo/variables/builtin.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/torch/_dynamo/variables/builtin.py b/torch/_dynamo/variables/builtin.py index 02536d7f72ce..2ae610bb9bcb 100644 --- a/torch/_dynamo/variables/builtin.py +++ b/torch/_dynamo/variables/builtin.py @@ -1831,6 +1831,8 @@ class BuiltinVariable(VariableTracker): ret = obj elif isinstance(obj, variables.RangeVariable): ret = obj.call_method(tx, "__iter__", [], {}) + elif isinstance(obj, variables.LocalGeneratorObjectVariable): + ret = obj # type: ignore[assignment] else: # Handle the case where we are iterating over a tuple, list or iterator ret = self._call_iter_tuple_list(tx, obj, *args, **kwargs)