0
# Patching and Configuration
1
2
Control functions for enabling Intel optimizations globally and managing package configuration. These functions determine how scikit-learn algorithms are accelerated and allow runtime configuration of optimization behavior.
3
4
## Capabilities
5
6
### Global Patching
7
8
Enable or disable Intel optimizations for all scikit-learn imports system-wide.
9
10
```python { .api }
11
def patch_sklearn():
12
"""
13
Enable Intel Extension optimizations for all scikit-learn algorithms.
14
15
After calling this function, all subsequent scikit-learn imports will
16
automatically use Intel-optimized implementations when available.
17
18
Example:
19
from sklearnex import patch_sklearn
20
patch_sklearn()
21
22
# Now uses Intel-optimized implementation
23
from sklearn.ensemble import RandomForestClassifier
24
"""
25
26
def unpatch_sklearn():
27
"""
28
Disable Intel Extension optimizations and restore original scikit-learn.
29
30
Returns scikit-learn to its original state, using standard implementations
31
for all subsequent imports and operations.
32
"""
33
34
def sklearn_is_patched() -> bool:
35
"""
36
Check if scikit-learn is currently patched with Intel optimizations.
37
38
Returns:
39
bool: True if Intel optimizations are active, False otherwise
40
"""
41
```
42
43
### Patch Information
44
45
Query available optimizations and check implementation status.
46
47
```python { .api }
48
def get_patch_map() -> dict:
49
"""
50
Get mapping of available Intel-optimized implementations.
51
52
Returns:
53
dict: Mapping from original sklearn classes to Intel implementations
54
"""
55
56
def get_patch_names() -> list:
57
"""
58
Get list of algorithm names available for Intel optimization.
59
60
Returns:
61
list: Names of algorithms that have Intel-accelerated versions
62
"""
63
64
def is_patched_instance(estimator) -> bool:
65
"""
66
Check if a specific estimator instance uses Intel optimization.
67
68
Parameters:
69
estimator: Scikit-learn estimator instance
70
71
Returns:
72
bool: True if instance uses Intel optimization, False otherwise
73
"""
74
```
75
76
### Configuration Management
77
78
Manage runtime configuration for Intel optimizations and scikit-learn settings.
79
80
```python { .api }
81
def set_config(**params):
82
"""
83
Set configuration parameters for Intel Extension and scikit-learn.
84
85
Parameters:
86
**params: Configuration parameters to set
87
"""
88
89
def get_config() -> dict:
90
"""
91
Retrieve current configuration values.
92
93
Returns:
94
dict: Current configuration parameters and their values
95
"""
96
97
def config_context(**params):
98
"""
99
Context manager for temporary configuration changes.
100
101
Parameters:
102
**params: Temporary configuration parameters
103
104
Example:
105
with config_context(assume_finite=True):
106
# Code with temporary configuration
107
model.fit(X, y)
108
"""
109
```
110
111
## Usage Examples
112
113
### Basic Patching
114
115
```python
116
from sklearnex import patch_sklearn, sklearn_is_patched
117
118
# Check initial state
119
print(f"Initially patched: {sklearn_is_patched()}") # False
120
121
# Enable optimizations
122
patch_sklearn()
123
print(f"After patching: {sklearn_is_patched()}") # True
124
125
# All sklearn imports now use Intel optimizations
126
from sklearn.cluster import KMeans
127
from sklearn.ensemble import RandomForestClassifier
128
129
# Create and use optimized models
130
kmeans = KMeans(n_clusters=3)
131
rf = RandomForestClassifier(n_estimators=100)
132
```
133
134
### Checking Optimizations
135
136
```python
137
from sklearnex import patch_sklearn, get_patch_names, is_patched_instance
138
from sklearn.ensemble import RandomForestClassifier
139
140
# Enable optimizations
141
patch_sklearn()
142
143
# See what's available
144
available_patches = get_patch_names()
145
print(f"Available optimizations: {available_patches}")
146
147
# Create model and check if optimized
148
rf = RandomForestClassifier()
149
print(f"Using Intel optimization: {is_patched_instance(rf)}") # True
150
```
151
152
### Configuration Context
153
154
```python
155
from sklearnex import config_context, get_config
156
from sklearn.ensemble import RandomForestClassifier
157
158
# Check current config
159
current_config = get_config()
160
print(f"Current config: {current_config}")
161
162
# Temporary configuration change
163
with config_context(assume_finite=True):
164
rf = RandomForestClassifier()
165
# Model created with temporary configuration
166
```
167
168
## Environment Variables
169
170
- **OFF_ONEDAL_IFACE**: Set to "1" to disable oneDAL interface entirely
171
- **SKLEARNEX_PREVIEW**: Enable experimental preview features
172
173
## Performance Notes
174
175
- Patching is a one-time operation per Python session
176
- No performance overhead from patching itself
177
- Original sklearn behavior can be restored with `unpatch_sklearn()`
178
- Patching affects globally imported modules but not already-imported ones