def prepare(self):
"""Prepares for an update.
This method initializes missing optimizer states (e.g. for newly added
parameters after the set up), and copies arrays in each state
dictionary to CPU or GPU according to the corresponding parameter
array.
"""
states = self._states
for name, param in self.target.namedparams():
if name not in states:
state = {}
self.init_state(param, state)
states[name] = state
else:
state = states[name]
with cuda.get_device(param.data) as dev:
if int(dev) == -1: # cpu
for key, value in six.iteritems(state):
if isinstance(value, cuda.ndarray):
state[key] = value.get()
else: # gpu
cupy = cuda.cupy
for key, value in six.iteritems(state):
if isinstance(value, numpy.ndarray):
state[key] = cuda.to_gpu(value)
elif (isinstance(value, cupy.ndarray) and
value.device != dev):
state[key] = cupy.copy(value)
评论列表
文章目录