FAQ (Frequently Asked Questions)
What is the relationship between scikit-learn and the Extension for scikit-learn*?
The Extension for scikit-learn* is a separate library providing classes and functions which build on top of their scikit-learn analogs (i.e. an extension), with some methods being overridden to replace calls to scikit-learn internals with calls to more optimized equivalents from the oneAPI Data Analytics Library while still producing the same results, up to numerical accuracy.
These optimized versions are under an importable module named sklearnex which provides an API-compatible alternative to sklearn for a selected subset of classes and functions (see supported algorithms). Given the API compatibility, it is possible to monkeypatch the sklearn module to automatically use the Extension for scikit-learn* whenever possible without further code changes.
Is sklearnex a drop-in replacement for sklearn?
Depending on usage mode:
Yes, when applying monkeypatching, the modified sklearn module does become a drop-in replacement for stock scikit-learn.
Not entirely, in case of direct sklearnex use. sklearnex offers only a subset of the classes and functions from sklearn, but when the same classes and functions are offered (such as sklearnex.linear_model.LinearRegression), they do act as drop-in replacements with the exact same API and behaviors.
Do I need a specific version of scikit-learn with the Extension for scikit-learn*?
The current release of the Extension for scikit-learn* aims to be compatible with the last 4 minor releases of scikit-learn available at any given time, typically covering a period of approximately 2 years of releases. See the README badges for more information about supported versions at any given moment.
How does the Extension for scikit-learn* ensure API compatibility with different versions of scikit-learn?
Classes from the Extension for scikit-learn* inherit from their analog classes in scikit-learn, exposing the same set of public attributes and methods, and reusing many functionalities from scikit-learn without modifications, oftentimes calling code from scikit-learn itself to fill in gaps in features between the oneAPI Data Analytics Library and scikit-learn.
Runtime version checks performed on the sklearn module enable dispatching of different code branches within the Extension for scikit-learn* to ensure matching behavior against multiple possible versions, ensuring compatibility in version-dependent details such as renamed attributes, user warnings, and similar.
The Extension for scikit-learn* is regularly tested for both API compatibility and for correctness against scikit-learn’s own test suite (see Scikit-learn’s test suite for more information) to verify this.
Does the Extension for scikit-learn* always produce the exact same results as scikit-learn?
When accelerated routines from the Extension for scikit-learn* are used, results (e.g. numbers returned by .predict(), coefficients fitted by sklearn.linear_model.LinearRegression, etc.) can be expected to match against what would be obtained through scikit-learn up to numerical precision for the floating point type (float32 / float64) supplied as input data (note that non-floating point types are casted to float64), but outputs might not necessarily be exactly equal byte-by-byte.
When it comes to procedures that employ random numbers (such as sklearn.model_selection.train_test_split()), results might differ a bit since the Extension for scikit-learn* will generate different sequences of random numbers, but they should match in expectation over large samples.
Additionally, some algorithms might have methodological differences that yield slightly different results - for instance, forest ensembles (such as sklearn.ensemble.RandomForestClassifier) use the histogram method (i.e. features are binned) for calculating splits, which might not always produce the same result on average as the sorted-indices method used by scikit-learn, but should result in observing very similar numbers in cross-validated metrics such as roc_auc_score or r2_score.
What makes the Extension for scikit-learn* faster?
The Extension for scikit-learn* replaces selected calls to internal functions and methods in scikit-learn with more optimized equivalents from the oneAPI Data Analytics Library, which is a C++ library offering high-performance implementations of common machine learning routines. These optimizations in turn come from leveraging SIMD instructions and exploiting cache structures of modern hardware, along with using the oneMKL library for linear algebra operations in place of the OpenBLAS library used by default by scikit-learn.
See the list of supported algorithms for more information about what is accelerated by the Extension for scikit-learn* and what isn’t.
Who develops the Extension for scikit-learn*?
The Extension for scikit-learn* is developed by the UXL Foundation, a consortium for open multi-vendor standards in accelerated computing.
Does patching modify files from scikit-learn?
Not by default. The scope of patch_sklearn() is limited to the Python process in which it was called, and will not persist across sessions.
However, patching can optionally be applied permanently by modifying an existing scikit-learn installation with patch_sklearn(global_patch=True) - see Patching Utilities for more details. This permanent patching in turn can be undone by unpatch_sklearn(global_patch=True).
Can I use the library without patching scikit-learn?
Yes, it is possible to import estimators and functions from the sklearnex module without doing any modifications to the sklearn module. See Quick Start for usage examples with and without patching.
Will all workflows run faster under patching?
No. The Extension for scikit-learn* only accelerates a selected subset of routines from scikit-learn. See Supported Algorithms for details.
What happens when some routine is not supported?
When an operation is not supported by the Extension for scikit-learn*, the original code branches from scikit-learn will be called instead, which is referred throughout these docs as a ‘fallback’. Therefore, results are the same as what would be obtained when not using the Extension for scikit-learn*.
In these cases however, there will be no acceleration. The underlying library being used for a given computation can be verified using verbose mode. See Supported Algorithms for more information about what is covered by the Extension for scikit-learn*.
Should I use the Extension for scikit-learn* if I already use scikit-learn with NumPy and SciPy backed by oneMKL?
Yes - beyond usage of accelerated routines from oneMKL, the Extension for scikit-learn* introduces other forms of optimizations that cannot be replicated by swapping BLAS or LAPACK implementations, such as different parallelization strategies for calculations of covariances and Gram matrices, or usage of more specialized linear algebra factorizations than the ones used by default NumPy solvers whenever applicable.
Should I use the Extension for scikit-learn* if I already use array API with Torch or DPNP?
Yes - for both CPU and GPU, the Extension for scikit-learn* is able to leverage finer-grained optimizations that are not feasible when using the limited subset of functionalities mandated by the array API standard. See other points throughout this doc page for more info.
How is the Extension for scikit-learn* faster than scikit-learn on GPU if both use array API?
Compared to scikit-learn, when array API inputs (such as Torch Tensors or dpnp.ndarray) are passed to the Extension for scikit-learn*, most computations will not be handled through the array’s namespace object the way scikit-learn would do.
Instead, memory pointers, devices, and queues are extracted from the array objects and forwarded to the oneAPI Data Analytics Library for the most computationally-heavy operations, which in turn employs more specialized compute kernels defined through the SYCL framework for some operations, and makes calls to the DPC routines from oneMKL whenever applicable.
See Array API support for more information.
Why is array API not working when I try it?
In order to use array API, it must first be enabled in both scikit-learn and SciPy, and operations must be run under a config_context or global settings must be modified for it to work.