DNN: Support some reduce layers of ONNX on CPU backend#21601
DNN: Support some reduce layers of ONNX on CPU backend#21601opencv-pushbot merged 1 commit intoopencv:4.xfrom
Conversation
cbc9576 to
0827ab2
Compare
rogday
left a comment
There was a problem hiding this comment.
I think, we have to assert the kernel_size to be 1 in the constructor, if the such "pooling" doesn't actually makes sense with any other kernel size, e.g. everything you added except for min, I think. Also, could you please fix code formatting issues? E.g. add/remove spaces where necessary(in ternary operators, after if and such). Another issue is naming: max_val = min(max, val) looks odd and can be misleading.
Thanks for code reviewing. I don't quite understand why it is necessary to add the assert the kernel_size to be 1. Can you describe it more clearly? |
26c4325 to
3ef4302
Compare
rogday
left a comment
There was a problem hiding this comment.
Thanks, looks good with a few code style issues.
alalek
left a comment
There was a problem hiding this comment.
BTW, Huge functions are not really maintainable. Consider avoiding extending them.
| for (int d = dstart; d < dend; ++d) { | ||
| for (int y = ystart; y < yend; ++y) { | ||
| for (int x = xstart; x < xend; ++x) { | ||
| const int index = d * inp_width * inp_height + y * inp_width + x; | ||
| float val = srcData[index]; | ||
|
|
||
| switch (poolingType) { | ||
| case AVE: case SUM: |
There was a problem hiding this comment.
Having a switch in triple nested for loops are terrible from performance perspective.
We need dedicated "processing runners" for each type. E.g., using templates with functors.
We should not have performance regressions during implementing features.
There was a problem hiding this comment.
@alalek, I think, the compiler should be able to optimize it since we're not changing poolingType, but you're right, dedicated runners make more sense.
|
@zihaomu, as we discussed, please, do the following changes:
|
Thanks for the code review, I'm refactoring the code. |
alalek
left a comment
There was a problem hiding this comment.
Thank you for update! Overall looks good.
| bool hasSIMD = false; | ||
| ReduceOpBase() {} | ||
| virtual ~ReduceOpBase() {}; | ||
| virtual float apply(const float*, const float*, const float ikarea = 1.0f) = 0; |
There was a problem hiding this comment.
Please drop inheritance from functors: alalek@pr21601_r
(with similar change for int8 implementation)
| class ReduceOpMIN : public ReduceOpBase | ||
| { | ||
| public: | ||
| bool hasSIMD = true; |
There was a problem hiding this comment.
hasSIMD
Looks like it is unused. Please remove.
| { | ||
| public: | ||
| int type; | ||
| std::vector<size_t> deletedDims; |
There was a problem hiding this comment.
deletedDims
reductionDims? or something else?
Could you please add link to some document, e.g. ONNX specification
| class CV_EXPORTS ReduceLayer : public Layer | ||
| { | ||
| public: | ||
| int type; |
There was a problem hiding this comment.
type
reduce_mode? (to avoid confusion with data type)
There was a problem hiding this comment.
Thanks for your code review, all comments have been fixed.
|
Hi @alalek, Can you take a look at why the CI of Win64 OpenCL is failed? Thx. |
|
|
||
| std::swap(shouldDelete[index], shouldDelete[i]); | ||
| std::swap(perm[index], perm[i]); | ||
| std::swap(inpShape[index], inpShape[i]); |
There was a problem hiding this comment.
Build error from logs:
37>C:\build\precommit_opencl\4.x\opencv\modules\dnn\src\onnx\onnx_importer.cpp(1218,63): error C2665: 'std::swap': none of the 2 overloads could convert all the argument types [C:\build\precommit_opencl\build\modules\dnn\opencv_dnn.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.27.29110\include\thread(200,13): message : could be 'void std::swap(std::thread &,std::thread &) noexcept' (compiling source file C:\build\precommit_opencl\4.x\opencv\modules\dnn\src\onnx\onnx_importer.cpp) [C:\build\precommit_opencl\build\modules\dnn\opencv_dnn.vcxproj]
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.27.29110\include\utility(104,19): message : or 'void std::swap<std::_Vb_reference<std::_Wrap_alloc<std::allocator<std::_Vbase>>>,0>(_Ty &,_Ty &) noexcept(<expr>)' [C:\build\precommit_opencl\build\modules\dnn\opencv_dnn.vcxproj]
with
[
_Ty=std::_Vb_reference<std::_Wrap_alloc<std::allocator<std::_Vbase>>>
] (compiling source file C:\build\precommit_opencl\4.x\opencv\modules\dnn\src\onnx\onnx_importer.cpp)
C:\build\precommit_opencl\4.x\opencv\modules\dnn\src\onnx\onnx_importer.cpp(1218,63): message : while trying to match the argument list '(std::_Vb_reference<std::_Wrap_alloc<std::allocator<std::_Vbase>>>, std::_Vb_reference<std::_Wrap_alloc<std::allocator<std::_Vbase>>>)' [C:\build\precommit_opencl\build\modules\dnn\opencv_dnn.vcxproj]
Looks like there is no std::swap() for elements of std::vector<bool> - as there are no elements references.
There was a problem hiding this comment.
Thanks a lot, I will try to solve it.
| public: | ||
| ReduceOpBase() {} | ||
| virtual ~ReduceOpBase() {}; | ||
| virtual int8_t apply(const int8_t*, const int8_t*) = 0; |
There was a problem hiding this comment.
We still have virtual stuff.
It should be dropped as we use templates.
There was a problem hiding this comment.
Thanks for the code review, I have removed all virtual stuff in the base class, both layers/reduce_layer.cpp and int8_layers/reduce_layer.cpp.
4a5b1eb to
a895e3e
Compare
| class ReduceOpBase | ||
| { | ||
| public: | ||
| ReduceOpBase() {} | ||
| ~ReduceOpBase() {}; | ||
| int8_t apply(const int8_t*, const int8_t*) { return 0; }; | ||
| }; |
There was a problem hiding this comment.
@rogday Thx for code reviewing, I have remove the base class, and refactor the code with struct instead of class.
e3220df to
b6b5c27
Compare
The following Reduce layers of ONNX are supported:
The
ReduceSumtakes theaxesas an input, instead of an attribute, more details, so it is not supported yet.I remove some cases in
dnn/test/test_onnx_conformance_layer_parser_denylist.inl.hppas unit tests.Related Issue
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.