-
Notifications
You must be signed in to change notification settings - Fork 280
Fix transformers 5.x drift: FP8Linear constructor signature and Gemma3 double-BOS strip under padding #766
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 | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -189,11 +189,23 @@ def _gemma3_call_impl( | |||||||||||||||||||||||||
| # text_inputs = self.tokenizer(text=text, **output_kwargs["text_kwargs"], return_tensors="np") | ||||||||||||||||||||||||||
| return_mm_token_type_ids = output_kwargs["text_kwargs"].pop("return_mm_token_type_ids", True) | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| text_inputs = self.tokenizer(text=text, **output_kwargs["text_kwargs"]) | ||||||||||||||||||||||||||
| # Fix double BOS tokens | ||||||||||||||||||||||||||
| # Tokenize WITHOUT padding: stripping a double BOS after padding only | ||||||||||||||||||||||||||
| # shortens rows that still start with [bos, bos] (under left padding, | ||||||||||||||||||||||||||
| # just the longest row), re-ragging the batch and desyncing | ||||||||||||||||||||||||||
| # attention_mask. Strip first, then pad once. | ||||||||||||||||||||||||||
| text_kwargs = dict(output_kwargs["text_kwargs"]) | ||||||||||||||||||||||||||
| pad_kwargs = {k: text_kwargs.pop(k) for k in ("padding", "max_length", "pad_to_multiple_of", "padding_side") if k in text_kwargs} | ||||||||||||||||||||||||||
| text_inputs = self.tokenizer(text=text, **text_kwargs) | ||||||||||||||||||||||||||
| # Fix double BOS tokens, keeping attention_mask in sync | ||||||||||||||||||||||||||
| double_bos_token_id = [self.tokenizer.bos_token_id]*2 | ||||||||||||||||||||||||||
| input_ids = text_inputs["input_ids"] | ||||||||||||||||||||||||||
| text_inputs["input_ids"] = [x[1:] if x[:2] == double_bos_token_id else x for x in input_ids] | ||||||||||||||||||||||||||
| stripped = [x[1:] if x[:2] == double_bos_token_id else x for x in input_ids] | ||||||||||||||||||||||||||
| if "attention_mask" in text_inputs: | ||||||||||||||||||||||||||
| text_inputs["attention_mask"] = [m[1:] if len(k) != len(x) else m for x, k, m in zip(input_ids, stripped, text_inputs["attention_mask"])] | ||||||||||||||||||||||||||
| text_inputs["input_ids"] = stripped | ||||||||||||||||||||||||||
| if pad_kwargs.get("padding", False) not in (False, None, "do_not_pad"): | ||||||||||||||||||||||||||
| pad_params = inspect.signature(self.tokenizer.pad).parameters | ||||||||||||||||||||||||||
| text_inputs = self.tokenizer.pad(text_inputs, **{k: v for k, v in pad_kwargs.items() if k in pad_params}) | ||||||||||||||||||||||||||
|
Comment on lines
+206
to
+208
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. When popping
Suggested change
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| # Add token type ids manually, as tokenizer can't do arbitrary position token types | ||||||||||||||||||||||||||
| # [TODO] FAILS for batched tokens since text_inputs["input_ids"] is a list of lists, so np.array creates an object! | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1426,12 +1426,13 @@ def _override_to(self, *args, **kwargs): | |||||||||||||||||||||||||||||||||||||||||||||
| layer.weight.input_scale_ub = kwargs['input_scale_ub'] | ||||||||||||||||||||||||||||||||||||||||||||||
| layer.quant_method = "fbgemm_fp8" | ||||||||||||||||||||||||||||||||||||||||||||||
| elif fp8_weight_scale.ndim == 2: | ||||||||||||||||||||||||||||||||||||||||||||||
| # FP8 dynamic quantized. transformers 5.0+ renamed | ||||||||||||||||||||||||||||||||||||||||||||||
| # bias -> has_bias and removed device. | ||||||||||||||||||||||||||||||||||||||||||||||
| if Version("transformers") < Version("5.0.0"): | ||||||||||||||||||||||||||||||||||||||||||||||
| fp8_kwargs = dict(in_features=0, out_features=0, bias=has_bias, dtype=dtype, block_size=kwargs['block_size'], activation_scheme=kwargs['activation_scheme'], device=get_target_device()) | ||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||
| fp8_kwargs = dict(in_features=0, out_features=0, has_bias=has_bias, dtype=dtype, block_size=kwargs['block_size'], activation_scheme=kwargs['activation_scheme']) | ||||||||||||||||||||||||||||||||||||||||||||||
| # FP8 dynamic quantized. FP8Linear's signature drifts across | ||||||||||||||||||||||||||||||||||||||||||||||
| # transformers versions (4.x: bias/dtype/device; 5.x: | ||||||||||||||||||||||||||||||||||||||||||||||
| # has_bias, no dtype/device), so keep only accepted kwargs. | ||||||||||||||||||||||||||||||||||||||||||||||
| fp8_kwargs = dict(in_features=0, out_features=0, bias=has_bias, has_bias=has_bias, dtype=dtype, block_size=kwargs['block_size'], activation_scheme=kwargs['activation_scheme'], device=get_target_device()) | ||||||||||||||||||||||||||||||||||||||||||||||
| fp8_params = inspect.signature(FP8Linear.__init__).parameters | ||||||||||||||||||||||||||||||||||||||||||||||
| if not any(p.kind is p.VAR_KEYWORD for p in fp8_params.values()): | ||||||||||||||||||||||||||||||||||||||||||||||
| fp8_kwargs = {k: v for k, v in fp8_kwargs.items() if k in fp8_params} | ||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1432
to
+1435
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. If
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||
| layer = FP8Linear(**fp8_kwargs) | ||||||||||||||||||||||||||||||||||||||||||||||
| layer.in_features = weight.shape[1] | ||||||||||||||||||||||||||||||||||||||||||||||
| layer.out_features = weight.shape[0] | ||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||
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 callers pass
max_lengthfor truncation, this comprehension removes it beforeself.tokenizer(...)runs and only forwards it later totokenizer.pad. Padding does not truncate already-tokenized sequences, and when padding is disabled the value is dropped entirely, so Gemma3 prompts that previously obeyedtruncation=True, max_length=Ncan now exceed the requested/model length and break batching or run past context limits.Useful? React with 👍 / 👎.