Update.
[profiler-torch.git] / profiler.lua
index 4e45787..9d493c0 100644 (file)
@@ -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,12 +64,20 @@ 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 classes, not the objects, and there is a `'
+                  .. name
+                  .. '\' function in '
+                  .. tostring(model))
       end
 
-      local toDecorate = getmetatable(model)
+      local toDecorate = model
+
+      while not rawget(toDecorate, name) do
+         toDecorate = getmetatable(toDecorate)
+      end
 
-      if toDecorate[name] and not toDecorate[nameOrig] then
+      if not toDecorate[nameOrig] then
+         -- print('Decorating ' .. toDecorate.__typename .. '.' .. name)
          toDecorate[nameOrig] = toDecorate[name]
          toDecorate[name] = function(self, ...)
             local startTime = sys.clock()
@@ -80,20 +98,55 @@ function profiler.decorate(model, functionsToDecorate)
 
 end
 
+function profiler.timing(l, t, nbSamples, totalTime)
+   local s
+
+   s = string.format('%s %.02fs %s[%.02f%%]',
+                     l, t,
+                     profiler.colors('blue'),
+                     100 * t / totalTime
+   )
+
+   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))
+   if not model.accTime then
+      error('The model does not seem decorated for profiling.')
+   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)
+   local localTotal = 0
+   for _, t in pairs(model.accTime) do
+      localTotal = localTotal + t
+   end
+
+   totalTime = totalTime or localTotal
+
+   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()