Skip to content

dnn: fix gather layer implementation#22993

Merged
opencv-pushbot merged 1 commit intoopencv:4.xfrom
alalek:fixup_21738
Dec 21, 2022
Merged

dnn: fix gather layer implementation#22993
opencv-pushbot merged 1 commit intoopencv:4.xfrom
alalek:fixup_21738

Conversation

@alalek
Copy link
Copy Markdown
Member

@alalek alalek commented Dec 20, 2022

  • support FP16 data

fixup #21738

Fixes failed tests:

[  FAILED  ] Test_ONNX_layers.Gather/1, where GetParam() = OCV/OCL_FP16
[  FAILED  ] Test_ONNX_layers.GatherMulti/1, where GetParam() = OCV/OCL_FP16
[  FAILED  ] Test_ONNX_layers.MatMul_init/1, where GetParam() = OCV/OCL_FP16

@asmorkalov
Copy link
Copy Markdown
Contributor

@rogday Could you look at it too?


const int axis = normalize_axis(m_axis, shape(inp));

// FIXIT: why should we work with non-normalized input? it should be handled in importer or layers's output generator
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.

It was originally normalized on the go:

for (size_t i = 0; i < outer_dims; ++i)
{
  // ...
  for (size_t j = 0 ; j < indices.total(); ++j)
  {
    const size_t index = (static_cast<int>(idx[j]) + inp.size[axis]) % inp.size[axis];
    // ..
  }
}

I think in onnx importer we should load what it is without any extra operation on constant input & attributes. Operations like normalization should be done in the layer constructor or somewhere. We need the original information when it comes to build operators for other backend, like CANN and TIM-VX, who have operator primitives aligned with ONNX, TF ...

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.

"On the go" normalization is not efficient as performs it multiple times (especially through division).
There is change to perform normalization once (per forward() call) before the main loop.

Copy link
Copy Markdown
Member

@rogday rogday Dec 21, 2022

Choose a reason for hiding this comment

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

It was done because the elements of index tensor can be negative per spec and non-constant at the same time. We cannot perform normalization ahead of time since we don't have the data.

@opencv-pushbot opencv-pushbot merged commit 6b4f3e5 into opencv:4.x Dec 21, 2022
@rogday
Copy link
Copy Markdown
Member

rogday commented Dec 21, 2022

Why did you extract normalization into its own for loop?

@alalek
Copy link
Copy Markdown
Member Author

alalek commented Dec 22, 2022

Why should we do that multiple times in nested loop? Including conversions float->int on each iteration.

@alalek alalek mentioned this pull request Jan 8, 2023
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.

5 participants