PawelGlomski-Intel commented 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 Andrzej's 
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 Bartłomiej's solution, as it was easy to understand, 
consistent with numpy and fast.
   
   Where Bartłomiej's solution is the one proposed in this PR, and here is the 
code of the other solution (without copying):
   ```
       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:
[email protected]


Reply via email to