From 542c14e535bfbcde2167cf4f01cd6b87f02d2315 Mon Sep 17 00:00:00 2001 From: Tai An Date: Fri, 22 May 2026 06:07:00 -0700 Subject: [PATCH] fix(modeling_utils): rename shadowed `tuple` loop var in get_parameter_dtype (#13789) Inside `get_parameter_dtype`, the inner `find_tensor_attributes` fallback (used for nn.DataParallel compatibility) is annotated with `list[tuple[str, Tensor]]`. The enclosing function also did `for tuple in gen:`, which makes `tuple` a local of `get_parameter_dtype` for the entire function scope. When the dtype path falls through to the DataParallel fallback (e.g. a replicated module on non-master devices has empty named_parameters/buffers), the inner function annotation evaluates `tuple[str, Tensor]` and raises `UnboundLocalError: cannot access local variable 'tuple'` because the loop has not yet executed. Rename the loop variable to `named_tensor` so the builtin is no longer shadowed. `get_parameter_device` already uses a distinct name (`first_tuple`) and is unaffected. --- src/diffusers/models/modeling_utils.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/diffusers/models/modeling_utils.py b/src/diffusers/models/modeling_utils.py index 0423b7287193..84baf1af1023 100644 --- a/src/diffusers/models/modeling_utils.py +++ b/src/diffusers/models/modeling_utils.py @@ -199,10 +199,10 @@ def find_tensor_attributes(module: nn.Module) -> list[tuple[str, Tensor]]: gen = parameter._named_members(get_members_fn=find_tensor_attributes) last_tuple = None - for tuple in gen: - last_tuple = tuple - if tuple[1].is_floating_point(): - return tuple[1].dtype + for named_tensor in gen: + last_tuple = named_tensor + if named_tensor[1].is_floating_point(): + return named_tensor[1].dtype if last_tuple is not None: # fallback to the last dtype