fix: avoid SIGFPE in fwrite_weight_data when Mat is empty#6726
Conversation
|
|
There was a problem hiding this comment.
Pull request overview
This PR updates the model serialization logic used by ncnn tools (e.g., ncnn2int8) to prevent crashes during save() when optional weight tensors (bias/affine/etc.) are empty, and adds writer support for the Dequantize layer type.
Changes:
- Guard several
fwrite_weight_data()calls so optional parameters (e.g.,bias_term,affine,bias_data_size) only write weights when enabled/present. - Add
Dequantizelayer serialization (params + scale/bias weights). - Adjust
Scaleserialization to skip writing weights for the specialscale_data_size == -233(multi-input) mode.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
Thanks for tracking this down and sending the fix. I’ve updated the patch to handle this at the ModelWriter call sites instead of making Thanks again for the contribution. |
Problem
When running
ncnn2int8to quantize YOLOv8 models, the tool crashes withSIGFPE(Arithmetic exception) during thesave()phase.The crash occurs in
ModelWriter::fwrite_weight_data()intools/modelwriter.h:When
datais an empty Mat (e.g. for certain layers in YOLOv8 detection head),data.w * data.h * data.d * data.cevaluates to 0, causing a division by zero insideMat::reshape().Fix
Add a null check before calling reshape:
Reproduction
ncnn2int8with a calibration table