From eeb4f8a2017dc622d8e2c26084c7ebd6dc8fd528 Mon Sep 17 00:00:00 2001 From: Alexander Kalistratov Date: Thu, 2 Mar 2023 17:47:47 +0100 Subject: [PATCH 1/3] Using cached queue instead of creating new one on type inference --- numba_dpex/core/types/usm_ndarray_type.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/numba_dpex/core/types/usm_ndarray_type.py b/numba_dpex/core/types/usm_ndarray_type.py index e3d1101633..6c0a1480b4 100644 --- a/numba_dpex/core/types/usm_ndarray_type.py +++ b/numba_dpex/core/types/usm_ndarray_type.py @@ -59,8 +59,10 @@ def __init__( "The device keyword arg should be a str object specifying " "a SYCL filter selector" ) - self.queue = dpctl.SyclQueue(device) - self.device = self.queue.sycl_device.filter_string + self.queue = dpctl.tensor._device.normalize_queue_device( + device=device + ) + self.device = device elif queue is not None and device == "unknown": if not isinstance(queue, dpctl.SyclQueue): raise TypeError( @@ -69,7 +71,7 @@ def __init__( self.device = self.queue.sycl_device.filter_string self.queue = queue else: - self.queue = dpctl.SyclQueue() + self.queue = dpctl.tensor._device.normalize_queue_device() self.device = self.queue.sycl_device.filter_string if not dtype: From 294b15b366a4c567c17f2c8e0d1197db7e49c5a2 Mon Sep 17 00:00:00 2001 From: Alexander Kalistratov Date: Fri, 3 Mar 2023 18:11:17 +0100 Subject: [PATCH 2/3] Avoid creating SyclDevice from filter_string --- numba_dpex/core/types/usm_ndarray_type.py | 77 ++++++++++++----------- numba_dpex/core/typing/typeof.py | 2 +- 2 files changed, 42 insertions(+), 37 deletions(-) diff --git a/numba_dpex/core/types/usm_ndarray_type.py b/numba_dpex/core/types/usm_ndarray_type.py index 6c0a1480b4..2e8aea88fe 100644 --- a/numba_dpex/core/types/usm_ndarray_type.py +++ b/numba_dpex/core/types/usm_ndarray_type.py @@ -31,48 +31,53 @@ def __init__( aligned=True, addrspace=address_space.GLOBAL, ): + # Creating SyclDevice from filter_string is expensive. So, USMNdArray should be able to + # accept and SyclDevice from usm_ndarray as device parameter + if not isinstance(device, (str, dpctl.SyclDevice)): + raise TypeError( + "The device keyword arg should be a str object specifying " + "a SYCL filter selector" + ) + + if not isinstance(queue, dpctl.SyclQueue) and queue is not None: + raise TypeError( + "The queue keyword arg should be a dpctl.SyclQueue object or None" + ) + self.usm_type = usm_type self.addrspace = addrspace - if queue is not None and device != "unknown": - if not isinstance(device, str): - raise TypeError( - "The device keyword arg should be a str object specifying " - "a SYCL filter selector" - ) - if not isinstance(queue, dpctl.SyclQueue): - raise TypeError( - "The queue keyword arg should be a dpctl.SyclQueue object" - ) - d1 = queue.sycl_device - d2 = dpctl.SyclDevice(device) - if d1 != d2: - raise TypeError( - "The queue keyword arg and the device keyword arg specify " - "different SYCL devices" - ) + def to_device(dev): + if isinstance(dev, dpctl.SyclDevice): + return dev + + return dpctl.SyclDevice(dev) + + def device_as_string(dev): + if isinstance(dev, dpctl.SyclDevice): + return dev.filter_string + + return dev + + if queue is not None: + if device != "unknown": + if queue.sycl_device != to_device(device): + raise TypeError( + "The queue keyword arg and the device keyword arg specify " + "different SYCL devices" + ) + self.queue = queue - self.device = device - elif queue is None and device != "unknown": - if not isinstance(device, str): - raise TypeError( - "The device keyword arg should be a str object specifying " - "a SYCL filter selector" - ) + else: + if device == "unknown": + device = None + + device_str = device_as_string(device) self.queue = dpctl.tensor._device.normalize_queue_device( - device=device + device=device_str ) - self.device = device - elif queue is not None and device == "unknown": - if not isinstance(queue, dpctl.SyclQueue): - raise TypeError( - "The queue keyword arg should be a dpctl.SyclQueue object" - ) - self.device = self.queue.sycl_device.filter_string - self.queue = queue - else: - self.queue = dpctl.tensor._device.normalize_queue_device() - self.device = self.queue.sycl_device.filter_string + + self.device = self.queue.sycl_device.filter_string if not dtype: dummy_tensor = dpctl.tensor.empty( diff --git a/numba_dpex/core/typing/typeof.py b/numba_dpex/core/typing/typeof.py index 0069d1e9be..9a5e5802b3 100644 --- a/numba_dpex/core/typing/typeof.py +++ b/numba_dpex/core/typing/typeof.py @@ -43,7 +43,7 @@ def _typeof_helper(val, array_class_type): ) try: - device = val.sycl_device.filter_string + device = val.sycl_device except AttributeError: raise ValueError("The device for the usm_ndarray could not be inferred") From c81f81689063d38b8c8d65fa3feeb7343dacab14 Mon Sep 17 00:00:00 2001 From: Alexander Kalistratov Date: Wed, 19 Apr 2023 00:12:18 +0200 Subject: [PATCH 3/3] Applying review comments --- numba_dpex/core/typeconv/array_conversion.py | 1 - numba_dpex/core/types/usm_ndarray_type.py | 36 ++++++-------------- numba_dpex/core/typing/typeof.py | 6 +--- 3 files changed, 11 insertions(+), 32 deletions(-) diff --git a/numba_dpex/core/typeconv/array_conversion.py b/numba_dpex/core/typeconv/array_conversion.py index 5096045a90..228265b6ec 100644 --- a/numba_dpex/core/typeconv/array_conversion.py +++ b/numba_dpex/core/typeconv/array_conversion.py @@ -37,7 +37,6 @@ def to_usm_ndarray(suai_attrs, addrspace=address_space.GLOBAL): ndim=suai_attrs.dimensions, layout=layout, usm_type=suai_attrs.usm_type, - device=suai_attrs.device, queue=suai_attrs.queue, readonly=not suai_attrs.is_writable, name=None, diff --git a/numba_dpex/core/types/usm_ndarray_type.py b/numba_dpex/core/types/usm_ndarray_type.py index 2e8aea88fe..9a4790de38 100644 --- a/numba_dpex/core/types/usm_ndarray_type.py +++ b/numba_dpex/core/types/usm_ndarray_type.py @@ -31,9 +31,7 @@ def __init__( aligned=True, addrspace=address_space.GLOBAL, ): - # Creating SyclDevice from filter_string is expensive. So, USMNdArray should be able to - # accept and SyclDevice from usm_ndarray as device parameter - if not isinstance(device, (str, dpctl.SyclDevice)): + if not isinstance(device, str): raise TypeError( "The device keyword arg should be a str object specifying " "a SYCL filter selector" @@ -47,35 +45,21 @@ def __init__( self.usm_type = usm_type self.addrspace = addrspace - def to_device(dev): - if isinstance(dev, dpctl.SyclDevice): - return dev + if device == "unknown": + device = None - return dpctl.SyclDevice(dev) - - def device_as_string(dev): - if isinstance(dev, dpctl.SyclDevice): - return dev.filter_string - - return dev + if queue is not None and device is not None: + raise TypeError( + "'queue' and 'device' keywords can not be both specified" + ) if queue is not None: - if device != "unknown": - if queue.sycl_device != to_device(device): - raise TypeError( - "The queue keyword arg and the device keyword arg specify " - "different SYCL devices" - ) - self.queue = queue else: - if device == "unknown": - device = None + if device is None: + device = dpctl.SyclDevice() - device_str = device_as_string(device) - self.queue = dpctl.tensor._device.normalize_queue_device( - device=device_str - ) + self.queue = dpctl.get_device_cached_queue(device) self.device = self.queue.sycl_device.filter_string diff --git a/numba_dpex/core/typing/typeof.py b/numba_dpex/core/typing/typeof.py index 9a5e5802b3..a9df706ad0 100644 --- a/numba_dpex/core/typing/typeof.py +++ b/numba_dpex/core/typing/typeof.py @@ -42,10 +42,7 @@ def _typeof_helper(val, array_class_type): "The usm_type for the usm_ndarray could not be inferred" ) - try: - device = val.sycl_device - except AttributeError: - raise ValueError("The device for the usm_ndarray could not be inferred") + assert val.sycl_queue is not None return array_class_type( dtype=dtype, @@ -53,7 +50,6 @@ def _typeof_helper(val, array_class_type): layout=layout, readonly=readonly, usm_type=usm_type, - device=device, queue=val.sycl_queue, addrspace=address_space.GLOBAL, )