-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Fix num_logits_to_keep regression on transformers >= 4.52 #5538
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
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 |
|---|---|---|
|
|
@@ -2109,11 +2109,24 @@ def unsloth_fast_generate( | |
|
|
||
| # For newer HF | ||
| kwargs["cache_implementation"] = "dynamic" | ||
| # For num_logits_to_keep | ||
| num_logits_to_keep = kwargs.get("num_logits_to_keep", None) | ||
| # transformers 4.50 renamed num_logits_to_keep -> logits_to_keep | ||
| # (with @deprecate_kwarg through 4.51.x, removed in 4.52+). Pick the | ||
| # spelling the actual runtime forward accepts so generation | ||
| # _validate_model_kwargs does not reject the legacy name. | ||
| num_logits_to_keep = kwargs.pop("num_logits_to_keep", None) | ||
| logits_to_keep = kwargs.get("logits_to_keep", None) | ||
| if num_logits_to_keep is not None and logits_to_keep is None: | ||
| kwargs["logits_to_keep"] = num_logits_to_keep | ||
| logits_to_keep = num_logits_to_keep | ||
| if num_logits_to_keep is None and logits_to_keep is None: | ||
| kwargs["num_logits_to_keep"] = 1 | ||
| try: | ||
| _fwd_params = inspect.signature(self.forward).parameters | ||
| except (TypeError, ValueError): | ||
| _fwd_params = {} | ||
| if "logits_to_keep" in _fwd_params: | ||
| kwargs["logits_to_keep"] = 1 | ||
|
Comment on lines
+2126
to
+2127
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.
When Useful? React with 👍 / 👎. |
||
| elif "num_logits_to_keep" in _fwd_params: | ||
| kwargs["num_logits_to_keep"] = 1 | ||
|
Comment on lines
+2116
to
+2129
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. The current logic for migrating Specifically, if a user provides Furthermore, A unified approach that normalizes both inputs and then uses signature inspection to decide which key to use is more robust. provided_num = kwargs.pop("num_logits_to_keep", None)
provided_logits = kwargs.pop("logits_to_keep", None)
val = provided_logits if provided_logits is not None else provided_num
try:
_fwd_params = inspect.signature(self.forward).parameters
except (TypeError, ValueError):
_fwd_params = {}
if "logits_to_keep" in _fwd_params:
kwargs["logits_to_keep"] = val if val is not None else 1
elif "num_logits_to_keep" in _fwd_params:
kwargs["num_logits_to_keep"] = val if val is not None else 1 |
||
|
|
||
| # Remove token_type_ids | ||
| kwargs.pop("token_type_ids", None) | ||
|
|
||
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.
When a caller supplies
num_logits_to_keepon transformers versions whose modelforwardstill only accepts that legacy spelling, this code always pops it and forwardslogits_to_keepinstead. Generation then hits HF's model-kwarg validation with an unexpectedlogits_to_keep, so existing callers on pre-rename transformers regress; the translation needs to be based on the inspected forward signature before replacing the kwarg.Useful? React with 👍 / 👎.