Skip to content
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

Support tests on single point precision GPUs #1143

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
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
114 changes: 114 additions & 0 deletions numba_dpex/tests/_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import shutil

import dpctl
import dpnp
import pytest

from numba_dpex import config, numba_sem_version
Expand Down Expand Up @@ -138,3 +139,116 @@ def override_config(name, value, config=config):

def _id(obj):
return obj


def get_complex_dtypes(device=None):
"""
Build a list of complex types supported by DPNP based on device capabilities.
"""

dev = dpctl.select_default_device() if device is None else device

# add complex types
dtypes = [dpnp.complex64]
if dev.has_aspect_fp64:
dtypes.append(dpnp.complex128)
return dtypes


def get_float_dtypes(no_float16=True, device=None):
"""
Build a list of floating types supported by DPNP based on device capabilities.
"""

dev = dpctl.select_default_device() if device is None else device

# add floating types
dtypes = []
if not no_float16 and dev.has_aspect_fp16:
dtypes.append(dpnp.float16)

dtypes.append(dpnp.float32)
if dev.has_aspect_fp64:
dtypes.append(dpnp.float64)
return dtypes


def get_float_complex_dtypes(no_float16=True, device=None):
"""
Build a list of floating and complex types supported by DPNP based on device capabilities.
"""

dtypes = get_float_dtypes(no_float16, device)
dtypes.extend(get_complex_dtypes(device))
return dtypes


def get_all_dtypes(
no_bool=False,
no_int=False,
no_float16=True,
no_float=False,
no_complex=False,
no_none=False,
device=None,
):
"""
Build a list of types supported by DPNP based on input flags and device capabilities.
"""

dev = dpctl.select_default_device() if device is None else device

# add boolean type
dtypes = [dpnp.bool] if not no_bool else []

# add integer types
if not no_int:
dtypes.extend([dpnp.int32, dpnp.int64])

# add floating types
if not no_float:
dtypes.extend(get_float_dtypes(no_float16=no_float16, device=dev))

# add complex types
if not no_complex:
dtypes.extend(get_complex_dtypes(device=dev))

# add None value to validate a default dtype
if not no_none:
dtypes.append(None)
return dtypes


def get_queue_or_skip(args=tuple()):
try:
q = dpctl.SyclQueue(*args)
except dpctl.SyclQueueCreationError:
pytest.skip(f"Queue could not be created from {args}")
return q


def skip_if_dtype_not_supported(dt, q_or_dev):
import dpctl.tensor as dpt

dt = dpt.dtype(dt)
if type(q_or_dev) is dpctl.SyclQueue:
dev = q_or_dev.sycl_device
elif type(q_or_dev) is dpctl.SyclDevice:
dev = q_or_dev
else:
raise TypeError(
"Expected dpctl.SyclQueue or dpctl.SyclDevice, "
f"got {type(q_or_dev)}"
)
dev_has_dp = dev.has_aspect_fp64
if dev_has_dp is False and dt in [dpt.float64, dpt.complex128]:
pytest.skip(
f"{dev.name} does not support double precision floating point types"
)
dev_has_hp = dev.has_aspect_fp16
if dev_has_hp is False and dt in [
dpt.float16,
]:
pytest.skip(
f"{dev.name} does not support half precision floating point type"
)
14 changes: 11 additions & 3 deletions numba_dpex/tests/core/passes/test_parfor_legalize_cfd_pass.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@

from numba_dpex import dpjit
from numba_dpex.core.exceptions import ExecutionQueueInferenceError
from numba_dpex.tests._helper import skip_no_opencl_cpu, skip_no_opencl_gpu
from numba_dpex.tests._helper import (
get_all_dtypes,
skip_no_opencl_cpu,
skip_no_opencl_gpu,
)

shapes = [10, (2, 5)]
dtypes = [dpnp.int32, dpnp.int64, dpnp.float32, dpnp.float64]
usm_types = ["device"]
devices = ["gpu"]

Expand All @@ -30,7 +33,12 @@ def func1(a, b):

@skip_no_opencl_gpu
@pytest.mark.parametrize("shape", shapes)
@pytest.mark.parametrize("dtype", dtypes)
@pytest.mark.parametrize(
"dtype",
get_all_dtypes(
no_bool=True, no_float16=True, no_none=True, no_complex=True
),
)
@pytest.mark.parametrize("usm_type", usm_types)
@pytest.mark.parametrize("device", devices)
def test_parfor_legalize_cfd_pass(shape, dtype, usm_type, device):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_boxing_unboxing():
def func(a):
return a

a = dpnp.empty(10)
a = dpnp.empty(10, dtype=dpnp.float32)
try:
b = func(a)
except:
Expand All @@ -40,8 +40,8 @@ def test_stride_calc_at_unboxing():
def _tester(a):
return a.strides

b = dpnp.empty((4, 16, 4))
b = dpnp.empty((4, 16, 4), dtype=dpnp.float32)
strides = dpjit(_tester)(b)

# Numba computes strides as bytes
assert list(strides) == [512, 32, 8]
assert list(strides) == [256, 16, 4]
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
import dpctl
import pytest

from numba_dpex.core.types import USMNdArray, dpctl_types
from numba_dpex.core.types import USMNdArray, dpctl_types, float32


def test_usmndarray_negative_tests():
default_device = dpctl.SyclDevice().filter_string

usmarr1 = USMNdArray(1, device=None, queue=None)
assert usmarr1.dtype.name == "float64"
usmarr1 = USMNdArray(1, device=None, queue=None, dtype=float32)
assert usmarr1.dtype.name == "float32"
assert usmarr1.ndim == 1
assert usmarr1.layout == "C"
assert usmarr1.addrspace == 1
assert usmarr1.usm_type == "device"

assert usmarr1.queue.sycl_device == default_device

usmarr2 = USMNdArray(1, device=default_device, queue=None)
assert usmarr2.dtype.name == "float64"
usmarr2 = USMNdArray(1, device=default_device, queue=None, dtype=float32)
assert usmarr2.dtype.name == "float32"
assert usmarr2.ndim == 1
assert usmarr2.layout == "C"
assert usmarr2.addrspace == 1
Expand All @@ -26,18 +26,18 @@ def test_usmndarray_negative_tests():

queue = dpctl_types.DpctlSyclQueue(dpctl.SyclQueue())

usmarr3 = USMNdArray(1, device=None, queue=queue)
assert usmarr3.dtype.name == "float64"
usmarr3 = USMNdArray(1, device=None, queue=queue, dtype=float32)
assert usmarr3.dtype.name == "float32"
assert usmarr3.ndim == 1
assert usmarr3.layout == "C"
assert usmarr3.addrspace == 1
assert usmarr3.usm_type == "device"

with pytest.raises(TypeError):
USMNdArray(1, device=default_device, queue=queue)
USMNdArray(1, device=default_device, queue=queue, dtype=float32)

with pytest.raises(TypeError):
USMNdArray(1, queue=0)
USMNdArray(1, queue=0, dtype=float32)

with pytest.raises(TypeError):
USMNdArray(1, device=0)
USMNdArray(1, device=0, dtype=float32)
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
#
# SPDX-License-Identifier: Apache-2.0

import dpctl
import dpctl.tensor as dpt
import numpy as np
import pytest
from numba.misc.special import typeof

from numba_dpex.core.types import USMNdArray
from numba_dpex.tests._helper import (
get_queue_or_skip,
skip_if_dtype_not_supported,
)

list_of_dtypes = [
np.int32,
Expand Down Expand Up @@ -35,6 +40,9 @@ def usm_type(request):


def test_usm_ndarray_type(dtype, usm_type):
q = get_queue_or_skip()
skip_if_dtype_not_supported(dtype, q)

a = np.array(np.random.random(10), dtype)
da = dpt.usm_ndarray(a.shape, dtype=a.dtype, buffer=usm_type)

Expand Down
20 changes: 16 additions & 4 deletions numba_dpex/tests/dpjit_tests/dpnp/test_dpnp_empty.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
from numba import errors

from numba_dpex import dpjit
from numba_dpex.tests._helper import get_all_dtypes

shapes = [11, (2, 5)]
dtypes = [dpnp.int32, dpnp.int64, dpnp.float32, dpnp.float64]
usm_types = ["device", "shared", "host"]


Expand Down Expand Up @@ -51,7 +51,12 @@ def func(shape):


@pytest.mark.parametrize("shape", shapes)
@pytest.mark.parametrize("dtype", dtypes)
@pytest.mark.parametrize(
"dtype",
get_all_dtypes(
no_bool=True, no_float16=True, no_none=True, no_complex=True
),
)
@pytest.mark.parametrize("usm_type", usm_types)
def test_dpnp_empty_from_device(shape, dtype, usm_type):
""" "Use device only in dpnp.emtpy() inside dpjit."""
Expand Down Expand Up @@ -84,7 +89,12 @@ def func(shape):


@pytest.mark.parametrize("shape", shapes)
@pytest.mark.parametrize("dtype", dtypes)
@pytest.mark.parametrize(
"dtype",
get_all_dtypes(
no_bool=True, no_float16=True, no_none=True, no_complex=True
),
)
@pytest.mark.parametrize("usm_type", usm_types)
def test_dpnp_empty_from_queue(shape, dtype, usm_type):
""" "Use queue only in dpnp.emtpy() inside dpjit."""
Expand Down Expand Up @@ -121,7 +131,9 @@ def test_dpnp_empty_exceptions():

@dpjit
def func(shape, queue):
c = dpnp.empty(shape, sycl_queue=queue, device=device)
c = dpnp.empty(
shape, sycl_queue=queue, device=device, dtype=dpnp.float32
)
return c

with pytest.raises(errors.TypingError):
Expand Down
20 changes: 15 additions & 5 deletions numba_dpex/tests/dpjit_tests/dpnp/test_dpnp_empty_like.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
from numba import errors

from numba_dpex import dpjit
from numba_dpex.tests._helper import get_all_dtypes

shapes = [10, (2, 5)]
dtypes = [dpnp.int32, dpnp.int64, dpnp.float32, dpnp.float64]
usm_types = ["device", "shared", "host"]


Expand Down Expand Up @@ -53,7 +53,12 @@ def func(x):


@pytest.mark.parametrize("shape", shapes)
@pytest.mark.parametrize("dtype", dtypes)
@pytest.mark.parametrize(
"dtype",
get_all_dtypes(
no_bool=True, no_float16=True, no_none=True, no_complex=True
),
)
@pytest.mark.parametrize("usm_type", usm_types)
def test_dpnp_empty_like_from_device(shape, dtype, usm_type):
""" "Use device only in dpnp.emtpy)like() inside dpjit."""
Expand Down Expand Up @@ -87,7 +92,12 @@ def func(x):


@pytest.mark.parametrize("shape", shapes)
@pytest.mark.parametrize("dtype", dtypes)
@pytest.mark.parametrize(
"dtype",
get_all_dtypes(
no_bool=True, no_float16=True, no_none=True, no_complex=True
),
)
@pytest.mark.parametrize("usm_type", usm_types)
def test_dpnp_empty_like_from_queue(shape, dtype, usm_type):
""" "Use queue only in dpnp.emtpy_like() inside dpjit."""
Expand Down Expand Up @@ -146,7 +156,7 @@ def func1(x, queue):

try:
queue = dpctl.SyclQueue()
a = dpnp.ones(10)
a = dpnp.ones(10, dtype=dpnp.float32)
func1(a, queue)
except Exception as e:
assert isinstance(e, errors.TypingError)
Expand Down Expand Up @@ -175,7 +185,7 @@ def func(x):
y = dpnp.empty_like(x)
return y

a = numpy.empty(10)
a = numpy.empty(10, dtype=numpy.float32)

with pytest.raises(Exception):
func(a)
Expand Down
Loading