Skip to content

Instantly share code, notes, and snippets.

@soumith
Forked from culurciello/gist:5189137
Last active December 19, 2015 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soumith/6011923 to your computer and use it in GitHub Desktop.
Save soumith/6011923 to your computer and use it in GitHub Desktop.
#!/usr/bin/env torch
require 'nn'
require 'image'
require 'xlua'
require 'pl'
opt = lapp[[
-t,--threads (default 8) number of threads
-p,--type (default float) float or cuda
-i,--devid (default 1) device ID (if using CUDA)
]]
p = xlua.Profiler()
torch.setnumthreads(opt.threads)
torch.manualSeed(1)
torch.setdefaulttensortype('torch.FloatTensor')
if opt.type == 'cuda' then
print('==> switching to CUDA')
require 'cunn'
cutorch.setDevice(opt.devid)
print('==> using GPU #' .. cutorch.getDevice())
nn.SpatialConvolutionMM = nn.SpatialConvolution
end
iH = 192
iW = 192
fin = 16
fout = 32
kH = 10
kW = 10
batchSize = 128
-- input:
lena1 = torch.Tensor(batchSize,fin,iH,iW)
-- model to test:
model = nn.SpatialConvolution(fin, fout, kW, kW)
-- copy to GPU if desired:
if opt.type == 'cuda' then
model = nn.SpatialConvolutionCUDA(fin, fout, kW, kW):cuda()
lena1 = torch.CudaTensor(fin,iH,iW,batchSize)
end
-- test speed:
p:start('spatialconv')
lena2 = model:forward(lena1)
if opt.type == 'cuda' then cutorch.synchronize() end
p:lap('spatialconv')
p:printAll{}
print('Gops/s:', ( batchSize*fin*fout*kH*kW*((iH-kH)+1)*((iW-kW)+1)*2 ) / p:cpu('spatialconv') / 1e9 ) -- 2 operations MUL, ACC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment