From 4696a46212ffc5bb54b0d8f3327dfa47693bbfdf Mon Sep 17 00:00:00 2001 From: Sergey Zagoruyko Date: Mon, 8 Feb 2016 01:22:07 +0100 Subject: [PATCH] lazy init --- BCECriterion.lua | 4 ++-- LogSigmoid.lua | 6 +----- PReLU.lua | 4 ++-- Reshape.lua | 6 ++---- SpatialAdaptiveMaxPooling.lua | 3 +-- SpatialConvolution.lua | 4 ++-- SpatialConvolutionLocal.lua | 9 ++++----- SpatialConvolutionMM.lua | 9 ++++----- SpatialCrossMapLRN.lua | 4 ++-- SpatialFractionalMaxPooling.lua | 2 +- SpatialFullConvolution.lua | 9 ++++----- SpatialMaxPooling.lua | 1 + TemporalMaxPooling.lua | 3 +-- VolumetricConvolution.lua | 9 ++++----- VolumetricFullConvolution.lua | 5 ++--- VolumetricMaxPooling.lua | 1 + WeightedMSECriterion.lua | 2 +- 17 files changed, 35 insertions(+), 46 deletions(-) diff --git a/BCECriterion.lua b/BCECriterion.lua index 2c614b32a..b31933589 100644 --- a/BCECriterion.lua +++ b/BCECriterion.lua @@ -30,7 +30,7 @@ function BCECriterion:updateOutput(input, target) assert( input:nElement() == target:nElement(), "input and target size mismatch") - self.buffer = self.buffer or torch.Tensor():typeAs(input) + self.buffer = self.buffer or input.new() local buffer = self.buffer local weights = self.weights @@ -74,7 +74,7 @@ function BCECriterion:updateGradInput(input, target) assert( input:nElement() == target:nElement(), "input and target size mismatch") - self.buffer = self.buffer or torch.Tensor():typeAs(input) + self.buffer = self.buffer or input.new() local buffer = self.buffer local weights = self.weights diff --git a/LogSigmoid.lua b/LogSigmoid.lua index 1e21d3f9a..cab848f4d 100644 --- a/LogSigmoid.lua +++ b/LogSigmoid.lua @@ -1,11 +1,7 @@ local LogSigmoid, parent = torch.class('nn.LogSigmoid', 'nn.Module') -function LogSigmoid:__init() - parent.__init(self) - self.buffer = torch.Tensor() -end - function LogSigmoid:updateOutput(input) + self.buffer = self.buffer or input.new() input.THNN.LogSigmoid_updateOutput( input:cdata(), self.output:cdata(), diff --git a/PReLU.lua b/PReLU.lua index 4405c6632..2e58fba4e 100644 --- a/PReLU.lua +++ b/PReLU.lua @@ -6,8 +6,6 @@ function PReLU:__init(nOutputPlane) self.nOutputPlane = nOutputPlane or 0 self.weight = torch.Tensor(nOutputPlane or 1):fill(0.25) self.gradWeight = torch.Tensor(nOutputPlane or 1) - self.gradWeightBuf = torch.Tensor() - self.gradWeightBuf2 = torch.Tensor() end function PReLU:updateOutput(input) @@ -32,6 +30,8 @@ function PReLU:updateGradInput(input, gradOutput) end function PReLU:accGradParameters(input, gradOutput, scale) + self.gradWeightBuf = self.gradWeightBuf or input.new() + self.gradWeightBuf2 = self.gradWeightBuf2 or input.new() input.THNN.PReLU_accGradParameters( input:cdata(), gradOutput:cdata(), diff --git a/Reshape.lua b/Reshape.lua index 80f8b014c..4f7c0a177 100644 --- a/Reshape.lua +++ b/Reshape.lua @@ -26,14 +26,12 @@ function Reshape:__init(...) self.nelement = self.nelement * self.size[i] self.batchsize[i+1] = self.size[i] end - - -- only used for non-contiguous input or gradOutput - self._input = torch.Tensor() - self._gradOutput = torch.Tensor() end function Reshape:updateOutput(input) if not input:isContiguous() then + self._input = self._input or input.new() + self._gradOutput = self._gradOutput or input.new() self._input:resizeAs(input) self._input:copy(input) input = self._input diff --git a/SpatialAdaptiveMaxPooling.lua b/SpatialAdaptiveMaxPooling.lua index efe4515c2..74d4cd654 100644 --- a/SpatialAdaptiveMaxPooling.lua +++ b/SpatialAdaptiveMaxPooling.lua @@ -5,11 +5,10 @@ function SpatialAdaptiveMaxPooling:__init(W, H) self.W = W self.H = H - - self.indices = torch.Tensor() end function SpatialAdaptiveMaxPooling:updateOutput(input) + self.indices = self.indices or input.new() input.THNN.SpatialAdaptiveMaxPooling_updateOutput( input:cdata(), self.output:cdata(), diff --git a/SpatialConvolution.lua b/SpatialConvolution.lua index 452ad82b4..323e9d646 100644 --- a/SpatialConvolution.lua +++ b/SpatialConvolution.lua @@ -155,8 +155,8 @@ function SpatialConvolution:accGradParameters(input, gradOutput, scale) end function SpatialConvolution:type(type,tensorCache) - self.finput = torch.Tensor() - self.fgradInput = torch.Tensor() + self.finput = self.finput and torch.Tensor() + self.fgradInput = self.fgradInput and torch.Tensor() return parent.type(self,type,tensorCache) end diff --git a/SpatialConvolutionLocal.lua b/SpatialConvolutionLocal.lua index 983dbc5a0..75090c519 100644 --- a/SpatialConvolutionLocal.lua +++ b/SpatialConvolutionLocal.lua @@ -26,9 +26,6 @@ function SpatialConvolutionLocal:__init(nInputPlane, nOutputPlane, iW, iH ,kW, k self.gradWeight = torch.Tensor():resizeAs(self.weight) self.gradBias = torch.Tensor():resizeAs(self.bias) - self.finput = torch.Tensor() - self.fgradInput = torch.Tensor() - self:reset() end @@ -117,6 +114,8 @@ local function checkOutputSize(self, input, output) end function SpatialConvolutionLocal:updateOutput(input) + self.finput = self.finput or input.new() + self.fgradInput = self.fgradInput or input.new() checkInputSize(self, input) viewWeight(self) input = makeContiguous(self, input) @@ -148,8 +147,8 @@ function SpatialConvolutionLocal:accGradParameters(input, gradOutput, scale) end function SpatialConvolutionLocal:type(type,tensorCache) - self.finput = torch.Tensor() - self.fgradInput = torch.Tensor() + self.finput = self.finput and torch.Tensor() + self.fgradInput = self.fgradInput and torch.Tensor() return parent.type(self,type,tensorCache) end diff --git a/SpatialConvolutionMM.lua b/SpatialConvolutionMM.lua index f523d8b03..6bcd2b908 100644 --- a/SpatialConvolutionMM.lua +++ b/SpatialConvolutionMM.lua @@ -21,9 +21,6 @@ function SpatialConvolutionMM:__init(nInputPlane, nOutputPlane, kW, kH, dW, dH, self.gradWeight = torch.Tensor(nOutputPlane, nInputPlane*kH*kW) self.gradBias = torch.Tensor(nOutputPlane) - self.finput = torch.Tensor() - self.fgradInput = torch.Tensor() - self:reset() end @@ -63,6 +60,8 @@ local function makeContiguous(self, input, gradOutput) end function SpatialConvolutionMM:updateOutput(input) + self.finput = self.finput or input.new() + self.fgradInput = self.fgradInput or input.new() -- backward compatibility if self.padding then self.padW = self.padding @@ -121,8 +120,8 @@ function SpatialConvolutionMM:accGradParameters(input, gradOutput, scale) end function SpatialConvolutionMM:type(type,tensorCache) - self.finput = torch.Tensor() - self.fgradInput = torch.Tensor() + self.finput = self.finput and torch.Tensor() + self.fgradInput = self.fgradInput and torch.Tensor() return parent.type(self,type,tensorCache) end diff --git a/SpatialCrossMapLRN.lua b/SpatialCrossMapLRN.lua index 674ca200f..2440cd445 100644 --- a/SpatialCrossMapLRN.lua +++ b/SpatialCrossMapLRN.lua @@ -7,14 +7,14 @@ function SpatialCrossMapLRN:__init(size, alpha, beta, k) self.alpha = alpha or 0.0001 self.beta = beta or 0.75 self.k = k or 1 - - self.scale = torch.Tensor() end function SpatialCrossMapLRN:updateOutput(input) assert(input:dim() == 3 or input:dim() == 4, 'Input must be 3D or 4D') + self.scale = self.scale or input.new() + if torch.type(input) == 'torch.CudaTensor' then input.nn.SpatialCrossMapLRN_updateOutput(self, input) else diff --git a/SpatialFractionalMaxPooling.lua b/SpatialFractionalMaxPooling.lua index b2267bad0..4aa2c1b70 100644 --- a/SpatialFractionalMaxPooling.lua +++ b/SpatialFractionalMaxPooling.lua @@ -15,7 +15,6 @@ function SpatialFractionalMaxPooling:__init(poolSizeW, poolSizeH, arg1, arg2) -- Pool size (how wide the pooling for each output unit is) self.poolSizeW = poolSizeW self.poolSizeH = poolSizeH - self.indices = torch.Tensor() -- Random samples are drawn for all -- batch * plane * (height, width; i.e., 2) points. This determines @@ -115,6 +114,7 @@ function SpatialFractionalMaxPooling:fixPoolingRegions(val) end function SpatialFractionalMaxPooling:updateOutput(input) + self.indices = self.indices or input.new() self:initSampleBuffer_(input) local outW, outH = self:getOutputSizes_(input) diff --git a/SpatialFullConvolution.lua b/SpatialFullConvolution.lua index 10142b8d9..dc9d9444f 100644 --- a/SpatialFullConvolution.lua +++ b/SpatialFullConvolution.lua @@ -28,9 +28,6 @@ function SpatialFullConvolution:__init(nInputPlane, nOutputPlane, self.bias = torch.Tensor(self.nOutputPlane) self.gradBias = torch.Tensor(self.nOutputPlane) - self.finput = torch.Tensor() - self.fgradInput = torch.Tensor() - self:reset() end @@ -69,6 +66,8 @@ function SpatialFullConvolution:backCompatibility() end function SpatialFullConvolution:updateOutput(input) + self.finput = self.finput or input.new() + self.fgradInput = self.fgradInput or input.new() self:backCompatibility() input = makeContiguous(self, input) @@ -92,8 +91,8 @@ function SpatialFullConvolution:accGradParameters(input, gradOutput, scale) end function SpatialFullConvolution:type(type, tensorCache) - self.finput = torch.Tensor() - self.fgradInput = torch.Tensor() + self.finput = self.finput and torch.Tensor() + self.fgradInput = self.fgradInput and torch.Tensor() return parent.type(self, type, tensorCache) end diff --git a/SpatialMaxPooling.lua b/SpatialMaxPooling.lua index 76b0d96c6..baf72da5e 100644 --- a/SpatialMaxPooling.lua +++ b/SpatialMaxPooling.lua @@ -29,6 +29,7 @@ function SpatialMaxPooling:floor() end function SpatialMaxPooling:updateOutput(input) + self.indices = self.indices or input.new() -- backward compatibility self.ceil_mode = self.ceil_mode or false self.padW = self.padW or 0 diff --git a/TemporalMaxPooling.lua b/TemporalMaxPooling.lua index 881eba21e..faa25eb5b 100644 --- a/TemporalMaxPooling.lua +++ b/TemporalMaxPooling.lua @@ -7,11 +7,10 @@ function TemporalMaxPooling:__init(kW, dW) self.kW = kW self.dW = dW - - self.indices = torch.Tensor() end function TemporalMaxPooling:updateOutput(input) + self.indices = self.indices or input.new() input.nn.TemporalMaxPooling_updateOutput(self, input) return self.output end diff --git a/VolumetricConvolution.lua b/VolumetricConvolution.lua index 60dcbc2dd..e5555e8e0 100644 --- a/VolumetricConvolution.lua +++ b/VolumetricConvolution.lua @@ -23,9 +23,6 @@ function VolumetricConvolution:__init(nInputPlane, nOutputPlane, kT, kW, kH, dT, self.bias = torch.Tensor(nOutputPlane) self.gradWeight = torch.Tensor(nOutputPlane, nInputPlane, kT, kH, kW) self.gradBias = torch.Tensor(nOutputPlane) - -- temporary buffers for unfolding (CUDA) - self.finput = torch.Tensor() - self.fgradInput = torch.Tensor() self:reset() end @@ -80,6 +77,8 @@ local function unviewWeight(self) end function VolumetricConvolution:updateOutput(input) + self.finput = self.finput or input.new() + self.fgradInput = self.fgradInput or input.new() if input:type() == 'torch.CudaTensor' then input.THNN.VolumetricConvolution_updateOutput( input:cdata(), @@ -171,8 +170,8 @@ function VolumetricConvolution:accGradParameters(input, gradOutput, scale) end function VolumetricConvolution:type(type, tensorCache) - self.finput:set() - self.fgradInput:set() + if self.finput then self.finput:set() end + if self.fgradInput then self.fgradInput:set() end return parent.type(self, type, tensorCache) end diff --git a/VolumetricFullConvolution.lua b/VolumetricFullConvolution.lua index f9e157228..70784c1f0 100644 --- a/VolumetricFullConvolution.lua +++ b/VolumetricFullConvolution.lua @@ -27,9 +27,6 @@ function VolumetricFullConvolution:__init(nInputPlane, nOutputPlane, kT, kH, kW, self.bias = torch.Tensor(nOutputPlane) self.gradWeight = torch.Tensor(nOutputPlane, nInputPlane, kT, kH, kW) self.gradBias = torch.Tensor(nOutputPlane) - -- temporary buffers for unfolding (CUDA) - self.finput = torch.Tensor() - self.fgradInput = torch.Tensor() self:reset() end @@ -54,6 +51,8 @@ function VolumetricFullConvolution:reset(stdv) end function VolumetricFullConvolution:updateOutput(input) + self.finput = self.finput or input.new() + self.fgradInput = self.fgradInput or input.new() input.THNN.VolumetricFullConvolution_updateOutput( input:cdata(), self.output:cdata(), diff --git a/VolumetricMaxPooling.lua b/VolumetricMaxPooling.lua index 8e2deaf47..54eb39cac 100644 --- a/VolumetricMaxPooling.lua +++ b/VolumetricMaxPooling.lua @@ -36,6 +36,7 @@ function VolumetricMaxPooling:floor() end function VolumetricMaxPooling:updateOutput(input) + self.indices = self.indices or input.new() input.THNN.VolumetricMaxPooling_updateOutput( input:cdata(), self.output:cdata(), diff --git a/WeightedMSECriterion.lua b/WeightedMSECriterion.lua index 274cb32aa..933472937 100644 --- a/WeightedMSECriterion.lua +++ b/WeightedMSECriterion.lua @@ -3,10 +3,10 @@ local WeightedMSECriterion, parent = torch.class('nn.WeightedMSECriterion','nn.M function WeightedMSECriterion:__init(w) parent.__init(self) self.weight = w:clone() - self.buffer = torch.Tensor() end function WeightedMSECriterion:updateOutput(input,target) + self.buffer = self.buffer or input.new() self.buffer:resizeAs(input):copy(target) if input:dim() - 1 == self.weight:dim() then for i=1,input:size(1) do