fix(modeling_utils): rename shadowed tuple loop var in get_parameter_dtype (#13789)#13791
Open
Anai-Guo wants to merge 1 commit into
Open
fix(modeling_utils): rename shadowed tuple loop var in get_parameter_dtype (#13789)#13791Anai-Guo wants to merge 1 commit into
tuple loop var in get_parameter_dtype (#13789)#13791Anai-Guo wants to merge 1 commit into
Conversation
…r_dtype (huggingface#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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Fixes #13789.
Inside
get_parameter_dtype(src/diffusers/models/modeling_utils.py), theinner
find_tensor_attributesfallback used fornn.DataParallelcompatibility is annotated with
list[tuple[str, Tensor]]. The enclosingfunction also did:
Assigning to
tupleanywhere inget_parameter_dtypemakes it a function-local for the entire scope. When execution reaches the DataParallel fallback
before the
forloop has ever boundtuple— which is exactly what happensunder
nn.DataParallelwhere the replicated module on the non-master devicehas empty
named_parameters()/named_buffers()— the inner functionannotation evaluates
tuple[str, Tensor]and raises:This breaks
UNet2DModel(...).cuda(); torch.nn.DataParallel(model, ...)forward, because
unet_2d.UNet2DModel.forwarddoest_emb.to(dtype=self.dtype).Fix
Rename the loop variable to
named_tensorso the builtintupleis nolonger shadowed.
get_parameter_devicealready uses a distinct name(
first_tuple) and is not affected.Minimal diff in
get_parameter_dtype: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].dtypeRepro
The repro from #13789 (two-GPU
nn.DataParallel(UNet2DModel(...))) raisesUnboundLocalErroronmainand runs to completion with this patch.AI-assisted, human reviewed.