|
| 1 | +.. Copyright contributors to the oneDAL project |
| 2 | +.. |
| 3 | +.. Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +.. you may not use this file except in compliance with the License. |
| 5 | +.. You may obtain a copy of the License at |
| 6 | +.. |
| 7 | +.. http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +.. |
| 9 | +.. Unless required by applicable law or agreed to in writing, software |
| 10 | +.. distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +.. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +.. See the License for the specific language governing permissions and |
| 13 | +.. limitations under the License. |
| 14 | +.. include:: substitutions.rst |
| 15 | +.. _config_contexts: |
| 16 | + |
| 17 | +========================================= |
| 18 | +Configuration Contexts and Global Options |
| 19 | +========================================= |
| 20 | + |
| 21 | +Overview |
| 22 | +======== |
| 23 | + |
| 24 | +Just like |sklearn|, the |sklearnex| offers configurable options which can be managed |
| 25 | +locally through a configuration context, or globally through process-wide settings, |
| 26 | +by extending the configuration-related functions from |sklearn| (see :obj:`sklearn.config_context` |
| 27 | +for details). |
| 28 | + |
| 29 | +Configurations in the |sklearnex| are particularly useful for :ref:`GPU functionalities <oneapi_gpu>` |
| 30 | +and :ref:`SMPD mode <distributed>`, and are necessary to modify for enabling :ref:`array API <array_api>`. |
| 31 | + |
| 32 | +Configuration context and global options manager for the |sklearnex| can either be imported directly |
| 33 | +from the module ``sklearnex``, or can be imported from the ``sklearn`` module after applying patching. |
| 34 | + |
| 35 | +Note that options in the |sklearnex| are a superset of options from |sklearn|, and options passed to |
| 36 | +the configuration contexts and global settings of the |sklearnex| will also affect |sklearn| if the |
| 37 | +option is supported by it - meaning: the same context manager or global option setter is used for |
| 38 | +both libraries. |
| 39 | + |
| 40 | +Example usage |
| 41 | +============= |
| 42 | + |
| 43 | +Example using the ``target_offload`` option to make computations run on a GPU: |
| 44 | + |
| 45 | +With a local context |
| 46 | +-------------------- |
| 47 | + |
| 48 | +Here, only the operations from |sklearn| and from the |sklearnex| that happen within the 'with' |
| 49 | +block will be affected by the options: |
| 50 | + |
| 51 | +.. code:: python |
| 52 | +
|
| 53 | + import numpy as np |
| 54 | + from sklearnex import config_context |
| 55 | + from sklearnex.cluster import DBSCAN |
| 56 | +
|
| 57 | + X = np.array([[1., 2.], [2., 2.], [2., 3.], |
| 58 | + [8., 7.], [8., 8.], [25., 80.]], dtype=np.float32) |
| 59 | + with config_context(target_offload="gpu"): |
| 60 | + clustering = DBSCAN(eps=3, min_samples=2).fit(X) |
| 61 | +
|
| 62 | +As a global option |
| 63 | +------------------ |
| 64 | + |
| 65 | +Here, all computations from |sklearn| and from the |sklearnex| that happen after the option |
| 66 | +is modified are affected: |
| 67 | + |
| 68 | +.. code:: python |
| 69 | +
|
| 70 | + import numpy as np |
| 71 | + from sklearnex import set_config |
| 72 | + from sklearnex.cluster import DBSCAN |
| 73 | +
|
| 74 | + X = np.array([[1., 2.], [2., 2.], [2., 3.], |
| 75 | + [8., 7.], [8., 8.], [25., 80.]], dtype=np.float32) |
| 76 | + |
| 77 | + set_config(target_offload="gpu") # set it globally |
| 78 | + clustering = DBSCAN(eps=3, min_samples=2).fit(X) |
| 79 | + set_config(target_offload="auto") # restore it back |
| 80 | +
|
| 81 | +API Reference |
| 82 | +============= |
| 83 | + |
| 84 | +Note that all of the options accepted by these functions in |sklearn| are also accepted |
| 85 | +here - these just list the additional options offered by the |sklearnex|. |
| 86 | + |
| 87 | +.. autofunction:: sklearnex.config_context |
| 88 | + |
| 89 | +.. autofunction:: sklearnex.get_config |
| 90 | + |
| 91 | +.. autofunction:: sklearnex.set_config |
0 commit comments