Skip to content

Commit

Permalink
add opencl to sample.lua and inspect_checkpoint.lua, add link to clto…
Browse files Browse the repository at this point in the history
…rch issues page
  • Loading branch information
hughperkins committed Jul 12, 2015
1 parent 3b2a01d commit b7f09bd
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ $ luarocks install cutorch
$ luarocks install cunn
```

If you'd like to use OpenCL GPU computing, you'll first need to install the `cltorch` and `clnn` packages, and then use the option `-opencl 1` during training:
If you'd like to use OpenCL GPU computing, you'll first need to install the `cltorch` and `clnn` packages, and then use the option `-opencl 1` during training ([cltorch issues](https://github.com/hughperkins/cltorch/issues)):

```bash
$ luarocks install cltorch
Expand Down
10 changes: 9 additions & 1 deletion inspect_checkpoint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,26 @@ cmd:text()
cmd:text('Options')
cmd:argument('-model','model to load')
cmd:option('-gpuid',0,'gpu to use')
cmd:option('-opencl',0,'use OpenCL (instead of CUDA)')
cmd:text()

-- parse input params
opt = cmd:parse(arg)

if opt.gpuid >= 0 then
if opt.gpuid >= 0 and opt.opencl == 0 then
print('using CUDA on GPU ' .. opt.gpuid .. '...')
require 'cutorch'
require 'cunn'
cutorch.setDevice(opt.gpuid + 1)
end

if opt.gpuid >= 0 and opt.opencl == 1 then
print('using OpenCL on GPU ' .. opt.gpuid .. '...')
require 'cltorch'
require 'clnn'
cltorch.setDevice(opt.gpuid + 1)
end

local model = torch.load(opt.model)

print('opt:')
Expand Down
29 changes: 25 additions & 4 deletions sample.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ cmd:option('-primetext',"",'used as a prompt to "seed" the state of the LSTM usi
cmd:option('-length',2000,'number of characters to sample')
cmd:option('-temperature',1,'temperature of sampling')
cmd:option('-gpuid',0,'which gpu to use. -1 = use CPU')
cmd:option('-opencl',0,'use OpenCL (instead of CUDA)')
cmd:option('-verbose',1,'set to 0 to ONLY print the sampled text, no diagnostics')
cmd:text()

Expand All @@ -43,7 +44,7 @@ function gprint(str)
end

-- check that cunn/cutorch are installed if user wants to use the GPU
if opt.gpuid >= 0 then
if opt.gpuid >= 0 and opt.opencl == 0 then
local ok, cunn = pcall(require, 'cunn')
local ok2, cutorch = pcall(require, 'cutorch')
if not ok then gprint('package cunn not found!') end
Expand All @@ -57,6 +58,23 @@ if opt.gpuid >= 0 then
opt.gpuid = -1 -- overwrite user setting
end
end

-- check that clnn/cltorch are installed if user wants to use OpenCL
if opt.gpuid >= 0 and opt.opencl == 1 then
local ok, cunn = pcall(require, 'clnn')
local ok2, cutorch = pcall(require, 'cltorch')
if not ok then print('package clnn not found!') end
if not ok2 then print('package cltorch not found!') end
if ok and ok2 then
print('using OpenCL on GPU ' .. opt.gpuid .. '...')
cltorch.setDevice(opt.gpuid + 1) -- note +1 to make it 0 indexed! sigh lua
torch.manualSeed(opt.seed)
else
gprint('Falling back on CPU mode')
opt.gpuid = -1 -- overwrite user setting
end
end

torch.manualSeed(opt.seed)

-- load the model checkpoint
Expand All @@ -80,7 +98,8 @@ current_state = {}
for L = 1,checkpoint.opt.num_layers do
-- c and h for all layers
local h_init = torch.zeros(1, checkpoint.opt.rnn_size)
if opt.gpuid >= 0 then h_init = h_init:cuda() end
if opt.gpuid >= 0 and opt.opencl == 0 then h_init = h_init:cuda() end
if opt.gpuid >= 0 and opt.opencl == 1 then h_init = h_init:cl() end
table.insert(current_state, h_init:clone())
table.insert(current_state, h_init:clone())
end
Expand All @@ -94,7 +113,8 @@ if string.len(seed_text) > 0 then
for c in seed_text:gmatch'.' do
prev_char = torch.Tensor{vocab[c]}
io.write(ivocab[prev_char[1]])
if opt.gpuid >= 0 then prev_char = prev_char:cuda() end
if opt.gpuid >= 0 and opt.opencl == 0 then prev_char = prev_char:cuda() end
if opt.gpuid >= 0 and opt.opencl == 1 then prev_char = prev_char:cl() end
local lst = protos.rnn:forward{prev_char, unpack(current_state)}
-- lst is a list of [state1,state2,..stateN,output]. We want everything but last piece
current_state = {}
Expand All @@ -106,7 +126,8 @@ else
gprint('missing seed text, using uniform probability over first character')
gprint('--------------------------')
prediction = torch.Tensor(1, #ivocab):fill(1)/(#ivocab)
if opt.gpuid >= 0 then prediction = prediction:cuda() end
if opt.gpuid >= 0 and opt.opencl == 0 then prediction = prediction:cuda() end
if opt.gpuid >= 0 and opt.opencl == 1 then prediction = prediction:cl() end
end

-- start sampling/argmaxing
Expand Down

0 comments on commit b7f09bd

Please sign in to comment.