|
| 1 | +# SPDX-FileCopyrightText: 2020 - 2024 Intel Corporation |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +import logging |
| 6 | + |
| 7 | +import dpnp |
| 8 | +import numpy as np |
| 9 | + |
| 10 | +from numba_dpex.core.typing import dpnpdecl |
| 11 | + |
| 12 | + |
| 13 | +def patch(): |
| 14 | + patch_is_ufunc() |
| 15 | + patch_ufuncs() |
| 16 | + |
| 17 | + |
| 18 | +def patch_is_ufunc(): |
| 19 | + """Patches the numba.np.ufunc.array_exprs._is_ufunc function to make it |
| 20 | + possible to support dpnp universal functions (ufuncs). |
| 21 | +
|
| 22 | + The extra condition is the check for the "is_dpnp_ufunc" attribute to |
| 23 | + identify a non-NumPy ufunc. |
| 24 | + """ |
| 25 | + import numpy |
| 26 | + from numba.np.ufunc.dufunc import DUFunc |
| 27 | + |
| 28 | + def _is_ufunc(func): |
| 29 | + return isinstance(func, (numpy.ufunc, DUFunc)) or hasattr( |
| 30 | + func, "is_dpnp_ufunc" |
| 31 | + ) |
| 32 | + |
| 33 | + from numba.np.ufunc import array_exprs |
| 34 | + |
| 35 | + array_exprs._is_ufunc = _is_ufunc |
| 36 | + |
| 37 | + |
| 38 | +def patch_ufuncs(): |
| 39 | + """Patches dpnp user functions to make them compatible with numpy, so we |
| 40 | + can reuse numba's implementation. |
| 41 | +
|
| 42 | + It adds "nin", "nout", "nargs" and "is_dpnp_ufunc" attributes to ufuncs. |
| 43 | + """ |
| 44 | + failed_dpnpop_types_lst = [] |
| 45 | + |
| 46 | + op = getattr(dpnp, "erf") |
| 47 | + op.nin = 1 |
| 48 | + op.nout = 1 |
| 49 | + op.nargs = 2 |
| 50 | + op.types = ["f->f", "d->d"] |
| 51 | + op.is_dpnp_ufunc = True |
| 52 | + |
| 53 | + for ufuncop in dpnpdecl.supported_ufuncs: |
| 54 | + if ufuncop == "erf": |
| 55 | + continue |
| 56 | + |
| 57 | + dpnpop = getattr(dpnp, ufuncop) |
| 58 | + npop = getattr(np, ufuncop) |
| 59 | + |
| 60 | + if not hasattr(dpnpop, "nin"): |
| 61 | + dpnpop.nin = npop.nin |
| 62 | + if not hasattr(dpnpop, "nout"): |
| 63 | + dpnpop.nout = npop.nout |
| 64 | + if not hasattr(dpnpop, "nargs"): |
| 65 | + dpnpop.nargs = dpnpop.nin + dpnpop.nout |
| 66 | + |
| 67 | + # Check if the dpnp operation has a `types` attribute and if an |
| 68 | + # AttributeError gets raised then "monkey patch" the attribute from |
| 69 | + # numpy. If the attribute lookup raised a ValueError, it indicates |
| 70 | + # that dpnp could not be resolve the supported types for the |
| 71 | + # operation. Dpnp will fail to resolve the `types` if no SYCL |
| 72 | + # devices are available on the system. For such a scenario, we log |
| 73 | + # dpnp operations for which the ValueError happened and print them |
| 74 | + # as a user-level warning. It is done this way so that the failure |
| 75 | + # to load the dpnpdecl registry due to the ValueError does not |
| 76 | + # impede a user from importing numba-dpex. |
| 77 | + try: |
| 78 | + dpnpop.types |
| 79 | + except ValueError: |
| 80 | + failed_dpnpop_types_lst.append(ufuncop) |
| 81 | + except AttributeError: |
| 82 | + dpnpop.types = npop.types |
| 83 | + |
| 84 | + dpnpop.is_dpnp_ufunc = True |
| 85 | + |
| 86 | + if len(failed_dpnpop_types_lst) > 0: |
| 87 | + try: |
| 88 | + getattr(dpnp, failed_dpnpop_types_lst[0]).types |
| 89 | + except ValueError: |
| 90 | + ops = " ".join(failed_dpnpop_types_lst) |
| 91 | + logging.exception( |
| 92 | + "The types attribute for the following dpnp ops could not be " |
| 93 | + f"determined: {ops}" |
| 94 | + ) |
0 commit comments