X-Git-Url: https://fleuret.org/cgi-bin/gitweb/gitweb.cgi?p=profiler-torch.git;a=blobdiff_plain;f=profiler.lua;h=4420af79b203464c276bc573bfb95459ddc461e9;hp=4e4578741c457c1af5aa0d7a99ceb7b2a4ce7dfc;hb=f59950ed20a2d471c59e2c23bc41b50111a54cd6;hpb=b8c7166b9123735e8226d34b717d3cbc2dc1fa02 diff --git a/profiler.lua b/profiler.lua index 4e45787..4420af7 100644 --- a/profiler.lua +++ b/profiler.lua @@ -37,6 +37,16 @@ require 'sys' profiler = {} +profiler.color = true + +profiler.colors = function(name) + if profiler.color then + return sys.COLORS[name] + else + return '' + end +end + function profiler.decorate(model, functionsToDecorate) local functionsToDecorate = functionsToDecorate or @@ -54,7 +64,10 @@ function profiler.decorate(model, functionsToDecorate) -- save models anymore. if rawget(model, name) then - error('We decorate the class, not the objects, and there is a ' .. name .. ' in ' .. model) + error('We decorate the class, not the objects, and there is a `' + .. name + .. '\' function in ' + .. tostring(model)) end local toDecorate = getmetatable(model) @@ -80,20 +93,42 @@ function profiler.decorate(model, functionsToDecorate) end +function profiler.timing(l, t, nbSamples, totalTime) + local s = string.format('%s %.02fs', l, t) + if totalTime then + s = s .. string.format(profiler.colors('blue') .. ' [%.02f%%]', 100 * t / totalTime) + end + if nbSamples then + s = s .. string.format(profiler.colors('green') .. ' (%.01fmus/sample)', 1e6 * t / nbSamples) + end + s = s .. profiler.colors('black') + return s +end + function profiler.print(model, nbSamples, totalTime, indent) local indent = indent or '' + local hint - print(string.format('%s* %s', indent, model.__typename)) + local localTotal = 0 + for _, t in pairs(model.accTime) do + localTotal = localTotal + t + end - for l, t in pairs(model.accTime) do - local s = string.format('%s %s %.02fs', indent, l, t) - if totalTime then - s = s .. string.format(' [%.02f%%]', 100 * t / totalTime) - end - if nbSamples then - s = s .. string.format(' (%.01fmus/sample)', 1e6 * t / nbSamples) + if torch.isTypeOf(model, nn.Container) then + hint = ' ' + else + if profiler.color then + hint = ' ' + else + hint = '*' end - print(s) + hint = hint .. profiler.colors('red') + end + + print(profiler.timing(indent .. hint .. ' ' .. model.__typename, localTotal, nbSamples, totalTime)) + + for l, t in pairs(model.accTime) do + print(profiler.timing(indent .. ' ' .. l, t, nbSamples, totalTime)) end print()