有关pycuda._driver.LogicError的问题:cuMemcpyDtoH失败:参数无效

时间:2019-08-08 04:29:56

标签: python python-3.x pycuda tensorrt

我试图运行基于以下链接的代码

https://documen.tician.de/pycuda/tutorial.html

在此链接中运行代码原来很好。

这是我的版本,具有类似的定义。请注意,由于要运行engine.execute函数,因此我在引擎上下文中运行。

import pycuda.driver as cuda 
import pycuda.autoinit 
import tensorrt as trt 

import numpy as np
from keras.datasets import mnist 

dims = (1, 28, 28) 
dims2 = (1, 1, 10) 
batch_size = 1000 

nbytes = batch_size * trt.volume(dims) * np.dtype(np.float32).itemsize 
nbytes2 = batch_size * trt.volume(dims2) * np.dtype(np.float32).itemsize 

self.d_src  = cuda.mem_alloc(nbytes) 
self.d_dst = cuda.mem_alloc(nbytes2) 

bindings = [int(self.d_src), int(self.d_dst)] 

(x_train, y_train), (x_test, y_test) = mnist.load_data()

img_h = x_test.shape[1]
img_w = x_test.shape[2]

x_test = x_test.reshape(x_test.shape[0], 1, img_h, img_w)

x_test = x_test.astype('float32')
x_test /= 255
num_test = x_test.shape[0]

output_size = batch_size * trt.volume(dims2)

y = np.empty((num_test,output_size), np.float32)

for i in range(0, num_test, batch_size): 
     x_part = x_test[i : i + batch_size] 
     y_part = y[i : i + batch_size] 
     cuda.memcpy_htod(self.d_src, x_part) 

     cuda.memcpy_dtoh(y_part, self.d_dst) 

但是它在memcpydtoh失败了,但memcpyhtod起作用了。

File "a.py", line 164, in infer
    cuda.memcpy_dtoh(y_part, self.d_dst)
pycuda._driver.LogicError: cuMemcpyDtoH failed: invalid argument

为什么会这样?这些定义类似于链接中的代码。

1 个答案:

答案 0 :(得分:1)

我已经解决了。

<ImageBrush ImageSource="/AB17035_ZLT_Client;component/Images/bum_big.PNG" Stretch="Uniform" Opacity="0.05" /> </Border.Background> <DataGrid Name="PlanGrid" HorizontalAlignment="Stretch" FontSize="16" RowHeight="30" HorizontalScrollBarVisibility="Visible" VerticalAlignment="Stretch" AutoGenerateColumns="True" SelectionMode="Extended" VerticalGridLinesBrush="Transparent" Background="Transparent" RowBackground="Transparent" ItemsSource="{Binding PlanDataView, Mode=TwoWay}" IsReadOnly="True" SelectionUnit="FullRow" IsSynchronizedWithCurrentItem="True" Cursor="Arrow" AllowDrop="True"> <DataGrid.InputBindings> <KeyBinding Gesture="Ctrl" Command="ApplicationCommands.NotACommand"></KeyBinding> </DataGrid.InputBindings> <i:Interaction.Triggers> <i:EventTrigger EventName="Loaded"> <ei:CallMethodAction MethodName="PlanGrid_Loaded" TargetObject="{Binding}"></ei:CallMethodAction> </i:EventTrigger> </i:Interaction.Triggers> </DataGrid> x_part的设备分配必须不同,因为它们的大小不同。

因此,如果我定义y_part,它就可以工作。

该错误消息对于以&开头不是很有帮助,并让我认为我输入了错误的参数。