-
Notifications
You must be signed in to change notification settings - Fork 234
Fix CSM depth decoder generate: preserve forward signature on wrapper #590
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
|
|
||
| import torch | ||
| import torch.nn as nn | ||
| import inspect | ||
| from typing import Optional, Tuple | ||
| from .common import TEMPORARY_PATCHES | ||
| from .utils import ( | ||
|
|
@@ -83,19 +84,25 @@ def forward( | |
| attn_output = self.o_proj(attn_output) | ||
| return attn_output, None | ||
|
|
||
| # Wrap so check_args_kwargs accepts removed params (e.g. output_attentions in v5) | ||
| # Wrap so check_args_kwargs accepts removed params (e.g. output_attentions in v5). | ||
| # Preserve the original signature on the wrapper so inspect.signature | ||
| # (used by transformers._validate_model_kwargs among others) still sees | ||
| # the real named parameters. | ||
| target_cls = transformers.models.pixtral.modeling_pixtral.PixtralAttention | ||
| _original_forward_signature = inspect.signature(target_cls.forward) | ||
| _full_forward = forward | ||
| def forward(self, *args, **kwargs): | ||
| return _full_forward(self, *args, **kwargs) | ||
| forward.__signature__ = _original_forward_signature | ||
|
|
||
| patch_function( | ||
| transformers.models.pixtral.modeling_pixtral.PixtralAttention, | ||
| target_cls, | ||
| "__init__", | ||
| __init__, | ||
| ) | ||
|
|
||
| patch_function( | ||
| transformers.models.pixtral.modeling_pixtral.PixtralAttention, | ||
| target_cls, | ||
| "forward", | ||
| forward, | ||
| ) | ||
|
Comment on lines
106
to
108
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For consistency with the patch_function(
target_cls,
"forward",
forward,
match_level="relaxed",
)References
|
||
|
|
||
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.
For consistency with the other patches in this PR (such as
patch_CsmDepthDecoderForCausalLM_forwardandpatch_MinistralAttention), it would be beneficial to add a brief comment explaining why the original signature is being preserved on the wrapper. This helps future maintainers understand that this is necessary fortransformers' kwarg validation during generation.