Skip to content

print_corners #1187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 27, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 41 additions & 20 deletions dpctl/tensor/_print.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
# limitations under the License.

import contextlib
import itertools
import operator

import numpy as np

import dpctl
import dpctl.tensor as dpt
import dpctl.tensor._tensor_impl as ti

__doc__ = "Print functions for :class:`dpctl.tensor.usm_ndarray`."

Expand Down Expand Up @@ -220,25 +223,44 @@ def print_options(*args, **kwargs):
dpt.set_print_options(**options)


def _nd_corners(x, edge_items, slices=()):
axes_reduced = len(slices)
if axes_reduced == x.ndim:
return x[slices]

if x.shape[axes_reduced] > 2 * edge_items:
return dpt.concat(
(
_nd_corners(
x, edge_items, slices + (slice(None, edge_items, None),)
),
_nd_corners(
x, edge_items, slices + (slice(-edge_items, None, None),)
),
),
axis=axes_reduced,
def _nd_corners(arr_in, edge_items):
_shape = arr_in.shape
max_shape = 2 * edge_items + 1
if max(_shape) <= max_shape:
return dpt.asnumpy(arr_in)
res_shape = tuple(
max_shape if _shape[i] > max_shape else _shape[i]
for i in range(arr_in.ndim)
)

arr_out = dpt.empty(
res_shape,
dtype=arr_in.dtype,
usm_type=arr_in.usm_type,
sycl_queue=arr_in.sycl_queue,
)

blocks = []
for i in range(len(_shape)):
if _shape[i] > max_shape:
blocks.append(
(
np.s_[:edge_items],
np.s_[-edge_items:],
)
)
else:
blocks.append((np.s_[:],))

hev_list = []
for slc in itertools.product(*blocks):
hev, _ = ti._copy_usm_ndarray_into_usm_ndarray(
src=arr_in[slc], dst=arr_out[slc], sycl_queue=arr_in.sycl_queue
)
else:
return _nd_corners(x, edge_items, slices + (slice(None, None, None),))
hev_list.append(hev)

dpctl.SyclEvent.wait_for(hev_list)
return dpt.asnumpy(arr_out)


def usm_ndarray_str(
Expand Down Expand Up @@ -345,8 +367,7 @@ def usm_ndarray_str(
edge_items = options["edgeitems"]

if x.size > threshold:
# need edge_items + 1 elements for np.array2string to abbreviate
data = dpt.asnumpy(_nd_corners(x, edge_items + 1))
data = _nd_corners(x, edge_items)
options["threshold"] = 0
else:
data = dpt.asnumpy(x)
Expand Down