onnxruntime
7059e239 - Validate Attention head counts (#33498)

Commit
3 days ago
Validate Attention head counts (#33498) This pull request strengthens the validation logic in the Attention operator by enforcing that the number of attention heads (`q_num_heads` and `kv_num_heads`) must always be greater than zero, both when provided as attributes and when inferred from input tensor shapes. It also adds comprehensive unit tests to ensure these constraints are enforced and that invalid configurations are correctly rejected. **Validation improvements:** * Added explicit checks in `attention_helper.h` to enforce that both `kv_num_heads` and `q_num_heads` are greater than zero, raising errors otherwise. This ensures invalid head counts are caught early, regardless of whether they are set via attributes or inferred from tensor shapes. [[1]](diffhunk://#diff-b41e261f1e5a9ffdc334663560199cb827103c2a8af9ef96d92d37e7d0fd3312R63-R64) [[2]](diffhunk://#diff-b41e261f1e5a9ffdc334663560199cb827103c2a8af9ef96d92d37e7d0fd3312R91-R92) **Code robustness and correctness:** * Changed the initialization of `kv_num_heads_` and `q_num_heads_` in `attention.cc` to use `narrow<int>`, ensuring type safety and consistency. * Ensured that parameter checking is called at the correct point in `ComputeOutputShapeForAttention`, after all relevant parameters are set. [[1]](diffhunk://#diff-b41e261f1e5a9ffdc334663560199cb827103c2a8af9ef96d92d37e7d0fd3312R130) [[2]](diffhunk://#diff-b41e261f1e5a9ffdc334663560199cb827103c2a8af9ef96d92d37e7d0fd3312L173-L175) **Testing enhancements:** * Added a new test case `Attention_RejectsZeroHeadCounts` in `attention_op_test.cc` that covers both 3D and 4D input scenarios, verifying that the operator fails as expected when zero head counts are provided either explicitly or via input shapes.
Author
Parents
Loading