-
Notifications
You must be signed in to change notification settings - Fork 3
/
mlp_l4.lua
87 lines (79 loc) · 2.66 KB
/
mlp_l4.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
-- Network: Multi-layer perceptron w/ 5 layers
-- Application: Object Detection
-- Dataset: CIFAR10
-- Number of parameters:
-- Reference: None
-- How to interpret I/O data sizes?
-- batchsize * input_size
-- A batch - multiple inputs
torch.setdefaulttensortype('torch.FloatTensor')
require 'xlua'
require 'sys'
cmd = torch.CmdLine()
cmd:option('-gpu', 0, 'Run on CPU/GPU')
cmd:option('-threads', 2, 'Number of threads for CPU')
cmd:option('-batch', 1, 'Number of batches')
cmd:option('-gpusample', 500, 'Sampling rate in ms')
cmd:option('-gputype','nvidia','Type of Nvidia GPU')
opt = cmd:parse(arg or {})
torch.setnumthreads(opt.threads)
-- dnn hyper-parameters
batchsize = opt.batch
inputsize = 1024
hiddensize = {1024, 2048, 1024}
outputsize = 10
gpusample = opt.gpusample
gputype = opt.gputype
-- build dnn
require 'nn'
local num_params = 0
model = nn.Sequential()
model:add(nn.Linear(inputsize, hiddensize[1]))
model:add(nn.Sigmoid())
model:add(nn.Linear(hiddensize[1], hiddensize[2]))
model:add(nn.Sigmoid())
model:add(nn.Linear(hiddensize[2], hiddensize[3]))
model:add(nn.Sigmoid())
model:add(nn.Linear(hiddensize[3], outputsize))
num_params = num_params + (inputsize+1)*hiddensize[1] +
(hiddensize[1]+1)*hiddensize[2] + (hiddensize[2]+1)*hiddensize[3] +
(hiddensize[3]+1)*outputsize
print (model)
print (num_params)
-- create input and output tensors
input = torch.Tensor(batchsize, inputsize)
output = torch.zeros(batchsize, outputsize)
output = model:forward (input)
-- dnn inference model
local run_dnn = function()
print('==> Type is '..input:type())
output = model:forward(input)
end
-- for running on GPU/CPU
if (opt.gpu == 1) then -- GPU run
require 'cunn'
model = model:cuda() -- move the model, i/o data to gpu memory
input = input:cuda()
output = output:cuda()
cmdstring1="nvidia-smi -i 0 --query-gpu=power.limit,power.draw,utilization.gpu,utilization.memory,memory.total,memory.used,memory.free --format=csv,nounits --loop-ms=%d >" %(gpusample)
cmdstring2=" gpu_profile_data/mlp_l4_gpulog_batchsize_%d" %(batchsize)
cmdstring3="_sample_ms_%d" %(gpusample)
cmdstring4="_%s.txt &" %(gputype)
cmdstring=cmdstring1 .. cmdstring2 .. cmdstring3 .. cmdstring4
os.execute(cmdstring)
-- measure gpu time
gputime0 = sys.clock()
run_dnn()
gputime1 = sys.clock()
-- run nvidia-smi for gpu power (Think about the placment of this later?)
gputime = gputime1 - gputime0
print('GPU Time: '.. (gputime*1000) .. 'ms')
os.execute('kill -9 `pidof nvidia-smi`')
else -- CPU run
-- measure CPU latency
cputime0 = sys.clock()
run_dnn()
cputime1 = sys.clock()
cputime = cputime1 - cputime0
print('CPU Time: '.. (cputime*1000) .. 'ms')
end