-
Notifications
You must be signed in to change notification settings - Fork 25.6k
[pytorch] Lazy Module Fix for kwargs argument support on forward #61606
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,41 @@ def __repr__(self): | |
__str__ = __repr__ | ||
|
||
|
||
class _PreForwardHookWrapper: | ||
def __init__(self, hook: Callable[..., None], with_kwargs=False) -> None: | ||
self.hook = hook | ||
self.with_kwargs = with_kwargs | ||
|
||
def _call_impl(self, module, input, kwargs): | ||
if self.with_kwargs: | ||
return self.hook(module, input, kwargs) | ||
else: | ||
return self.hook(module, input) | ||
|
||
|
||
__call__ : Callable[..., Any] = _call_impl | ||
|
||
|
||
class _ForwardHookWrapper: | ||
def __init__(self, hook: Callable[..., None], with_kwargs=False) -> None: | ||
self.hook = hook | ||
self.with_kwargs = with_kwargs | ||
|
||
def _call_impl(self, module, input, kwargs, result): | ||
print("self module input kwargs result") | ||
print(self) | ||
print(module) | ||
print(input) | ||
print(kwargs) | ||
print(result) | ||
if self.with_kwargs: | ||
return self.hook(module, input, kwargs, result) | ||
else: | ||
return self.hook(module, input, result) | ||
|
||
__call__ : Callable[..., Any] = _call_impl | ||
|
||
|
||
|
||
def _addindent(s_, numSpaces): | ||
s = s_.split('\n') | ||
# don't do anything for single-line stuff | ||
|
@@ -49,7 +84,7 @@ def _addindent(s_, numSpaces): | |
_EXTRA_STATE_KEY_SUFFIX = '_extra_state' | ||
|
||
|
||
def register_module_forward_pre_hook(hook: Callable[..., None]) -> RemovableHandle: | ||
def register_module_forward_pre_hook(hook: Callable[..., None], with_kwargs=False) -> RemovableHandle: | ||
r"""Registers a forward pre-hook common to all modules. | ||
|
||
.. warning :: | ||
|
@@ -77,11 +112,11 @@ def register_module_forward_pre_hook(hook: Callable[..., None]) -> RemovableHand | |
``handle.remove()`` | ||
""" | ||
handle = hooks.RemovableHandle(_global_forward_pre_hooks) | ||
_global_forward_pre_hooks[handle.id] = hook | ||
_global_forward_pre_hooks[handle.id] = _PreForwardHookWrapper(hook, with_kwargs) | ||
return handle | ||
|
||
|
||
def register_module_forward_hook(hook: Callable[..., None]) -> RemovableHandle: | ||
def register_module_forward_hook(hook: Callable[..., None], with_kwargs=False) -> RemovableHandle: | ||
r"""Registers a global forward hook for all the modules | ||
|
||
.. warning :: | ||
|
@@ -109,7 +144,7 @@ def register_module_forward_hook(hook: Callable[..., None]) -> RemovableHandle: | |
``register_forward_hook``. | ||
""" | ||
handle = hooks.RemovableHandle(_global_forward_hooks) | ||
_global_forward_hooks[handle.id] = hook | ||
_global_forward_hooks[handle.id] = _ForwardHookWrapper(hook, with_kwargs) | ||
return handle | ||
|
||
def register_module_backward_hook( | ||
|
@@ -1027,7 +1062,7 @@ def _maybe_warn_non_full_backward_hook(self, inputs, result, grad_fn): | |
"some grad_input. Please use register_full_backward_hook to get the documented " | ||
"behavior.") | ||
|
||
def register_forward_pre_hook(self, hook: Callable[..., None]) -> RemovableHandle: | ||
def register_forward_pre_hook(self, hook: Callable[..., None], with_kwargs=False) -> RemovableHandle: | ||
r"""Registers a forward pre-hook on the module. | ||
|
||
The hook will be called every time before :func:`forward` is invoked. | ||
|
@@ -1047,10 +1082,10 @@ def register_forward_pre_hook(self, hook: Callable[..., None]) -> RemovableHandl | |
``handle.remove()`` | ||
""" | ||
handle = hooks.RemovableHandle(self._forward_pre_hooks) | ||
self._forward_pre_hooks[handle.id] = hook | ||
self._forward_pre_hooks[handle.id] = _PreForwardHookWrapper(hook, with_kwargs) | ||
return handle | ||
|
||
def register_forward_hook(self, hook: Callable[..., None]) -> RemovableHandle: | ||
def register_forward_hook(self, hook: Callable[..., None], with_kwargs=False) -> RemovableHandle: | ||
r"""Registers a forward hook on the module. | ||
|
||
The hook will be called every time after :func:`forward` has computed an output. | ||
|
@@ -1070,7 +1105,7 @@ def register_forward_hook(self, hook: Callable[..., None]) -> RemovableHandle: | |
``handle.remove()`` | ||
""" | ||
handle = hooks.RemovableHandle(self._forward_hooks) | ||
self._forward_hooks[handle.id] = hook | ||
self._forward_hooks[handle.id] = _ForwardHookWrapper(hook, with_kwargs) | ||
return handle | ||
|
||
def _slow_forward(self, *input, **kwargs): | ||
|
@@ -1105,8 +1140,8 @@ def _call_impl(self, *input, **kwargs): | |
if self._backward_hooks or _global_backward_hooks: | ||
full_backward_hooks, non_full_backward_hooks = self._get_backward_hooks() | ||
if _global_forward_pre_hooks or self._forward_pre_hooks: | ||
for hook in (*_global_forward_pre_hooks.values(), *self._forward_pre_hooks.values()): | ||
result = hook(self, input) | ||
for hook_wrapper in (*_global_forward_pre_hooks.values(), *self._forward_pre_hooks.values()): | ||
result = hook_wrapper(self, input, kwargs) | ||
if result is not None: | ||
if not isinstance(result, tuple): | ||
result = (result,) | ||
|
@@ -1119,8 +1154,8 @@ def _call_impl(self, *input, **kwargs): | |
|
||
result = forward_call(*input, **kwargs) | ||
if _global_forward_hooks or self._forward_hooks: | ||
for hook in (*_global_forward_hooks.values(), *self._forward_hooks.values()): | ||
hook_result = hook(self, input, result) | ||
for hook_wrapper in (*_global_forward_hooks.values(), *self._forward_hooks.values()): | ||
hook_result = hook_wrapper(self, input, kwargs, result) | ||
if hook_result is not None: | ||
result = hook_result | ||
|
||
|
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.
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.
This would be BC-breaking no?
Even for simple things like the LazyLinear, this will make it fail if any argument is passed as a kwargs.