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 2020/12/07 13:31:22 UTC

[GitHub] [incubator-mxnet] PawelGlomski-Intel edited a comment on pull request #19608: Change __str__ representation of bfloat16 NDArray from uints to floats.

PawelGlomski-Intel edited a comment on pull request #19608:
URL: https://github.com/apache/incubator-mxnet/pull/19608#issuecomment-738648171


   Yes, sorry, here is part of our discussion on this:
   
   > I was looking for a solution that doesn't require copying, and the solution of setting numpy's formatter seems to be the only way to achieve this, but there are 2 problems.
   > 
   > The first one is that the user cannot change the way floats are printed, as numpy does not format scalars (the lambda we provide to the formatter takes scalar as argument). There is a hack to overcome this problem: putting each element into a 1-element ndarray and removing '\[' and '\]' from its string representation, but because now each element is treated separately, each may be displayed in a different form (chosen by numpy), which differs from the way numpy prints ndarrays - using only one notation.
   > 
   > The second problem is the performance. Printing bigger arrays is significantly slower using this solution, as we are doing computation on the python side (changing from bfloat16 to float32).
   > 
   > In the end, I chose the solution with copying, as it was easy to understand, consistent with numpy and fast.
   
   Here is the solution without copying which I am referring to above:
   ```
       def __str__(self):
           if self.dtype == np.dtype([('bfloat16', np.uint16)]):
               def castToBFloat16(value):
                   return struct.unpack("@f", struct.pack("@I", np.uint32(value) << 16))[0]
               po = np.get_printoptions()
               po['formatter'] = {} 
               po['formatter']['int_kind'] = lambda x: str(np.array([castToBFloat16(x)]))[1:-1]
               with np.printoptions(**po):
                   return super(NDArray, self).__str__()
           else:
               return super(NDArray, self).__str__()
   ```


----------------------------------------------------------------
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