Skip to content

dnn: add exhaustive fusion tests, enable more eltwise fusions#17976

Merged
alalek merged 2 commits intoopencv:3.4from
YashasSamaga:dnn-fusion-tests-fix-ocl
Aug 13, 2020
Merged

dnn: add exhaustive fusion tests, enable more eltwise fusions#17976
alalek merged 2 commits intoopencv:3.4from
YashasSamaga:dnn-fusion-tests-fix-ocl

Conversation

@YashasSamaga
Copy link
Copy Markdown
Contributor

@YashasSamaga YashasSamaga commented Jul 29, 2020

fixes #17946 for 3.4

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 OpenCV (BSD) License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or other license that is incompatible with OpenCV
  • The PR is proposed to proper branch
  • There is reference to 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=Custom,Custom Win,Custom Mac
build_image:Custom=ubuntu-openvino-2020.2.0:16.04
build_image:Custom Win=openvino-2020.3.0
build_image:Custom Mac=openvino-2020.2.0

test_modules:Custom=dnn,python2,python3,java
test_modules:Custom Win=dnn,python2,python3,java
test_modules:Custom Mac=dnn,python2,python3,java

buildworker:Custom=linux-1
# disabled due high memory usage: test_opencl:Custom=ON
test_opencl:Custom=ON
test_bigdata:Custom=1
test_filter:Custom=*TestLayerFusion*

test_opencl:Custom Win=ON
test_bigdata:Custom Win=1
test_filter:Custom Win=*TestLayerFusion*

static testing::internal::ParamGenerator<std::string> activationLayersList()
{
// TODO: automate list generation
return Values("ReLU", "ReLU6", "ChannelsPReLU", "TanH", "Swish", "Mish", "Sigmoid", "ELU", "AbsVal", "BNLL", "Power");
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I was wondering if it would be possible to automatically generate this list.


static testing::internal::ParamGenerator<tuple<Backend, Target>> dnnBackendsAndTargetsForFusionTests()
{
return dnnBackendsAndTargets(false, false, false, false); // only OpenCL
Copy link
Copy Markdown
Contributor Author

@YashasSamaga YashasSamaga Jul 29, 2020

Choose a reason for hiding this comment

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

The fusion tests are checked for OpenCL only in 3.4. I am not sure if these should be enabled for other backends since they don't fuse activations or eltwise.

Note that this line might require special handling in 3.4 merge PR. The withCUDA appears before withNgraph on master. The tests should be turned on for CUDA.

@YashasSamaga
Copy link
Copy Markdown
Contributor Author

YashasSamaga commented Jul 29, 2020

Fusion information for the tests added by this PR after enabling debug printfs: https://gist.github.com/YashasSamaga/70e7fe1fc8c9d670c73eb7ea47ec592d

The tests do not ensure that fusions do take place; they just check that the outputs match with the outputs from OCV CPU backend with fusions disabled. If a future PR accidentally stops a fusion, the tests will still pass since the unfused outputs will be corrected. I don't know if there is a way to enforce that fusions do take place.

@asmorkalov
Copy link
Copy Markdown
Contributor

@dkurt @sl-sergei Could you look at it?

nextActivLayer = nextData->layerInstance.dynamicCast<ActivationLayer>();

if( !nextActivLayer.empty() && pinsToKeep.count(lpNext) == 0 &&
if( !nextActivLayer.empty() &&
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This change need not be propagated to master. The eltwise fusion logic is divergent. #17939 applies the same fix on master.

@YashasSamaga
Copy link
Copy Markdown
Contributor Author

Can I squash the commits and rebase? Asking because it could break any reviews in progress.

@YashasSamaga
Copy link
Copy Markdown
Contributor Author

Tests seem to be failing in CI. I am not able to reproduce them locally.

C++ exception with description "OpenCV(3.4.11-dev) /build/precommit_custom_linux/3.4/opencv/modules/dnn/test/test_layers.cpp:2128: error: (-215:Assertion failed) refTimings[i] != 0.0 in function 'test'

This indicates that OCV CPU backend with fusion disabled had a layer timing of 0.0. I assumed that a zero timing implies fusion.

And *TestLayerFusion* can be used as the gtest filter to run all the tests added by this PR. *ConvolutionActivation* won't run all of them.

@alalek
Copy link
Copy Markdown
Member

alalek commented Aug 4, 2020

@YashasSamaga We will take a look on these failures.

@alalek
Copy link
Copy Markdown
Member

alalek commented Aug 7, 2020

@YashasSamaga Please fix copy-parse typo (should fix current red custom builds too) and squash commits onto one.

@YashasSamaga YashasSamaga force-pushed the dnn-fusion-tests-fix-ocl branch from c9c8324 to 52f7bf4 Compare August 7, 2020 17:33
float conv_init_magnitude = 1.0f / in_channels / kernel_h / kernel_w;
int weightsShape[] = {num_filters, in_channels, kernel_h, kernel_w};
Mat weights(4, &weightsShape[0], CV_32F);
randu(weights, -conv_init_magnitude, conv_init_magnitude);
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I am not sure if this is a good initialization. The weights might be too small.

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.

Why do you think if that's a problem? I think it also good chance to track if we have no regressions with such kind of optimizations: #17295

Copy link
Copy Markdown
Contributor Author

@YashasSamaga YashasSamaga Aug 10, 2020

Choose a reason for hiding this comment

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

It will squeeze all weights to a very small range [-0.007, 0.007]. I don't think there is any serious problem but not sure.

@YashasSamaga
Copy link
Copy Markdown
Contributor Author

YashasSamaga commented Aug 7, 2020

@YashasSamaga Please fix copy-parse typo (should fix current red custom builds too) and squash commits onto one.

Done. Sorry for the typo.

Tests will fail on master if these tests are enabled for CUDA backend. Need to add some code for each test case indicating which layers are fused (the code enforcing the fusions expects no layers to be fused by default). I'll make a PR after this arrives in master to enable these tests for the CUDA backend.

Copy link
Copy Markdown
Member

@dkurt dkurt left a comment

Choose a reason for hiding this comment

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

👍 Thank you!

@alalek alalek merged commit 2171cae into opencv:3.4 Aug 13, 2020
@alalek alalek mentioned this pull request Aug 14, 2020
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.

dnn(opencl, cuda): missed eltwise fusion opportunity

4 participants