mirror of
https://github.com/opencv/opencv.git
synced 2026-12-03 05:33:03 -06:00
Added Backend Agnostic fusion in DNN - #29656 # What This Adds A way for any layer, on any backend, to absorb trailing per-element math — with the pass knowing nothing about either layer and holding no list of op types. - **Description**: `LayerMath` (one layer's math) → `AdjacencyGraph` (hash-consed chain) - **Contract**: two `bool` virtuals on `Layer` - **CPU implementations**: three, behind that contract Pointwise fusion is the first consumer, not the point. ## The Problem ```cpp ActivationLayer* activ = dynamic_cast<ActivationLayer*>(layer_ptr); Conv2Layer* conv = getLayer<Conv2Layer>(newprog, conv_layer_idx); if (conv) conv->fuseActivation(layer); ``` Guest type, host type, method name all hardcoded in the pass. `Clip` never fused (fails the cast), `Gemm` never fused (no virtual), no 2-op chain fused at all, and no backend could fuse anything without a CPU-specific method. ## Results | Model | 5.x | this PR | speedup | |-----------------------|----------|----------|---------| | MPHand | 2.34 ms | 1.03 ms | 2.27 | | EfficientNet | 9.82 ms | 5.00 ms | 1.96 | | MPPose | 5.14 ms | 2.81 ms | 1.83 | | MobileNet_SSD_v1_ONNX | 12.58 ms | 8.38 ms | 1.50 | | BlazeFace | 0.95 ms | 0.77 ms | 1.23 | | DenseNet_121 | 20.56 ms | 19.08 ms | 1.08 | | MobileNetv2_ONNX | 2.07 ms | 1.99 ms | 1.04 | | MobileViT_XS | 6.33 ms | 6.11 ms | 1.04 | | YuNet_320 | 1.33 ms | 1.27 ms | 1.04 | | PPOCRv3 | 43.05 ms | 42.15 ms | 1.02 | | BERT | 9.78 ms | 9.68 ms | 1.01 | | BEiT_Base_Patch16_224 | 27.48 ms | 27.08 ms | 1.01 | | DeiT_Tiny_Patch16_224 | 4.74 ms | 4.72 ms | 1.01 | | MPPalm | 1.88 ms | 1.85 ms | 1.01 | | SSD | 50.19 ms | 49.82 ms | 1.01 | | YOLOv4_tiny | 7.10 ms | 7.03 ms | 1.01 | ### 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 - [ ] 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. - [ ] The feature is well documented and sample code can be built with the project CMake
52 lines
1.3 KiB
C++
52 lines
1.3 KiB
C++
// This file is part of OpenCV project.
|
|
// It is subject to the license terms in the LICENSE file found in the top-level directory
|
|
// of this distribution and at http://opencv.org/license.html.
|
|
// Copyright (C) 2026, BigVision LLC, all rights reserved.
|
|
// Third party copyrights are property of their respective owners.
|
|
|
|
#include "precomp.hpp"
|
|
#include "adjacency_graph.hpp"
|
|
|
|
namespace cv { namespace dnn {
|
|
CV__DNN_INLINE_NS_BEGIN
|
|
|
|
namespace {
|
|
|
|
typedef std::map<std::type_index, FusionOps> FusionRegistry;
|
|
|
|
// std::map, not unordered_map: fusionOpsFor hands out a pointer to a mapped value,
|
|
// and only a node-based container keeps that valid across later registrations.
|
|
FusionRegistry& registry()
|
|
{
|
|
static FusionRegistry reg;
|
|
return reg;
|
|
}
|
|
|
|
Mutex& registryMutex()
|
|
{
|
|
static Mutex m;
|
|
return m;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void registerFusionOps(const std::type_info& layerType, const FusionOps& ops)
|
|
{
|
|
AutoLock lock(registryMutex());
|
|
registry()[std::type_index(layerType)] = ops;
|
|
}
|
|
|
|
const FusionOps* fusionOpsFor(const Layer* layer)
|
|
{
|
|
if (!layer)
|
|
return nullptr;
|
|
|
|
AutoLock lock(registryMutex());
|
|
const FusionRegistry& reg = registry();
|
|
FusionRegistry::const_iterator it = reg.find(std::type_index(typeid(*layer)));
|
|
return it == reg.end() ? nullptr : &it->second;
|
|
}
|
|
|
|
CV__DNN_INLINE_NS_END
|
|
}} // namespace cv::dnn
|