Openresty torch GPU模块加载问题

我正在使用带有Lua的openresty。当我没有cutorch,cunn(GPU)加载torch时,它可以很好地工作。但是它无法加载cutorch模块。

这是我的nginx.conf

error_log stderr notice;
daemon off;

events{}

http {
lua_code_cache on;
#lua_package_cpath '/usr/local/cuda/include/?.so;;';
init_by_lua_file 'evaluate.lua';
server {

listen 6788;
lua_code_cache on;
lua_need_request_body on;
client_body_buffer_size 10M;
client_max_body_size 10M;
location /ff {
# 仅限POST请求
if ($request_method !~ ^(POST)$ ) {
return 403;
}

content_by_lua '
local body = ngx.req.get_body_data()

if body == nil then
ngx.say("空的body")
else
local resp =FeedForward(body)
ngx.say(cjson.encode({result=resp}))
end
';
}
}
}

这是我的evaluate.lua代码

-- 加载所需的Lua模型
torch = require("torch")
nn = require("nn")
gm = require("image")
cutorch = require "cutorch"
cunn = require "cunn"
cjson = require("cjson")
-- 模型
modelPath='./model.t7'

ninputs = 3
model = nn.Sequential()
model:add(nn.Linear(ninputs,2))

-- 让我们保存一个虚拟模型
-- 以演示功能
torch.save(modelPath, model)

-- 加载预训练模型
net = torch.load(modelPath)
net:cuda()
net:evaluate()

-- 我们的终点函数
-- 此函数由ngx服务器调用
-- 接受一个Json body
function FeedForward(json)
print("开始")
-- 解码和提取x字段
local data = cjson.decode(json)
local input = torch.Tensor(data.x)

local response = {}
-- 示例检查
if input == nil then
print("未提供输入")
elseif input:size(1) ~=ninputs then
print("输入大小错误")
else
-- 评估输入并创建响应
local output = net:forward(input:cuda()):float()
-- 从张量到表
for i=1,output:size(1) do
response[i] = output[i]
end
end
-- 返回响应
return response
end

我正在尝试使用以下命令运行该模型

/usr/local/openresty/nginx/sbin/nginx -p "$(pwd)" -c "nginx.conf"

它启动得很好,但当我发送如下的curl请求时

curl -H  "Content-Type: application/json" -X POST -d '{"x":[1,2,3]}' http://localhost:6788/ff

我得到以下错误。

2016/09/29 12:59:59 [notice] 10355#0: *1 [lua] evaluate.lua:28: FeedForward(): 开始, client: 127.0.0.1, server: , request: "POST /ff HTTP/1.1", host: "localhost:6788"

THCudaCheck FAIL file=/tmp/luarocks_cutorch-scm-1-700/cutorch/lib/THC/generic/THCStorage.cu line=40 error=3 : initialization error

2016/09/29 12:59:59 [error] 10355#0: *1 lua entry thread aborted: runtime error: unknown reason

stack traceback:
coroutine 0:
        [C]: in function 'resize'
        /home/ubuntu/torch/install/share/lua/5.1/cutorch/Tensor.lua:14: in function 'cuda'
       /rootpath/evaluate.lua:41: in function 'FeedForward'
        content_by_lua(nginx.conf:31):7: in function <content_by_lua(nginx.conf:31):1>, client: 127.0.0.1, server: , request: "POST /ff HTTP/1.1", host: "localhost:6788"

没有cutorch的情况下,该模型可以很好地运行,就像我删除了

net:cuda()

并将行替换为

local output = net:forward(input):float()

它也很好用。我还尝试使用th运行evaluate.lua,那里有cutorch和cunn包,并且它也正常工作。

点赞