Update.
[profiler-torch.git] / test-profiler.lua
1 #!/usr/bin/env luajit
2
3 --[[
4
5    Written by Francois Fleuret (francois@fleuret.org)
6
7    This is free and unencumbered software released into the public
8    domain.
9
10    Anyone is free to copy, modify, publish, use, compile, sell, or
11    distribute this software, either in source code form or as a
12    compiled binary, for any purpose, commercial or non-commercial, and
13    by any means.
14
15    In jurisdictions that recognize copyright laws, the author or
16    authors of this software dedicate any and all copyright interest in
17    the software to the public domain. We make this dedication for the
18    benefit of the public at large and to the detriment of our heirs
19    and successors. We intend this dedication to be an overt act of
20    relinquishment in perpetuity of all present and future rights to
21    this software under copyright law.
22
23    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26    NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
27    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
28    CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30
31    For more information, please refer to <http://unlicense.org/>
32
33 ]]--
34
35 require 'torch'
36 require 'nn'
37
38 require 'profiler'
39
40 -- Create a model
41
42 local w, h, fs = 50, 50, 3
43 local nhu =  (w - fs + 1) * (h - fs + 1)
44
45 local model = nn.Sequential()
46    :add(nn.Sequential()
47            :add(nn.SpatialConvolution(1, 1, fs, fs))
48            :add(nn.Reshape(nhu))
49            :add(nn.Linear(nhu, 1000))
50            :add(nn.ReLU())
51        )
52    :add(nn.Linear(1000, 100))
53
54 -- Decor it for profiling
55
56 profiler.decorate(model)
57 print()
58
59 torch.save('model.t7', model)
60
61 -- Create the data and criterion
62
63 local input = torch.Tensor(1000, 1, h, w)
64 local target = torch.Tensor(input:size(1), 100)
65 local criterion = nn.MSECriterion()
66
67 local nbSamples = 0
68 local modelTime = 0
69 local dataTime = 0
70
71 -- Loop five times through the data forward and backward
72
73 for k = 1, 5 do
74    local t1 = sys.clock()
75
76    input:uniform(-1, 1)
77    target:uniform()
78
79    local t2 = sys.clock()
80
81    local output = model:forward(input)
82    local loss = criterion:forward(output, target)
83    local dloss = criterion:backward(output, target)
84    model:backward(input, dloss)
85
86    local t3 = sys.clock()
87
88    dataTime = dataTime + (t2 - t1)
89    modelTime = modelTime + (t3 - t2)
90
91    nbSamples = nbSamples + input:size(1)
92 end
93
94 -- Print the accumulated timings
95
96 profiler.print(model, nbSamples, modelTime)
97 -- profiler.print(model)
98
99 print()
100 print(string.format('Total model time %.02fs', modelTime))
101 print(string.format('Total data time %.02fs', dataTime))