eric-haibin-lin commented on a change in pull request #16893: Multi-tensor LAMB
URL: https://github.com/apache/incubator-mxnet/pull/16893#discussion_r363403135
 
 

 ##########
 File path: python/mxnet/optimizer/optimizer.py
 ##########
 @@ -1259,53 +1260,121 @@ def __init__(self, learning_rate=0.001, beta1=0.9, 
beta2=0.999, epsilon=1e-6,
         self.lower_bound = lower_bound
         self.upper_bound = upper_bound
         self.bias_correction = bias_correction
-
+        self.aggregate_num = max(1, min(45, 
int(os.getenv('MXNET_OPTIMIZER_AGGREGATION_SIZE', "45"))))
 
     def create_state(self, index, weight):
         stype = weight.stype
-        return (zeros(weight.shape, weight.context, dtype=numpy.float32, 
stype=stype),
-                zeros(weight.shape, weight.context, dtype=numpy.float32, 
stype=stype))
+        dtype = weight.dtype
+        return (zeros(weight.shape, weight.context, dtype=dtype, stype=stype),
+                zeros(weight.shape, weight.context, dtype=dtype, stype=stype))
 
     def _update_impl(self, index, weight, grad, state, multi_precision=False):
-        assert(isinstance(weight, NDArray))
-        assert(isinstance(grad, NDArray))
-        self._update_count(index)
-        lr = self._get_lr(index)
-        wd = self._get_wd(index)
-        t = self._index_update_count[index]
-
         kwargs = {'beta1': self.beta1, 'beta2': self.beta2, 'epsilon': 
self.epsilon,
-                  'bias_correction': self.bias_correction, 't': t,
+                  'bias_correction': self.bias_correction,
                   'rescale_grad': self.rescale_grad}
 
-        if self.clip_gradient:
-            kwargs['clip_gradient'] = self.clip_gradient
-
-        if multi_precision:
-            mean, var = state[1]
-            weight32 = state[0]
-            g = mp_lamb_update_phase1(weight, grad, mean, var, weight32, 
wd=wd, **kwargs)
+        if self.aggregate_num <= 1 or not isinstance(index, (tuple, list)):
+            if not isinstance(index, (tuple, list)):
+              assert(isinstance(weight, NDArray))
+              assert(isinstance(grad, NDArray))
+              self._update_count(index)
+              lr = self._get_lr(index)
+              wd = self._get_wd(index)
+              t = self._index_update_count[index]
+              weight_ptr = weight
+              grad_ptr = grad
+              if multi_precision:
+                mean, var = state[1]
+                weight32 = state[0]
+              else:
+                mean, var = state
+            else:
+              assert(len(index)==self.aggregate_num)
 
 Review comment:
   maybe it's cleaner to have one line for this case:
   ```
   if isinstance(index, (tuple, list)):
       index, weight, grad, state = index[0], weight[0], grad[0], state[0]
   
   # then followed by line 1278 - 1290 shared by both cases
   ```

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to