Skip to content

Commit a718039

Browse files
Align normalize_queue_device call with CFD (#1200)
* Align normalize_queue_device call with CFD in dpnp * Update dpnp/dpnp_iface.py Co-authored-by: Oleksandr Pavlyk <[email protected]> * Update dpnp/dpnp_iface.py Co-authored-by: Oleksandr Pavlyk <[email protected]> Co-authored-by: Oleksandr Pavlyk <[email protected]>
1 parent cfb62a6 commit a718039

File tree

4 files changed

+85
-32
lines changed

4 files changed

+85
-32
lines changed

dpnp/dpnp_array.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
# *****************************************************************************
3-
# Copyright (c) 2016-2020, Intel Corporation
3+
# Copyright (c) 2016-2022, Intel Corporation
44
# All rights reserved.
55
#
66
# Redistribution and use in source and binary forms, with or without
@@ -25,10 +25,9 @@
2525
# *****************************************************************************
2626

2727
import dpctl.tensor as dpt
28-
from dpctl.tensor._device import normalize_queue_device
29-
import dpnp
3028
import numpy
3129

30+
import dpnp
3231

3332
class dpnp_array:
3433
"""
@@ -64,7 +63,7 @@ def __init__(self,
6463
copy=False,
6564
order=order)
6665
else:
67-
sycl_queue_normalized = normalize_queue_device(sycl_queue=sycl_queue, device=device)
66+
sycl_queue_normalized = dpnp.get_normalized_queue_device(sycl_queue=sycl_queue, device=device)
6867
self._array_obj = dpt.usm_ndarray(shape,
6968
dtype=dtype,
7069
strides=strides,

dpnp/dpnp_container.py

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# -*- coding: utf-8 -*-
22
# *****************************************************************************
3-
# Copyright (c) 2016-2020, Intel Corporation
3+
# Copyright (c) 2016-2022, Intel Corporation
44
# All rights reserved.
55
#
66
# Redistribution and use in source and binary forms, with or without
@@ -34,28 +34,10 @@
3434
"""
3535

3636

37-
import dpnp.config as config
38-
# from dpnp.dparray import dparray
39-
from dpnp.dpnp_array import dpnp_array
40-
41-
import numpy
42-
4337
import dpctl.tensor as dpt
44-
from dpctl.tensor._device import normalize_queue_device
4538

46-
47-
if config.__DPNP_OUTPUT_DPCTL__:
48-
try:
49-
"""
50-
Detect DPCtl availability to use data container
51-
"""
52-
import dpctl.tensor as dpctl
53-
54-
except ImportError:
55-
"""
56-
No DPCtl data container available
57-
"""
58-
config.__DPNP_OUTPUT_DPCTL__ = 0
39+
from dpnp.dpnp_array import dpnp_array
40+
import dpnp
5941

6042

6143
__all__ = [
@@ -77,14 +59,15 @@ def asarray(x1,
7759
else:
7860
x1_obj = x1
7961

80-
sycl_queue_normalized = normalize_queue_device(sycl_queue=sycl_queue, device=device)
62+
sycl_queue_normalized = dpnp.get_normalized_queue_device(x1_obj, sycl_queue=sycl_queue, device=device)
63+
64+
"""Converts incoming 'x1' object to 'dpnp_array'."""
8165
array_obj = dpt.asarray(x1_obj,
8266
dtype=dtype,
8367
copy=copy,
8468
order=order,
8569
usm_type=usm_type,
8670
sycl_queue=sycl_queue_normalized)
87-
8871
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)
8972

9073

@@ -94,13 +77,12 @@ def empty(shape,
9477
device=None,
9578
usm_type="device",
9679
sycl_queue=None):
97-
"""Creates `dpnp_array` from uninitialized USM allocation."""
98-
sycl_queue_normalized = normalize_queue_device(sycl_queue=sycl_queue, device=device)
80+
sycl_queue_normalized = dpnp.get_normalized_queue_device(sycl_queue=sycl_queue, device=device)
9981

82+
"""Creates `dpnp_array` from uninitialized USM allocation."""
10083
array_obj = dpt.empty(shape,
10184
dtype=dtype,
10285
order=order,
10386
usm_type=usm_type,
10487
sycl_queue=sycl_queue_normalized)
105-
10688
return dpnp_array(array_obj.shape, buffer=array_obj, order=order)

dpnp/dpnp_iface.py

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# distutils: language = c++
33
# -*- coding: utf-8 -*-
44
# *****************************************************************************
5-
# Copyright (c) 2016-2020, Intel Corporation
5+
# Copyright (c) 2016-2022, Intel Corporation
66
# All rights reserved.
77
#
88
# Redistribution and use in source and binary forms, with or without
@@ -47,6 +47,7 @@
4747
import collections
4848

4949
import dpctl
50+
import dpctl.tensor as dpt
5051

5152
from dpnp.dpnp_algo import *
5253
from dpnp.dpnp_utils import *
@@ -62,7 +63,8 @@
6263
"dpnp_queue_initialize",
6364
"dpnp_queue_is_cpu",
6465
"get_dpnp_descriptor",
65-
"get_include"
66+
"get_include",
67+
"get_normalized_queue_device"
6668
]
6769

6870
from dpnp.dpnp_iface_arraycreation import *
@@ -248,3 +250,50 @@ def get_include():
248250
dpnp_path = os.path.join(os.path.dirname(__file__), "backend", "include")
249251

250252
return dpnp_path
253+
254+
255+
def get_normalized_queue_device(obj=None,
256+
device=None,
257+
sycl_queue=None):
258+
"""
259+
Utility to process complementary keyword arguments 'device' and 'sycl_queue'
260+
in subsequent calls of functions from `dpctl.tensor` module.
261+
262+
If both arguments 'device' and 'sycl_queue' have default value `None`
263+
and 'obj' has `sycl_queue` attribute, it assumes that Compute Follows Data
264+
approach has to be applied and so the resulting SYCL queue will be normalized
265+
based on the queue value from 'obj'.
266+
267+
Args:
268+
obj (optional): A python object. Can be an instance of `dpnp_array`,
269+
`dpctl.tensor.usm_ndarray`, an object representing SYCL USM allocation
270+
and implementing `__sycl_usm_array_interface__` protocol,
271+
an instance of `numpy.ndarray`, an object supporting Python buffer protocol,
272+
a Python scalar, or a (possibly nested) sequence of Python scalars.
273+
sycl_queue (:class:`dpctl.SyclQueue`, optional):
274+
explicitly indicates where USM allocation is done
275+
and the population code (if any) is executed.
276+
Value `None` is interpreted as get the SYCL queue
277+
from `obj` parameter if not None, from `device` keyword,
278+
or use default queue.
279+
Default: None
280+
device (string, :class:`dpctl.SyclDevice`, :class:`dpctl.SyclQueue,
281+
:class:`dpctl.tensor.Device`, optional):
282+
array-API keyword indicating non-partitioned SYCL device
283+
where array is allocated.
284+
Returns
285+
:class:`dpctl.SyclQueue` object normalized by `normalize_queue_device` call
286+
of `dpctl.tensor` module invoked with 'device' and 'sycl_queue' values.
287+
If both incoming 'device' and 'sycl_queue' are None and 'obj' has `sycl_queue` attribute,
288+
the normalization will be performed for 'obj.sycl_queue' value.
289+
Raises:
290+
TypeError: if argument is not of the expected type, or keywords
291+
imply incompatible queues.
292+
"""
293+
if device is None and sycl_queue is None and obj is not None and hasattr(obj, 'sycl_queue'):
294+
sycl_queue = obj.sycl_queue
295+
296+
# TODO: remove check dpt._device has attribute 'normalize_queue_device'
297+
if hasattr(dpt._device, 'normalize_queue_device'):
298+
return dpt._device.normalize_queue_device(sycl_queue=sycl_queue, device=device)
299+
return sycl_queue

tests/test_sycl_queue.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,26 @@ def test_to_device(device_from, device_to):
319319
y = x.to_device(device_to)
320320

321321
assert y.get_array().sycl_device == device_to
322+
323+
324+
@pytest.mark.parametrize("device",
325+
valid_devices,
326+
ids=[device.filter_string for device in valid_devices])
327+
@pytest.mark.parametrize("func",
328+
["array", "asarray"])
329+
@pytest.mark.parametrize("device_param",
330+
["", "None", "sycl_device"],
331+
ids=['Empty', 'None', "device"])
332+
@pytest.mark.parametrize("queue_param",
333+
["", "None", "sycl_queue"],
334+
ids=['Empty', 'None', "queue"])
335+
def test_array_copy(device, func, device_param, queue_param):
336+
data = numpy.ones(100)
337+
dpnp_data = getattr(dpnp, func)(data, device=device)
338+
339+
kwargs_items = {'device': device_param, 'sycl_queue': queue_param}.items()
340+
kwargs = {k: getattr(dpnp_data, v, None) for k,v in kwargs_items if v != ""}
341+
342+
result = dpnp.array(dpnp_data, **kwargs)
343+
344+
assert_sycl_queue_equal(result.sycl_queue, dpnp_data.sycl_queue)

0 commit comments

Comments
 (0)