ncnn icon indicating copy to clipboard operation
ncnn copied to clipboard

ex.extract时,没有任何报错信息,返回0xC0000005(STATUS_ACCESS_VIOLATION)。input正常。

Open emofalling opened this issue 8 months ago • 5 comments

error log | 日志或报错信息 | ログ

ex.extract时,没有任何报错信息,返回0xC0000005。input正常。

context | 编译/运行环境 | バックグラウンド

平台:Windows 11 专业工作站版 可用设备:Intel Core Ultra 9, Intel Graphics, RTX4070Super Python版本:3.14.2

how to reproduce | 复现步骤 | 再現方法

如下代码:

import os, sys
import ncnn
import pnnx
import numpy as np

# 模型缓存目录
cache_directory = ".cache/ncnn/"

# 模型系列名称
model_name = "rife_v4.25_lite"

# 编译模型时的量化精度。小写。
precision = "fp16"

# ONNX模型文件目录
model_path = f"models/{model_name}/model.onnx"

# NCNN模型文件目录
ncnn_model_dir = f"models/{model_name}/{precision}/ncnn"

# PNNX使用哪个设备进行模型转换
cvt_device = "gpu"

# 设备
device = "GPU.1"

# 前后帧
frame_0_path = f"f0.jpg"
frame_1_path = f"f1.jpg"

# 插帧时间
timestamp = 0.5

# 对齐字节数
alignment_byte = 1

# 编译时模型默认输入尺寸
default_input_width = 1920
default_input_height = 1080

# NCNN特定:是否启用Vulkan
Use_Vulkan = False
# NCNN特定:推理线程数
num_threads = 4

def onnx_to_ncnn():
    """(当ncnn_model_path不存在时调用)将ONNX模型转换为NCNN模型,并自动存储在合适的目录下"""
    if precision == "fp16":
        print("量化到FP16精度")
        Use_FP16 = True
    elif precision == "fp32":
        print("不进行量化,使用原始的FP32精度")
        Use_FP16 = False
    else:
        raise ValueError(f"未知的量化精度 {precision}")
    # 若不存在,则创建模型目录
    os.makedirs(ncnn_model_dir, exist_ok=True)
    # 转换ONNX模型为NCNN模型
    pnnx.convert(
        model_path, 
        input_shapes = [
            [1, 7, default_input_height, default_input_width]
           ], 
        input_types = ["f32"],
        ncnnparam = os.path.join(ncnn_model_dir, "model.param"),
        ncnnbin = os.path.join(ncnn_model_dir, "model.bin"),
        ncnnpy = os.path.join(ncnn_model_dir, "example.py"),
        pnnxbin = os.path.join(ncnn_model_dir, "model.pnnx.bin"),
        pnnxparam = os.path.join(ncnn_model_dir, "model.pnnx.param"),
        pnnxonnx = os.path.join(ncnn_model_dir, "model.pnnx.onnx"),
        pnnxpy = os.path.join(ncnn_model_dir, "example_pnnx.py"),
        device=cvt_device,
        fp16=Use_FP16,
        # optlevel=0, #如果指定了optlevel, 则不会生成ncnn模型
        customop=["rife.Warp"],
    )
    # 检查模型文件是否生成成功
    if not os.path.exists(os.path.join(ncnn_model_dir, "model.bin")) or \
       not os.path.exists(os.path.join(ncnn_model_dir, "model.param")):
        raise RuntimeError("NCNN模型转换失败")


# 主程序
# 检查IR模型文件是否存在
if not os.path.exists(os.path.join(ncnn_model_dir, "model.bin")) or \
   not os.path.exists(os.path.join(ncnn_model_dir, "model.param")):
    print(f"NCNN模型文件 {ncnn_model_dir} 不存在,正在转换ONNX模型...")
    onnx_to_ncnn()
    print(f"ONNX模型已成功转换为NCNN模型,并保存到 {ncnn_model_dir}")
# 列出设备
print("可用设备列表:")
for i in range(ncnn.get_gpu_count()): # type: ignore
    info = ncnn.get_gpu_info(i) # type: ignore
    print(f"""设备 {i}: 
        Device ID: {info.device_id()}
        Device Name: {info.device_name()}
        Driver Version: {info.driver_version()}
        Type: {info.type()}
        Vendor ID: {info.vendor_id()}
    """)
# 加载ONNX模型
print(f"正在加载 {model_name} 模型...")
model = ncnn.Net() # type: ignore
model.opt.use_vulkan_compute = Use_Vulkan
model.opt.num_threads = num_threads

model.opt.use_fp16_storage = False
model.opt.use_fp16_arithmetic = False

model.load_param(os.path.join(ncnn_model_dir, "model.param")) # type: ignore
model.load_model(os.path.join(ncnn_model_dir, "model.bin")) # type: ignore

print(f"{model_name} 模型加载完成")

# 加载图像,分离RGB通道,转换为numpy数组,归一化到[0, 1]
from PIL import Image
frame_0_img = Image.open(frame_0_path)
frame_1_img = Image.open(frame_1_path)
img_width = frame_0_img.width
img_height = frame_0_img.height
img_width_aligned = (img_width + alignment_byte - 1) // alignment_byte * alignment_byte
img_height_aligned = (img_height + alignment_byte - 1) // alignment_byte * alignment_byte
print(f"图像尺寸: {img_width}x{img_height}")
print(f"对齐后图像尺寸: {img_width_aligned}x{img_height_aligned}")
if frame_1_img.size != (img_width, img_height):
    raise ValueError("前后帧图像尺寸不一致")
# 得到[H, W, 3](NHWC)的RGB数组,并归一化到[0, 1]
f0_rgb_arr = np.array(frame_0_img, dtype=np.float32) / 255.0
f1_rgb_arr = np.array(frame_1_img, dtype=np.float32) / 255.0
# 扩容到对齐的尺寸,填充部分用0
if img_width_aligned != img_width or img_height_aligned != img_height:
    f0_rgb_arr_padded = np.zeros((img_height_aligned, img_width_aligned, 3), dtype=np.float32)
    f1_rgb_arr_padded = np.zeros((img_height_aligned, img_width_aligned, 3), dtype=np.float32)
    f0_rgb_arr_padded[:img_height, :img_width, :] = f0_rgb_arr
    f1_rgb_arr_padded[:img_height, :img_width, :] = f1_rgb_arr
else:
    f0_rgb_arr_padded = f0_rgb_arr
    f1_rgb_arr_padded = f1_rgb_arr
# 分离rgb
r_f0 = f0_rgb_arr_padded[:, :, 0]
g_f0 = f0_rgb_arr_padded[:, :, 1]
b_f0 = f0_rgb_arr_padded[:, :, 2]
r_f1 = f1_rgb_arr_padded[:, :, 0]
g_f1 = f1_rgb_arr_padded[:, :, 1]
b_f1 = f1_rgb_arr_padded[:, :, 2]
# 生成输入层 [Batch,7,H,W]
input_data = np.zeros((1, 7, img_height_aligned, img_width_aligned), dtype=np.float32)
# 填充输入数据
input_data[0, 0, :, :] = r_f0
input_data[0, 1, :, :] = g_f0
input_data[0, 2, :, :] = b_f0
input_data[0, 3, :, :] = r_f1
input_data[0, 4, :, :] = g_f1
input_data[0, 5, :, :] = b_f1
# 6不需要填充
# 固化模型维度

def new_frame(t: float, change_img: np.ndarray):


    print("正在进行计算...")

    # 填充t
    input_data[0, 6, :, :] = t
        
    with model.create_extractor() as ex:

        input_mat = ncnn.Mat(input_data).clone() # type: ignore

        # 检查是否为空指针
        assert input_mat.w > 0 and input_mat.h > 0 and input_mat.c > 0, "输入Mat尺寸无效"


        input_names = model.input_names()
        output_names = model.output_names()

        input_name = 'in0'
        output_name = 'out0'
        print(f"模型输入名称: {input_name}, 可用输入名称列表: {input_names}")
        print(f"模型输出名称: {output_name}, 可用输出名称列表: {output_names}")


        print("开始推理")

        ex.input(input_name, input_mat) # type: ignore

        print("开始导出")

        _, output_mat = ex.extract(output_name) # type: ignore

        print("计算完成")

        output_data = np.array(output_mat)



    # 提取出RGB通道
    r_out = output_data[0, 0, :img_height, :img_width]
    g_out = output_data[0, 1, :img_height, :img_width]
    b_out = output_data[0, 2, :img_height, :img_width]

    # 紧凑地将RGB通道合并为图像
    output_img_arr = np.stack((r_out, g_out, b_out), axis=-1)
    # 反归一化到[0, 255]并转换为uint8

    output_img_arr *= 255.0

    # 写入到change_img
    np.copyto(change_img, output_img_arr.astype(np.uint8))

test_frame = np.zeros((img_height, img_width, 3), dtype=np.uint8)
new_frame(timestamp, test_frame)

print("初始化完成,即将进入GUI")

from PySide6.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QHBoxLayout, QWidget, QSlider
from PySide6.QtGui import QPixmap, QImage
from PySide6.QtCore import Qt
from CustomWidgets import CustomUI

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("RIFE Test for Python OpenVINO")
        self.frame = np.zeros((img_height, img_width, 3), dtype=np.uint8)
        new_frame(timestamp, self.frame)
        self.setcontent()
    def setcontent(self):
        # 主类
        central_widget = QWidget()
        self.setCentralWidget(central_widget)
        layout = QVBoxLayout()
        central_widget.setLayout(layout)

        img_layout = QHBoxLayout()
        layout.addLayout(img_layout)

        self.img_0 = CustomUI.GenerelPicViewer(
            np.array(frame_0_img, dtype=np.uint8),
            "前帧",
            QImage.Format.Format_RGB888
        )
        self.img_1 = CustomUI.GenerelPicViewer(
            np.array(frame_1_img, dtype=np.uint8),
            "后帧",
            QImage.Format.Format_RGB888
        )
        self.img_i = CustomUI.GenerelPicViewer(
            self.frame,
            "插帧",
            QImage.Format.Format_RGB888
        )

        img_layout.addWidget(self.img_0)
        img_layout.addWidget(self.img_i)
        img_layout.addWidget(self.img_1)

        # 底部添加一个滑动条
        self.slider = QSlider()
        self.slider.setOrientation(Qt.Orientation.Horizontal) # 水平
        self.slider.setMinimum(0)
        self.slider.setMaximum(100)
        self.slider.setValue(int(timestamp * 100))
        layout.addWidget(self.slider)

        self.slider.valueChanged.connect(self.update_img_i)


    def update_img_i(self, value):
        t = value / 100.0
        new_frame(t, self.frame)
        self.img_i.update_()

app = QApplication(sys.argv)
window = MainWindow()
window.show()

sys.exit(app.exec())

编译过程能够顺利进行。

但是在开始导出时产生错误码0xC0000005(STATUS_ACCESS_VIOLATION)。

运行结果:

PS D:\emofalling\LimitRIFE\LimitRIFE_Env> & "D:/Program Files/Python314/python.exe" d:/emofalling/LimitRIFE/LimitRIFE_Env/rife_demo_ncnn.py
可用设备列表:
[0 NVIDIA GeForce RTX 4070 SUPER]  queueC=2[8]  queueT=1[2]  rebar=1  r-score=80
[0 NVIDIA GeForce RTX 4070 SUPER]  fp16-p/s/u/a=1/1/1/1  int8-p/s/u/a=1/1/1/1  bf16-p/s=1/1
[0 NVIDIA GeForce RTX 4070 SUPER]  subgroup=32(32~32)  ops=1/1/1/1/1/1/1/1/1/1
[0 NVIDIA GeForce RTX 4070 SUPER]  fp16-cm=16x16x16/16x8x16/16x8x8  int8-cm=16x16x32/16x8x32  bf16-cm=16x16x16  fp8-cm=16x16x32
[1 Intel(R) Graphics]  queueC=1[4]  queueT=2[2]  rebar=1  r-score=10
[1 Intel(R) Graphics]  fp16-p/s/u/a=1/1/1/1  int8-p/s/u/a=1/1/1/1  bf16-p/s=1/0
[1 Intel(R) Graphics]  subgroup=32(8~32)  ops=1/1/1/1/1/1/1/1/1/1
[1 Intel(R) Graphics]  fp16-cm=0  int8-cm=0  bf16-cm=0  fp8-cm=0
设备 0:
        Device ID: 10115
        Device Name: NVIDIA GeForce RTX 4070 SUPER
        Driver Version: 2496905216
        Type: 0
        Vendor ID: 4318

设备 1:
        Device ID: 32103
        Device Name: Intel(R) Graphics
        Driver Version: 1663410
        Type: 1
        Vendor ID: 32902

正在加载 rife_v4.25_lite 模型...
rife_v4.25_lite 模型加载完成
图像尺寸: 1920x1080
对齐后图像尺寸: 1920x1080
正在进行计算...
模型输入名称: in0, 可用输入名称列表: ['in0']
模型输出名称: out0, 可用输出名称列表: ['172', 'out0']
开始推理
开始导出
PS D:\emofalling\LimitRIFE\LimitRIFE_Env> $LASTEXITCODE                                                                                           
-1073741819

more | 其他 | その他

源ONNX模型:rife_v4.25_lite.7z下的rife_v2\rife_v4.25_lite.onnx

我尝试使用了一个成功案例现成的ncnn模型 VapourSynth-RIFE-ncnn-Vulkan/tree/master/models/rife-v4.25-lite_ensembleFalse

问题仍旧。

输出:

PS D:\emofalling\LimitRIFE\LimitRIFE_Env> & "D:/Program Files/Python314/python.exe" d:/emofalling/LimitRIFE/LimitRIFE_Env/rife_demo_ncnn.py
可用设备列表:
[0 NVIDIA GeForce RTX 4070 SUPER]  queueC=2[8]  queueT=1[2]  rebar=1  r-score=80
[0 NVIDIA GeForce RTX 4070 SUPER]  fp16-p/s/u/a=1/1/1/1  int8-p/s/u/a=1/1/1/1  bf16-p/s=1/1
[0 NVIDIA GeForce RTX 4070 SUPER]  subgroup=32(32~32)  ops=1/1/1/1/1/1/1/1/1/1
[0 NVIDIA GeForce RTX 4070 SUPER]  fp16-cm=16x16x16/16x8x16/16x8x8  int8-cm=16x16x32/16x8x32  bf16-cm=16x16x16  fp8-cm=16x16x32
[1 Intel(R) Graphics]  queueC=1[4]  queueT=2[2]  rebar=1  r-score=10
[1 Intel(R) Graphics]  fp16-p/s/u/a=1/1/1/1  int8-p/s/u/a=1/1/1/1  bf16-p/s=1/0
[1 Intel(R) Graphics]  subgroup=32(8~32)  ops=1/1/1/1/1/1/1/1/1/1
[1 Intel(R) Graphics]  fp16-cm=0  int8-cm=0  bf16-cm=0  fp8-cm=0
设备 0:
        Device ID: 10115
        Device Name: NVIDIA GeForce RTX 4070 SUPER
        Driver Version: 2496905216
        Type: 0
        Vendor ID: 4318

设备 1:
        Device ID: 32103
        Device Name: Intel(R) Graphics
        Driver Version: 1663410
        Type: 1
        Vendor ID: 32902

正在加载 rife_v4.25_lite 模型...
layer rife.Warp not exists or registered
network graph not ready
rife_v4.25_lite 模型加载完成
图像尺寸: 1920x1080
对齐后图像尺寸: 1920x1080
正在进行计算...
模型输入名称: in0, 可用输入名称列表: []
模型输出名称: out0, 可用输出名称列表: []
开始推理
find_blob_index_by_name in0 failed
Try
开始导出
find_blob_index_by_name out0 failed
Try
计算完成
PS D:\emofalling\LimitRIFE\LimitRIFE_Env> $LASTEXITCODE
-1073741819

于是我自己尝试编译NCNN并自行加载,NCNN里面的所有example都通过了,但我的程序仍然会返回这个错误码,问题仍未解决。 经过Visual Studio的调试,找到了函数点

int ncnn::Reshape_x86_fma::forward(const std::vector<Mat>& bottom_blobs, std::vector<Mat>& top_blobs, const Option& opt) const

emofalling avatar Mar 30 '26 06:03 emofalling

Hi @emofalling! I'm Dosu and I’m helping the ncnn team.

这个 0xC0000005 (STATUS_ACCESS_VIOLATION) 崩溃很可能由以下几个原因导致,建议逐一排查:

1. 4D numpy 数组的 batch 维度问题(最可能的原因)

你的 input_data shape 是 (1, 7, H, W),这是一个 4D 数组。ncnn.Mat 接受 4D 数组时,会把第一个维度映射到 ncnn 内部的 channel 维度,而不是真正的 batch 维度 [1]。ncnn 不支持 batch 推理,应该去掉 batch 维度后传入:

# 错误:4D (1, 7, H, W)
input_mat = ncnn.Mat(input_data).clone()

# 正确:去掉 batch 维,传入 3D (7, H, W)
input_mat = ncnn.Mat(input_data[0]).clone()

2. rife.Warp 自定义算子未注册

检查生成的 model.param 文件中是否包含 rife.Warp 层。如果 ncnn 运行时没有注册该自定义算子,推理时会静默崩溃而不报错 [2]。你在 pnnx 转换时已经指定了 customop=[&quot;rife.Warp&quot;],但需要确认 param 文件中该层是否被正确转换为了 ncnn 内置算子,或者是否仍需要在 Python 端注册。

3. numpy 数组生命周期问题

ncnn.Mat 默认是引用 numpy 数组的内存,不做拷贝 [3]。虽然你调用了 .clone(),但建议确保 input_data 在整个推理期间不被 GC 回收。

4. Python 3.14 兼容性

你使用的是 Python 3.14.2。ncnn 的 pybind11 绑定曾在 Python 3.14 上存在崩溃问题,已通过 PR #6396 修复(升级到 pybind11 3.0.1)[4]。请确认你使用的 ncnn 版本包含了该修复。

5. Windows 上 OpenMP 线程问题

在 Windows 上,建议在 import ncnn 之前设置 [5]:

import os
os.environ["OMP_WAIT_POLICY"] = "passive"

建议先从第1点开始排查——将 input_data[0](去掉 batch 维)传给 ncnn.Mat,这是最可能的原因。

To reply, just mention @dosu.


Share context across your team and agents. Try Dosu.

Leave Feedback Learn about ncnn with Dosu Add Dosu to your team

dosubot[bot] avatar Mar 30 '26 06:03 dosubot[bot]

经检查,发现编译后的model.param模型中不存在rife.Warp(它理应有的啊)

7767517
155 191
Input                    in0                      0 1 in0
Padding                  pad_125                  1 1 in0 1 0=0 1=72 2=0 3=0 4=2 5=0 6=0
Slice                    split_0                  1 2 1 2 3 -23300=2,6,1 1=0
Split                    splitncnn_1              1 5 3 4 5 6 7 8
Split                    splitncnn_0              1 2 2 9 10
Reshape                  reshape_133              1 1 10 11 0=1920 1=1152 11=3 2=2
Split                    splitncnn_2              1 3 11 12 13 14
Convolution              convrelu_0               1 1 14 15 0=16 1=3 -23310=1,0.2 11=3 12=1 13=2 14=1 2=1 3=2 4=1 5=1 6=432 9=2
Convolution              convrelu_1               1 1 15 16 0=16 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=2304 9=2
Convolution              convrelu_2               1 1 16 17 0=16 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=2304 9=2
Deconvolution            deconv_58                1 1 17 18 0=4 1=4 11=4 12=1 13=2 14=1 18=0 19=0 2=1 3=2 4=1 5=1 6=1024
Split                    splitncnn_3              1 2 18 19 20
Reshape                  reshape_134              1 1 20 21 0=1920 1=1152 2=8
Concat                   cat_0                    2 1 12 19 22 0=0
Split                    splitncnn_4              1 4 22 23 24 25 26
Concat                   cat_1                    3 1 9 21 4 27 0=0
Interp                   interpolate_64           1 1 27 28 0=2 1=0.03125 2=0.03125 6=0
Convolution              convrelu_3               1 1 28 29 0=96 1=3 -23310=1,0.2 11=3 12=1 13=2 14=1 2=1 3=2 4=1 5=1 6=12960 9=2
Convolution              convrelu_4               1 1 29 30 0=192 1=3 -23310=1,0.2 11=3 12=1 13=2 14=1 2=1 3=2 4=1 5=1 6=165888 9=2
Convolution              convrelu_5               1 1 30 31 0=192 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=331776 9=2
Convolution              convrelu_6               1 1 31 32 0=192 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=331776 9=2
Convolution              convrelu_7               1 1 32 33 0=192 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=331776 9=2
Convolution              convrelu_8               1 1 33 34 0=192 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=331776 9=2
Convolution              convrelu_9               1 1 34 35 0=192 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=331776 9=2
Convolution              convrelu_10              1 1 35 36 0=192 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=331776 9=2
Convolution              convrelu_11              1 1 36 37 0=192 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=331776 9=2
Convolution              convrelu_12              1 1 37 38 0=192 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=331776 9=2
Deconvolution            deconv_59                1 1 38 39 0=52 1=4 11=4 12=1 13=2 14=1 18=0 19=0 2=1 3=2 4=1 5=1 6=159744
PixelShuffle             pixel_shuffle_127        1 1 39 40 0=2 1=0
Interp                   interpolate_65           1 1 40 41 0=2 1=32.0 2=32.0 6=0
Slice                    split_1                  1 2 41 42 43 -23300=2,4,9 1=0
BinaryOp                 mul_0                    1 1 42 44 0=2 1=1 2=32.0
Split                    splitncnn_5              1 3 44 45 46 47
Reshape                  reshape_135              1 1 47 48 0=1920 1=1152 11=2 2=2
MemoryData               /Reshape_6_output_0      0 1 49 0=1 1=1 2=2
Split                    splitncnn_0              1 5 49 50 51 52 53 54
MemoryData               /Concat_8_output_0       0 1 55 0=1920 1=1152 2=2
Split                    splitncnn_1              1 5 55 56 57 58 59 60
Reshape                  reshape_136              1 1 50 61 0=1 1=1 11=2 2=1
BinaryOp                 mul_1                    2 1 48 61 62 0=2
Reshape                  reshape_137              1 1 56 63 0=1920 1=1152 11=2 2=1
BinaryOp                 add_2                    2 1 63 62 64 0=0
GridSample               permutegridsample_0      2 1 26 64 65 0=1 1=2 2=1 3=1
Reshape                  reshape_138              1 1 65 66 0=1920 1=1152 2=14
BinaryOp                 mul_3                    1 1 45 67 0=2 1=1 2=0.0625
Concat                   cat_2                    4 1 66 5 43 67 68 0=0
Interp                   interpolate_66           1 1 68 69 0=2 1=0.0625 2=0.0625 6=0
Convolution              convrelu_13              1 1 69 70 0=64 1=3 -23310=1,0.2 11=3 12=1 13=2 14=1 2=1 3=2 4=1 5=1 6=16128 9=2
Convolution              convrelu_14              1 1 70 71 0=128 1=3 -23310=1,0.2 11=3 12=1 13=2 14=1 2=1 3=2 4=1 5=1 6=73728 9=2
Convolution              convrelu_15              1 1 71 72 0=128 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=147456 9=2
Convolution              convrelu_16              1 1 72 73 0=128 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=147456 9=2
Convolution              convrelu_17              1 1 73 74 0=128 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=147456 9=2
Convolution              convrelu_18              1 1 74 75 0=128 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=147456 9=2
Convolution              convrelu_19              1 1 75 76 0=128 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=147456 9=2
Convolution              convrelu_20              1 1 76 77 0=128 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=147456 9=2
Convolution              convrelu_21              1 1 77 78 0=128 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=147456 9=2
Convolution              convrelu_22              1 1 78 79 0=128 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=147456 9=2
Deconvolution            deconv_60                1 1 79 80 0=52 1=4 11=4 12=1 13=2 14=1 18=0 19=0 2=1 3=2 4=1 5=1 6=106496
PixelShuffle             pixel_shuffle_128        1 1 80 81 0=2 1=0
Interp                   interpolate_67           1 1 81 82 0=2 1=16.0 2=16.0 6=0
Slice                    split_2                  1 2 82 83 84 -23300=2,4,9 1=0
BinaryOp                 mul_4                    1 1 83 85 0=2 1=1 2=16.0
BinaryOp                 add_5                    2 1 46 85 86 0=0
Split                    splitncnn_6              1 3 86 87 88 89
Reshape                  reshape_139              1 1 89 90 0=1920 1=1152 11=2 2=2
Reshape                  reshape_140              1 1 54 91 0=1 1=1 11=2 2=1
BinaryOp                 mul_6                    2 1 90 91 92 0=2
Reshape                  reshape_141              1 1 60 93 0=1920 1=1152 11=2 2=1
BinaryOp                 add_7                    2 1 93 92 94 0=0
GridSample               permutegridsample_1      2 1 25 94 95 0=1 1=2 2=1 3=1
Reshape                  reshape_142              1 1 95 96 0=1920 1=1152 2=14
BinaryOp                 mul_8                    1 1 87 97 0=2 1=1 2=0.125
Concat                   cat_3                    4 1 96 6 84 97 98 0=0
Interp                   interpolate_68           1 1 98 99 0=2 1=0.125 2=0.125 6=0
Convolution              convrelu_23              1 1 99 100 0=48 1=3 -23310=1,0.2 11=3 12=1 13=2 14=1 2=1 3=2 4=1 5=1 6=12096 9=2
Convolution              convrelu_24              1 1 100 101 0=96 1=3 -23310=1,0.2 11=3 12=1 13=2 14=1 2=1 3=2 4=1 5=1 6=41472 9=2
Convolution              convrelu_25              1 1 101 102 0=96 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=82944 9=2
Convolution              convrelu_26              1 1 102 103 0=96 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=82944 9=2
Convolution              convrelu_27              1 1 103 104 0=96 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=82944 9=2
Convolution              convrelu_28              1 1 104 105 0=96 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=82944 9=2
Convolution              convrelu_29              1 1 105 106 0=96 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=82944 9=2
Convolution              convrelu_30              1 1 106 107 0=96 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=82944 9=2
Convolution              convrelu_31              1 1 107 108 0=96 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=82944 9=2
Convolution              convrelu_32              1 1 108 109 0=96 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=82944 9=2
Deconvolution            deconv_61                1 1 109 110 0=52 1=4 11=4 12=1 13=2 14=1 18=0 19=0 2=1 3=2 4=1 5=1 6=79872
PixelShuffle             pixel_shuffle_129        1 1 110 111 0=2 1=0
Interp                   interpolate_69           1 1 111 112 0=2 1=8.0 2=8.0 6=0
Slice                    split_3                  1 2 112 113 114 -23300=2,4,9 1=0
BinaryOp                 mul_9                    1 1 113 115 0=2 1=1 2=8.0
BinaryOp                 add_10                   2 1 88 115 116 0=0
Split                    splitncnn_7              1 3 116 117 118 119
Reshape                  reshape_143              1 1 119 120 0=1920 1=1152 11=2 2=2
Reshape                  reshape_144              1 1 53 121 0=1 1=1 11=2 2=1
BinaryOp                 mul_11                   2 1 120 121 122 0=2
Reshape                  reshape_145              1 1 59 123 0=1920 1=1152 11=2 2=1
BinaryOp                 add_12                   2 1 123 122 124 0=0
GridSample               permutegridsample_2      2 1 24 124 125 0=1 1=2 2=1 3=1
Reshape                  reshape_146              1 1 125 126 0=1920 1=1152 2=14
BinaryOp                 mul_13                   1 1 117 127 0=2 1=1 2=0.25
Concat                   cat_4                    4 1 126 7 114 127 128 0=0
Interp                   interpolate_70           1 1 128 129 0=2 1=0.25 2=0.25 6=0
Convolution              convrelu_33              1 1 129 130 0=32 1=3 -23310=1,0.2 11=3 12=1 13=2 14=1 2=1 3=2 4=1 5=1 6=8064 9=2
Convolution              convrelu_34              1 1 130 131 0=64 1=3 -23310=1,0.2 11=3 12=1 13=2 14=1 2=1 3=2 4=1 5=1 6=18432 9=2
Convolution              convrelu_35              1 1 131 132 0=64 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=36864 9=2
Convolution              convrelu_36              1 1 132 133 0=64 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=36864 9=2
Convolution              convrelu_37              1 1 133 134 0=64 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=36864 9=2
Convolution              convrelu_38              1 1 134 135 0=64 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=36864 9=2
Convolution              convrelu_39              1 1 135 136 0=64 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=36864 9=2
Convolution              convrelu_40              1 1 136 137 0=64 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=36864 9=2
Convolution              convrelu_41              1 1 137 138 0=64 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=36864 9=2
Convolution              convrelu_42              1 1 138 139 0=64 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=36864 9=2
Deconvolution            deconv_62                1 1 139 140 0=52 1=4 11=4 12=1 13=2 14=1 18=0 19=0 2=1 3=2 4=1 5=1 6=53248
PixelShuffle             pixel_shuffle_130        1 1 140 141 0=2 1=0
Interp                   interpolate_71           1 1 141 142 0=2 1=4.0 2=4.0 6=0
Slice                    split_4                  1 2 142 143 144 -23300=2,4,9 1=0
BinaryOp                 mul_14                   1 1 143 145 0=2 1=1 2=4.0
BinaryOp                 add_15                   2 1 118 145 146 0=0
Split                    splitncnn_8              1 3 146 147 148 149
Reshape                  reshape_147              1 1 149 150 0=1920 1=1152 11=2 2=2
Reshape                  reshape_148              1 1 52 151 0=1 1=1 11=2 2=1
BinaryOp                 mul_16                   2 1 150 151 152 0=2
Reshape                  reshape_149              1 1 58 153 0=1920 1=1152 11=2 2=1
BinaryOp                 add_17                   2 1 153 152 154 0=0
GridSample               permutegridsample_3      2 1 23 154 155 0=1 1=2 2=1 3=1
Reshape                  reshape_150              1 1 155 156 0=1920 1=1152 2=14
Concat                   cat_5                    4 1 156 8 144 147 157 0=0
Convolution              convrelu_43              1 1 157 158 0=12 1=3 -23310=1,0.2 11=3 12=1 13=2 14=1 2=1 3=2 4=1 5=1 6=3024 9=2
Convolution              convrelu_44              1 1 158 159 0=24 1=3 -23310=1,0.2 11=3 12=1 13=2 14=1 2=1 3=2 4=1 5=1 6=2592 9=2
Convolution              convrelu_45              1 1 159 160 0=24 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=5184 9=2
Convolution              convrelu_46              1 1 160 161 0=24 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=5184 9=2
Convolution              convrelu_47              1 1 161 162 0=24 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=5184 9=2
Convolution              convrelu_48              1 1 162 163 0=24 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=5184 9=2
Convolution              convrelu_49              1 1 163 164 0=24 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=5184 9=2
Convolution              convrelu_50              1 1 164 165 0=24 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=5184 9=2
Convolution              convrelu_51              1 1 165 166 0=24 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=5184 9=2
Convolution              convrelu_52              1 1 166 167 0=24 1=3 -23310=1,0.2 11=3 12=1 13=1 14=1 2=1 3=1 4=1 5=1 6=5184 9=2
Deconvolution            deconv_63                1 1 167 168 0=52 1=4 11=4 12=1 13=2 14=1 18=0 19=0 2=1 3=2 4=1 5=1 6=19968
PixelShuffle             pixel_shuffle_131        1 1 168 169 0=2 1=0
Slice                    split_5                  1 3 169 170 171 172 -23300=3,4,1,8 1=0
BinaryOp                 add_18                   2 1 148 170 173 0=0
Reshape                  reshape_151              1 1 173 174 0=1920 1=1152 11=2 2=2
Reshape                  reshape_152              1 1 51 175 0=1 1=1 11=2 2=1
BinaryOp                 mul_19                   2 1 174 175 176 0=2
Reshape                  reshape_153              1 1 57 177 0=1920 1=1152 11=2 2=1
BinaryOp                 add_20                   2 1 177 176 178 0=0
GridSample               permutegridsample_4      2 1 13 178 179 0=1 1=2 2=1 3=1
Sigmoid                  sigmoid_132              1 1 171 180
Split                    splitncnn_9              1 2 180 181 182
Reshape                  reshape_154              1 1 179 183 0=1920 1=1152 2=6
Slice                    split_6                  1 2 183 184 185 -23300=2,3,3 1=0
BinaryOp                 sub_21                   1 1 181 186 0=7 1=1 2=1.0
BinaryOp                 mul_22                   2 1 185 186 187 0=2
BinaryOp                 mul_23                   2 1 184 182 188 0=2
BinaryOp                 add_24                   2 1 188 187 189 0=0
Crop                     pad_126                  1 1 189 out0 -23310=2,-72,0 -23311=2,-2,-1 -23309=2,0,0

我尝试修改优化参数,但是当optlevel!=2时,不会生成ncnn模型。

同时,@dosu提供的建议对解决问题均无效

emofalling avatar Mar 30 '26 06:03 emofalling

确认了你的 param 文件中确实不存在 rife.Warp——pnnx 已经把它分解成了 GridSample(permutegridsample_0 到 permutegridsample_4)等 ncnn 内置算子,这是正常的。所以自定义算子不是崩溃的原因。

崩溃的最可能原因仍然是 4D numpy 数组的维度映射问题。 你的代码传入了 shape 为 (1, 7, H, W) 的 4D 数组,ncnn.Mat 会将其映射为 w=W, h=H, d=7, c=1(4维 Mat),而模型实际期望的是 3D Mat w=W, h=H, c=7 [1]。这导致后续的 GridSample 层使用错误的步长计算偏移量,而 x86 SIMD 路径只检查偏移 &gt;= 0 不检查上界 [2],从而产生越界内存访问 → 0xC0000005。

修复方法——去掉 batch 维度:

# 错误:(1, 7, H, W) → ncnn 解释为 w=W, h=H, d=7, c=1
input_mat = ncnn.Mat(input_data).clone()

# 正确:(7, H, W) → ncnn 解释为 w=W, h=H, c=7
input_mat = ncnn.Mat(input_data[0]).clone()

另外提醒一下,param 文件中多个 Reshape 层(如 reshape_133)硬编码了 0=1920 1=1152,这意味着模型只能在 pnnx 转换时的分辨率(padding 后 1920×1152)下工作。如果将来需要其他分辨率,需要重新用 pnnx 转换或手动修改 param 中的 Reshape 参数。

To reply, just mention @dosu.


Docs are dead. Just use Dosu.

Leave Feedback Ask Dosu about ncnn Share Dosu with your team

dosubot[bot] avatar Mar 30 '26 06:03 dosubot[bot]

@dosu 我得到了报错的详细信息代码: lr_ncnn.dll!ncnn::Reshape_x86_fma::forward$omp$3() 行 575 C++

            #pragma omp parallel for num_threads(opt.num_threads)
            for (int q = 0; q < top_blob.c; q++)
            {
                const float* ptr0 = (const float*)bottom_blob_flattened + size * q * 8;
                const float* ptr1 = (const float*)bottom_blob_flattened + size * (q * 8 + 1);
                const float* ptr2 = (const float*)bottom_blob_flattened + size * (q * 8 + 2);
                const float* ptr3 = (const float*)bottom_blob_flattened + size * (q * 8 + 3);
                const float* ptr4 = (const float*)bottom_blob_flattened + size * (q * 8 + 4);
                const float* ptr5 = (const float*)bottom_blob_flattened + size * (q * 8 + 5);
                const float* ptr6 = (const float*)bottom_blob_flattened + size * (q * 8 + 6);
                const float* ptr7 = (const float*)bottom_blob_flattened + size * (q * 8 + 7);
                float* outptr = top_blob.channel(q);

                int i = 0;
                for (; i + 7 < size; i += 8)
                {
                    __m256 _row0 = _mm256_loadu_ps(ptr0);
                    __m256 _row1 = _mm256_loadu_ps(ptr1);
                    __m256 _row2 = _mm256_loadu_ps(ptr2);
                    __m256 _row3 = _mm256_loadu_ps(ptr3);
                    __m256 _row4 = _mm256_loadu_ps(ptr4);
                    __m256 _row5 = _mm256_loadu_ps(ptr5);
                    __m256 _row6 = _mm256_loadu_ps(ptr6);
                    __m256 _row7 = _mm256_loadu_ps(ptr7); *报错点

                    transpose8x8_ps(_row0, _row1, _row2, _row3, _row4, _row5, _row6, _row7);

                    _mm256_storeu_ps(outptr, _row0);
                    _mm256_storeu_ps(outptr + 8, _row1);
                    _mm256_storeu_ps(outptr + 16, _row2);
                    _mm256_storeu_ps(outptr + 24, _row3);
                    _mm256_storeu_ps(outptr + 32, _row4);
                    _mm256_storeu_ps(outptr + 40, _row5);
                    _mm256_storeu_ps(outptr + 48, _row6);
                    _mm256_storeu_ps(outptr + 56, _row7);

后面禁用了FMA,禁用了AVX2,只要涉及到扩展指令集都没法避免。 我不希望禁用扩展指令集运行。

emofalling avatar Apr 03 '26 15:04 emofalling

感谢你提供了详细的崩溃堆栈!这个崩溃点进一步确认了之前的分析——问题不在扩展指令集本身,而是输入数据的维度映射错误导致越界访问。

崩溃发生在 Reshape 层的 elempack=8 路径中,ptr7 = bottom_blob_flattened + size * (q * 8 + 7) 越界了 [1]。原因是:

你传入的 (1, 7, H, W) 4D numpy 数组被 ncnn.Mat 映射为 w=1920, h=1080, d=7, c=1(dims=4)[2],而模型期望的是 w=1920, h=1080, c=7(dims=3)。当 Reshape 层 flatten() 这个 4D Mat 时,它只遍历 c=1 个通道 [3],但后续的 elempack=8 重排代码却尝试按 8 通道一组来读取数据——第 8 个指针 ptr7 直接越界,触发 0xC0000005。

禁用 FMA/AVX2 无法解决问题,因为 SSE 和标量路径同样会执行类似的通道重排逻辑,只是崩溃点不同。根本原因是输入维度不匹配,不是指令集问题。

修复方法非常简单——去掉 batch 维度:

# 将 (1, 7, H, W) 变成 (7, H, W)
input_mat = ncnn.Mat(input_data[0]).clone()

这样 ncnn.Mat 会正确映射为 w=1920, h=1080, c=7, dims=3,与模型期望一致,Reshape 层的 elempack 逻辑也能正确工作,不需要禁用任何扩展指令集。

To reply, just mention @dosu.


Share context across your team and agents. Try Dosu.

Leave Feedback Learn about ncnn with Dosu Add Dosu to your team

dosubot[bot] avatar Apr 03 '26 16:04 dosubot[bot]