4 Written by Francois Fleuret (francois@fleuret.org)
6 This is free and unencumbered software released into the public
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
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.
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.
30 For more information, please refer to <http://unlicense.org/>
42 profiler.colors = function(name)
43 if profiler.color then
44 return sys.COLORS[name]
50 function profiler.decorate(model, functionsToDecorate)
52 local functionsToDecorate = functionsToDecorate or
58 for _, name in pairs(functionsToDecorate) do
61 local nameOrig = name .. '__orig'
63 -- We decorate the class and not the object, otherwise we cannot
64 -- save models anymore.
66 if rawget(model, name) then
67 error('We decorate the class, not the objects, and there is a ' .. name .. ' in ' .. model)
70 local toDecorate = getmetatable(model)
72 if toDecorate[name] and not toDecorate[nameOrig] then
73 toDecorate[nameOrig] = toDecorate[name]
74 toDecorate[name] = function(self, ...)
75 local startTime = sys.clock()
76 local result = { self[nameOrig](self, unpack({...})) }
77 local endTime = sys.clock()
78 self.accTime[name] = (self.accTime[name] or 0) + endTime - startTime
85 if torch.isTypeOf(model, nn.Container) then
86 for _, m in ipairs(model.modules) do
87 profiler.decorate(m, functionsToDecorate)
93 function profiler.timing(l, t, nbSamples, totalTime)
94 local s = string.format('%s %.02fs', l, t)
96 s = s .. string.format(profiler.colors('blue') .. ' [%.02f%%]', 100 * t / totalTime)
99 s = s .. string.format(profiler.colors('green') .. ' (%.01fmus/sample)', 1e6 * t / nbSamples)
101 s = s .. profiler.colors('black')
105 function profiler.print(model, nbSamples, totalTime, indent)
106 local indent = indent or ''
110 for _, t in pairs(model.accTime) do
111 localTotal = localTotal + t
114 if torch.isTypeOf(model, nn.Container) then
117 if profiler.color then
122 hint = hint .. profiler.colors('red')
125 print(profiler.timing(indent .. hint .. ' ' .. model.__typename, localTotal, nbSamples, totalTime))
127 for l, t in pairs(model.accTime) do
128 print(profiler.timing(indent .. ' ' .. l, t, nbSamples, totalTime))
133 if torch.isTypeOf(model, nn.Container) then
134 for _, m in ipairs(model.modules) do
135 profiler.print(m, nbSamples, totalTime, indent .. ' ')