Skip to content

dnn: add QLinearConv models for onnx parseQConv bias fold tests - #1445

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

asmorkalov merged 1 commit into
opencv:5.xfrom
uwezkhan:onnx-qconv-scale-bias-bound

Conversation

@uwezkhan

Copy link
Copy Markdown
Contributor

Test data for opencv/opencv#30395.

Adds two load-only QLinearConv models and their generator in generate_onnx_models.py. Both carry constant x_scale, w_scale and int32 B, so the importer takes the path that folds the bias to float. No reference input/output data.

  • qconv_bias_scale_mismatch.onnx: per-channel w_scale with 64 entries, B with 1. Before: the fold loop runs over w_scale and writes past the bias buffer (ASan heap-buffer-overflow). After: the importer rejects it and the net loads empty.
  • qconv_empty_x_scale.onnx: x_scale with zero elements. Before: x_scale[0] is read from an empty tensor (segfault). After: rejected, net loads empty.

Both pass onnx.checker.check_model, since it does not check that the scale and bias lengths agree.

asmorkalov pushed a commit to opencv/opencv that referenced this pull request Oct 21, 2026
dnn: bound per-channel w_scale to bias length in onnx parseQConv 🤖🤖🤖 - #30395

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

- [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
@asmorkalov
asmorkalov merged commit 92bf816 into opencv:5.x Oct 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants