Skip to content

dnn: bound per-channel w_scale to bias length in onnx parseQConv 🤖🤖🤖 - #30395

Merged
asmorkalov merged 2 commits into
opencv:5.xfrom
uwezkhan:onnx-qconv-scale-bias-bound
Oct 21, 2026
Merged

asmorkalov merged 2 commits into
opencv:5.xfrom
uwezkhan:onnx-qconv-scale-bias-bound

Conversation

@uwezkhan

@uwezkhan uwezkhan commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

OpenCV Extra: opencv/opencv_extra#1445

QLinearConv import folds a constant int32 bias to float in parseQConv (modules/dnn/src/onnx/onnx_importer2.cpp): each bias element is multiplied by its per-channel weight scale and written into bias_float, which is sized from the bias tensor. The fold loop runs over the length of w_scale, but w_scale and the bias are independent inputs of the model, so a 1-D w_scale longer than the bias writes past bias_float. It is reached from readNetFromONNX on any .onnx carrying such a QLinearConv, and the scalar x_scale read is likewise unguarded against an empty tensor.

ASan on the default engine loading a model whose bias has one element and whose per-channel w_scale has 64:

==ERROR: AddressSanitizer: heap-buffer-overflow ... READ of size 4
    #0 ONNXImporter2::parseQConv(...)
    #1 ONNXImporter2::parseNode(...)
    #2 ONNXImporter2::parseGraph(...)
    #5 readNetFromONNX2(...)
    #6 readNetFromONNX(...)
0x... is located 0 bytes after 4-byte region allocated by Mat::convertTo in parseQConv

Require the per-channel w_scale to hold one entry per bias element before the loop, the per-axis quantization invariant that pairs one scale and one bias with each output channel, and check the scalar x_scale is non-empty before the read. A well-formed model is unaffected; a mismatched one now throws from the importer and the net loads empty instead of overrunning the buffer.

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

@asmorkalov asmorkalov added category: dnn category: dnn (onnx) ONNX suport issues in DNN module labels Oct 19, 2026
@asmorkalov asmorkalov added this to the 5.1 milestone Oct 19, 2026
@asmorkalov asmorkalov self-assigned this Oct 19, 2026
@asmorkalov
asmorkalov self-requested a review October 19, 2026 09:49
Comment thread modules/dnn/test/test_onnx_importer.cpp Resolved

@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 per-channel scale/bias length validation is sound, but the separately added empty-x_scale guard is not exercised by the new regression. Please add focused coverage for that malformed input 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]

Comment thread modules/dnn/src/onnx/onnx_importer2.cpp Resolved

@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-x_scale model and test now exercise the previously uncovered guard, and the companion opencv_extra PR contains both malformed inputs and their generators. No code issue remains, but this head conflicts with current 5.x and has no CI; please rebase and rerun the importer tests.

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

@uwezkhan Could you rebase the PR and fix conflicts. Some other DNN RP has been merged before.

@asmorkalov
asmorkalov force-pushed the onnx-qconv-scale-bias-bound branch from bce31e5 to 47eb3a1 Compare October 21, 2026 06:52
@asmorkalov
asmorkalov requested a balanced review from Copilot October 21, 2026 06:54

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

@asmorkalov
asmorkalov merged commit 97a5d7e into opencv:5.x Oct 21, 2026
13 of 29 checks passed
@uwezkhan

Copy link
Copy Markdown
Contributor Author

Thanks for merging this, and for the rebase nudge.

asmorkalov pushed a commit that referenced this pull request Oct 21, 2026
dnn: faster ConvTranspose2 on CPU - #30396

Merge pull request #30395 from uwezkhan:onnx-qconv-scale-bias-bound

- 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.
- Attention accepts [..., S, C] and treats the leading dims as batch; the SAM3 decoder feeds it a 4D input and failed before.

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

- [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
- [ ] 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
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants