sxjscience opened a new issue #10167: HybridBlock can be slower than Block
URL: https://github.com/apache/incubator-mxnet/issues/10167
 
 
   Here's an interesting example that HybridBlock can sometimes be much slower 
than Block. We define an Identity block using two strategies and compare the 
running time.
   
   ```python
   import mxnet as mx
   import time
   from mxnet.gluon import Block, HybridBlock
   
   
   class HybridIdentity(HybridBlock):
       def hybrid_forward(self, F, x):
           return x
   
   class NoHybridIdentity(Block):
       def forward(self, x):
           return x
   
   
   class LoopIdentityBlock(Block):
       def __init__(self, use_hybrid=False, prefix=None, params=None):
           super(LoopIdentityBlock, self).__init__(prefix=prefix, params=params)
           if use_hybrid:
               self.identity = HybridIdentity()
           else:
               self.identity = NoHybridIdentity()
   
       def forward(self, x):
           for i in range(100000):
               x = self.identity(x)
           return x
   
   
   for _ in range(5):
       _ = mx.nd.ones((100, 100)).sum().asscalar()
   
   hybrid_loop_block = LoopIdentityBlock(use_hybrid=True)
   loop_block = LoopIdentityBlock(use_hybrid=False)
   x = mx.nd.ones((1024, 1024, 512))
   start = time.time()
   for _ in range(5):
       loop_block(x)
       mx.nd.waitall()
   end = time.time()
   print('Identity W/o Hybrid:', end - start)
   
   start = time.time()
   for _ in range(5):
       hybrid_loop_block(x)
       mx.nd.waitall()
   end = time.time()
   print('Identity With Hybrid:', end - start)
   
   
   hybrid_loop_block.hybridize()
   start = time.time()
   for _ in range(5):
       hybrid_loop_block(x)
       mx.nd.waitall()
   end = time.time()
   print('Identity With Hybrid + hybridize:', end - start)
   ```
   
   The result is:
   ```log
   Identity W/o Hybrid: 0.3092458248138428
   Identity With Hybrid: 3.6667261123657227
   Identity With Hybrid + hybridize: 16.659943342208862
   ```
   We can find that in this case, using Block is a better choice than using 
HybridBlock.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to