Skip to content

Commit 37407eb

Browse files
authored
Add dpnp.exceptions submodule to aggregate generic exceptions (#2616)
The PR adds `dpnp.exceptions` submodule to aggregate generic exceptions used by dpnp. Also it implements a dedicated documentation page for them
1 parent 80af9c3 commit 37407eb

File tree

8 files changed

+134
-3
lines changed

8 files changed

+134
-3
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ dpnp.egg-info
77
# Byte-compiled / optimized / DLL files
88
__pycache__/
99

10+
# Doc build and generated files
11+
doc/_build/
12+
doc/reference/generated/
13+
doc/reference/*.inc
14+
1015
# Code project files
1116
.vscode
1217

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This release changes the license from `BSD-2-Clause` to `BSD-3-Clause`.
1212

1313
* Added the docstrings to `dpnp.linalg.LinAlgError` exception [#2613](https://github.com/IntelPython/dpnp/pull/2613)
1414
* Added implementation of `dpnp.linalg.lu_solve` for batch inputs (SciPy-compatible) [#2619](https://github.com/IntelPython/dpnp/pull/2619)
15+
* Added `dpnp.exceptions` submodule to aggregate the generic exceptions used by dpnp [#2616](https://github.com/IntelPython/dpnp/pull/2616)
1516

1617
### Changed
1718

doc/reference/exceptions.rst

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
.. _routines.exceptions:
2+
3+
.. py:module:: dpnp.exceptions
4+
5+
Exceptions and Warnings
6+
=======================
7+
8+
General exceptions used by DPNP. Note that some exceptions may be module
9+
specific, such as linear algebra errors.
10+
11+
Exceptions
12+
----------
13+
14+
.. exception:: AxisError
15+
16+
Given when an axis is invalid.
17+
18+
.. exception:: DLPackCreationError
19+
20+
Given when constructing DLPack capsule from either :class:`dpnp.ndarray` or
21+
:class:`dpctl.tensor.usm_ndarray` based on a USM allocation
22+
on a partitioned SYCL device.
23+
24+
.. rubric:: Examples
25+
26+
.. code-block:: python
27+
28+
>>> import dpnp as np
29+
>>> import dpctl
30+
>>> dev = dpctl.SyclDevice('cpu')
31+
>>> sdevs = dev.create_sub_devices(partition=[1, 1])
32+
>>> q = dpctl.SyclQueue(sdevs[0])
33+
>>> x = np.ones(10, sycl_queue=q)
34+
>>> np.from_dlpack(x)
35+
Traceback (most recent call last):
36+
...
37+
DLPackCreationError: to_dlpack_capsule: DLPack can only export arrays based on USM allocations bound to a default platform SYCL context
38+
39+
.. exception:: ExecutionPlacementError
40+
41+
Given when execution placement target can not be unambiguously determined
42+
from input arrays. Make sure that input arrays are associated with the same
43+
:class:`dpctl.SyclQueue`, or migrate data to the same
44+
:class:`dpctl.SyclQueue` using :meth:`dpnp.ndarray.to_device` method.
45+
46+
.. exception:: SyclContextCreationError
47+
48+
Given when :class:`dpctl.SyclContext` instance could not be created.
49+
50+
.. exception:: SyclDeviceCreationError
51+
52+
Given when :class:`dpctl.SyclDevice` instance could not be created.
53+
54+
.. exception:: SyclQueueCreationError
55+
56+
Given when :class:`dpctl.SyclQueue` instance could not be created.
57+
The creation can fail if the filter string is invalid, or the backend or
58+
device type values are not supported.
59+
60+
.. exception:: USMAllocationError
61+
62+
Given when Unified Shared Memory (USM) allocation call returns a null
63+
pointer, signaling a failure to perform the allocation.
64+
Some common reasons for allocation failure are:
65+
66+
* insufficient free memory to perform the allocation request
67+
* allocation size exceeds the maximum supported by targeted backend
68+
69+
70+
.. automodule:: dpnp.exceptions
71+
:no-index:

doc/reference/routines.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ These functions cover a subset of
1515
array-manipulation
1616
bitwise
1717
dtype
18+
exceptions
1819
fft
1920
functional
2021
indexing

dpnp/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,15 @@
7373
from .dpnp_iface_utils import *
7474
from .dpnp_iface_utils import __all__ as _ifaceutils__all__
7575
from ._version import get_versions
76+
from . import exceptions as exceptions
7677
from . import linalg as linalg
7778
from . import scipy as scipy
7879

7980
__all__ = _iface__all__
8081
__all__ += _ifaceutils__all__
8182

8283
# add submodules
83-
__all__ += ["linalg", "scipy"]
84+
__all__ += ["exceptions", "linalg", "scipy"]
8485

8586

8687
__version__ = get_versions()["version"]

dpnp/dpnp_iface_arraycreation.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2234,9 +2234,12 @@ def from_dlpack(x, /, *, device=None, copy=None):
22342234
Raises
22352235
------
22362236
TypeError
2237-
if `obj` does not implement ``__dlpack__`` method
2237+
If `x` does not implement ``__dlpack__`` method.
22382238
ValueError
2239-
if data of the input object resides on an unsupported device
2239+
If data of the input object resides on an unsupported device.
2240+
DLPackCreationError
2241+
When `x` is allocated on a partitioned SYCL device, or with
2242+
a non-default context.
22402243
22412244
Notes
22422245
-----

dpnp/exceptions/__init__.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# -*- coding: utf-8 -*-
2+
# *****************************************************************************
3+
# Copyright (c) 2025, Intel Corporation
4+
# All rights reserved.
5+
#
6+
# Redistribution and use in source and binary forms, with or without
7+
# modification, are permitted provided that the following conditions are met:
8+
# - Redistributions of source code must retain the above copyright notice,
9+
# this list of conditions and the following disclaimer.
10+
# - Redistributions in binary form must reproduce the above copyright notice,
11+
# this list of conditions and the following disclaimer in the documentation
12+
# and/or other materials provided with the distribution.
13+
# - Neither the name of the copyright holder nor the names of its contributors
14+
# may be used to endorse or promote products derived from this software
15+
# without specific prior written permission.
16+
#
17+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18+
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19+
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20+
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21+
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22+
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23+
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24+
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25+
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26+
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27+
# THE POSSIBILITY OF SUCH DAMAGE.
28+
# *****************************************************************************
29+
30+
from dpctl import (
31+
SyclContextCreationError,
32+
SyclDeviceCreationError,
33+
SyclQueueCreationError,
34+
)
35+
from dpctl.memory import USMAllocationError
36+
from dpctl.tensor._dlpack import DLPackCreationError
37+
from dpctl.utils import ExecutionPlacementError
38+
from numpy.exceptions import AxisError
39+
40+
__all__ = [
41+
"AxisError",
42+
"DLPackCreationError",
43+
"ExecutionPlacementError",
44+
"SyclDeviceCreationError",
45+
"SyclContextCreationError",
46+
"SyclQueueCreationError",
47+
"USMAllocationError",
48+
]

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"dpnp",
3737
"dpnp.dpnp_algo",
3838
"dpnp.dpnp_utils",
39+
"dpnp.exceptions",
3940
"dpnp.fft",
4041
"dpnp.linalg",
4142
"dpnp.memory",

0 commit comments

Comments
 (0)