3bbccaf88ba65e400b26f625ccb971d41bcbde94
[profiler-torch.git] / profiler.lua
1
2 --[[
3
4    Written by Francois Fleuret (francois@fleuret.org)
5
6    This is free and unencumbered software released into the public
7    domain.
8
9    Anyone is free to copy, modify, publish, use, compile, sell, or
10    distribute this software, either in source code form or as a
11    compiled binary, for any purpose, commercial or non-commercial, and
12    by any means.
13
14    In jurisdictions that recognize copyright laws, the author or
15    authors of this software dedicate any and all copyright interest in
16    the software to the public domain. We make this dedication for the
17    benefit of the public at large and to the detriment of our heirs
18    and successors. We intend this dedication to be an overt act of
19    relinquishment in perpetuity of all present and future rights to
20    this software under copyright law.
21
22    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25    NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
26    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
27    CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28    WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30    For more information, please refer to <http://unlicense.org/>
31
32 ]]--
33
34 require 'torch'
35 require 'nn'
36 require 'sys'
37
38 profiler = {}
39
40 function profiler.decorate(model, functionsToDecorate)
41
42    local functionsToDecorate = functionsToDecorate or
43       {
44          'updateOutput',
45          'backward'
46       }
47
48    for _, name in pairs(functionsToDecorate) do
49       model.accTime = {}
50
51       local nameOrig = name .. '__orig'
52
53       -- We decorate the class and not the object, otherwise we cannot
54       -- save models anymore.
55
56       if rawget(model, name) then
57          error('We decorate the class, not the objects, and there is a ' .. name .. ' in ' .. model)
58       end
59
60       local toDecorate = getmetatable(model)
61
62       if toDecorate[name] and not toDecorate[nameOrig] then
63          toDecorate[nameOrig] = toDecorate[name]
64          toDecorate[name] = function(self, ...)
65             local startTime = sys.clock()
66             local result = { self[nameOrig](self, unpack({...})) }
67             local endTime = sys.clock()
68             self.accTime[name] = (self.accTime[name] or 0) + endTime - startTime
69             return unpack(result)
70          end
71       end
72
73    end
74
75    if torch.isTypeOf(model, nn.Container) then
76       for _, m in ipairs(model.modules) do
77          profiler.decorate(m, functionsToDecorate)
78       end
79    end
80
81 end
82
83 function profiler.print(model, nbSamples, totalTime, indent)
84    local indent = indent or ''
85    local hint
86
87    if torch.isTypeOf(model, nn.Container) then
88       hint = ' '
89    else
90       hint = '*'
91    end
92
93    print(string.format('%s%s %s', indent, hint, model.__typename))
94
95    for l, t in pairs(model.accTime) do
96       local s = string.format('%s  %s %.02fs', indent, l, t)
97       if totalTime then
98          s = s .. string.format(' [%.02f%%]', 100 * t / totalTime)
99       end
100       if nbSamples then
101          s = s .. string.format(' (%.01fmus/sample)', 1e6 * t / nbSamples)
102       end
103       print(s)
104    end
105
106    print()
107
108    if torch.isTypeOf(model, nn.Container) then
109       for _, m in ipairs(model.modules) do
110          profiler.print(m, nbSamples, totalTime, indent .. '  ')
111       end
112    end
113 end
114
115 return profiler