PriorBox aborts with an integer divide by zero during Vulkan weight upload when aspect_ratios is absent
Environment
- ncnn master
55aa7e0(2026-11-09), built from source withNCNN_VULKAN=ON,RelWithDebInfo - Windows 11, Visual Studio 2026 (19.50), Vulkan SDK 1.4.363.0
- NVIDIA RTX 3060 Laptop GPU (driver reports fp16/int8/bf16 support), no Mesa involved
- Also reproduced through a Rust binding, but the reproducer below links ncnn directly and uses no other project
Steps to reproduce
A network containing a PriorBox layer that sets min_sizes but omits aspect_ratios, loaded with Vulkan enabled:
#include "net.h"
#include "datareader.h"
static const char* PARAM = "7767517\n"
"3 3\n"
"Input a 0 1 a\n"
"Input b 0 1 b\n"
"PriorBox box 2 1 a b output -23300=1,16 9=8 10=8\n";
int main()
{
ncnn::Net net;
net.opt.num_threads = 1;
net.opt.use_vulkan_compute = true;
net.set_vulkan_device(0);
net.load_param_mem(PARAM); // returns 0
const unsigned char* empty = 0; // PriorBox has no weights
ncnn::DataReaderFromMemory dr(empty);
int ret = net.load_model(dr); // aborts here
printf("load_model returned %d\n", ret);
}
Build (MSVC): cl /EHsc /O2 /MD /DNCNN_VULKAN=1 /I include\ncnn repro.cpp /link ncnn.lib ...
Expected
The network loads and reports load_model returned 0, as it does with use_vulkan_compute = false for the same param text.
Actual
The process terminates with 0xC0000094 (integer divide by zero) inside load_model, before it returns:
ncnn::Mat::reshape+0x2bb
ncnn::VkTransfer::record_upload+0x1b7
ncnn::PriorBox_vulkan::upload_model+0x7f
ncnn::Net::load_model+0x2eb
Cause
src/layer/vulkan/priorbox_vulkan.cpp, PriorBox_vulkan::upload_model:
cmd.record_upload(min_sizes, min_sizes_gpu, opt);
if (max_sizes.w > 0)
cmd.record_upload(max_sizes, max_sizes_gpu, opt);
cmd.record_upload(aspect_ratios, aspect_ratios_gpu, opt);
max_sizes is guarded against being empty, but min_sizes and aspect_ratios are not. When a
layer instance leaves aspect_ratios empty, record_upload packs the empty Mat, and the
Mat::reshape inside the transfer divides by the zero element count.
Suggested fix
Guard the two remaining uploads the same way as max_sizes:
if (min_sizes.w > 0)
cmd.record_upload(min_sizes, min_sizes_gpu, opt);
if (max_sizes.w > 0)
cmd.record_upload(max_sizes, max_sizes_gpu, opt);
if (aspect_ratios.w > 0)
cmd.record_upload(aspect_ratios, aspect_ratios_gpu, opt);
Workaround
Always specify aspect_ratios (key 2) when a PriorBox layer takes part in a Vulkan network.
Root cause is one level below upload_model: VkTransfer::record_upload has no empty-input check.
An array param that is not set in the param file is a default-constructed Mat, with elemsize = 0 and elempack = 0. Mat::reshape() then passes the w*h*d*c != _w check (0 != 0) and evaluates alignSize(_w * elemsize, 16) / elemsize, i.e. 0 / 0. Note that an explicit 2=0 empty array does not crash, only a completely absent key does.
Reproduced on master 380f0cd, Windows 11, RTX 3060 Laptop, Vulkan SDK 1.4.363.0, RelWithDebInfo.
#7265 guards all three uploads in PriorBox_vulkan::upload_model. record_pipeline already binds the dummy buffer for empty weights, so no other change is needed.
| case | CPU | Vulkan before | Vulkan after |
|---|---|---|---|
aspect_ratios absent, load_model |
0 | 0xC0000094 |
0 |
aspect_ratios absent, 4x4 input, fp32 |
128 values | - | 128 values, bit identical to CPU |
aspect_ratios present, 4x4 input, fp32 |
768 values | ok, max rel 1e-7 | unchanged |
With the default fp16 storage the absent case differs from CPU by 4.9e-05, on the four variance constants only. That is fp16 rounding, not a correctness issue.