Skip to content

Commit f636736

Browse files
anijain2305pytorchmergebot
authored andcommitted
[dynamo][guards] Skip guards on constant func.__defaults__ elements (pytorch#159209)
Func.__defaults__ is a tuple. Therefore, we can skip guards on immutable elements. Mutable elements are still guarded. Pull Request resolved: pytorch#159209 Approved by: https://github.com/jansel
1 parent 37638c3 commit f636736

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

torch/_dynamo/config.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,9 @@
354354
# the dictionary tag is same across invocation calls.
355355
skip_tensor_guards_with_matching_dict_tags = True
356356

357+
# Skips guards on func.__defaults__ if the element to be guarded is a constant
358+
skip_guards_on_constant_func_defaults = True
359+
357360
# If True, raises exception if TorchDynamo is called with a context manager
358361
raise_on_ctx_manager_usage = True
359362

torch/_dynamo/variables/functions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,10 @@ def bind_args_cached(func, tx, fn_source, args, kwargs):
161161
elif name in spec.pos_default_map:
162162
idx = spec.pos_default_map[name]
163163
default_source = None
164-
if fn_source:
164+
if fn_source and not (
165+
ConstantVariable.is_literal(spec.defaults[idx])
166+
and config.skip_guards_on_constant_func_defaults
167+
):
165168
default_source = DefaultsSource(fn_source, idx)
166169
ba[name] = wrap_bound_arg(tx, spec.defaults[idx], default_source)
167170
else:

0 commit comments

Comments
 (0)