You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/09/12 18:01:09 UTC

[GitHub] szha commented on a change in pull request #12512: [MXNET-938] Allow fusing weight ahead of calling forward()

szha commented on a change in pull request #12512: [MXNET-938] Allow fusing weight ahead of calling forward()
URL: https://github.com/apache/incubator-mxnet/pull/12512#discussion_r217132369
 
 

 ##########
 File path: python/mxnet/gluon/rnn/rnn_layer.py
 ##########
 @@ -183,6 +184,16 @@ def begin_state(self, batch_size=0, func=ndarray.zeros, **kwargs):
             states.append(func(name='%sh0_%d'%(self.prefix, i), **info))
         return states
 
+    def fuse_weights(self, F):
+        if F is ndarray:
+            kwargs = {i: j.data() for i, j in self._reg_params.items()}
+            params = (kwargs['{}{}_{}_{}'.format(d, l, g, t)].reshape(-1)
+                  for t in ['weight', 'bias']
+                  for l in range(self._num_layers)
+                  for d in ['l', 'r'][:self._dir]
+                  for g in ['i2h', 'h2h'])
+            self.fused_params = F.concat(*params, dim=0)
 
 Review comment:
   The fused parameter wouldn't reflect updates in the parameters.
   ```
   In [1]: import mxnet as mx
   
   In [2]: a = mx.nd.arange(5)
   
   In [3]: b = mx.nd.ones((3,))
   
   In [4]: c = mx.nd.concat(a, b, dim=0)
   
   In [5]: c
   Out[5]:
   
   [0. 1. 2. 3. 4. 1. 1. 1.]
   <NDArray 8 @cpu(0)>
   
   In [6]: a[0] = 5
   
   In [7]: a
   Out[7]:
   
   [5. 1. 2. 3. 4.]
   <NDArray 5 @cpu(0)>
   
   In [8]: c
   Out[8]:
   
   [0. 1. 2. 3. 4. 1. 1. 1.]
   <NDArray 8 @cpu(0)>
   ```

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


With regards,
Apache Git Services