Skip to content

Commit c793313

Browse files
author
Diptorup Deb
committed
Patch dpnpdecl to handle removal of nin, nout in dpnp.
1 parent ff5b714 commit c793313

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

numba_dpex/core/typing/dpnpdecl.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#
33
# SPDX-License-Identifier: Apache-2.0
44

5+
import logging
6+
57
import dpnp
68
import numpy as np
79
from numba.core import types
@@ -34,14 +36,33 @@ class DpnpRulesArrayOperator(NumpyRulesArrayOperator):
3436
@property
3537
def ufunc(self):
3638
try:
37-
op = getattr(dpnp, self._op_map[self.key])
39+
dpnpop = getattr(dpnp, self._op_map[self.key])
3840
npop = getattr(np, self._op_map[self.key])
39-
op.nin = npop.nin
40-
op.nout = npop.nout
41-
op.nargs = npop.nargs
42-
op.types = npop.types
43-
op.is_dpnp_ufunc = True
44-
return op
41+
if not hasattr(dpnpop, "nin"):
42+
dpnpop.nin = npop.nin
43+
if not hasattr(dpnpop, "nout"):
44+
dpnpop.nout = npop.nout
45+
if not hasattr(dpnpop, "nargs"):
46+
dpnpop.nargs = dpnpop.nin + dpnpop.nout
47+
48+
# Check if the dpnp operation has a `types` attribute and if an
49+
# AttributeError gets raised then "monkey patch" the attribute from
50+
# numpy. If the attribute lookup raised a ValueError, it indicates
51+
# that dpnp could not be resolve the supported types for the
52+
# operation. Dpnp will fail to resolve the `types` if no SYCL
53+
# devices are available on the system. For such a scenario, we print
54+
# a user-level warning.
55+
try:
56+
dpnpop.types
57+
except ValueError:
58+
logging.exception(
59+
f"The types attribute for the {dpnpop} fuction could not "
60+
"be determined."
61+
)
62+
except AttributeError:
63+
dpnpop.types = npop.types
64+
dpnpop.is_dpnp_ufunc = True
65+
return dpnpop
4566
except:
4667
pass
4768

numba_dpex/dpnp_iface/dpnp_ufunc_db.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,16 @@ def _fill_ufunc_db_with_dpnp_ufuncs(ufunc_db):
8383
if not hasattr(dpnpop, "nargs"):
8484
dpnpop.nargs = dpnpop.nin + dpnpop.nout
8585

86-
# Check for `types` attribute for dpnp op.
87-
# AttributeError:
88-
# If the `types` attribute is not present for dpnp op,
89-
# use the `types` attribute from corresponding numpy op.
90-
# ValueError:
91-
# Store all dpnp ops that failed when `types` attribute
92-
# is present but failure occurs when read.
93-
# Log all failing dpnp outside this loop.
86+
# Check if the dpnp operation has a `types` attribute and if an
87+
# AttributeError gets raised then "monkey patch" the attribute from
88+
# numpy. If the attribute lookup raised a ValueError, it indicates
89+
# that dpnp could not be resolve the supported types for the
90+
# operation. Dpnp will fail to resolve the `types` if no SYCL
91+
# devices are available on the system. For such a scenario, we log
92+
# dpnp operations for which the ValueError happened and print them
93+
# as a user-level warning. It is done this way so that the failure
94+
# to load the dpnpdecl registry due to the ValueError does not
95+
# impede a user from importing numba-dpex.
9496
try:
9597
dpnpop.types
9698
except ValueError:

0 commit comments

Comments
 (0)