stephenrawls opened a new issue #14062: Bug in Hybridize w/ Concat
URL: https://github.com/apache/incubator-mxnet/issues/14062
 
 
   Hi,
   
   I think I found a bug in the Concat operator after calling hybridize() on my 
network. It is possible it is a mis-understanding on my part about how 
hybridization is supposed to work, however, so please let me know if that is 
the case.
   
   I have the following code (which works when `model.hybridize()` is commented 
out, and crashes when it is not):
   
   ```
   import mxnet as mx
   
   class Foo(mx.gluon.HybridBlock):
       def __init__(self):
           super(Foo, self).__init__()
   
       def hybrid_forward(self, F, x, valid_lengths):
           # Input is NCW. Ultimately Want mask of size N1W for broadcasting 
purposes.                                                                       
                
           # For now however keep mask as size NW and expand the extra dim 
later.                                                                          
                  
           mask = F.ones_like(x).slice(begin=(0,0,0), 
end=(0,1,0)).reshape(shape=(0,-1))
   
           # Need to insert an extra row in the W dimension                     
                                                                                
             
           extra_timestep = F.ones_like(valid_lengths.reshape(0,1))
           mask = F.concat(mask, extra_timestep, dim=1)
           valid_lengths_p1 = valid_lengths + 1
           mask = F.SequenceMask(mask, sequence_length=valid_lengths_p1, 
use_sequence_length=True, axis=1)
           mask = mask.reshape(shape=(0,1,-1))
   
           return mask
   
   model = Foo()
   model.hybridize()
   
   batch_size = 1
   feat_dim=2
   for seq_len in [2, 4]:
       x = mx.nd.ones((batch_size,feat_dim,seq_len))
       valid_lengths = mx.nd.full(shape=(batch_size), val=seq_len)
       y = model(x, valid_lengths)
       print(y)
   ```
   
   The expected behavior, which occurs when I do not hybridize, is the code 
prints out the following:
   ```
   [[[1. 1. 1.]]]
   <NDArray 1x1x3 @cpu(0)>
   
   [[[1. 1. 1. 1. 1.]]]
   <NDArray 1x1x5 @cpu(0)>
   ```
   
   The behavior that occurs when I do hybridize is a crash with the following 
error message:
   ```
   mxnet.base.MXNetError: Error in operator foo0_concat0: [10:44:05] 
src/operator/nn/../tensor/broadcast_reduce_op.h:151: Check failed: axis < ndim 
&& axis >= -ndim axis 1 exceeds the input dimension of 1
   ```
   
   I was very careful inside my hybrid_forward function to restrict myself to 
dynamic shaping operators like `F.ones_like()`, `slice()` and using the special 
values for `reshape()` like 0 and -1, so that my code should work with 
arbitrary input shapes using the Symbolic api. So it is my understanding that 
hybridize() should work on this code, since I didn't do anything that depended 
on either the ndarray api, or hard-coded shape information.
   
   Can you please let me know if this is a bug, or if I have some fundamental 
mis-understanding of how hybridize() is supposed to work?
   
   (As to *why* I would want to use the code above ... I have a use case where 
it is more efficient to construct a broadcast-able mask once, and then use it 
many times inside a loop, versus calling SequenceMask() repeatedly in the loop, 
especially because SequenceMask only works when the timestep dimension is on 
axis 0 or axis 1, which with my data layout would mean I would need a lot of 
transposing back and forth inside a loop, which I would rather avoid).
   

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to