Skip to content

dnn: faster ConvTranspose2 on CPU - #30396

Merged
asmorkalov merged 2 commits into
opencv:5.xfrom
sanjayk9:dnn-deconv-gemm
Oct 21, 2026
Merged

asmorkalov merged 2 commits into
opencv:5.xfrom
sanjayk9:dnn-deconv-gemm

Conversation

@sanjayk9

@sanjayk9 sanjayk9 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor
  • With MLAS, every fp32 ConvTranspose2 runs as a packed GEMM per group, followed by a col2im that adds each kernel tap into the output. Threads own bands of output rows, so col2im needs no locking. When taps overlap, output channels are processed in chunks so the column buffer stays in cache. When kernel == stride, each output pixel gets one tap and value + bias is written directly.
  • conv2_deconv is now CPU-dispatched (AVX, AVX2, NEON) and uses four accumulators, so the FMAs no longer wait on each other. It is still used when MLAS is not available or the input is not fp32.

opencv_extra: opencv/opencv_extra#1442 (test models for the single-tap path, channel chunks, and groups that start inside a channel block)

Measured on an i9-14900KS, times in ms.

Whole decoder

Model Threads 5.x This PR ORT 1.30
SAM2 decoder 1 68.5 36.8 36.5
SAM2 decoder 8 14.0 9.7 8.2
SAM2 decoder 32 12.8 10.5 9.8
SAM3 decoder 1 87.9* 51.1 47.1
SAM3 decoder 8 19.0* 14.1 12.6
SAM3 decoder 32 16.4* 14.4 13.0

* SAM3 does not run on 5.x; this column includes the Attention change from this PR.

Best of several interleaved rounds, measured on the first commit. The col2im commit leaves the SAM ConvTranspose layers within a few percent.

ConvTranspose layer

Layer Threads 5.x This PR ORT 1.30
SAM2 256→64 ch, 2×2 s2, 64×64 1 24.0 3.35 3.54
8 3.30 0.59 0.92
32 1.53 0.40 0.95
SAM2 64→32 ch, 2×2 s2, 128×128 1 13.2 2.10 2.52
8 1.76 0.31 1.15
32 0.85 0.24 1.88
SAM3 256→64 ch, 2×2 s2, 72×72 1 27.4 4.25 4.50
8 3.85 0.59 1.16
32 1.88 0.45 1.22
SAM3 64→32 ch, 2×2 s2, 144×144 1 15.5 2.72 3.41
8 2.08 0.41 1.44
32 1.00 0.32 1.62
U-Net 256→128 ch, 4×4 s2 p1, 32×32 1 46.6 7.63 6.70
8 6.49 1.19 1.45
32 4.50 0.97 1.68
GAN 128→64 ch, 4×4 s2 p1, 64×64 1 50.1 8.57 7.51
8 7.01 1.37 2.22
32 4.28 1.16 2.53
128→64 ch, 3×3 s2 p1 op1, 64×64 1 28.3 4.70 4.24
8 3.95 0.76 1.31
32 1.79 0.66 1.75
64→64 ch, 3×3 s1 p1, 64×64 1 13.4 2.52 2.35
8 1.91 0.45 0.97
32 0.80 0.35 0.98
128→128 ch, 4 groups, 4×4 s2 p1, 32×32 1 6.73 1.65 1.30
8 0.97 0.26 0.73
32 0.44 0.21 1.00
64→64 ch, depthwise, 4×4 s2 p1, 64×64 1 30.1 7.54 1.54
8 4.21 2.51 1.89
32 1.92 1.96 2.77

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

Run fp32 ConvTranspose2 as MLAS GEMM + col2im, with a CPU-dispatched
direct kernel as the fallback. Adds ConvTranspose2 and SAM3 decoder
perf tests.
@asmorkalov asmorkalov self-assigned this Oct 19, 2026
@asmorkalov
asmorkalov requested review from abhishek-gola and asmorkalov and removed request for asmorkalov October 19, 2026 09:41
@asmorkalov asmorkalov added this to the 5.1 milestone Oct 19, 2026
@asmorkalov
asmorkalov self-requested a review October 19, 2026 10:21
@asmorkalov

Copy link
Copy Markdown
Contributor

Jetson Orin:

Geometric mean (ms)

                          Name of Test                            5.x-2  patch-2  patch-2  
                                                                                     vs    
                                                                                   5.x-2   
                                                                                 (x-factor)
forward::Layer_ConvTranspose2::("64_64_k3s1p1", 64, 64)           3.129   1.469     2.13   
forward::Layer_ConvTranspose2::("128_64_k3s2p1op1", 128, 64)      6.739   2.921     2.31   
forward::Layer_ConvTranspose2::("depthwise_64_k4s2p1", 64, 64)    5.664   4.512     1.26   
forward::Layer_ConvTranspose2::("gan_128_64_k4s2p1", 128, 64)     11.655  4.605     2.53   
forward::Layer_ConvTranspose2::("group4_128_128_k4s2p1", 128, 32) 1.822   0.764     2.38   
forward::Layer_ConvTranspose2::("sam2_64_32", 64, 128)            3.909   1.847     2.12   
forward::Layer_ConvTranspose2::("sam2_256_64", 256, 64)           6.058   2.392     2.53   
forward::Layer_ConvTranspose2::("sam3_64_32", 64, 144)            4.711   2.108     2.23   
forward::Layer_ConvTranspose2::("sam3_256_64", 256, 72)           7.381   2.607     2.83   
forward::Layer_ConvTranspose2::("unet_256_128_k4s2p1", 256, 32)   10.658  4.292     2.48   

@abhishek-gola abhishek-gola left a comment

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.

The MLAS packing and dispatch are otherwise well structured, but the new path introduces a divide-by-zero for empty spatial tensors. Please handle zero-sized spatial shapes and add focused coverage before merge.

Note: This is an initial review generated by Codex on behalf of @abhishek-gola. Abhishek's final review is still pending.

Codex-reviewed[abhishek-gola]

@asmorkalov

Copy link
Copy Markdown
Contributor

Jetson Orin:

Geometric mean (ms)

                                        Name of Test                                          5.x-1  patch-1  patch-1  
                                                                                                                 vs    
                                                                                                               5.x-1   
                                                                                                             (x-factor)
VisionTransformer::Layer_Attention::OCV/CPU                                                   5.909   4.435     1.33   
forward::Layer_ConvTranspose2::("64_64_k3s1p1", 64, 64)                                       3.183   1.478     2.15   
forward::Layer_ConvTranspose2::("128_64_k3s2p1op1", 128, 64)                                  6.755   2.897     2.33   
forward::Layer_ConvTranspose2::("depthwise_64_k4s2p1", 64, 64)                                5.607   4.564     1.23   
forward::Layer_ConvTranspose2::("gan_128_64_k4s2p1", 128, 64)                                 11.658  4.453     2.62   
forward::Layer_ConvTranspose2::("group4_128_128_k4s2p1", 128, 32)                             1.828   0.767     2.38   
forward::Layer_ConvTranspose2::("sam2_64_32", 64, 128)                                        3.951   1.846     2.14   
forward::Layer_ConvTranspose2::("sam2_256_64", 256, 64)                                       5.973   2.390     2.50   
forward::Layer_ConvTranspose2::("sam3_64_32", 64, 144)                                        4.702   2.112     2.23   
forward::Layer_ConvTranspose2::("sam3_256_64", 256, 72)                                       7.346   2.597     2.83   
forward::Layer_ConvTranspose2::("unet_256_128_k4s2p1", 256, 32)                               10.657  4.343     2.45   
gemm::Gemm::(A=[50, 768], B=[768, 2304], trans_a=0, trans_b=0, OCV/CPU)                       0.532   0.531     1.00   
gemm::Gemm::(A=[50, 1024], B=[1024, 3072], trans_a=0, trans_b=0, OCV/CPU)                     0.979   0.983     1.00   
gemm::Gemm::(A=[197, 768], B=[768, 2304], trans_a=0, trans_b=0, OCV/CPU)                      1.888   1.886     1.00   
gemm::Gemm::(A=[197, 1024], B=[1024, 3072], trans_a=0, trans_b=0, OCV/CPU)                    3.365   3.370     1.00   
gemm::Gemm::(A=[768, 768], B=[768, 768], C=[768], trans_a=0, trans_b=0, OCV/CPU)              3.181   3.190     1.00   
gemm::Gemm::(A=[1024, 1024], B=[1024, 1024], C=[1024], trans_a=0, trans_b=0, OCV/CPU)         7.320   6.954     1.05   
innerproduct::Gemm::(A=[50, 768], B=[768, 2304], trans_a=0, trans_b=0, OCV/CPU)               6.433   6.293     1.02   
innerproduct::Gemm::(A=[50, 1024], B=[1024, 3072], trans_a=0, trans_b=0, OCV/CPU)             12.222  9.325     1.31   
innerproduct::Gemm::(A=[197, 768], B=[768, 2304], trans_a=0, trans_b=0, OCV/CPU)              23.505 20.247     1.16   
innerproduct::Gemm::(A=[197, 1024], B=[1024, 3072], trans_a=0, trans_b=0, OCV/CPU)            34.080 34.494     0.99   
innerproduct::Gemm::(A=[768, 768], B=[768, 768], C=[768], trans_a=0, trans_b=0, OCV/CPU)      13.594 13.602     1.00   
innerproduct::Gemm::(A=[1024, 1024], B=[1024, 1024], C=[1024], trans_a=0, trans_b=0, OCV/CPU) 43.628 41.673     1.05   
innerproduct::MatMul::(A=[1, 1, 768], B=[1, 768, 768], trans_a=0, trans_b=0, OCV/CPU)         0.026   0.025     1.03   
innerproduct::MatMul::(A=[1, 1, 768], B=[1, 768, 3072], trans_a=0, trans_b=0, OCV/CPU)        0.116   0.122     0.95   
innerproduct::MatMul::(A=[1, 1, 3072], B=[1, 3072, 768], trans_a=0, trans_b=0, OCV/CPU)       0.110   0.118     0.93   
innerproduct::MatMul::(A=[1, 2, 768], B=[1, 768, 768], trans_a=0, trans_b=0, OCV/CPU)         0.047   0.042     1.13   
innerproduct::MatMul::(A=[1, 4, 768], B=[1, 768, 768], trans_a=0, trans_b=0, OCV/CPU)         0.085   0.082     1.04   
innerproduct::MatMul::(A=[12, 50, 50], B=[12, 50, 64], trans_a=0, trans_b=0, OCV/CPU)         0.170   0.164     1.04   
innerproduct::MatMul::(A=[12, 50, 64], B=[12, 64, 50], trans_a=0, trans_b=0, OCV/CPU)         0.170   0.164     1.04   
innerproduct::MatMul::(A=[12, 197, 64], B=[12, 64, 197], trans_a=0, trans_b=0, OCV/CPU)       0.423   0.406     1.04   
innerproduct::MatMul::(A=[12, 197, 197], B=[12, 197, 64], trans_a=0, trans_b=0, OCV/CPU)      0.408   0.394     1.03   
innerproduct::MatMul::(A=[16, 50, 50], B=[16, 50, 64], trans_a=0, trans_b=0, OCV/CPU)         0.229   0.219     1.05   
innerproduct::MatMul::(A=[16, 50, 64], B=[16, 64, 50], trans_a=0, trans_b=0, OCV/CPU)         0.232   0.223     1.04   
innerproduct::MatMul::(A=[16, 197, 64], B=[16, 64, 197], trans_a=0, trans_b=0, OCV/CPU)       0.564   0.544     1.04   
innerproduct::MatMul::(A=[16, 197, 197], B=[16, 197, 64], trans_a=0, trans_b=0, OCV/CPU)      0.540   0.538     1.00   
matmul::MatMul::(A=[1, 1, 768], B=[1, 768, 768], trans_a=0, trans_b=0, OCV/CPU)               0.073   0.072     1.01   
matmul::MatMul::(A=[1, 1, 768], B=[1, 768, 3072], trans_a=0, trans_b=0, OCV/CPU)              0.249   0.247     1.01   
matmul::MatMul::(A=[1, 1, 3072], B=[1, 3072, 768], trans_a=0, trans_b=0, OCV/CPU)             0.247   0.245     1.01   
matmul::MatMul::(A=[1, 2, 768], B=[1, 768, 768], trans_a=0, trans_b=0, OCV/CPU)               0.074   0.074     1.00   
matmul::MatMul::(A=[1, 4, 768], B=[1, 768, 768], trans_a=0, trans_b=0, OCV/CPU)               0.076   0.075     1.02   

@asmorkalov

Copy link
Copy Markdown
Contributor

Hm, "EfficientDet_int8::DNNTestNetwork::OCV/CPU" regression is stable. Could you take a look?

@abhishek-gola

Copy link
Copy Markdown
Member

@sanjayk9 Reminder about red CI.

@abhishek-gola abhishek-gola left a comment

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.

The empty-tensor handling and regression coverage resolve the previous correctness finding, and the non-power-of-two stride rounding fix is sound. No new code issue was found in this delta, and CI is green. The stable EfficientDet_int8 regression raised in the existing thread is still unexplained; please resolve that performance question before merge.

Note: This is an initial review generated by Codex on behalf of @abhishek-gola. Abhishek's final review is still pending.

Codex-reviewed[abhishek-gola]

@asmorkalov

Copy link
Copy Markdown
Contributor

@sanjayk9 @abhishek-gola Please take a look on int8 perf regression.

@sanjayk9

Copy link
Copy Markdown
Contributor Author

@sanjayk9 @abhishek-gola Please take a look on int8 perf regression.

@asmorkalov i dont see any regression for EfficientDet_int8 :

Threads 5.x This PR PR / 5.x
10 61.95 61.77 0.997
4 61.80 61.95 1.003
1 84.02 83.23 0.991

@asmorkalov
asmorkalov merged commit 336f5fa into opencv:5.x Oct 21, 2026
29 checks passed
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.

3 participants