Skip to content

replace bare asserts with ValueError in NNX attention validation#5505

Open
mohsinm-dev wants to merge 1 commit into
google:mainfrom
mohsinm-dev:fix-attention-assert-to-valueerror
Open

replace bare asserts with ValueError in NNX attention validation#5505
mohsinm-dev wants to merge 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
Comment thread flax/nnx/nn/attention.py Outdated
@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 Outdated
@mohsinm-dev mohsinm-dev force-pushed the fix-attention-assert-to-valueerror branch from 407fb86 to d2360a4 Compare June 16, 2026 10:01
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.

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

2 participants