Skip to content

Fix PaliGemma labels for unpadded batches - #51222

Open
stefanielizeth73-art wants to merge 1 commit into
huggingface:mainfrom
stefanielizeth73-art:fix/paligemma-ragged-labels
Open

stefanielizeth73-art wants to merge 1 commit into
huggingface:mainfrom
stefanielizeth73-art:fix/paligemma-ragged-labels

Conversation

@stefanielizeth73-art

@stefanielizeth73-art stefanielizeth73-art commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

🤗 Transformers CI

What does this PR do?

Fix label construction for unpadded PaliGemma batches with different sequence lengths. The tokenizer accepts these batches, but converting all input IDs into one NumPy array raises before the processor can return the encoded examples for later collation.

Build labels per sequence, keeping the prefix masked with -100. BatchFeature handles the requested tensor conversion. The regression test compares an unpadded batch with processing each example separately and checks that both masked prefix and supervised suffix tokens remain present.

Reproducer

Run on CPU against main e35e1a5087907deb2cbdd7304dd3ff047dca4135. This loads only processor/tokenizer assets from a published checkpoint.

import numpy as np
from transformers import AutoProcessor

processor = AutoProcessor.from_pretrained("mlx-community/paligemma-3b-mix-224-8bit")
image = np.zeros((224, 224, 3), dtype=np.uint8)
inputs = processor(
    images=[image, image],
    text=["<image>Describe the image.", "<image>Describe the image in more detail."],
    suffix=["A cat.", "A cat sitting on a wooden bench."],
)
lengths = [len(sequence) for sequence in inputs.input_ids]
assert lengths[0] != lengths[1]
assert [len(labels) for labels in inputs.labels] == lengths
print(lengths)

Main raises ValueError: setting an array element with a sequence at label construction. With this change it returns sequence and label lengths [266, 274].

Validation

  • The new regression fails on unchanged main and passes with the fix.
  • pytest tests/models/paligemma/test_processing_paligemma.py -q: 37 passed, 36 skipped.
  • pytest tests/models/paligemma/test_modeling_paligemma.py -q --timeout=180 (the additional module selected by tests_fetcher.py): 145 passed, 112 skipped, 33 xfailed, 6 xpassed, with 1,563 subtests passing.
  • All five make style checks passed through the equivalent utils/checkers.py command.
  • The model structure check passed.
  • The make fix-repo equivalent completed its other fixers; the docstring checker raises an IndexError on its /src/ path split on Windows. Unrelated generated auto-mapping changes were discarded.
  • Repository typing reports the same 83 diagnostics on main and this branch in the local environment.

The implementation and local tests were completed with Codex assistance.

@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: paligemma

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant