tvm-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] [incubator-tvm] Menooker commented on a change in pull request #5601: [TVM Core] Add bfloat16
Date Tue, 19 May 2020 13:34:40 GMT

Menooker commented on a change in pull request #5601:
URL: https://github.com/apache/incubator-tvm/pull/5601#discussion_r427306003



##########
File path: tests/python/unittest/test_target_codegen_llvm.py
##########
@@ -710,6 +710,52 @@ def _transform(f, *_):
         module(a_, b_, c_)
         tvm.testing.assert_allclose(c_.asnumpy(), (a_.asnumpy() * 2).astype('int32'))
 
+def np_float2np_bf16(arr):
+    ''' Convert a numpy array of float to a numpy array 
+    of bf16 in uint16'''
+    orig = arr.view('<u4')
+    bias = np.bitwise_and(np.right_shift(orig, 16), 1) + 0x7FFF
+    return np.right_shift(orig + bias, 16).astype('uint16')
+
+def np_float2tvm_bf16(arr):
+    ''' Convert a numpy array of float to a TVM array 
+    of bf16'''
+    nparr = np_float2np_bf16(arr)
+    return tvm.nd.empty(nparr.shape, 'bf16').copyfrom(nparr)
+
+def np_bf162np_float(arr):
+    ''' Convert a numpy array of bf16 (uint16) to a numpy array 
+    of float'''
+    u32 = np.left_shift(arr.astype('uint32'), 16)

Review comment:
       In my understanding, fp32=>bf16 casting preserves the higher-ordered bits (bits
31-16). We don't need to know whether the higher-ordered bits are stored in a larger address
or a smaller address (which is the endianness), we just need to get the bits by shifting,
which is well-defined - just using shifting is enough.  
   
   Reference: [wiki for fp32 bit order](https://en.wikipedia.org/wiki/Single-precision_floating-point_format)
   
   [PyTorch's bf16 casting] (https://github.com/pytorch/pytorch/blob/master/c10/util/BFloat16.h)




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



Mime
View raw message