Fixed a stupid and embarrassing typo.
[profiler-torch.git] / profiler.lua
index 6f63861..34e180b 100644 (file)
@@ -37,7 +37,7 @@ require 'sys'
 
 profiler = {}
 
-function profiler.decor(model, functionsToDecorate)
+function profiler.decorate(model, functionsToDecorate)
 
    local functionsToDecorate = functionsToDecorate or
       {
@@ -48,22 +48,11 @@ function profiler.decor(model, functionsToDecorate)
    for _, name in pairs(functionsToDecorate) do
       model.accTime = {}
 
-      local functionTable = model
-
-      -- We decorate the function where it is defined in the class
-      -- hierarchy, so we have to go up the metatables until we find
-      -- it with rawget
-
-      while functionTable and not rawget(functionTable, name) do
-         functionTable = getmetatable(functionTable)
-      end
-
       local nameOrig = name .. '__orig'
 
-      if functionTable[name] and not functionTable[nameOrig] then
-         print('Profiler decoring ' .. functionTable.__typename .. '.' .. name)
-         functionTable[nameOrig] = functionTable[name]
-         functionTable[name] = function(self, ...)
+      if model[name] and not model[nameOrig] then
+         model[nameOrig] = model[name]
+         model[name] = function(self, ...)
             local startTime = sys.clock()
             local result = { self[nameOrig](self, unpack({...})) }
             local endTime = sys.clock()
@@ -76,7 +65,7 @@ function profiler.decor(model, functionsToDecorate)
 
    if torch.isTypeOf(model, nn.Container) then
       for _, m in ipairs(model.modules) do
-         profiler.decor(m, functionsToDecorate)
+         profiler.decorate(m, functionsToDecorate)
       end
    end
 
@@ -87,21 +76,14 @@ function profiler.print(model, nbSamples, indent)
 
    print(string.format('%s* %s', indent, model.__typename))
 
-   if nbSamples then
-      for l, t in pairs(model.accTime) do
-         print(string.format('%s  %s %.02fs (%.01fmus/sample)',
-                             indent,
-                             l,
-                             t,
-                             1e6 * t / nbSamples))
-      end
-   else
-      for l, t in pairs(model.accTime) do
-         print(string.format('%s  %s %.02fs',
-                             indent,
-                             l,
-                             t))
+   for l, t in pairs(model.accTime) do
+      local s
+      if nbSamples then
+         s = string.format(' (%.01fmus/sample)', 1e6 * t / nbSamples)
+      else
+         s = ''
       end
+      print(string.format('%s  %s %.02fs%s', indent, l, t, s))
    end
 
    if torch.isTypeOf(model, nn.Container) then