Tencent / ncnn

ncnn is a high-performance neural network inference framework optimized for the mobile platform
Other
20.54k stars 4.18k forks source link

use dll with ncnn build with vulkan crash at exit #2733

Open caishanli opened 3 years ago

caishanli commented 3 years ago

build ncnn with vulkan, and use it in a shared library(tested on windows now), will crash at exit. code is here, yolov4 param is here code tree: ----shared library --------yolov4_vk.h

#pragma once

#ifdef YOLOV4_VK_EXPORT
#define YOLOV4_VK_API __declspec( dllexport )
#else
#define YOLOV4_VK_API __declspec( dllimport )
#endif

void YOLOV4_VK_API detect();

--------yolov4_vk.cpp

#include <net.h>
#include "yolov4_vk.h"

void detect()
{
    ncnn::Net yolov4;
    yolov4.opt.use_vulkan_compute = true;
    yolov4.load_param("yolov4-tiny-opt.param");
    //yolov4.load_model("yolov4-tiny-opt.bin");

    return;
}

----main --------main.cpp

#include "yolov4_vk.h"

int main()
{
    detect();
    return 0;
}

----CMakeLists.txt

cmake_minimum_required(VERSION 3.4)

project(test_vulkan_so)

#find_package(Python3 REQUIRED Interpreter Development)
find_package(ncnn CONFIG REQUIRED)

add_library(yolov4_vk_so SHARED yolov4_vk.cpp)
target_compile_definitions(yolov4_vk_so PRIVATE YOLOV4_VK_EXPORT)
add_executable(yolov4 main.cpp)

target_include_directories(yolov4_vk_so PRIVATE ncnn)
target_link_libraries(yolov4_vk_so PRIVATE ncnn)
target_include_directories(yolov4 PRIVATE)
target_link_libraries(yolov4 PRIVATE yolov4_vk_so)

crash screen shot: QQ截图20210308154036

ncnnnnn commented 3 years ago
{
    ncnn::destroy_gpu_instance();
}
void detect()
{
    ncnn::Net yolov4;
    yolov4.opt.use_vulkan_compute = true;
    yolov4.load_param("yolov4-tiny-opt.param");
    yolov4.load_model("yolov4-tiny-opt.bin");

    return;
}

main:
  detect();
 clearvk();

void detect2()
{
    ncnn::Net *yolov4=new ncnn::Net;
    yolov4->opt.use_vulkan_compute = true;
    yolov4->load_param("yolov4-tiny-opt.param");
    yolov4->load_model("yolov4-tiny-opt.bin");
    delete yolov4;
    ncnn::destroy_gpu_instance();
    return;
}

main:
  detect2();
ncnn -- > dll 
caishanli commented 3 years ago
{
  ncnn::destroy_gpu_instance();
}
void detect()
{
    ncnn::Net yolov4;
    yolov4.opt.use_vulkan_compute = true;
    yolov4.load_param("yolov4-tiny-opt.param");
    yolov4.load_model("yolov4-tiny-opt.bin");

    return;
}

main:
  detect();
 clearvk();

void detect2()
{
    ncnn::Net *yolov4=new ncnn::Net;
    yolov4->opt.use_vulkan_compute = true;
    yolov4->load_param("yolov4-tiny-opt.param");
    yolov4->load_model("yolov4-tiny-opt.bin");
    delete yolov4;
    ncnn::destroy_gpu_instance();
    return;
}

main:
  detect2();
ncnn -- > dll 

Great work! But when binding ncnn to python extension, up says that delete net manually in python is very ugly...

ncnnnnn commented 3 years ago
   yolov4.clear();
    ncnn::destroy_gpu_instance();

在vs2017下运行,看到gpu信息后关闭cmd窗口,仍然会在vkDestroyDevice(d->device, 0); 崩。