cornell-zhang/heterocl

hcl.asarray doesn't work with 64-bit "python" integers

jcasas00 opened this issue · 0 comments

Code:

    inp = [[1, 1085102592571150095], [13, 14106333703424951235]]
    out = hcl.asarray(inp, dtype=hcl.UInt(64))
    print(f"out={out}")

Output:

out=[[4607182418800017408 4876868561191968286]
 [4623507967449235456 4893293453950613624]]

From briefly trying to debug this, the issue seems to be:

  • hcl.asarray eventually calls ndarray.py:array which has the following code:
  def array(arr, dtype=None, ctx=cpu(0)):
   ...
   if not isinstance(arr, (_np.ndarray, NDArray)):
       arr = _np.array(arr)
   if dtype is None:
     dtype = arr.dtype
   return empty(arr.shape, dtype, ctx).copyfrom(arr)
  • Since arr is just a python list in this case, it calls arr = np.array(arr). However, since this call doesn't have a numpy dtype specified, numpy will use "the most appropriate data type that fits the size of the data" .... which in this case is a float.
  • The copyfrom function then copies the array values that is now in floating point representation in memory.

W/A is to do something like: hcl.asarray (np.array (inp, dtype=np.ulonglong), dtype=hcl.UInt(64)).

Should fix hcl.asarray, especially since the dtype is specified. As is, it is basically silently passing garbage data.