|
| 1 | +------------------------------------------------------------------------ |
| 2 | +--[[ GPU ]]-- |
| 3 | +-- Decorates a module such that its parameters are |
| 4 | +-- hosted on a specified GPU device. |
| 5 | +-- The operations are also executed on that device. |
| 6 | +-- Arguments input and gradOutput are converted to the specified device |
| 7 | +-- before being fed to the decorated module. |
| 8 | +-- Returned output is on the specified outdevice (defaults to device). |
| 9 | +-- Returned gradInput is allocated on the same device as the input. |
| 10 | +-- The unit test is located in cunn. |
| 11 | +------------------------------------------------------------------------ |
| 12 | +local GPU, parent = torch.class("nn.GPU", "nn.Container") |
| 13 | + |
| 14 | +function GPU:__init(module, device, outdevice) |
| 15 | + parent.__init(self) |
| 16 | + assert(torch.type(device) == 'number') |
| 17 | + self.device = device |
| 18 | + self.outdevice = outdevice or device |
| 19 | + |
| 20 | + assert(torch.isTypeOf(module, 'nn.Module')) |
| 21 | + self.modules[1] = module |
| 22 | + |
| 23 | + if module:type() == 'torch.CudaTensor' then |
| 24 | + self:cuda() |
| 25 | + end |
| 26 | +end |
| 27 | + |
| 28 | +function GPU.recursiveModuleDevice(obj, device) |
| 29 | + if type(obj) == 'table' and not torch.isTypeOf(obj, 'nn.GPU') and not obj.__noGPU__ then |
| 30 | + for k,v in pairs(obj) do |
| 31 | + obj[k] = GPU.recursiveModuleDevice(v, device) |
| 32 | + end |
| 33 | + elseif torch.type(obj):match('torch.Cuda.*Tensor') then |
| 34 | + if obj:getDevice() ~= device then |
| 35 | + obj = obj:clone() -- this will reallocate it to device |
| 36 | + local newdevice = obj:getDevice() |
| 37 | + -- when nElement() == 0 newdevice is 0 |
| 38 | + assert(newdevice == device or newdevice == 0) |
| 39 | + end |
| 40 | + end |
| 41 | + assert(obj ~= nil) |
| 42 | + return obj |
| 43 | +end |
| 44 | + |
| 45 | +-- set the device of the decorated module |
| 46 | +function GPU:setDevice(device) |
| 47 | + self.device = device or self.device |
| 48 | + |
| 49 | + assert(self.modules[1]) |
| 50 | + self.modules[1] = cutorch.withDevice(self.device, function() |
| 51 | + return self.recursiveModuleDevice(self.modules[1], self.device) |
| 52 | + end) |
| 53 | + return self |
| 54 | +end |
| 55 | + |
| 56 | +-- when proto is a device number, returns a dst that has device device for each element in src |
| 57 | +-- otherwise, if proto is a table/tensor, makes sure dst is a identical to src, yet on the same device as proto |
| 58 | +function GPU.recursiveSetDevice(dst, src, proto) |
| 59 | + local device, prototable |
| 60 | + if torch.isTensor(proto) then |
| 61 | + device = proto:getDevice() |
| 62 | + elseif torch.type(proto) == 'number' then |
| 63 | + device = proto |
| 64 | + elseif torch.type(proto) == 'table' then |
| 65 | + prototable = true |
| 66 | + else |
| 67 | + error"Expecting number, table or tensor for arg 3 (proto)" |
| 68 | + end |
| 69 | + if torch.type(src) == 'table' then |
| 70 | + dst = torch.type(dst) == 'table' and dst or {} |
| 71 | + for k,v in ipairs(src) do |
| 72 | + dst[k] = GPU.recursiveSetDevice(dst[k], v, prototable and proto[k] or device) |
| 73 | + end |
| 74 | + for k=#src+1,#dst do |
| 75 | + dst[k] = nil |
| 76 | + end |
| 77 | + elseif torch.type(src):match('torch.Cuda.*Tensor') and src:getDevice() ~= device and src:getDevice() ~= 0 then |
| 78 | + if not (torch.type(dst):match('torch.Cuda.*Tensor') and dst:getDevice() == device) then |
| 79 | + dst = src.new() |
| 80 | + end |
| 81 | + cutorch.withDevice(device, function() dst:resizeAs(src):copy(src) end) |
| 82 | + else |
| 83 | + dst = src |
| 84 | + end |
| 85 | + return dst |
| 86 | +end |
| 87 | + |
| 88 | +function GPU:updateOutput(input) |
| 89 | + if self._type == 'torch.CudaTensor' then |
| 90 | + self._input = self.recursiveSetDevice(self._input, input, self.device) |
| 91 | + |
| 92 | + local output = cutorch.withDevice(self.device, function() |
| 93 | + return self.modules[1]:updateOutput(self._input) |
| 94 | + end) |
| 95 | + |
| 96 | + if self.device ~= self.outdevice then |
| 97 | + self.output = self.recursiveSetDevice(self.output, output, self.outdevice) |
| 98 | + else |
| 99 | + self.output = output |
| 100 | + end |
| 101 | + else |
| 102 | + self.output = self.modules[1]:updateOutput(input) |
| 103 | + end |
| 104 | + |
| 105 | + return self.output |
| 106 | +end |
| 107 | + |
| 108 | +function GPU:updateGradInput(input, gradOutput) |
| 109 | + if self._type == 'torch.CudaTensor' then |
| 110 | + self._gradOutput = self.recursiveSetDevice(self._gradOutput, gradOutput, self.device) |
| 111 | + |
| 112 | + local gradInput = cutorch.withDevice(self.device, function() |
| 113 | + return self.modules[1]:updateGradInput(self._input, self._gradOutput) |
| 114 | + end) |
| 115 | + |
| 116 | + self.gradInput = self.recursiveSetDevice(self.gradInput, gradInput, input) |
| 117 | + else |
| 118 | + self.gradInput = self.modules[1]:updateGradInput(input, gradOutput) |
| 119 | + end |
| 120 | + |
| 121 | + return self.gradInput |
| 122 | +end |
| 123 | + |
| 124 | +function GPU:accGradParameters(input, gradOutput, scale) |
| 125 | + if self._type == 'torch.CudaTensor' then |
| 126 | + cutorch.withDevice(self.device, function() |
| 127 | + self.modules[1]:accGradParameters(self._input, self._gradOutput, scale) |
| 128 | + end) |
| 129 | + else |
| 130 | + self.modules[1]:accGradParameters(input, gradOutput, scale) |
| 131 | + end |
| 132 | +end |
| 133 | + |
| 134 | +function GPU:apply(callback) |
| 135 | + if self._type == 'torch.CudaTensor' then |
| 136 | + cutorch.withDevice(self.device, function() parent.apply(self, callback) end) |
| 137 | + else |
| 138 | + parent.apply(self, callback) |
| 139 | + end |
| 140 | +end |
| 141 | + |
| 142 | +function GPU:type(type, typecache) |
| 143 | + if type and type == 'torch.CudaTensor' then |
| 144 | + cutorch.withDevice(self.device, function() parent.type(self, type, typecache) end) |
| 145 | + self:setDevice() |
| 146 | + else |
| 147 | + self.output = nil |
| 148 | + self.gradInput = nil |
| 149 | + self._input = nil |
| 150 | + self._gradOutput = nil |
| 151 | + parent.type(self, type, typecache) |
| 152 | + end |
| 153 | + return self |
| 154 | +end |
| 155 | + |
| 156 | +function GPU:clearState() |
| 157 | + nn.utils.clear(self, 'output', 'gradInput') |
| 158 | + self._input = nil |
| 159 | + self._gradOutput = nil |
| 160 | + if self._type == 'torch.CudaTensor' then |
| 161 | + cutorch.withDevice(self.device, function() parent.clearState(self) end) |
| 162 | + else |
| 163 | + parent.clearState(self) |
| 164 | + end |
| 165 | +end |
| 166 | + |
| 167 | +function GPU:zeroGradParameters() |
| 168 | + if self._type == 'torch.CudaTensor' then |
| 169 | + cutorch.withDevice(self.device, function() parent.zeroGradParameters(self) end) |
| 170 | + else |
| 171 | + parent.zeroGradParameters(self) |
| 172 | + end |
| 173 | +end |
| 174 | + |
| 175 | +function GPU:updateParameters(lr) |
| 176 | + if self._type == 'torch.CudaTensor' then |
| 177 | + cutorch.withDevice(self.device, function() parent.updateParameters(self, lr) end) |
| 178 | + else |
| 179 | + parent.updateParameters(self, lr) |
| 180 | + end |
| 181 | +end |
| 182 | + |
| 183 | +function GPU:training() |
| 184 | + if self._type == 'torch.CudaTensor' then |
| 185 | + cutorch.withDevice(self.device, function() parent.training(self) end) |
| 186 | + else |
| 187 | + parent.training(self) |
| 188 | + end |
| 189 | +end |
| 190 | + |
| 191 | +function GPU:evaluate() |
| 192 | + if self._type == 'torch.CudaTensor' then |
| 193 | + cutorch.withDevice(self.device, function() parent.evaluate(self) end) |
| 194 | + else |
| 195 | + parent.evaluate(self) |
| 196 | + end |
| 197 | +end |
| 198 | + |
| 199 | +function GPU:share(mlp, ...) |
| 200 | + local args = {...} |
| 201 | + if self._type == 'torch.CudaTensor' then |
| 202 | + cutorch.withDevice(self.device, function() parent.share(self, mlp, unpack(args)) end) |
| 203 | + else |
| 204 | + parent.share(self, mlp, unpack(args)) |
| 205 | + end |
| 206 | + return self |
| 207 | +end |
| 208 | + |
| 209 | +function GPU:reset(...) |
| 210 | + local args = {...} |
| 211 | + if self._type == 'torch.CudaTensor' then |
| 212 | + cutorch.withDevice(self.device, function() parent.reset(self, unpack(args)) end) |
| 213 | + else |
| 214 | + parent.reset(self, unpack(args)) |
| 215 | + end |
| 216 | + return self |
| 217 | +end |
| 218 | + |
| 219 | +function GPU:clone(...) |
| 220 | + local args = {...} |
| 221 | + if self._type == 'torch.CudaTensor' then |
| 222 | + return cutorch.withDevice(self.device, function() parent.clone(self, unpack(args)) end) |
| 223 | + else |
| 224 | + return parent.clone(self, unpack(args)) |
| 225 | + end |
| 226 | +end |
| 227 | + |
| 228 | +function GPU:write(file) |
| 229 | + -- Write all values in the object as a table. |
| 230 | + local object = {} |
| 231 | + for k, v in pairs(self) do |
| 232 | + object[k] = v |
| 233 | + end |
| 234 | + local header = {self._type, self.device} |
| 235 | + file:writeObject(header) |
| 236 | + file:writeObject(object) |
| 237 | +end |
| 238 | + |
| 239 | +function GPU:read(file) |
| 240 | + local header = file:readObject() |
| 241 | + local object |
| 242 | + if header[1] == 'torch.CudaTensor' then |
| 243 | + local device = header[2] |
| 244 | + if device > cutorch.getDeviceCount() then |
| 245 | + print"Warning : model was saved with more devices than available on current host." |
| 246 | + print"Attempting to load module onto device 1" |
| 247 | + device = 1 |
| 248 | + end |
| 249 | + object = cutorch.withDevice(device, function() return file:readObject() end) |
| 250 | + else |
| 251 | + object = file:readObject() |
| 252 | + end |
| 253 | + |
| 254 | + for k, v in pairs(object) do |
| 255 | + self[k] = v |
| 256 | + end |
| 257 | +end |
| 258 | + |
| 259 | +function GPU:__tostring__() |
| 260 | + if self.modules[1].__tostring__ then |
| 261 | + return torch.type(self) .. '(' .. self.device ..') @ ' .. self.modules[1]:__tostring__() |
| 262 | + else |
| 263 | + return torch.type(self) .. '(' .. self.device ..') @ ' .. torch.type(self.modules[1]) |
| 264 | + end |
| 265 | +end |
| 266 | + |
| 267 | +function GPU:accUpdateGradParameters(input, gradOutput, lr) |
| 268 | + error("Not Implemented for "..torch.type(self)) |
| 269 | +end |
| 270 | + |
| 271 | +function GPU:sharedAccUpdateGradParameters(input, gradOutput, lr) |
| 272 | + error("Not Implemented for "..torch.type(self)) |
| 273 | +end |
0 commit comments