Cosmetics + added a header.
[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 = 0
50
51       local functionTable = model
52
53       if not rawget(functionTable, name) then
54          functionTable = getmetatable(model)
55       end
56
57       if functionTable[name] and not (functionTable.orig and functionTable.orig[name]) then
58          print('Profiler decoring ' .. functionTable.__typename .. '.' .. name)
59          functionTable.orig = functionTable.orig or {}
60          functionTable.orig[name] = functionTable[name]
61          functionTable[name] = function(self, ...)
62             local startTime = sys.clock()
63             local result = { self.orig[name](self, unpack({...})) }
64             local endTime = sys.clock()
65             self.accTime = self.accTime + endTime - startTime
66             return unpack(result)
67          end
68       end
69
70    end
71
72    if torch.isTypeOf(model, nn.Container) then
73       for _, m in ipairs(model.modules) do
74          profiler.decor(m, functionsToDecorate)
75       end
76    end
77
78 end
79
80 function profiler.print(model, nbSamples)
81    print('----------------------------------------------------------------------')
82    print(model)
83    if nbSamples then
84       print(string.format('acc_time %.02fs (%.01fmus/sample)',
85                           model.accTime,
86                           1e6 * model.accTime / nbSamples))
87    else
88       print(string.format('acc_time %.02fs', model.accTime))
89    end
90
91    if torch.isTypeOf(model, nn.Container) then
92       for _, m in ipairs(model.modules) do
93          profiler.print(m, nbSamples)
94       end
95    end
96 end
97
98 return profiler