Skip to content

[gemma3] fix bidirectional image mask - #39396

Merged
zucchini-nlp merged 9 commits into
huggingface:mainfrom
zucchini-nlp:gemma3-mask-fix
Jul 22, 2025
Merged

zucchini-nlp merged 9 commits into
huggingface:mainfrom
zucchini-nlp:gemma3-mask-fix

Conversation

@zucchini-nlp

Copy link
Copy Markdown
Member

What does this PR do?

Fixes #39389. There were two issues. First, using getattr on dict will never look for a dict key thus we were skipping on token_type_ids_mask.

Second, kv_idx can go beyond token_type_ids length because it includes the new tokens to be generated. Using a simple check on ids.shape fails in vmap due to dynamic control flow. One option would be to create dummy padded token type ids using kv_idx but I think a simple try-except is also fine

@zucchini-nlp

Copy link
Copy Markdown
Member Author

run-slow: gemma3

@zucchini-nlp
zucchini-nlp requested a review from Cyrilvallez July 14, 2025 07:31
@github-actions

Copy link
Copy Markdown
Contributor

This comment contains run-slow, running the specified jobs:

models: ['models/gemma3']
quantizations: [] ...

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

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.

@zucchini-nlp

Copy link
Copy Markdown
Member Author

run-slow: gemma3

@github-actions

Copy link
Copy Markdown
Contributor

This comment contains run-slow, running the specified jobs:

models: ['models/gemma3']
quantizations: [] ...

@zucchini-nlp

Copy link
Copy Markdown
Member Author

Just realized the is_same_image doesn't work correctly and unmasks bidirectional mask to future images. Working on it, don't review yet

@zucchini-nlp

Copy link
Copy Markdown
Member Author

run-slow: gemma3

@github-actions

Copy link
Copy Markdown
Contributor

This comment contains run-slow, running the specified jobs:

models: ['models/gemma3']
quantizations: [] ...

image_group_ids_at_kv_idx = torch.where(kv_idx < image_group_ids.shape[1], image_group_ids_at_kv_idx, -1)

is_image_block = (token_type_ids[batch_idx, q_idx] == 1) & (token_type_ids_at_kv_idx == 1)
same_image_block = image_group_ids[batch_idx, q_idx] == image_group_ids_at_kv_idx

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could use only token_type_ids but since vmap doesn't support slicing, it always fails with an error. I tried out with different torch ops, none of them worked. Passing image_group_ids was the most straightforward woraround

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks nice!

@zucchini-nlp

zucchini-nlp commented Jul 14, 2025 •

Copy link
Copy Markdown
Member Author

Can be reviewed now, slow tests are failing and I'll fix them tomorrow. No idea how to get read_token from inside runners, will ask Yih-Dar

Edit: yeah, we need to login with our own access tokens after ssh-ing

@zucchini-nlp

Copy link
Copy Markdown
Member Author

run-slow: gemma3

@github-actions

Copy link
Copy Markdown
Contributor

This comment contains run-slow, running the specified jobs:

models: ['models/gemma3']
quantizations: [] ...

@ArthurZucker ArthurZucker left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a fast test for this? 🤗

image_group_ids_at_kv_idx = torch.where(kv_idx < image_group_ids.shape[1], image_group_ids_at_kv_idx, -1)

is_image_block = (token_type_ids[batch_idx, q_idx] == 1) & (token_type_ids_at_kv_idx == 1)
same_image_block = image_group_ids[batch_idx, q_idx] == image_group_ids_at_kv_idx

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks nice!

@zucchini-nlp

Copy link
Copy Markdown
Member Author

Actually the slow tests should fail whenever it breaks, prob we didn't notice it because slow tests were failing for another reason already. I will try to make a fast test to check attentions

@github-actions

Copy link
Copy Markdown
Contributor

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

run-slow: gemma3

@zucchini-nlp
zucchini-nlp merged commit 3bc726b into huggingface:main Jul 22, 2025
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
* fix gemma3 mask

* make compile happy, and use only torch ops

* no full attention between images

* update tests

* fix tests

* add a fast test
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
* fix gemma3 mask

* make compile happy, and use only torch ops

* no full attention between images

* update tests

* fix tests

* add a fast test
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
* fix gemma3 mask

* make compile happy, and use only torch ops

* no full attention between images

* update tests

* fix tests

* add a fast test
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
* fix gemma3 mask

* make compile happy, and use only torch ops

* no full attention between images

* update tests

* fix tests

* add a fast test
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
* fix gemma3 mask

* make compile happy, and use only torch ops

* no full attention between images

* update tests

* fix tests

* add a fast test
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
* fix gemma3 mask

* make compile happy, and use only torch ops

* no full attention between images

* update tests

* fix tests

* add a fast test
zaristei pushed a commit to zaristei/transformers that referenced this pull request Sep 9, 2025
* fix gemma3 mask

* make compile happy, and use only torch ops

* no full attention between images

* update tests

* fix tests

* add a fast test
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.

Gemma3 bidirectional mask for image tokens isn't reaching attention forward

3 participants