This file should not be executable.
[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.decor(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 functionTable = model
52
53       -- We decorate the function where it is defined in the class
54       -- hierarchy, so we have to go up the metatables until we find
55       -- it with rawget
56
57       while functionTable and not rawget(functionTable, name) do
58          functionTable = getmetatable(functionTable)
59       end
60
61       local nameOrig = name .. '__orig'
62
63       if functionTable[name] and not functionTable[nameOrig] then
64          print('Profiler decoring ' .. functionTable.__typename .. '.' .. name)
65          functionTable[nameOrig] = functionTable[name]
66          functionTable[name] = function(self, ...)
67             local startTime = sys.clock()
68             local result = { self[nameOrig](self, unpack({...})) }
69             local endTime = sys.clock()
70             self.accTime[name] = (self.accTime[name] or 0) + endTime - startTime
71             return unpack(result)
72          end
73       end
74
75    end
76
77    if torch.isTypeOf(model, nn.Container) then
78       for _, m in ipairs(model.modules) do
79          profiler.decor(m, functionsToDecorate)
80       end
81    end
82
83 end
84
85 function profiler.print(model, nbSamples, indent)
86    local indent = indent or ''
87
88    print(string.format('%s* %s', indent, model.__typename))
89
90    if nbSamples then
91       for l, t in pairs(model.accTime) do
92          print(string.format('%s  %s %.02fs (%.01fmus/sample)',
93                              indent,
94                              l,
95                              t,
96                              1e6 * t / nbSamples))
97       end
98    else
99       for l, t in pairs(model.accTime) do
100          print(string.format('%s  %s %.02fs',
101                              indent,
102                              l,
103                              t))
104       end
105    end
106
107    if torch.isTypeOf(model, nn.Container) then
108       for _, m in ipairs(model.modules) do
109          profiler.print(m, nbSamples, indent .. '  ')
110       end
111    end
112 end
113
114 return profiler