--- /dev/null
+
+require 'torch'
+require 'nn'
+require 'sys'
+
+profiler = {}
+
+function profiler.decor(model, functionsToDecorate)
+
+ local functionsToDecorate = functionsToDecorate or
+ {
+ 'updateOutput',
+ 'backward'
+ }
+
+ for _, name in pairs(functionsToDecorate) do
+ model.orig = model.orig or {}
+ model.timings = 0
+
+ if model[name] and not model.orig[name] then
+ model.orig[name] = model[name]
+ model[name] = function(self, ...)
+ local startTime = sys.clock()
+ local result = { self.orig[name](self, unpack({...})) }
+ local endTime = sys.clock()
+ self.timings = self.timings + endTime - startTime
+ return unpack(result)
+ end
+ end
+
+ end
+
+ if torch.isTypeOf(model, nn.Container) then
+ for _, m in ipairs(model.modules) do
+ profiler.decor(m, functionsToDecorate)
+ end
+ end
+
+end
+
+function profiler.print(model)
+ print('----------------------------------------------------------------------')
+ print(model)
+ print(string.format('TIMING %.02fs', model.timings))
+ if torch.isTypeOf(model, nn.Container) then
+ model:applyToModules(profiler.print)
+ end
+end
+
+return profiler
--- /dev/null
+#!/usr/bin/env luajit
+
+require 'torch'
+require 'nn'
+
+require 'profiler'
+
+local model = nn.Sequential()
+model:add(nn.Linear(1000, 1000))
+model:add(nn.ReLU())
+model:add(nn.Linear(1000, 100))
+
+profiler.decor(model)
+
+for k = 1, 10 do
+ local input = torch.Tensor(1000, 1000):uniform(-1, 1)
+ local target = torch.Tensor(input:size(1), 100):uniform()
+ local criterion = nn.MSECriterion()
+ local output = model:forward(input)
+ local loss = criterion:forward(output, target)
+ local dloss = criterion:backward(output, target)
+ model:backward(input, dloss)
+end
+
+profiler.print(model)