Skip to content

fix: avoid SIGFPE in fwrite_weight_data when Mat is empty#6726

Merged
nihui merged 5 commits into
Tencent:masterfrom
lx-99-lyt:fix/ncnn2int8-sigfpe-empty-mat
May 22, 2026
Merged

fix: avoid SIGFPE in fwrite_weight_data when Mat is empty#6726
nihui merged 5 commits into
Tencent:masterfrom
lx-99-lyt:fix/ncnn2int8-sigfpe-empty-mat

Conversation

@lx-99-lyt
Copy link
Copy Markdown
Contributor

Problem

When running ncnn2int8 to quantize YOLOv8 models, the tool crashes with SIGFPE (Arithmetic exception) during the save() phase.

The crash occurs in ModelWriter::fwrite_weight_data() in tools/modelwriter.h:

ncnn::Mat data_flattened = data.reshape(data.w * data.h * data.d * data.c);

When data is an empty Mat (e.g. for certain layers in YOLOv8 detection head), data.w * data.h * data.d * data.c evaluates to 0, causing a division by zero inside Mat::reshape().

Fix

Add a null check before calling reshape:

if (!data.data || data.w * data.h * data.d * data.c == 0) return 0;

Reproduction

  1. Convert YOLOv8n to ncnn format
  2. Run ncnn2int8 with a calibration table
  3. Tool crashes with SIGFPE after quantizing all convolution layers

@github-actions github-actions Bot added the tool label May 16, 2026
@tencent-adm
Copy link
Copy Markdown
Member

tencent-adm commented May 16, 2026

CLA assistant check
Thank you for your submission, we really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ lx-99-lyt
❌ nihui
You have signed the CLA already but the status is still pending? Let us recheck it.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

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 Dequantize layer serialization (params + scale/bias weights).
  • Adjust Scale serialization to skip writing weights for the special scale_data_size == -233 (multi-input) mode.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread tools/modelwriter.h
Comment thread tools/modelwriter.h Outdated
nihui and others added 2 commits May 22, 2026 17:06
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@nihui
Copy link
Copy Markdown
Member

nihui commented May 22, 2026

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 fwrite_weight_data() silently skip empty Mats. This keeps optional weights aligned with each layer’s load_model() logic, while still
preserving errors for required missing weights.

Thanks again for the contribution.

@nihui nihui merged commit cc16734 into Tencent:master May 22, 2026
21 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants