Skip to content

Commit

Permalink
Patch dpnpdecl to handle removal of nin, nout in dpnp.
Browse files Browse the repository at this point in the history
  • Loading branch information
Diptorup Deb committed Apr 17, 2024
1 parent ff5b714 commit c793313
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 15 deletions.
35 changes: 28 additions & 7 deletions numba_dpex/core/typing/dpnpdecl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#
# SPDX-License-Identifier: Apache-2.0

import logging

import dpnp
import numpy as np
from numba.core import types
Expand Down Expand Up @@ -34,14 +36,33 @@ class DpnpRulesArrayOperator(NumpyRulesArrayOperator):
@property
def ufunc(self):
try:
op = getattr(dpnp, self._op_map[self.key])
dpnpop = getattr(dpnp, self._op_map[self.key])
npop = getattr(np, self._op_map[self.key])
op.nin = npop.nin
op.nout = npop.nout
op.nargs = npop.nargs
op.types = npop.types
op.is_dpnp_ufunc = True
return op
if not hasattr(dpnpop, "nin"):
dpnpop.nin = npop.nin
if not hasattr(dpnpop, "nout"):
dpnpop.nout = npop.nout
if not hasattr(dpnpop, "nargs"):
dpnpop.nargs = dpnpop.nin + dpnpop.nout

# Check if the dpnp operation has a `types` attribute and if an
# AttributeError gets raised then "monkey patch" the attribute from
# numpy. If the attribute lookup raised a ValueError, it indicates
# that dpnp could not be resolve the supported types for the
# operation. Dpnp will fail to resolve the `types` if no SYCL
# devices are available on the system. For such a scenario, we print
# a user-level warning.
try:
dpnpop.types
except ValueError:
logging.exception(
f"The types attribute for the {dpnpop} fuction could not "
"be determined."
)
except AttributeError:
dpnpop.types = npop.types
dpnpop.is_dpnp_ufunc = True
return dpnpop
except:
pass

Expand Down
18 changes: 10 additions & 8 deletions numba_dpex/dpnp_iface/dpnp_ufunc_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,16 @@ def _fill_ufunc_db_with_dpnp_ufuncs(ufunc_db):
if not hasattr(dpnpop, "nargs"):
dpnpop.nargs = dpnpop.nin + dpnpop.nout

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

0 comments on commit c793313

Please sign in to comment.