Skip to content
Open
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
4 changes: 4 additions & 0 deletions src/oclpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def _buffer_shape(shape):

def _from_host(host, *, dtype, device=None):
shape = host.shape
# dtype may differ from host.dtype when require_supported() has selected a
# fallback; cast before pushing so the device buffer has the correct type.
if host.dtype != dtype:
host = host.astype(dtype)
buffer = host.reshape(_buffer_shape(shape))
return Array(get_backend().push(buffer, dtype=dtype, device=device), shape=shape)

Expand Down
29 changes: 24 additions & 5 deletions src/oclpy/_dtypes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from __future__ import annotations

import warnings

import numpy as np

bool = np.dtype("bool")
Expand All @@ -11,21 +13,38 @@
uint32 = np.dtype("uint32")
float32 = np.dtype("float32")

# Exposed for namespace compatibility, but rejected before device allocation
# because pyclesperanto does not currently support them.
# 64-bit and boolean dtypes are not natively supported by pyclesperanto.
# They are exposed for Array API namespace compatibility and automatically
# downcast to their nearest supported equivalents with a UserWarning.
int64 = np.dtype("int64")
uint64 = np.dtype("uint64")
float64 = np.dtype("float64")

SUPPORTED_DTYPES = {int8, int16, int32, uint8, uint16, uint32, float32}

# Maps unsupported dtypes to their nearest supported fallback.
DTYPE_FALLBACKS = {
np.dtype("bool"): np.dtype("int8"),
np.dtype("int64"): np.dtype("int32"),
np.dtype("uint64"): np.dtype("uint32"),
np.dtype("float64"): np.dtype("float32"),
}


def normalize_dtype(dtype):
return None if dtype is None else np.dtype(dtype)


def require_supported(dtype):
dtype = np.dtype(dtype)
if dtype not in SUPPORTED_DTYPES:
raise TypeError(f"dtype {dtype} is not supported by pyclesperanto")
return dtype
if dtype in SUPPORTED_DTYPES:
return dtype
fallback = DTYPE_FALLBACKS.get(dtype)
if fallback is not None:
warnings.warn(
f"dtype {dtype} is not supported by pyclesperanto; falling back to {fallback}",
UserWarning,
stacklevel=3,
)
return fallback
raise TypeError(f"dtype {dtype} is not supported by pyclesperanto")
14 changes: 10 additions & 4 deletions tests/test_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@ def test_full_and_arange():
np.testing.assert_array_equal(xp.asnumpy(xp.arange(1, 5, dtype=xp.int32)), [1, 2, 3, 4])


@pytest.mark.parametrize("dtype", [xp.bool, xp.int64, xp.uint64, xp.float64])
def test_backend_rejects_unsupported_dtype(dtype):
with pytest.raises(TypeError, match="not supported"):
xp.asarray([1], dtype=dtype)
@pytest.mark.parametrize("dtype,expected", [
(xp.bool, xp.int8),
(xp.int64, xp.int32),
(xp.uint64, xp.uint32),
(xp.float64, xp.float32),
])
def test_unsupported_dtype_falls_back_with_warning(dtype, expected):
with pytest.warns(UserWarning, match="falling back"):
arr = xp.asarray([1], dtype=dtype)
assert arr.dtype == expected


def test_asarray_round_trips_four_dimensional_data():
Expand Down
Loading