Removed the signature.
[profiler-torch.git] / profiler.lua
1
2 --[[
3
4    Copyright (c) 2016 Idiap Research Institute, http://www.idiap.ch/
5    Written by Francois Fleuret <francois.fleuret@idiap.ch>
6
7    This file is free software: you can redistribute it and/or modify
8    it under the terms of the GNU General Public License version 3 as
9    published by the Free Software Foundation.
10
11    It is distributed in the hope that it will be useful, but WITHOUT
12    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
14    License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this file.  If not, see <http://www.gnu.org/licenses/>.
18
19 ]]--
20
21 require 'torch'
22 require 'nn'
23 require 'sys'
24
25 profiler = {}
26
27 profiler.color = true
28
29 profiler.colors = function(name)
30    if profiler.color then
31       return sys.COLORS[name]
32    else
33       return ''
34    end
35 end
36
37 function profiler.decorate(model, functionsToDecorate)
38
39    local functionsToDecorate = functionsToDecorate or
40       {
41          'updateOutput',
42          'backward'
43       }
44
45    for _, name in pairs(functionsToDecorate) do
46       model.accTime = {}
47
48       -- We decorate the class and not the object, otherwise we cannot
49       -- save models anymore.
50
51       if rawget(model, name) then
52          error('We decorate the classes, not the objects, and there is a `'
53                   .. name
54                   .. '\' function in '
55                   .. tostring(model))
56       end
57
58       local toDecorate = getmetatable(model)
59
60       while not rawget(toDecorate, name) do
61          toDecorate = getmetatable(toDecorate)
62       end
63
64       local nameOrig = name .. '__orig'
65
66       if not toDecorate[nameOrig] then
67          -- print('Decorating ' .. toDecorate.__typename .. '.' .. name)
68          toDecorate[nameOrig] = toDecorate[name]
69          toDecorate[name] = function(self, ...)
70             local startTime = sys.clock()
71             local result = { self[nameOrig](self, unpack({...})) }
72             local endTime = sys.clock()
73             self.accTime[name] = (self.accTime[name] or 0) + endTime - startTime
74             return unpack(result)
75          end
76       end
77
78    end
79
80    if torch.isTypeOf(model, nn.Container) then
81       for _, m in ipairs(model.modules) do
82          profiler.decorate(m, functionsToDecorate)
83       end
84    end
85
86 end
87
88 function profiler.timingString(l, t, nbSamples, totalTime)
89    local s
90
91    s = string.format('%s %.02fs %s[%.02f%%]',
92                      l, t,
93                      profiler.colors('blue'),
94                      100 * t / totalTime
95    )
96
97    if nbSamples then
98       s = s .. string.format(profiler.colors('green') .. ' (%.01fmus/sample)',
99                              1e6 * t / nbSamples)
100    end
101
102    s = s .. profiler.colors('black')
103
104    return s
105 end
106
107 function profiler.print(model, nbSamples, totalTime, indent)
108    local indent = indent or ''
109    local hint
110
111    if not model.accTime then
112       error('The model does not seem decorated for profiling.')
113    end
114
115    local localTotal = 0
116    for _, t in pairs(model.accTime) do
117       localTotal = localTotal + t
118    end
119
120    totalTime = totalTime or localTotal
121
122    if torch.isTypeOf(model, nn.Container) then
123       hint = ' '
124    else
125       if profiler.color then
126          hint = '  ' .. profiler.colors('red')
127       else
128          hint = '* '
129       end
130    end
131
132    print(profiler.timingString(indent .. hint .. model.__typename,
133                                localTotal, nbSamples, totalTime))
134
135    for l, t in pairs(model.accTime) do
136       print(profiler.timingString(indent .. '  :' .. l, t, nbSamples, totalTime))
137    end
138
139    print()
140
141    if torch.isTypeOf(model, nn.Container) then
142       for _, m in ipairs(model.modules) do
143          profiler.print(m, nbSamples, totalTime, indent .. '  ')
144       end
145    end
146 end
147
148 return profiler