Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that tied parameter is children of module #3327

Merged
merged 1 commit into from
Jan 9, 2025

Conversation

pablomlago
Copy link
Contributor

Fixes #3308

This PR fixes the check in get_module_size_with_ties used to determine the module to which a tied parameter belongs. This is done by checking that the name of the tied parameter starts with module name plus ".".

The previous check (n in tied_param) was not returning the expected results in models with a ModuleList, where the contained modules have a tied parameter. I added a unit test covering this situation.

cc @BenjaminBossan

@muellerzr muellerzr requested a review from SunMarc January 7, 2025 16:46
Copy link
Member

@SunMarc SunMarc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense ! Thanks for the nice test !

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Member

@BenjaminBossan BenjaminBossan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for providing the PR. In general, this LGTM, but I have 2 comments, please check.

@@ -882,6 +883,48 @@ def test_get_balanced_memory(self):
max_memory = get_balanced_memory(model, max_memory={0: 0, "cpu": 100})
assert {0: 0, "cpu": 100} == max_memory

def test_get_module_size_with_ties(self):
# Create a model with a ModuleList containing more than 10 elements
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a reference to issue #3308 here.

# Retrieve the tied parameters. Each group of tied parameters is returned
# by find_tied_parameters in alphabetical order
tied_parameters = find_tied_parameters(model)
assert tied_parameters == [sorted([f"{i}.weight" for i in range(num_layers)])]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For my understanding, are the checks starting from here really necessary to cover the bug fix? Or could the test be finished here?

Copy link
Contributor Author

@pablomlago pablomlago Jan 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The call to find_tied_parameters could be omitted, as it is not really the function under test, and just set tied_parameters=[sorted([f"{i}.weight" for i in range(num_layers)])], thus removing the first assert. I included that call to emphasise the flow in infer_auto_device_map. I agree that it would be better to remove it, so the test only covers the bug fix (which is in get_module_size_with_ties). I guess that the rest of the test would be fine as it is, what do you think?

@pablomlago pablomlago force-pushed the fix-get-modules-with-ties branch from 348c158 to 5ff6fb7 Compare January 9, 2025 09:45
Ensure that tied parameters are assigned to their parent module in
get_module_size_with_ties

Fixes: huggingface#3308
@pablomlago pablomlago force-pushed the fix-get-modules-with-ties branch from 5ff6fb7 to 21df343 Compare January 9, 2025 09:49
Copy link
Member

@BenjaminBossan BenjaminBossan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the further explanation. Personally, I would have been fine with just having the assert tied_modules == expected_tied_modules check, as that is sufficient to cover the bug, but it's not a big deal either way.

@muellerzr muellerzr merged commit 58f1436 into huggingface:main Jan 9, 2025
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Subset of modules with tied parameters being retrieved in get_module_size_with_ties
5 participants