Skip to content

replace bare asserts with ValueError in NNX attention validation - #5505

Merged
copybara-service[bot] merged 1 commit into
google:mainfrom
mohsinm-dev:fix-attention-assert-to-valueerror
Jun 26, 2026
Merged

copybara-service[bot] merged 1 commit into
google:mainfrom
mohsinm-dev:fix-attention-assert-to-valueerror

Conversation

@mohsinm-dev

Copy link
Copy Markdown
Contributor

Fixes #5496

dot_product_attention, dot_product_attention_weights, and combine_masks used bare assert for input shape validation. Under python -O, these are stripped and invalid inputs silently produce wrong-shaped output on the dropout/module path.

Replaced with if/raise ValueError. Also removed one dead-code assert in the non-GQA else branch where the condition was already guaranteed by the enclosing if/else.

@vfdev-5 vfdev-5 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.

Thanks @mohsinm-dev , I left few comments

Comment thread tests/nnx/nn/attention_test.py Resolved
Comment thread flax/nnx/nn/attention.py Resolved
@mohsinm-dev
mohsinm-dev force-pushed the fix-attention-assert-to-valueerror branch from a5984ea to 407fb86 Compare June 16, 2026 09:47
Comment thread tests/nnx/nn/attention_test.py Resolved
@mohsinm-dev
mohsinm-dev force-pushed the fix-attention-assert-to-valueerror branch from 407fb86 to d2360a4 Compare June 16, 2026 10:01
@vfdev-5
vfdev-5 force-pushed the fix-attention-assert-to-valueerror branch from d2360a4 to 4f85c88 Compare June 17, 2026 20:58

@vfdev-5 vfdev-5 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.

LGTM

@copybara-service
copybara-service Bot merged commit 51b3d8f into google:main Jun 26, 2026
25 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

flax.nnx.dot_product_attention silently returns wrong-shaped output under python -O (rank asserts dropped)

3 participants