【问题标题】:Why do my ONNXRuntime Inference crash on GPU without any log?为什么我的 ONNXRuntime Inference 在没有任何日志的情况下在 GPU 上崩溃?
【发布时间】:2022-11-16 09:41:26
【问题描述】:

我正在尝试在 C# 中运行一个 ONNX 模型,该模型是在 Python 中使用 pytorch 创建的,用于图像分割。当我在 CPU 上运行它时一切正常,但是当我尝试使用 GPU 时我的应用程序在尝试运行推理时崩溃。 (使用 GPU 在 python 中进行推理时一切正常)

我唯一拥有的是 Windows 10 事件查看器中的一个事件:

错误的应用程序名称:DeepLearningONNX.exe,版本:1.0.0.0, 时间戳:0x6331eb0e 故障模块名称:cudnn64_8.dll,版本: 6.14.11.6050,时间戳:0x62e9c226 异常代码:0xc0000409 故障偏移量:0x000000000001420d 故障进程 ID:0x2cc0 故障 应用程序启动时间:0x01d8f830aac6f0a2 故障应用程序路径: C:\R&D\DeepLearningONNX\DeepLearningONNX\bin\x64\Debug\net6.0-windows\DeepLearningONNX.exe 故障模块路径:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\bin\cudnn64_8.dll 报告编号: 40803e1a-e84d-4645-bfb6-4ebbb6ba1b78 故障包全名: 错误包相关的应用程序 ID:

我的硬件:

NVIDIA Quadro P620 (4GB)。驱动程序 31.0.15.1740

英特尔酷睿 i7-10850H

Windows 10 22H2 操作系统版本 19045.2251

在我的环境系统变量中:

CUDA_路径:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6

CUDA_PATH_V11_6 :C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6

小路 :C:\Program Files\NVIDIA\CUDNN\v8.5;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\bin;C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.6\libnvvp

在我的 C# (.NET 6) 解决方案中。安装的nuget:

Microsoft.ML.OnnxRuntime.Gpu 版本 1.13.1

安装的软件:

Visual Studio 社区 2022(64 位)版本 17.3.6

cuda_11.6.2_511.65_windows.exe

提取的cudnn-windows-x86_64-8.5.0.96_cuda11-archiveC:\Program Files\NVIDIA\CUDNN\v8.5

我的代码 C#:

private void InferenceDebug(string modelPath, bool useGPU)
        {
            InferenceSession session;

            if (useGPU)
            {
                var cudaProviderOptions = new OrtCUDAProviderOptions();
                var providerOptionsDict = new Dictionary<string, string>();
                providerOptionsDict["device_id"] = "0";
                providerOptionsDict["gpu_mem_limit"] = "2147483648";
                providerOptionsDict["arena_extend_strategy"] = "kSameAsRequested";
                providerOptionsDict["cudnn_conv_algo_search"] = "DEFAULT";
                providerOptionsDict["do_copy_in_default_stream"] = "1";
                providerOptionsDict["cudnn_conv_use_max_workspace"] = "1";
                providerOptionsDict["cudnn_conv1d_pad_to_nc1d"] = "1";

                cudaProviderOptions.UpdateOptions(providerOptionsDict);

                SessionOptions options = SessionOptions.MakeSessionOptionWithCudaProvider(cudaProviderOptions);
                session = new InferenceSession(modelPath, options);
            }
            else
                session = new InferenceSession(modelPath);

            int w = 128;
            int h = 128;
            Tensor<float> input = new DenseTensor<float>(new int[] { 1, 3, h, w });
            Random random = new Random(42);

            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    input[0, 0, y, x] = (float)(random.NextDouble() / 255);
                    input[0, 1, y, x] = (float)(random.NextDouble() / 255);
                    input[0, 2, y, x] = (float)(random.NextDouble() / 255);
                }
            }

            var inputs = new List<NamedOnnxValue> { NamedOnnxValue.CreateFromTensor<float>("modelInput", input) };
            using IDisposableReadOnlyCollection<DisposableNamedOnnxValue> results = session.Run(inputs); // The crash is when executing this line
        }

我的代码 Python(3.10 64 位):

import torch # version '1.12.1+cu116'
from torch import nn
import segmentation_models_pytorch as smp
from segmentation_models_pytorch.losses import DiceLoss

class SegmentationModel(nn.Module):
  def __init__(self):
    super(SegmentationModel, self).__init__()

    self.arc = smp.UnetPlusPlus(encoder_name= 'timm-efficientnet-b0',
                        encoder_weights='imagenet',
                        in_channels= 3,
                        classes = 1,
                        activation=None)
    
  def forward(self,images, masks=None):
    logits = self.arc(images)

    if masks != None :
      loss1 =DiceLoss(mode='binary')(logits, masks)
      loss2 = nn.BCEWithLogitsLoss()(logits, masks)
      return logits, loss1+loss2
    
    return logits

modelPath = "D:/model.pt"
device = "cuda"#input("Enter device (cpu or cuda) : ")
model = SegmentationModel()
model.to(device);
model.load_state_dict(torch.load(modelPath,map_location=torch.device(device) ))
model.eval()

dummy_input = torch.randn(1,3,128,128,device=device)

torch.onnx.export(model,         # model being run 
        dummy_input,       # model input (or a tuple for multiple inputs) 
        "model.onnx",       # where to save the model  
        export_params=True,  # store the trained parameter weights inside the model file 
        do_constant_folding=True,  # whether to execute constant folding for optimization 
        input_names = ['modelInput'],   # the model's input names 
        output_names = ['modelOutput'], # the model's output names 
        dynamic_axes={'modelInput' : [0,2,3],    # variable length axes 
    

                    'modelOutput' : [0,2,3]}) 

崩溃的原因是什么,我该如何解决?

【问题讨论】:

标签: c# deep-learning pytorch onnxruntime


【解决方案1】:

我发现了我的错误。我忘了下载这里提到的 zlib : https://docs.nvidia.com/deeplearning/cudnn/install-guide/index.html#prerequisites-windows

在我的环境变量 PATH 中添加 zlibwapi.dll 文件夹的路径后,一切正常。

【讨论】:

    猜你喜欢
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-08
    • 2021-12-03
    • 2021-11-24
    • 1970-01-01
    相关资源
    最近更新 更多