|
| 1 | +#=============================================================================== |
| 2 | +# Copyright 2014-2021 Intel Corporation |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +#=============================================================================== |
| 16 | + |
| 17 | +from functools import wraps |
| 18 | + |
| 19 | +try: |
| 20 | + from sklearnex._config import get_config |
| 21 | + from sklearnex._device_offload import (_get_global_queue, |
| 22 | + _transfer_to_host, |
| 23 | + _copy_to_usm) |
| 24 | + _sklearnex_available = True |
| 25 | +except ImportError: |
| 26 | + import logging |
| 27 | + logging.warning('Device support is limited in daal4py patching. ' |
| 28 | + 'Use Intel(R) Extension for Scikit-learn* ' |
| 29 | + 'for full experience.') |
| 30 | + _sklearnex_available = False |
| 31 | + |
| 32 | + |
| 33 | +def _get_host_inputs(*args, **kwargs): |
| 34 | + q = _get_global_queue() |
| 35 | + q, hostargs = _transfer_to_host(q, *args) |
| 36 | + q, hostvalues = _transfer_to_host(q, *kwargs.values()) |
| 37 | + hostkwargs = dict(zip(kwargs.keys(), hostvalues)) |
| 38 | + return q, hostargs, hostkwargs |
| 39 | + |
| 40 | + |
| 41 | +def _extract_usm_iface(*args, **kwargs): |
| 42 | + allargs = (*args, *kwargs.values()) |
| 43 | + if len(allargs) == 0: |
| 44 | + return None |
| 45 | + return getattr(allargs[0], |
| 46 | + '__sycl_usm_array_interface__', |
| 47 | + None) |
| 48 | + |
| 49 | + |
| 50 | +def _run_on_device(func, queue, obj=None, *args, **kwargs): |
| 51 | + def dispatch_by_obj(obj, func, *args, **kwargs): |
| 52 | + if obj is not None: |
| 53 | + return func(obj, *args, **kwargs) |
| 54 | + return func(*args, **kwargs) |
| 55 | + |
| 56 | + if queue is not None: |
| 57 | + from daal4py.oneapi import sycl_context, _get_in_sycl_ctxt |
| 58 | + |
| 59 | + if _get_in_sycl_ctxt() is False: |
| 60 | + host_offload = get_config()['allow_fallback_to_host'] |
| 61 | + |
| 62 | + with sycl_context('gpu' if queue.sycl_device.is_gpu else 'cpu', |
| 63 | + host_offload_on_fail=host_offload): |
| 64 | + return dispatch_by_obj(obj, func, *args, **kwargs) |
| 65 | + return dispatch_by_obj(obj, func, *args, **kwargs) |
| 66 | + |
| 67 | + |
| 68 | +def support_usm_ndarray(freefunc=False): |
| 69 | + def decorator(func): |
| 70 | + def wrapper_impl(obj, *args, **kwargs): |
| 71 | + if _sklearnex_available: |
| 72 | + usm_iface = _extract_usm_iface(*args, **kwargs) |
| 73 | + q, hostargs, hostkwargs = _get_host_inputs(*args, **kwargs) |
| 74 | + result = _run_on_device(func, q, obj, *hostargs, **hostkwargs) |
| 75 | + if usm_iface is not None and hasattr(result, '__array_interface__'): |
| 76 | + return _copy_to_usm(q, result) |
| 77 | + return result |
| 78 | + return _run_on_device(func, None, obj, *args, **kwargs) |
| 79 | + |
| 80 | + if freefunc: |
| 81 | + @wraps(func) |
| 82 | + def wrapper_free(*args, **kwargs): |
| 83 | + return wrapper_impl(None, *args, **kwargs) |
| 84 | + return wrapper_free |
| 85 | + else: |
| 86 | + @wraps(func) |
| 87 | + def wrapper_with_self(self, *args, **kwargs): |
| 88 | + return wrapper_impl(self, *args, **kwargs) |
| 89 | + return wrapper_with_self |
| 90 | + return decorator |
0 commit comments