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 2019/10/28 09:48:59 UTC

[GitHub] [incubator-mxnet] sxjscience opened a new issue #16658: [Numpy] Boolean array bug

sxjscience opened a new issue #16658: [Numpy] Boolean array bug
URL: https://github.com/apache/incubator-mxnet/issues/16658
 
 
   ```python
   import mxnet as mx
   from mxnet.gluon import HybridBlock
   mx.npx.set_np()
   
   def gen_self_attn_mask(F, data, valid_length, dtype, attn_type='full'):
       """Generate the mask used for the encoder, i.e, self-attention
   
       Parameters
       ----------
       F
       data :
           Shape (batch_size, seq_length, C)
       valid_length :
           Shape (batch_size,)
       attn_type : str
           Can be 'full' or 'causal'
   
       Returns
       -------
       mask :
           Shape (batch_size, seq_length, seq_length)
       """
       assert is_np_array()
       valid_length = valid_length.astype(dtype)
       if attn_type == 'full':
           steps = F.npx.arange_like(data, axis=1)  # (seq_length,)
           # Need to cast to dtype first, see https://github.com/apache/incubator-mxnet/issues/16650
           mask1 = (F.npx.reshape(steps, (1, 1, -1)) < F.npx.reshape(valid_length, (-2, 1, 1))).astype(dtype)
           mask2 = (F.npx.reshape(steps, (1, -1, 1)) < F.npx.reshape(valid_length, (-2, 1, 1))).astype(dtype)
           mask = mask1 * mask2
       elif attn_type == 'causal':
           steps = F.npx.arange_like(data, axis=1)
           # mask shape = (seq_length, seq_length)
           # batch_mask shape = (batch_size, seq_length)
           mask = (F.np.expand_dims(steps, axis=0) <= F.np.expand_dims(steps, axis=1)).astype(dtype)
           batch_mask = (F.np.expand_dims(steps, axis=0) < F.np.expand_dims(valid_length.astype(dtype), axis=-1)).astype(dtype)
           mask = mask * F.np.expand_dims(batch_mask, axis=-1)
       else:
           raise NotImplementedError
       return mask
   
   class GenSelfAttnMask(HybridBlock):
       def __init__(self, dtype, attn_type, prefix=None, params=None):
           super(GenSelfAttnMask, self).__init__(prefix=prefix, params=params)
           self._dtype = dtype
           self._attn_type = attn_type
   
       def hybrid_forward(self, F, data, valid_length):
           return gen_self_attn_mask(F, data, valid_length,
                                     dtype=self._dtype, attn_type=self._attn_type)
   
   batch_size = 4
   query_length = 8
   mem_length = 6
   nchannel = 5
   data = mx.np.random.normal(0, 1, (batch_size, query_length, nchannel), dtype=np.float32)
   valid_length = mx.np.random.randint(1, query_length + 1, (batch_size,))
   mask_gen = GenSelfAttnMask(dtype=np.float32, attn_type='causal')
   mask_gen.hybridize()
   mask = mask_gen(data, valid_length)
   ```
   
   
   ```
   MXNetError: [09:43:24] src/operator/tensor/elemwise_unary_op.h:461: Unknown type enum 7
   ```

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


With regards,
Apache Git Services