Artificial Intelligence

Save Time and Money with Intel Extension for Scikit-learn

Get Up to a Hundredfold Performance Improvement in Your Machine Learning Applications

Egor Smirnov
5 min readJul 7, 2021

--

Scikit-learn is the most widely used package for data science and machine learning (ML), but to be honest, its performance is not always optimal. It’s mostly implemented in Python so some ML algorithms can take hours to run, which is expensive. Intel Extension for Scikit-learn can deliver significant performance improvements just by adding a couple of lines of code. It’s also open source.

(Note: The extension contains scikit-learn optimizations that were originally in the daal4py package, but it was decided to extract them into a separate Python package: scikit-learn-intelex. All future updates for scikit-learn will be available only in Intel Extension for Scikit-learn. We recommend using scikit-learn-intelex instead of daal4py.)

Benefits of Intel Extension for Scikit-learn

Intel Extension for Scikit-learn provides optimized implementations of many scikit-learn algorithms (Table 1), which are conformant with the original version and show the same results (validated by scikit-learn CI). New algorithms (e.g., SVR, NuSVC, NuSVR) will be added in future releases. When you are using algorithms or parameters not supported by the extension, the package just falls back into the original scikit-learn. This makes the user experience seamless. Your application works as before or faster without any need to rewrite the code.

Table 1. Accelerated algorithms in Intel Extension for Scikit-learn (as of v2021.2.3)

To see how much time and money you can save by using Intel® Extension for Scikit-learn, we compare the patched scikit-learn with the original package for ML training and inference (Figures 1 and 2). We used a c5.24xlarge AWS EC2 instance for these benchmarks (hardware and software details are below).

Figure 1. Training speedup with Intel Extension for Scikit-learn over the original package
Figure 2. Inference speedup with Intel Extension for Scikit-learn over the original package

To get a sense of the performance improvement, let’s look at kNN Brute inference with 250K samples, 16 features, and 10 classes in Figure 2. The original scikit-learn took 52.24 minutes to run, while Intel® Extension for Scikit-learn took only 0.823 seconds to achieve the same accuracy. That’s a speedup of approximately 3,808x.

Reduction in execution time also reduces the cost. For example, a c5.24xlarge instance costs $4.08/hour, which means the kNN example using the original scikit-learn package costs $3.55 ($4.08 * 52.24 / 60), but costs less than a penny ($4.08 * 0.823 / 60 / 60) when using Intel Extension for Scikit-learn. When you consider that model inference is performed many times per day, using the unoptimized package gets very expensive.

Source of the Improvements

Intel Extension for Scikit-learn uses the Intel oneAPI Data Analytics Library (oneDAL) to achieve its acceleration. This library enables all the latest vector instructions, such as the Intel Advanced Vector Extensions (Intel AVX-512). It also uses cache-friendly data blocking, fast BLAS operations with the Intel oneAPI Math Kernel Library (oneMKL), and scalable multithreading with the Intel oneAPI Threading Building Blocks (oneTBB).

Installation and Usage

Intel Extension for Scikit-learn supports Linux, Windows, and Mac systems on x86 architectures. It can be downloaded using either PyPI or Anaconda Cloud (available from main, conda-forge, and intel channels):

pip install scikit-learn-intelexconda install scikit-learn-intelex -c conda-forge

It’s also available in Intel oneAPI AI Analytics Toolkit starting in v2021.3.

Once scikit-learn-intelex is installed, you can accelerate your scikit-learn (v0.22 or higher) in one of two ways. You can do this explicitly in your Python script by patching scikit-learn dynamically:

from sklearnex import patch_sklearn
patch_sklearn()
from sklearn.svm import SVC# Your subsequent code without any changes
...

If for some reason you don’t want to patch all scikit-learn algorithms, you can just import the required accelerated scikit-learn estimator and use it as usual:

# from sklearn.svm import SVC
from sklearnex.svm import SVC
# Your subsequent code without any changes
...

You can learn more about Intel Extension for Scikit-learn usage from the documentation or notebook examples.

Concluding Remarks

Intel Extension for Scikit-learn:

  • Optimizes the performance of common ML algorithms
  • Saves money by reducing ML training and inference time
  • Offers a seamless experience (just add two lines of code to enable acceleration)

The project is growing fast, so there are improvements in every release. Follow us on Medium and GitHub to keep up with the latest updates.

Learn more about other machine learning and AI optimized end-to-end workloads at intel.com/oneAPI-AIKit.

Hardware and Software Benchmark Configurations

All configurations were tested by Intel.

Hardware: c5.24xlarge AWS EC2 (3.0 GHz Intel Xeon Platinum 8275CL, two sockets, 24 cores per socket).

Software: Python 3.8, scikit-learn 0.24.2, scikit-learn-intelex 2021.2.3.

To reproduce the performance numbers or test on your environment, run the following commands using scikit-learn_bench.

With Intel® Extension for Scikit-learn enabled:

python runner.py --configs configs/blogs/skl_conda_config.json \
--output-file result1.json –report

With the original scikit-learn:

python runner.py --configs configs/blogs/skl_conda_config.json \
--output-file result2.json –report \
--no-intel-optimized

Notices and Disclaimers

Performance results are based on testing as of dates shown in configurations and may not reflect all publicly available options. Learn more at www.Intel.com/PerformanceIndex​.

Intel technologies may require enabled hardware, software or service activation. No product or component can be absolutely secure.

© Intel Corporation. Intel, the Intel logo, and other Intel marks are trademarks of Intel Corporation or its subsidiaries. Other names and brands may be claimed as the property of others.

--

--