fix(models): Fix LayoutLMv2 NER crash and broken batched truncation/padding#44187
Merged
zucchini-nlp merged 1 commit intohuggingface:mainfrom Feb 23, 2026
Conversation
Contributor
|
[For maintainers] Suggested jobs to run (before merge) run-slow: layoutlmv2 |
LalithaMV
approved these changes
Feb 22, 2026
4 tasks
zucchini-nlp
approved these changes
Feb 23, 2026
Member
zucchini-nlp
left a comment
There was a problem hiding this comment.
Thanks for the detailed explanation, lgtm! Btw, @ArthurZucker , I think we should raise a warning/error asking to pass a max_length arg when model_max_length > VERY_LARGE_INT because the current error message is misleading
Member
|
run-slow: layoutlmv2 |
Contributor
|
This comment contains models: ["models/layoutlmv2"] |
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
What does this PR do?
The following issues were identified and fixed in this PR:
→ The NER/token classification issue and the downstream bug uncovered in the batched preprocessing use case with
LayoutLMv2Tokenizer.→ Reasoning: The NER use case makes it apparent that the error is hit at this line in LayoutLMv2Tokenizer, which is missing
self.only_label_first_subword.PretrainedTokenizerBasedoesn't createself.only_label_first_subwordeither, so this must be added along with the other custom attributes, and this directly resolved the NER use case as shown in the screenshot.→ For the second fix; any padding="max_length" or truncation=True call without an explicit max_length arg compares self.model_max_length > LARGE_INTEGER (1e20), which in this case evaluates to
True(since model_max_length falls back to VERY_LARGE_INTEGER), and both get translated into no-ops. Sequences in the batch are of different lengths and can't be tensorized, and the misleadingValueErrortells the user to addpadding=Trueandtruncation=True, but they already did? The fix restoresmodel_max_length=512. I confirmed both the base and large model configs havemax_position_embeddings=512, so 512 as a default is correct, and followed the same pattern as MarianTokenizer and TapasTokenizer :)Originally removed in #42894, just wanted to double-check if this was intentional; happy to adjust this fix if I’ve missed something :)
Fixes #44186.
Before both fixes applied:
Attribute fix resolves NER; batched use case still fails (feel free to cross-check; the errors are reproducible):
After both fixes applied; NER + batched use case work (feel free to cross-check):
Before submitting
Pull Request section?
to it if that's the case.
documentation guidelines, and
here are tips on formatting docstrings.