Skip to content

Commit c0f3198

Browse files
committed
hint at numpy scalars when a submit with python scalars fails
Kernel submission errors now say that Python scalars are passed as numpy.int64/float64/complex128 when any argument was a Python scalar.
1 parent c8b3f0f commit c0f3198

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

‎dpctl/_sycl_queue.pyx‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1546,6 +1546,8 @@ cdef class SyclQueue(_SyclQueue):
15461546
cdef size_t nDE = len(dEvents) if dEvents is not None else 0
15471547
cdef size_t nKA = 0
15481548
cdef list _converted = []
1549+
cdef list scalars = []
1550+
cdef bint has_py_scalars = False
15491551

15501552
if kernel.get_kernel_ref() is NULL:
15511553
raise SyclKernelSubmitError(
@@ -1562,7 +1564,11 @@ cdef class SyclQueue(_SyclQueue):
15621564
)
15631565

15641566
# Python scalars are passed as the NumPy scalars they convert to
1565-
args = [as_scalar(arg) for arg in args]
1567+
for arg in args:
1568+
scalar = as_scalar(arg)
1569+
has_py_scalars |= scalar is not arg
1570+
scalars.append(scalar)
1571+
args = scalars
15661572

15671573
# Allocate the arrays to be sent to DPCTLQueue_Submit
15681574
kargs = <void**>malloc(len(args) * sizeof(void*))
@@ -1668,9 +1674,14 @@ cdef class SyclQueue(_SyclQueue):
16681674
free(depEvents)
16691675

16701676
if Eref is NULL:
1671-
raise SyclKernelSubmitError(
1672-
"Kernel submission to Sycl queue failed."
1673-
)
1677+
msg = "Kernel submission to Sycl queue failed."
1678+
if has_py_scalars:
1679+
msg += (
1680+
" Python scalars are passed as numpy.int64, numpy.float64 "
1681+
"or numpy.complex128; pass a NumPy scalar matching the "
1682+
"kernel parameter type."
1683+
)
1684+
raise SyclKernelSubmitError(msg)
16741685

16751686
return SyclEvent._create(Eref)
16761687

‎dpctl/tests/test_kernel_arg_dtypes.py‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,19 @@ def test_python_int_arg_out_of_range():
231231
q.submit(krn, [out_usm, 2**63], [1])
232232

233233

234+
def test_python_scalar_arg_size_mismatch():
235+
try:
236+
q = dpctl.SyclQueue("opencl:cpu")
237+
except dpctl.SyclQueueCreationError:
238+
pytest.skip("OpenCL CPU queue could not be created")
239+
src = "kernel void put(global float *out, float d) { out[0] = d; }"
240+
krn = dpc.create_kernel_bundle_from_source(q, src).get_sycl_kernel("put")
241+
out_usm = dpm.MemoryUSMDevice(4, queue=q)
242+
with pytest.raises(dpctl.SyclKernelSubmitError, match="NumPy scalar"):
243+
q.submit(krn, [out_usm, 1.5], [1]).wait()
244+
q.submit(krn, [out_usm, np.float32(1.5)], [1]).wait()
245+
246+
234247
def test_bfloat16_scalar_arg():
235248
ml_dtypes = pytest.importorskip("ml_dtypes")
236249
q = _opencl_queue_or_skip()

0 commit comments

Comments
 (0)