Skip to content

Commit dfcb644

Browse files
DOC: Showcase usage without patching in readme (#2411)
* mention usage without patching * move unpatched examples to separate section * add snippet also to online docs * use tabs for different ways of showcasing usage
1 parent c1efdce commit dfcb644

File tree

2 files changed

+125
-25
lines changed

2 files changed

+125
-25
lines changed

README.md

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,11 @@ With Extension for Scikit-learn, you can:
5959

6060
## Optimizations
6161

62+
Easiest way to benefit from accelerations from the extension is by patching scikit-learn with it:
63+
6264
- **Enable CPU optimizations**
6365

64-
```py
66+
```python
6567
import numpy as np
6668
from sklearnex import patch_sklearn
6769
patch_sklearn()
@@ -77,7 +79,7 @@ With Extension for Scikit-learn, you can:
7779

7880
_Note: executing on GPU has [additional system software requirements](https://www.intel.com/content/www/us/en/developer/articles/system-requirements/intel-oneapi-dpcpp-system-requirements.html) - see [details](https://uxlfoundation.github.io/scikit-learn-intelex/latest/oneapi-gpu.html)._
7981

80-
```py
82+
```python
8183
import numpy as np
8284
from sklearnex import patch_sklearn, config_context
8385
patch_sklearn()
@@ -89,21 +91,50 @@ With Extension for Scikit-learn, you can:
8991
with config_context(target_offload="gpu:0"):
9092
clustering = DBSCAN(eps=3, min_samples=2).fit(X)
9193
```
94+
9295
:eyes: Check out available [notebooks](https://github.com/uxlfoundation/scikit-learn-intelex/tree/master/examples/notebooks) for more examples.
9396

97+
### Usage without patching
98+
99+
Alternatively, all functionalities are also available under a separate module which can be imported directly, without involving any patching.
100+
101+
* To run on CPU:
102+
103+
```python
104+
import numpy as np
105+
from sklearnex.cluster import DBSCAN
106+
107+
X = np.array([[1., 2.], [2., 2.], [2., 3.],
108+
[8., 7.], [8., 8.], [25., 80.]], dtype=np.float32)
109+
clustering = DBSCAN(eps=3, min_samples=2).fit(X)
110+
```
111+
112+
* To run on GPU:
113+
114+
```python
115+
import numpy as np
116+
from sklearnex import config_context
117+
from sklearnex.cluster import DBSCAN
118+
119+
X = np.array([[1., 2.], [2., 2.], [2., 3.],
120+
[8., 7.], [8., 8.], [25., 80.]], dtype=np.float32)
121+
with config_context(target_offload="gpu:0"):
122+
clustering = DBSCAN(eps=3, min_samples=2).fit(X)
123+
```
124+
94125
## Installation
95126

96127
To install Extension for Scikit-learn, run:
97128

98-
```
129+
```shell
99130
pip install scikit-learn-intelex
100131
```
101132

102-
See all installation instructions in the [Installation Guide](https://github.com/uxlfoundation/scikit-learn-intelex/blob/main/INSTALL.md).
133+
Package is also offered through other channels such as conda-forge. See all installation instructions in the [Installation Guide](https://github.com/uxlfoundation/scikit-learn-intelex/blob/main/INSTALL.md).
103134

104135
## Integration
105136

106-
The software acceleration is achieved through patching. It means, replacing the stock scikit-learn algorithms with their optimized versions provided by the extension.
137+
The easiest way of accelerating scikit-learn workflows with the extension is through through [patching](https://uxlfoundation.github.io/scikit-learn-intelex/latest/quick-start.html#patching), which replaces the stock scikit-learn algorithms with their optimized versions provided by the extension using the same namespaces in the same modules as scikit-learn.
107138

108139
The patching only affects [supported algorithms and their parameters](https://uxlfoundation.github.io/scikit-learn-intelex/latest/algorithms.html).
109140
You can still use not supported ones in your code, the package simply fallbacks into the stock version of scikit-learn.
@@ -112,16 +143,25 @@ You can still use not supported ones in your code, the package simply fallbacks
112143

113144
To patch scikit-learn, you can:
114145
* Use the following command-line flag:
115-
```
146+
```shell
116147
python -m sklearnex my_application.py
117148
```
118149
* Add the following lines to the script:
119-
```
150+
```python
120151
from sklearnex import patch_sklearn
121152
patch_sklearn()
122153
```
123154

124-
:eyes: Read about [other ways to patch scikit-learn](https://uxlfoundation.github.io/scikit-learn-intelex/index.html#usage).
155+
:eyes: Read about [other ways to patch scikit-learn](https://uxlfoundation.github.io/scikit-learn-intelex/latest/quick-start.html#patching).
156+
157+
As an alternative, accelerated classes from the extension can also be imported directly without patching, thereby allowing to keep them separate from stock scikit-learn ones - for example:
158+
159+
```python
160+
from sklearnex.cluster import DBSCAN as exDBSCAN
161+
from sklearn.cluster import DBSCAN as stockDBSCAN
162+
163+
# ...
164+
```
125165

126166
## Documentation
127167

doc/sources/index.rst

Lines changed: 77 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,36 +52,96 @@ Optimizations
5252
Enable CPU Optimizations
5353
*********************************
5454

55-
::
55+
.. tabs::
56+
.. tab:: By patching
57+
.. code-block:: python
5658
57-
import numpy as np
58-
from sklearnex import patch_sklearn
59-
patch_sklearn()
59+
import numpy as np
60+
from sklearnex import patch_sklearn
61+
patch_sklearn()
6062
61-
from sklearn.cluster import DBSCAN
63+
from sklearn.cluster import DBSCAN
64+
65+
X = np.array([[1., 2.], [2., 2.], [2., 3.],
66+
[8., 7.], [8., 8.], [25., 80.]], dtype=np.float32)
67+
clustering = DBSCAN(eps=3, min_samples=2).fit(X)
68+
69+
.. tab:: Without patching
70+
.. code-block:: python
71+
72+
import numpy as np
73+
from sklearnex.cluster import DBSCAN
74+
75+
X = np.array([[1., 2.], [2., 2.], [2., 3.],
76+
[8., 7.], [8., 8.], [25., 80.]], dtype=np.float32)
77+
clustering = DBSCAN(eps=3, min_samples=2).fit(X)
6278
63-
X = np.array([[1., 2.], [2., 2.], [2., 3.],
64-
[8., 7.], [8., 8.], [25., 80.]], dtype=np.float32)
65-
clustering = DBSCAN(eps=3, min_samples=2).fit(X)
6679
6780
Enable GPU optimizations
6881
*********************************
6982

7083
Note: executing on GPU has `additional system software requirements <https://www.intel.com/content/www/us/en/developer/articles/system-requirements/intel-oneapi-dpcpp-system-requirements.html>`__ - see :doc:`oneapi-gpu`.
7184

72-
::
85+
.. tabs::
86+
.. tab:: By patching
87+
.. tabs::
88+
.. tab:: By moving data to device
89+
.. code-block:: python
90+
91+
import numpy as np
92+
from sklearnex import patch_sklearn, config_context
93+
patch_sklearn()
94+
95+
from sklearn.cluster import DBSCAN
96+
97+
X = np.array([[1., 2.], [2., 2.], [2., 3.],
98+
[8., 7.], [8., 8.], [25., 80.]], dtype=np.float32)
99+
with config_context(target_offload="gpu:0"):
100+
clustering = DBSCAN(eps=3, min_samples=2).fit(X)
101+
102+
.. tab:: With GPU arrays
103+
.. code-block:: python
104+
105+
import numpy as np
106+
import dpnp
107+
from sklearnex import patch_sklearn
108+
patch_sklearn()
109+
110+
from sklearn.cluster import DBSCAN
111+
112+
X = np.array([[1., 2.], [2., 2.], [2., 3.],
113+
[8., 7.], [8., 8.], [25., 80.]], dtype=np.float32)
114+
X = dpnp.array(X, device="gpu")
115+
clustering = DBSCAN(eps=3, min_samples=2).fit(X)
116+
117+
.. tab:: Without patching
118+
.. tabs::
119+
.. tab:: By moving data to device
120+
.. code-block:: python
121+
122+
import numpy as np
123+
from sklearnex import config_context
124+
from sklearnex.cluster import DBSCAN
125+
126+
X = np.array([[1., 2.], [2., 2.], [2., 3.],
127+
[8., 7.], [8., 8.], [25., 80.]], dtype=np.float32)
128+
with config_context(target_offload="gpu:0"):
129+
clustering = DBSCAN(eps=3, min_samples=2).fit(X)
130+
131+
.. tab:: With GPU arrays
132+
.. code-block:: python
73133
74-
import numpy as np
75-
from sklearnex import patch_sklearn, config_context
76-
patch_sklearn()
134+
import numpy as np
135+
import dpnp
136+
from sklearnex.cluster import DBSCAN
77137
78-
from sklearn.cluster import DBSCAN
138+
X = np.array([[1., 2.], [2., 2.], [2., 3.],
139+
[8., 7.], [8., 8.], [25., 80.]], dtype=np.float32)
140+
X = dpnp.array(X, device="gpu")
141+
clustering = DBSCAN(eps=3, min_samples=2).fit(X)
79142
80-
X = np.array([[1., 2.], [2., 2.], [2., 3.],
81-
[8., 7.], [8., 8.], [25., 80.]], dtype=np.float32)
82-
with config_context(target_offload="gpu:0"):
83-
clustering = DBSCAN(eps=3, min_samples=2).fit(X)
84143
144+
See :ref:`oneapi_gpu` for other ways of executing on GPU.
85145

86146

87147
.. toctree::

0 commit comments

Comments
 (0)