Skip to content

dnn onnx: add instance norm layer#24378

Merged
asmorkalov merged 26 commits intoopencv:4.xfrom
fengyuentau:instance_norm
Nov 7, 2023
Merged

dnn onnx: add instance norm layer#24378
asmorkalov merged 26 commits intoopencv:4.xfrom
fengyuentau:instance_norm

Conversation

@fengyuentau
Copy link
Copy Markdown
Member

@fengyuentau fengyuentau commented Oct 9, 2023

Resolves #24377
Relates #24092 (comment)

Perf multi-thread single-thread
x: [2, 64, 180, 240] 3.95ms 11.12ms

Todo:

Pull Request Readiness Checklist

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
force_builders=Linux OpenCL,Win64 OpenCL,Custom
buildworker:Custom=linux-4
build_image:Custom=ubuntu:18.04
modules_filter:Custom=none
disable_ipp:Custom=ON

@fengyuentau fengyuentau added feature category: dnn category: dnn (onnx) ONNX suport issues in DNN module labels Oct 9, 2023
@fengyuentau fengyuentau added this to the 4.9.0 milestone Oct 9, 2023
@fengyuentau fengyuentau requested a review from dkurt October 9, 2023 12:12
@dkurt
Copy link
Copy Markdown
Member

dkurt commented Oct 9, 2023

This is 3rd layer which will do the same compute. There are already MVN and LayerNorm, can they be fixed?

@fengyuentau
Copy link
Copy Markdown
Member Author

fengyuentau commented Oct 9, 2023

can they be fixed?

I prefer to keep all of those (keep mvn for caffe models). Dedicated layers are more flexible when it comes to backend support and optimization. And we don't have to break a single layer into several layers, for example,

https://github.com/opencv/opencv/blob/590f150d5e032165e27d81294c9b7ac710b77f11/modules/dnn/src/onnx/onnx_importer.cpp#L1870C6-L1883

@dkurt
Copy link
Copy Markdown
Member

dkurt commented Oct 9, 2023

@fengyuentau, MVN already provides OpenCL, CUDA and OpenVINO optimizations. Let's rename it instead and use in ONNX importer.

@fengyuentau
Copy link
Copy Markdown
Member Author

MVN already provides OpenCL, CUDA and OpenVINO optimizations

We still have CANN and some other backends which has its own graph. Merging several layers into one may have trouble catching layer type and corresponding parameters. Dedicated layers are more maintainable.

@dkurt
Copy link
Copy Markdown
Member

dkurt commented Oct 9, 2023

@fengyuentau, agree, but what about fixing LayerNorm? Can it be replaced in ONNX importer instead if MVN?

@fengyuentau
Copy link
Copy Markdown
Member Author

@fengyuentau, agree, but what about fixing LayerNorm? Can it be replaced in ONNX importer instead if MVN?

I propose to keep it as-is. There is a plan adding CANN backend support for it #23550. If the layer type is missed, it may most probably fool the CANN graph simplifier and lead to performance drop.

@dkurt
Copy link
Copy Markdown
Member

dkurt commented Oct 9, 2023

This way, can InstanceNorm replace LayerNorm in the simplifier completely?

class LayerNormSubGraph : public Subgraph
If this PR will remove LayerNorm, that looks fine. Otherwise, it duplicates the math.

@fengyuentau
Copy link
Copy Markdown
Member Author

fengyuentau commented Oct 9, 2023

Speaking of OpenVINO, is there a documentation on its operator spec? Something like the ONNX operator spec documentation.

@dkurt
Copy link
Copy Markdown
Member

dkurt commented Oct 9, 2023

Speaking of OpenVINO, is there a documentation on its operator spec?

https://docs.openvino.ai/2023.1/openvino_docs_operations_specifications.html

@fengyuentau
Copy link
Copy Markdown
Member Author

Speaking of OpenVINO, is there a documentation on its operator spec?

https://docs.openvino.ai/2023.1/openvino_docs_operations_specifications.html

Thank you!

@vpisarev
Copy link
Copy Markdown
Contributor

@dkurt, @fengyuentau, I think, ONNX with its InstanceNormalization is much more popular nowadays vs. Caffe & MVN. I would be nice, I think, to have a dedicated layer with conventional name (InstanceNormalization), but internally it can call MVN kernel (which could be declared in some internal headers)

@fengyuentau
Copy link
Copy Markdown
Member Author

In this way, we can extract the kernel, put it in layers/cpu_kernels/, call it anywhere that we need it (mvn, layer norm, instance norm, ...).

@asmorkalov
Copy link
Copy Markdown
Contributor

@fengyuentau Please pay attention on the merge conflict.

@asmorkalov
Copy link
Copy Markdown
Contributor

@dkurt could you take a look again?

@dkurt
Copy link
Copy Markdown
Member

dkurt commented Oct 30, 2023

@asmorkalov, I expected that we should merge #24409 first

asmorkalov pushed a commit that referenced this pull request Nov 1, 2023
dnn: add shared fastNorm kernel for mvn, instance norm and layer norm #24409

Relates #24378 (comment)

TODO:

- [x] add fastNorm
- [x] refactor layer norm with fastNorm
- [x] refactor mvn with fastNorm
- [ ] add onnx mvn in importer (in a new PR?)
- [ ] refactor instance norm with fastNorm (in another PR #24378, need to merge this one first though)

### 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
@asmorkalov
Copy link
Copy Markdown
Contributor

@dkurt Done.

@asmorkalov
Copy link
Copy Markdown
Contributor

@fengyuentau please rebase to get PR 24409 included.


virtual bool supportBackend(int backendId) CV_OVERRIDE {
return backendId == DNN_BACKEND_OPENCV;
}
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.

Breaks coverage with other backends that initialized in MVN layer (OpenVINO, CUDA).

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.

I will try to add them back.

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.

OpenVINO and CUDA backend are added.

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.

Can you handle OpenCL backend as well?

bool forward_ocl(InputArrayOfArrays inputs_, OutputArrayOfArrays outputs_, OutputArrayOfArrays internals_)

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.

I can give it a shot.

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.

May I ask why returning false in forward_ocl in mvn_layer.cpp if the input umat type is int16?

if (inputs[0].depth() == CV_16S)
return false;

Initially I thought it was CV_16F, but it is CV_16S indeed. So the opencl backend supports int8 quantization as well? But the weirder thing is we don't support int16 quantization right?

Copy link
Copy Markdown
Member

@dkurt dkurt Nov 3, 2023

Choose a reason for hiding this comment

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

CV_16S is an original data type for FP16 in OpenCV (before CV_16F which is used in dnn for FP16 inference on CPU). However every OpenCL kernel uses CV_16S which actually just a short int buffer for float16. See convertFp16

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.

Done with adding OpenCL backend (no fp16 though).

@fengyuentau
Copy link
Copy Markdown
Member Author

fengyuentau commented Nov 2, 2023

Update: Resolved.


@dkurt Do you know how is memory allocated in CUDA op? For example, here in cuda4dnn/primitives/mvn.hpp:

            csl::WorkspaceBuilder builder;
            builder.require<float>(max_outer_size);
            if (normalize_variance)
                builder.require<float>(max_outer_size);
            scratch_mem_in_bytes = builder.required_workspace_size();

@fengyuentau
Copy link
Copy Markdown
Member Author

All tests including the ones on different backends (OpenVINO, CUDA and OpenCL) are passed 👍

@fengyuentau
Copy link
Copy Markdown
Member Author

By the way, I though dnn with OpenCL on macOS is broken because it just did not work, but actually I found it is disable and no log is output during building opencv:

ocv_option(OPENCV_DNN_OPENCL "Build with OpenCL support" HAVE_OPENCL AND NOT APPLE)

It has been disabled back to 2018 with a comment saying it was temporarily, but it actually lasts till today and goes on. Is it time to enable it? cc @vpisarev

@asmorkalov asmorkalov merged commit ee0822d into opencv:4.x Nov 7, 2023
@asmorkalov asmorkalov assigned dkurt and unassigned vpisarev Nov 7, 2023
@fengyuentau fengyuentau deleted the instance_norm branch November 7, 2023 10:03
@dkurt dkurt mentioned this pull request Nov 30, 2023
12 tasks
IskXCr pushed a commit to Haosonn/opencv that referenced this pull request Dec 20, 2023
dnn: add shared fastNorm kernel for mvn, instance norm and layer norm opencv#24409

Relates opencv#24378 (comment)

TODO:

- [x] add fastNorm
- [x] refactor layer norm with fastNorm
- [x] refactor mvn with fastNorm
- [ ] add onnx mvn in importer (in a new PR?)
- [ ] refactor instance norm with fastNorm (in another PR opencv#24378, need to merge this one first though)

### 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
IskXCr pushed a commit to Haosonn/opencv that referenced this pull request Dec 20, 2023
dnn onnx: add instance norm layer opencv#24378

Resolves opencv#24377
Relates opencv#24092 (comment)

| Perf | multi-thread | single-thread |
| - | - | - |
| x: [2, 64, 180, 240] | 3.95ms | 11.12ms |

Todo:

- [x] speed up by multi-threading
- [x] add perf
- [x] add backend: OpenVINO
- [x] add backend: CUDA
- [x] add backend: OpenCL (no fp16)
- [ ] add backend: CANN (will be done via opencv#24462)


### 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

```
force_builders=Linux OpenCL,Win64 OpenCL,Custom
buildworker:Custom=linux-4
build_image:Custom=ubuntu:18.04
modules_filter:Custom=none
disable_ipp:Custom=ON
```
thewoz pushed a commit to thewoz/opencv that referenced this pull request Jan 4, 2024
dnn: add shared fastNorm kernel for mvn, instance norm and layer norm opencv#24409

Relates opencv#24378 (comment)

TODO:

- [x] add fastNorm
- [x] refactor layer norm with fastNorm
- [x] refactor mvn with fastNorm
- [ ] add onnx mvn in importer (in a new PR?)
- [ ] refactor instance norm with fastNorm (in another PR opencv#24378, need to merge this one first though)

### 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
thewoz pushed a commit to thewoz/opencv that referenced this pull request Jan 4, 2024
dnn onnx: add instance norm layer opencv#24378

Resolves opencv#24377
Relates opencv#24092 (comment)

| Perf | multi-thread | single-thread |
| - | - | - |
| x: [2, 64, 180, 240] | 3.95ms | 11.12ms |

Todo:

- [x] speed up by multi-threading
- [x] add perf
- [x] add backend: OpenVINO
- [x] add backend: CUDA
- [x] add backend: OpenCL (no fp16)
- [ ] add backend: CANN (will be done via opencv#24462)


### 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

```
force_builders=Linux OpenCL,Win64 OpenCL,Custom
buildworker:Custom=linux-4
build_image:Custom=ubuntu:18.04
modules_filter:Custom=none
disable_ipp:Custom=ON
```
@asmorkalov asmorkalov mentioned this pull request Jan 19, 2024
@fengyuentau fengyuentau mentioned this pull request Feb 21, 2024
48 tasks
thewoz pushed a commit to thewoz/opencv that referenced this pull request May 29, 2024
dnn: add shared fastNorm kernel for mvn, instance norm and layer norm opencv#24409

Relates opencv#24378 (comment)

TODO:

- [x] add fastNorm
- [x] refactor layer norm with fastNorm
- [x] refactor mvn with fastNorm
- [ ] add onnx mvn in importer (in a new PR?)
- [ ] refactor instance norm with fastNorm (in another PR opencv#24378, need to merge this one first though)

### 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
thewoz pushed a commit to thewoz/opencv that referenced this pull request May 29, 2024
dnn onnx: add instance norm layer opencv#24378

Resolves opencv#24377
Relates opencv#24092 (comment)

| Perf | multi-thread | single-thread |
| - | - | - |
| x: [2, 64, 180, 240] | 3.95ms | 11.12ms |

Todo:

- [x] speed up by multi-threading
- [x] add perf
- [x] add backend: OpenVINO
- [x] add backend: CUDA
- [x] add backend: OpenCL (no fp16)
- [ ] add backend: CANN (will be done via opencv#24462)


### 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

```
force_builders=Linux OpenCL,Win64 OpenCL,Custom
buildworker:Custom=linux-4
build_image:Custom=ubuntu:18.04
modules_filter:Custom=none
disable_ipp:Custom=ON
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

dnn: inference results are different from ONNX Runtime on the InstanceNorm layer

4 participants