Skip to content

Replace Slice optional inputs removal to adjustment#24672

Merged
asmorkalov merged 1 commit intoopencv:4.xfrom
dkurt:adjust_slice_optional_inputs
Dec 10, 2023
Merged

Replace Slice optional inputs removal to adjustment#24672
asmorkalov merged 1 commit intoopencv:4.xfrom
dkurt:adjust_slice_optional_inputs

Conversation

@dkurt
Copy link
Copy Markdown
Member

@dkurt dkurt commented Dec 8, 2023

Pull Request Readiness Checklist

resolves #24671

related discussion: #24655 (comment)

TODO:

  • Test with attention layer PR for which initial subgraph has been added.
patch
--- a/modules/dnn/src/onnx/onnx_graph_simplifier.cpp
+++ b/modules/dnn/src/onnx/onnx_graph_simplifier.cpp
@@ -313,18 +313,30 @@ class AttentionSubGraph : public Subgraph {
         att_add = addNodeToMatch("Add", addNodeToMatch(""), att_matmul);
 
         // v_path
-        slice_v = addNodeToMatch("Slice", att_add, addNodeToMatch(""), addNodeToMatch(""));
+        slice_v = addNodeToMatch("Slice", {att_add,
+                                             addNodeToMatch(""),
+                                             addNodeToMatch(""),
+                                             addNodeToMatch(""),
+                                             addNodeToMatch("")});
         int reshape_v = addNodeToMatch("Reshape", slice_v, addNodeToMatch(""));
         int transpose_v = addNodeToMatch("Transpose", reshape_v);
 
         // q_path
-        slice_q = addNodeToMatch("Slice", att_add, addNodeToMatch(""), addNodeToMatch(""));
+        slice_q = addNodeToMatch("Slice", {att_add,
+                                             addNodeToMatch(""),
+                                             addNodeToMatch(""),
+                                             addNodeToMatch(""),
+                                             addNodeToMatch("")});
         reshape_q = addNodeToMatch("Reshape", slice_q, addNodeToMatch(""));
         int transpose_q = addNodeToMatch("Transpose", reshape_q);
         div_q = addNodeToMatch("Div", transpose_q, addNodeToMatch(""));
 
         // k_path
-        slice_k = addNodeToMatch("Slice", att_add, addNodeToMatch(""), addNodeToMatch(""));
+        slice_k = addNodeToMatch("Slice", {att_add,
+                                             addNodeToMatch(""),
+                                             addNodeToMatch(""),
+                                             addNodeToMatch(""),
+                                             addNodeToMatch("")});
         int reshape_k = addNodeToMatch("Reshape", slice_k, addNodeToMatch(""));
         int transpose_k = addNodeToMatch("Transpose", reshape_k);
 
@@ -422,16 +434,28 @@ class AttentionSingleHeadSubGraph : public Subgraph {
         att_add = addNodeToMatch("Add", addNodeToMatch(""), att_matmul);
 
         // v_path
-        slice_v = addNodeToMatch("Slice", att_add, addNodeToMatch(""), addNodeToMatch(""));
+        slice_v = addNodeToMatch("Slice", {att_add,
+                                             addNodeToMatch(""),
+                                             addNodeToMatch(""),
+                                             addNodeToMatch(""),
+                                             addNodeToMatch("")});
         int transpose_v = addNodeToMatch("Transpose", slice_v);
 
         // q_path
-        slice_q = addNodeToMatch("Slice", att_add, addNodeToMatch(""), addNodeToMatch(""));
+        slice_q = addNodeToMatch("Slice", {att_add,
+                                             addNodeToMatch(""),
+                                             addNodeToMatch(""),
+                                             addNodeToMatch(""),
+                                             addNodeToMatch("")});
         int transpose_q = addNodeToMatch("Transpose", slice_q);
         div_q = addNodeToMatch("Div", transpose_q, addNodeToMatch(""));
 
         // k_path
-        slice_k = addNodeToMatch("Slice", att_add, addNodeToMatch(""), addNodeToMatch(""));
+        slice_k = addNodeToMatch("Slice", {att_add,
+                                             addNodeToMatch(""),
+                                             addNodeToMatch(""),
+                                             addNodeToMatch(""),
+                                             addNodeToMatch("")});
         int transpose_k = addNodeToMatch("Transpose", slice_k);
 
         // qk

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

  • I agree to contribute to the project under Apache 2 License.
  • 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
  • The PR is proposed to the proper branch
  • There is a reference to the original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake

@dkurt dkurt added the category: dnn (onnx) ONNX suport issues in DNN module label Dec 8, 2023
@dkurt dkurt mentioned this pull request Dec 8, 2023
4 tasks
@opencv-alalek opencv-alalek added this to the 4.9.0 milestone Dec 8, 2023
@dkurt dkurt mentioned this pull request Dec 9, 2023
13 tasks
{
opencv_onnx::NodeProto* node = fusedNode.dynamicCast<ONNXNodeWrapper>()->node;
for (int i = num_inputs_; i < 5; ++i) {
node->add_input("");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Doesn't it lose connections to the existing constants (starts, ends, axes and steps)?

Copy link
Copy Markdown
Member Author

@dkurt dkurt Dec 9, 2023

Choose a reason for hiding this comment

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

No, It should append missed connections to axes and steps pointing to a fake empty initializer added by

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How does this appending happen? What I see here is input names are missed already.

Copy link
Copy Markdown
Member Author

@dkurt dkurt Dec 9, 2023

Choose a reason for hiding this comment

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

For example, node which found with 3 real inputs (data, starts, ends) is appended up to 5 inputs with add_input:

inputs: "data_name", "starts_name", "ends_name", "", ""

net.add_initializer(); adds an empty tensor with an empty name. So the final version of the Slice refers to it.

I have observed such behavior with resize_nearest_unfused_opset11_torch1.4 model:
image

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, I got you point here. But actually my problem is why this

- setFusedNode("Slice", std::vector<int>{input, starts, ends});
+ setFusedNode("Slice", inputs);

does not drop the starts and ends constants.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ahhh, sorry, I missed that. I thought it was input.

Copy link
Copy Markdown
Contributor

@Abdurrahheem Abdurrahheem Dec 9, 2023

Choose a reason for hiding this comment

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

@dkurt is there a way for me to check these fixes locally before merging? Wanna make sure if yolox test do pass.

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.

@Abdurrahheem, do the following:

git remote add dkurt https://github.com/dkurt/opencv
git fetch dkurt adjust_slice_optional_inputs
git checkout dkurt/adjust_slice_optional_inputs

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@dkurt is there a way for me to check these fixes locally before merging? Wanna make sure if yolox test do pass.

git cherry-pick should also work right? The simplest way is do the same changes as this PR manually, so you don’t need to mess around with git operations.

Copy link
Copy Markdown
Contributor

@Abdurrahheem Abdurrahheem Dec 10, 2023

Choose a reason for hiding this comment

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

@dkurt just checked test with the new patch. Everything related to yolo detectors passes. Thank you for a quick fix!

@asmorkalov asmorkalov merged commit 098efb6 into opencv:4.x Dec 10, 2023
@asmorkalov asmorkalov mentioned this pull request Jan 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug category: dnn (onnx) ONNX suport issues in DNN module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Yolox ONNX graph parsing error

5 participants