IntelPython / dpctl

Python SYCL bindings and SYCL-based Python Array API library
https://intelpython.github.io/dpctl/
Apache License 2.0
102 stars 29 forks source link

Array printing fails for `ndim` > 32 with misleading `ValueError` #1479

Open ndgrigorian opened 1 year ago

ndgrigorian commented 1 year ago

When printing an array x with x.ndim > 32 the following exception is raised:

In [1]: import dpctl.tensor as dpt, numpy as np

In [2]: dpt.ones((1,) * 32)
Out[2]:
usm_ndarray([[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[1.]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]],
            dtype=float32)

In [3]: dpt.ones((1,) * 33)
Out[3]: ---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
File ~/miniconda3/envs/dpctl_dev/lib/python3.10/site-packages/IPython/core/formatters.py:708, in PlainTextFormatter.__call__(self, obj)
    701 stream = StringIO()
    702 printer = pretty.RepresentationPrinter(stream, self.verbose,
    703     self.max_width, self.newline,
    704     max_seq_length=self.max_seq_length,
    705     singleton_pprinters=self.singleton_printers,
    706     type_pprinters=self.type_printers,
    707     deferred_pprinters=self.deferred_printers)
--> 708 printer.pretty(obj)
    709 printer.flush()
    710 return stream.getvalue()

File ~/miniconda3/envs/dpctl_dev/lib/python3.10/site-packages/IPython/lib/pretty.py:410, in RepresentationPrinter.pretty(self, obj)
    407                         return meth(obj, self, cycle)
    408                 if cls is not object \
    409                         and callable(cls.__dict__.get('__repr__')):
--> 410                     return _repr_pprint(obj, self, cycle)
    412     return _default_pprint(obj, self, cycle)
    413 finally:

File ~/miniconda3/envs/dpctl_dev/lib/python3.10/site-packages/IPython/lib/pretty.py:778, in _repr_pprint(obj, p, cycle)
    776 """A pprint that just redirects to the normal repr function."""
    777 # Find newlines and replace them with p.break_()
--> 778 output = repr(obj)
    779 lines = output.splitlines()
    780 with p.group():

File ~/repos/dpctl/dpctl/tensor/_usmarray.pyx:1205, in dpctl.tensor._usmarray.usm_ndarray.__repr__()
   1203
   1204     def __repr__(self):
-> 1205         return usm_ndarray_repr(self)
   1206
   1207

File ~/repos/dpctl/dpctl/tensor/_print.py:427, in usm_ndarray_repr(x, line_width, precision, suppress, prefix)
    424 prefix = prefix + "("
    425 suffix = ")"
--> 427 s = usm_ndarray_str(
    428     x,
    429     line_width=line_width,
    430     precision=precision,
    431     suppress=suppress,
    432     separator=", ",
    433     prefix=prefix,
    434     suffix=suffix,
    435 )
    437 if show_dtype:
    438     dtype_str = "dtype={}".format(x.dtype.name)

File ~/repos/dpctl/dpctl/tensor/_print.py:373, in usm_ndarray_str(x, line_width, edge_items, threshold, precision, floatmode, suppress, sign, numpy, separator, prefix, suffix)
    371     options["threshold"] = 0
    372 else:
--> 373     data = dpt.asnumpy(x)
    374 with np.printoptions(**options):
    375     s = np.array2string(
    376         data, separator=separator, prefix=prefix, suffix=suffix
    377     )

File ~/repos/dpctl/dpctl/tensor/_copy_utils.py:183, in asnumpy(usm_ary)
    167 def asnumpy(usm_ary):
    168     """
    169     asnumpy(usm_ary)
    170
   (...)
    181             of `usm_ary`
    182     """
--> 183     return _copy_to_numpy(usm_ary)

File ~/repos/dpctl/dpctl/tensor/_copy_utils.py:48, in _copy_to_numpy(ary)
     46 strides_bytes = tuple(si * itsz for si in ary.strides)
     47 offset = ary.__sycl_usm_array_interface__.get("offset", 0) * itsz
---> 48 return np.ndarray(
     49     ary.shape,
     50     dtype=ary.dtype,
     51     buffer=h,
     52     strides=strides_bytes,
     53     offset=offset,
     54 )

ValueError: maximum supported dimension for an ndarray is 32, found 33

Unlike Numpy, dpctl permits arrays with dimensions greater than 33. Therefore, this array is still usable, printing simply fails due to reliance on Numpy.

oleksandr-pavlyk commented 4 days ago

We could perhaps write our own formatting function for arrays of large dimensions (simple and limiting solution would be to output values of flattened array as well as the shape), but we must raise a meaningful exception in asnumpy as well as in from_dlpack where device "kDLCPU" was requested.