parameters: (self, index, grad, weight)
docstring: Updates weight given gradient and index.
docstring_summary: Updates weight given gradient and index.
docstring_tokens: Updates weight given gradient and index .
function: def __call__(self, index, grad, weight):
          """Updates weight given gradient and index."""
          allow_np = self.optimizer.allow_np_array if hasattr(self.optimizer, "allow_np_array") else is_np_array()
          if not isinstance(index, (list, tuple)):
              indices = [index]
              grads = [_as_classic(grad, allow_np)]
              weights = [_as_classic(weight, allow_np)]
          else:
              indices = index
              grads = _as_classic(grad, allow_np)
              weights = _as_classic(weight, allow_np)
          if weights:
              self.optimizer._set_current_device(weights[0].context.device_id)
          for i, idx in enumerate(indices):
              # convert ctypes.char_p.value back to python str if needed
              if isinstance(idx, bytes):
                  indices[i] = py_str(idx)
                  idx = indices[i]
              if idx not in self.states:
                  with profiler_scope("updater:optimizer_state"):
                      self.states[idx] = self.optimizer.create_state_multi_precision(idx, weights[i])
                  self.states_synced[idx] = True
              elif not self.states_synced[idx]:
                  self.states[idx] = \
                      self.sync_state_context(self.states[idx], weights[i].context)
                  self.states_synced[idx] = True
          if self.aggregate_updates:
              # segregate values based on type
              if self.optimizer.aggregate_num is not numpy.inf:
                  type_map = {}
                  for i, w, g in zip(indices, weights, grads):
                      if w.dtype in type_map:
                          type_map[w.dtype].append((i, w, g))
                      else:
                          type_map[w.dtype] = [(i, w, g)]
                  for idx in type_map:
                      current_index = 0
                      indices, weights, grads = zip(*type_map[idx])
                      while current_index < len(indices):
                          states = []
                          step = min(self.optimizer.aggregate_num, len(indices) - current_index)
                          for j in range(step):
                              states.append(self.states[indices[current_index + j]])
                          self.optimizer.update_multi_precision(
                              indices[current_index:current_index + self.optimizer.aggregate_num],
                              weights[current_index:current_index + self.optimizer.aggregate_num],
                              grads[current_index:current_index + self.optimizer.aggregate_num],
                              states)
                          current_index += self.optimizer.aggregate_num
              else:
                  states = [self.states[i] for i in indices]
                  self.optimizer.update_multi_precision(indices, weights, grads, states)
          else:
              for i, w, g in zip(indices, weights, grads):
                  self.optimizer.update_multi_precision([i], [w], [g], [self.states[i]])
function_tokens: def __call__ ( self , index , grad , weight ) : allow_np = self . optimizer . allow_np_array if hasattr ( self . optimizer , "allow_np_array" ) else is_np_array ( ) if not isinstance ( index , ( list , tuple ) ) : indices = [ index ] grads = [ _as_classic ( grad , allow_np ) ] weights = [ _as_classic ( weight , allow_np ) ] else : indices = index grads = _as_classic ( grad , allow_np ) weights = _as_classic ( weight , allow_np ) if weights : self . optimizer . _set_current_device ( weights [ 0 ] . context . device_id ) for i , idx in enumerate ( indices ) : # convert ctypes.char_p.value back to python str if needed if isinstance ( idx , bytes ) : indices [ i ] = py_str ( idx ) idx = indices [ i ] if idx not in self . states : with profiler_scope ( "updater:optimizer_state" ) : self . states [ idx ] = self . optimizer . create_state_multi_precision ( idx , weights [ i ] ) self . states_synced [ idx ] = True elif not self . states_synced [ idx ] : self . states [ idx ] = self . sync_state_context ( self . states [ idx ] , weights [ i ] . context ) self . states_synced [ idx ] = True if self . aggregate_updates : # segregate values based on type if self . optimizer . aggregate_num is not numpy . inf : type_map = { } for i , w , g in zip ( indices , weights , grads ) : if w . dtype in type_map : type_map [ w . dtype ] . append ( ( i , w , g ) ) else : type_map [ w . dtype ] = [ ( i , w , g ) ] for idx in type_map : current_index = 0 indices , weights , grads = zip ( * type_map [ idx ] ) while current_index < len ( indices ) : states = [ ] step = min ( self . optimizer . aggregate_num , len ( indices ) - current_index ) for j in range ( step ) : states . append ( self . states [ indices [ current_index + j ] ] ) self . optimizer . update_multi_precision ( indices [ current_index : current_index + self . optimizer . aggregate_num ] , weights [ current_index : current_index + self . optimizer . aggregate_num ] , grads [ current_index : current_index + self . optimizer . aggregate_num ] , states ) current_index += self . optimizer . aggregate_num else : states = [ self . states [ i ] for i in indices ] self . optimizer . update_multi_precision ( indices , weights , grads , states ) else : for i , w , g in zip ( indices , weights , grads ) : self . optimizer . update_multi_precision ( [ i ] , [ w ] , [ g ] , [ self . states [ i ] ] )