1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 15:53:03 +04:00

Merge pull request #29333 from abhishek-gola:attention_layer_extension

Extended Attention layer support #29333

Implemented present/past KV support.

Merge with: https://github.com/opencv/opencv_extra/pull/1381

Co-authored by: @Akansha-977 

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch
- [x] There is a reference to the original bug report and related work
- [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable
      Patch to opencv_extra has the same branch name.
- [x] The feature is well documented and sample code can be built with the project CMake
This commit is contained in:
Abhishek Gola
2026-07-01 18:11:44 +05:30
committed by GitHub
parent 5866bbd9ff
commit c7b8fb28b6
9 changed files with 504 additions and 201 deletions
@@ -1903,6 +1903,15 @@ TEST_P(Test_ONNX_conformance, Layer_Test)
if (name == "test_nllloss_NCd1d2d3d4d5_mean_weight_expanded") {
default_l1 = 2e-5; // Expected: (normL1) <= (l1), actual: 1.06394e-05 vs 1e-05
}
// fp16 Attention models retain fp16 accumulation precision (~9e-5 L1, ~2.4e-4 Inf)
// even when executed on an fp32 target.
if (name == "test_attention_4d_fp16" ||
name == "test_attention_4d_fp16_expanded" ||
name == "test_attention_4d_gqa_with_past_and_present_fp16" ||
name == "test_attention_4d_gqa_with_past_and_present_fp16_expanded") {
default_l1 = std::max(default_l1, 2e-4);
default_lInf = std::max(default_lInf, 1e-3);
}
}
#ifdef HAVE_HALIDE
else if (backend == DNN_BACKEND_HALIDE)
@@ -1965,6 +1974,15 @@ TEST_P(Test_ONNX_conformance, Layer_Test)
if (name == "test_roialign_aligned_false" || name == "test_roialign_aligned_true") {
default_l1 = 3e-5;
}
// fp16 Attention models retain fp16 accumulation precision (~9e-5 L1, ~2.4e-4 Inf)
// even when executed on an fp32 target (the layer falls back to the CPU path).
if (name == "test_attention_4d_fp16" ||
name == "test_attention_4d_fp16_expanded" ||
name == "test_attention_4d_gqa_with_past_and_present_fp16" ||
name == "test_attention_4d_gqa_with_past_and_present_fp16_expanded") {
default_l1 = std::max(default_l1, 2e-4);
default_lInf = std::max(default_lInf, 1e-3);
}
}
#endif
else
@@ -3162,6 +3162,170 @@ CASE(test_attention_4d_attn_mask_bool)
SKIP;
CASE(test_attention_4d_attn_mask_bool_4d)
SKIP;
CASE(test_attention_3d_expanded)
SKIP;
CASE(test_attention_3d_attn_mask_expanded)
SKIP;
CASE(test_attention_3d_diff_heads_sizes_attn_mask_expanded)
SKIP;
CASE(test_attention_3d_scaled_expanded)
SKIP;
CASE(test_attention_3d_gqa_attn_mask_expanded)
SKIP;
CASE(test_attention_3d_diff_heads_sizes_expanded)
SKIP;
CASE(test_attention_3d_transpose_verification_expanded)
SKIP;
CASE(test_attention_3d_softcap_expanded)
SKIP;
CASE(test_attention_3d_gqa_scaled_expanded)
SKIP;
CASE(test_attention_3d_with_past_and_present_qk_matmul_softmax_expanded)
SKIP;
CASE(test_attention_3d_with_past_and_present_qk_matmul_bias_expanded)
SKIP;
CASE(test_attention_3d_with_past_and_present_qk_matmul_expanded)
SKIP;
CASE(test_attention_3d_with_past_and_present_expanded)
SKIP;
CASE(test_attention_3d_gqa_with_past_and_present_expanded)
SKIP;
CASE(test_attention_3d_causal_expanded)
SKIP;
CASE(test_attention_3d_diff_heads_sizes_causal_expanded)
SKIP;
CASE(test_attention_3d_diff_heads_sizes_scaled_expanded)
SKIP;
CASE(test_attention_3d_diff_heads_sizes_softcap_expanded)
SKIP;
CASE(test_attention_3d_diff_heads_with_past_and_present)
SKIP;
CASE(test_attention_3d_diff_heads_with_past_and_present_expanded)
SKIP;
CASE(test_attention_3d_gqa_causal_expanded)
SKIP;
CASE(test_attention_3d_gqa_expanded)
SKIP;
CASE(test_attention_3d_gqa_softcap_expanded)
SKIP;
CASE(test_attention_4d_attn_mask_3d_causal_expanded)
SKIP;
CASE(test_attention_4d_attn_mask_3d_expanded)
SKIP;
CASE(test_attention_4d_causal_expanded)
SKIP;
CASE(test_attention_4d_diff_heads_sizes_attn_mask_expanded)
SKIP;
CASE(test_attention_4d_diff_heads_mask4d_padded_kv_expanded)
SKIP;
CASE(test_attention_4d_attn_mask_4d_causal_expanded)
SKIP;
CASE(test_attention_4d_attn_mask_4d_expanded)
SKIP;
CASE(test_attention_4d_attn_mask_bool_4d_expanded)
SKIP;
CASE(test_attention_4d_attn_mask_bool_expanded)
SKIP;
CASE(test_attention_4d_attn_mask_expanded)
SKIP;
CASE(test_attention_4d_diff_heads_sizes_causal_expanded)
SKIP;
CASE(test_attention_4d_diff_heads_sizes_expanded)
SKIP;
CASE(test_attention_4d_diff_heads_sizes_scaled_expanded)
SKIP;
CASE(test_attention_4d_diff_heads_sizes_softcap_expanded)
SKIP;
CASE(test_attention_4d_expanded)
SKIP;
CASE(test_attention_3d_gqa_with_past_and_present)
SKIP;
CASE(test_attention_3d_with_past_and_present)
SKIP;
CASE(test_attention_3d_with_past_and_present_qk_matmul)
SKIP;
CASE(test_attention_3d_with_past_and_present_qk_matmul_bias)
SKIP;
CASE(test_attention_4d_diff_heads_with_past_and_present)
SKIP;
CASE(test_attention_4d_diff_heads_with_past_and_present_expanded)
SKIP;
CASE(test_attention_4d_diff_heads_with_past_and_present_mask3d)
SKIP;
CASE(test_attention_4d_diff_heads_with_past_and_present_mask3d_expanded)
SKIP;
CASE(test_attention_4d_diff_heads_with_past_and_present_mask4d)
SKIP;
CASE(test_attention_4d_diff_heads_with_past_and_present_mask4d_expanded)
SKIP;
CASE(test_attention_4d_fp16_expanded)
SKIP;
CASE(test_attention_4d_gqa_attn_mask_expanded)
SKIP;
CASE(test_attention_4d_gqa_causal_expanded)
SKIP;
CASE(test_attention_4d_gqa_expanded)
SKIP;
CASE(test_attention_4d_gqa_scaled_expanded)
SKIP;
CASE(test_attention_4d_gqa_softcap_expanded)
SKIP;
CASE(test_attention_3d_with_past_and_present_qk_matmul_softmax)
SKIP;
CASE(test_attention_4d_fp16)
SKIP;
CASE(test_attention_4d_gqa_with_past_and_present)
SKIP;
CASE(test_attention_4d_gqa_with_past_and_present_expanded)
SKIP;
CASE(test_attention_4d_gqa_with_past_and_present_fp16)
SKIP;
CASE(test_attention_4d_gqa_with_past_and_present_fp16_expanded)
SKIP;
CASE(test_attention_4d_scaled_expanded)
SKIP;
CASE(test_attention_4d_softcap_expanded)
SKIP;
CASE(test_attention_4d_with_past_and_present)
SKIP;
CASE(test_attention_4d_with_past_and_present_expanded)
SKIP;
CASE(test_attention_4d_with_past_and_present_qk_matmul)
SKIP;
CASE(test_attention_4d_with_past_and_present_qk_matmul_bias)
SKIP;
CASE(test_attention_4d_with_past_and_present_qk_matmul_bias_3d_mask)
SKIP;
CASE(test_attention_4d_with_past_and_present_qk_matmul_bias_3d_mask_causal)
SKIP;
CASE(test_attention_4d_with_past_and_present_qk_matmul_bias_3d_mask_causal_expanded)
SKIP;
CASE(test_attention_4d_with_past_and_present_qk_matmul_bias_3d_mask_expanded)
SKIP;
CASE(test_attention_4d_with_past_and_present_qk_matmul_bias_4d_mask)
SKIP;
CASE(test_attention_4d_with_past_and_present_qk_matmul_bias_4d_mask_causal)
SKIP;
CASE(test_attention_4d_with_past_and_present_qk_matmul_bias_4d_mask_causal_expanded)
SKIP;
CASE(test_attention_4d_with_past_and_present_qk_matmul_bias_4d_mask_expanded)
SKIP;
CASE(test_attention_4d_with_past_and_present_qk_matmul_bias_expanded)
SKIP;
CASE(test_attention_4d_with_past_and_present_qk_matmul_expanded)
SKIP;
CASE(test_attention_4d_with_qk_matmul)
SKIP;
CASE(test_attention_4d_with_qk_matmul_bias)
SKIP;
CASE(test_attention_4d_with_qk_matmul_bias_expanded)
SKIP;
CASE(test_attention_4d_with_qk_matmul_expanded)
SKIP;
CASE(test_attention_4d_with_qk_matmul_softmax)
SKIP;
CASE(test_attention_4d_with_qk_matmul_softmax_expanded)
SKIP;
END_SWITCH()
#undef EOF_LABEL
#undef BEGIN_SWITCH
@@ -832,4 +832,87 @@
"test_dynamicquantizelinear_max_adjusted",
"test_dynamicquantizelinear_max_adjusted_expanded",
"test_dynamicquantizelinear_min_adjusted",
"test_dynamicquantizelinear_min_adjusted_expanded"
"test_dynamicquantizelinear_min_adjusted_expanded",
"test_attention_3d_expanded",
"test_attention_3d_attn_mask_expanded",
"test_attention_3d_diff_heads_sizes_attn_mask_expanded",
"test_attention_3d_scaled_expanded",
"test_attention_3d_gqa_attn_mask_expanded",
"test_attention_3d_diff_heads_sizes_expanded",
"test_attention_3d_transpose_verification_expanded",
"test_attention_3d_softcap_expanded",
"test_attention_3d_gqa_scaled_expanded",
"test_attention_3d_with_past_and_present_qk_matmul_softmax_expanded",
"test_attention_3d_with_past_and_present_qk_matmul_bias_expanded",
"test_attention_3d_with_past_and_present_qk_matmul_expanded",
"test_attention_3d_with_past_and_present_expanded",
"test_attention_3d_gqa_with_past_and_present_expanded",
"test_attention_3d_causal_expanded",
"test_attention_3d_diff_heads_sizes_causal_expanded",
"test_attention_3d_diff_heads_sizes_scaled_expanded",
"test_attention_3d_diff_heads_sizes_softcap",
"test_attention_3d_diff_heads_sizes_softcap_expanded",
"test_attention_3d_diff_heads_with_past_and_present",
"test_attention_3d_diff_heads_with_past_and_present_expanded",
"test_attention_3d_gqa_causal_expanded",
"test_attention_3d_gqa_expanded",
"test_attention_3d_gqa_softcap_expanded",
"test_attention_4d_attn_mask_3d_causal_expanded",
"test_attention_4d_attn_mask_3d_expanded",
"test_attention_4d_causal_expanded",
"test_attention_4d_diff_heads_sizes_attn_mask_expanded",
"test_attention_4d_diff_heads_mask4d_padded_kv_expanded",
"test_attention_4d_attn_mask_4d_causal_expanded",
"test_attention_4d_attn_mask_4d_expanded",
"test_attention_4d_attn_mask_bool_4d_expanded",
"test_attention_4d_attn_mask_bool_expanded",
"test_attention_4d_attn_mask_expanded",
"test_attention_4d_diff_heads_sizes_causal_expanded",
"test_attention_4d_diff_heads_sizes_expanded",
"test_attention_4d_diff_heads_sizes_scaled_expanded",
"test_attention_4d_diff_heads_sizes_softcap_expanded",
"test_attention_4d_expanded",
"test_attention_3d_gqa_with_past_and_present",
"test_attention_3d_with_past_and_present",
"test_attention_3d_with_past_and_present_qk_matmul",
"test_attention_3d_with_past_and_present_qk_matmul_bias",
"test_attention_4d_diff_heads_with_past_and_present",
"test_attention_4d_diff_heads_with_past_and_present_expanded",
"test_attention_4d_diff_heads_with_past_and_present_mask3d",
"test_attention_4d_diff_heads_with_past_and_present_mask3d_expanded",
"test_attention_4d_diff_heads_with_past_and_present_mask4d",
"test_attention_4d_diff_heads_with_past_and_present_mask4d_expanded",
"test_attention_4d_fp16_expanded",
"test_attention_4d_gqa_attn_mask_expanded",
"test_attention_4d_gqa_causal_expanded",
"test_attention_4d_gqa_expanded",
"test_attention_4d_gqa_scaled_expanded",
"test_attention_4d_gqa_softcap_expanded",
"test_attention_3d_with_past_and_present_qk_matmul_softmax",
"test_attention_4d_fp16",
"test_attention_4d_gqa_with_past_and_present",
"test_attention_4d_gqa_with_past_and_present_expanded",
"test_attention_4d_gqa_with_past_and_present_fp16",
"test_attention_4d_gqa_with_past_and_present_fp16_expanded",
"test_attention_4d_scaled_expanded",
"test_attention_4d_softcap_expanded",
"test_attention_4d_with_past_and_present",
"test_attention_4d_with_past_and_present_expanded",
"test_attention_4d_with_past_and_present_qk_matmul",
"test_attention_4d_with_past_and_present_qk_matmul_bias",
"test_attention_4d_with_past_and_present_qk_matmul_bias_3d_mask",
"test_attention_4d_with_past_and_present_qk_matmul_bias_3d_mask_causal",
"test_attention_4d_with_past_and_present_qk_matmul_bias_3d_mask_causal_expanded",
"test_attention_4d_with_past_and_present_qk_matmul_bias_3d_mask_expanded",
"test_attention_4d_with_past_and_present_qk_matmul_bias_4d_mask",
"test_attention_4d_with_past_and_present_qk_matmul_bias_4d_mask_causal",
"test_attention_4d_with_past_and_present_qk_matmul_bias_4d_mask_causal_expanded",
"test_attention_4d_with_past_and_present_qk_matmul_bias_4d_mask_expanded",
"test_attention_4d_with_past_and_present_qk_matmul_bias_expanded",
"test_attention_4d_with_past_and_present_qk_matmul_expanded",
"test_attention_4d_with_qk_matmul",
"test_attention_4d_with_qk_matmul_bias",
"test_attention_4d_with_qk_matmul_bias_expanded",
"test_attention_4d_with_qk_matmul_expanded",
"test_attention_4d_with_qk_matmul_softmax",
"test_attention_4d_with_qk_matmul_softmax_expanded",
@@ -16,104 +16,18 @@
"test_ai_onnx_ml_label_encoder_tensor_value_only_mapping",
"test_ai_onnx_ml_tree_ensemble_set_membership",
"test_ai_onnx_ml_tree_ensemble_single_tree",
// autoSize <= INT_MAX && autoSize*outTotal == inpTotal in function 'getOutShape' @ C++ exception with description
//"OpenCV(5.0.0-pre) opencv/modules/dnn/src/layers/reshape2_layer.cpp:110
// expanded graphs are not imported correctly
"test_attention_3d_expanded",
"test_attention_3d_attn_mask_expanded",
"test_attention_3d_diff_heads_sizes_attn_mask_expanded",
"test_attention_3d_scaled_expanded",
"test_attention_3d_gqa_attn_mask_expanded",
"test_attention_3d_diff_heads_sizes_expanded",
"test_attention_3d_transpose_verification_expanded",
"test_attention_3d_softcap_expanded",
"test_attention_3d_gqa_scaled_expanded",
"test_attention_3d_with_past_and_present_qk_matmul_softmax_expanded",
"test_attention_3d_with_past_and_present_qk_matmul_softcap_expanded",
"test_attention_3d_with_past_and_present_qk_matmul_bias_expanded",
"test_attention_3d_with_past_and_present_qk_matmul_expanded",
"test_attention_3d_with_past_and_present_expanded",
"test_attention_3d_gqa_with_past_and_present_expanded",
"test_attention_3d_causal_expanded",
"test_attention_3d_diff_heads_sizes_causal_expanded",
"test_attention_3d_diff_heads_sizes_scaled_expanded",
"test_attention_3d_diff_heads_sizes_softcap",
"test_attention_3d_diff_heads_sizes_softcap_expanded",
"test_attention_3d_diff_heads_with_past_and_present",
"test_attention_3d_diff_heads_with_past_and_present_expanded",
"test_attention_3d_gqa_causal_expanded",
"test_attention_3d_gqa_expanded",
"test_attention_3d_gqa_softcap_expanded",
"test_attention_4d_attn_mask_3d_causal_expanded",
"test_attention_4d_attn_mask_3d_expanded",
"test_attention_4d_causal_expanded",
"test_attention_4d_diff_heads_sizes_attn_mask_expanded",
"test_attention_4d_diff_heads_mask4d_padded_kv_expanded",
"test_attention_4d_attn_mask_4d_causal_expanded",
"test_attention_4d_attn_mask_4d_expanded",
"test_attention_4d_attn_mask_bool_4d_expanded",
"test_attention_4d_attn_mask_bool_expanded",
"test_attention_4d_attn_mask_expanded",
"test_attention_4d_diff_heads_sizes_causal_expanded",
"test_attention_4d_diff_heads_sizes_expanded",
"test_attention_4d_diff_heads_sizes_scaled_expanded",
"test_attention_4d_diff_heads_sizes_softcap_expanded",
"test_attention_4d_expanded",
"test_attention_3d_gqa_with_past_and_present",
"test_attention_3d_with_past_and_present",
"test_attention_3d_with_past_and_present_qk_matmul",
"test_attention_3d_with_past_and_present_qk_matmul_bias",
"test_attention_3d_with_past_and_present_qk_matmul_softcap",
"test_attention_3d_with_past_and_present_qk_matmul_softmax",
"test_attention_4d_diff_heads_mask4d_padded_kv",
"test_attention_4d_diff_heads_with_past_and_present",
"test_attention_4d_diff_heads_with_past_and_present_expanded",
"test_attention_4d_diff_heads_with_past_and_present_mask3d",
"test_attention_4d_diff_heads_with_past_and_present_mask3d_expanded",
"test_attention_4d_diff_heads_with_past_and_present_mask4d",
"test_attention_4d_diff_heads_with_past_and_present_mask4d_expanded",
"test_attention_4d_fp16_expanded",
"test_attention_4d_gqa_attn_mask_expanded",
"test_attention_4d_gqa_causal_expanded",
"test_attention_4d_gqa_expanded",
"test_attention_4d_gqa_scaled_expanded",
"test_attention_4d_gqa_softcap_expanded",
// fixme
"test_attention_4d_fp16",
"test_attention_4d_gqa_with_past_and_present",
"test_attention_4d_gqa_with_past_and_present_expanded",
"test_attention_4d_gqa_with_past_and_present_fp16",
"test_attention_4d_gqa_with_past_and_present_fp16_expanded",
"test_attention_4d_scaled_expanded",
"test_attention_4d_softcap_expanded",
"test_attention_4d_with_past_and_present",
"test_attention_4d_with_past_and_present_expanded",
"test_attention_4d_with_past_and_present_qk_matmul",
"test_attention_4d_with_past_and_present_qk_matmul_bias",
"test_attention_4d_with_past_and_present_qk_matmul_bias_3d_mask",
"test_attention_4d_with_past_and_present_qk_matmul_bias_3d_mask_causal",
"test_attention_4d_with_past_and_present_qk_matmul_bias_3d_mask_causal_expanded",
"test_attention_4d_with_past_and_present_qk_matmul_bias_3d_mask_expanded",
"test_attention_4d_with_past_and_present_qk_matmul_bias_4d_mask",
"test_attention_4d_with_past_and_present_qk_matmul_bias_4d_mask_causal",
"test_attention_4d_with_past_and_present_qk_matmul_bias_4d_mask_causal_expanded",
"test_attention_4d_with_past_and_present_qk_matmul_bias_4d_mask_expanded",
"test_attention_4d_with_past_and_present_qk_matmul_bias_expanded",
"test_attention_4d_with_past_and_present_qk_matmul_expanded",
"test_attention_4d_with_qk_matmul",
"test_attention_4d_with_qk_matmul_bias",
"test_attention_4d_with_qk_matmul_bias_expanded",
"test_attention_4d_with_qk_matmul_expanded",
"test_attention_4d_with_qk_matmul_softcap",
"test_attention_4d_with_qk_matmul_softcap_expanded",
"test_attention_4d_with_qk_matmul_softmax",
"test_attention_4d_with_qk_matmul_softmax_expanded",
"test_attention_3d_with_past_and_present_qk_matmul_softcap_expanded", // softcap qk_matmul_output reference disagrees with onnxruntime; bad ref data
"test_attention_3d_with_past_and_present_qk_matmul_softcap", // softcap qk_matmul_output reference disagrees with onnxruntime; bad ref data
"test_attention_4d_diff_heads_mask4d_padded_kv", // needs the nonpad_kv_seqlen input (padded-KV variant) which is not supported yet
"test_attention_4d_with_qk_matmul_softcap", // softcap qk_matmul_output reference disagrees with onnxruntime (~0.77); bad ref data
"test_attention_4d_with_qk_matmul_softcap_expanded", // ditto (same reference data)
"test_averagepool_2d_ceil_last_window_starts_on_pad",
"test_averagepool_2d_dilations",
"test_averagepool_3d_dilations_large_count_include_pad_is_0_ceil_mode_is_False",
"test_averagepool_3d_dilations_large_count_include_pad_is_0_ceil_mode_is_True",
"test_averagepool_3d_dilations_large_count_include_pad_is_1_ceil_mode_is_False",
"test_averagepool_3d_dilations_large_count_include_pad_is_1_ceil_mode_is_True",
"test_averagepool_3d_dilations_small",
"test_basic_convinteger", // Issues::Layer::Can't create layer "onnx_node_output_0!y" of type "ConvInteger" in function 'getLayerInstance'
"test_basic_deform_conv_with_padding",
"test_basic_deform_conv_without_padding",