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

Fix model copy after dispatch_model #1971

Merged
merged 3 commits into from
Sep 19, 2023

Conversation

austinapatel
Copy link
Contributor

What does this PR do?

Fixes #1835 where a model dispatched with dispatch_model was unable to be properly copied with copy.deepcopy. Specifically, the copied model would incorrectly still reference the align device hook forward function of the original model, rather than its own forward function. This PR includes a fix to this issue as well as a test to validate the fix.

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link
    to it if that's the case.
  • Did you make sure to update the documentation with your changes? Here are the
    documentation guidelines, and
    here are tips on formatting docstrings.
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@SunMarc @sgugger

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint.

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.

Hi @austinapatel, thanks a lot for fixing this ! Just a few nits :) Can you have a second look @BenjaminBossan ?

src/accelerate/hooks.py Outdated Show resolved Hide resolved
tests/test_big_modeling.py Show resolved Hide resolved
src/accelerate/hooks.py Show resolved Hide resolved
@austinapatel austinapatel force-pushed the copy_after_dispatch_model branch from 50e0896 to b7a57e3 Compare September 15, 2023 03:57
@austinapatel
Copy link
Contributor Author

Thanks for the review @SunMarc! I have pushed changes per your feedback!

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.

In general, this LGTM. I only have one question, apart from that this can be merged.

return module._hf_hook.post_forward(module, output)

module.forward = new_forward
module.forward = functools.update_wrapper(functools.partial(new_forward, module), old_forward)
Copy link
Member

Choose a reason for hiding this comment

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

I wonder about this line versus:

module.forward = MethodType(functools.update_wrapper(new_forward, old_forward), module)

@austinapatel Do you think that would be preferable? I thought it might affect pickling, but unfortunately, both variants don't allow it, but the error messages are different.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the suggestion and review! I just tried your code snippet, and I run into the same issue as this comment: #1971 (comment) since functools.update_wrapper needs to be called last (test_add_and_remove_hooks in test_hooks.py fails). If I try module.forward = functools.update_wrapper(MethodType(new_forward, module), old_forward) I get the following error: AttributeError: 'method' object has no attribute '__module__' when calling update_wrapper. My understanding is that update_wrapper is now being called on a method rather than on a function, which doesn't play as nicely with update_wrapper.

Copy link
Member

Choose a reason for hiding this comment

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

Thanks for explaining and testing, this makes sense. I'm a bit wary about binding the module via partial to basically imitate a bound method, but it seems that all other solutions don't work, so I guess it's the best we can do.

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 so much @austinapatel for all the work you put into this.

@SunMarc SunMarc merged commit 03deec2 into huggingface:main Sep 19, 2023
24 checks passed
@austinapatel austinapatel deleted the copy_after_dispatch_model branch September 19, 2023 17:22
@austinapatel
Copy link
Contributor Author

Thanks @SunMarc and @BenjaminBossan for the reviews and feedback!

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.

Can't copy model after dispatch_model called
4 participants