-
Notifications
You must be signed in to change notification settings - Fork 989
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
Fix model copy after dispatch_model
#1971
Conversation
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. |
There was a problem hiding this 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 ?
50e0896
to
b7a57e3
Compare
Thanks for the review @SunMarc! I have pushed changes per your feedback! |
There was a problem hiding this 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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
.
There was a problem hiding this comment.
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.
There was a problem hiding this 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.
Thanks @SunMarc and @BenjaminBossan for the reviews and feedback! |
What does this PR do?
Fixes #1835 where a model dispatched with
dispatch_model
was unable to be properly copied withcopy.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
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.
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