You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by la...@apache.org on 2020/12/14 18:11:21 UTC

[incubator-mxnet] branch master updated: Change __str__ representation of bfloat16 NDArray from uints to floats (#19608)

This is an automated email from the ASF dual-hosted git repository.

lausen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-mxnet.git


The following commit(s) were added to refs/heads/master by this push:
     new ed4aa23  Change __str__ representation of bfloat16 NDArray from uints to floats (#19608)
ed4aa23 is described below

commit ed4aa23dba78def23abfe08c7e74eb4dbab1bbbb
Author: Paweł Głomski <pa...@intel.com>
AuthorDate: Mon Dec 14 19:09:28 2020 +0100

    Change __str__ representation of bfloat16 NDArray from uints to floats (#19608)
---
 CONTRIBUTORS.md                       | 1 +
 python/mxnet/ndarray/ndarray.py       | 7 +++++++
 tests/python/unittest/test_ndarray.py | 6 ++++++
 3 files changed, 14 insertions(+)

diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md
index 53e67a6..5135d19 100644
--- a/CONTRIBUTORS.md
+++ b/CONTRIBUTORS.md
@@ -287,6 +287,7 @@ List of Contributors
 * [Wei Chu](https://github.com/waytrue17)
 * [Joe Evans](https://github.com/josephevans)
 * [Nikolay Ulmasov](https://github.com/r3stl355)
+* [Paweł Głomski](https://github.com/PawelGlomski-Intel)
 
 Label Bot
 ---------
diff --git a/python/mxnet/ndarray/ndarray.py b/python/mxnet/ndarray/ndarray.py
index af97ed6..f5d3c26 100644
--- a/python/mxnet/ndarray/ndarray.py
+++ b/python/mxnet/ndarray/ndarray.py
@@ -458,6 +458,13 @@ fixed-size items.
 
     __nonzero__ = __bool__
 
+    def __str__(self):
+        """Returns a readable string representation of the array."""
+        if self.dtype == np.dtype([('bfloat16', np.uint16)]):
+            return super(NDArray, self.astype(float)).__str__()
+        else:
+            return super(NDArray, self).__str__()
+
     def __len__(self):
         """Number of element along the first axis."""
         return self.shape[0]
diff --git a/tests/python/unittest/test_ndarray.py b/tests/python/unittest/test_ndarray.py
index f59906b..b935290 100644
--- a/tests/python/unittest/test_ndarray.py
+++ b/tests/python/unittest/test_ndarray.py
@@ -2057,3 +2057,9 @@ def test_load_saved_gpu_array_when_no_gpus_are_present():
     # but there are no GPUs
     array.__setstate__(ndarray_state)
 
+def test_readable_bfloat16_print():
+    arr_bfloat16 = mx.nd.linspace(0, 1, 16).reshape((2, 2, 2, 2)).astype(np.dtype([('bfloat16', np.uint16)]))
+    arr_uint16 = arr_bfloat16.asnumpy()
+    arr_float = arr_bfloat16.astype(float)
+    assert (arr_bfloat16.__str__() == arr_float.__str__())
+    assert (arr_bfloat16.__repr__().find(arr_uint16.__str__()) != -1)