scikit-learn-intelex

repository·main·Indexed 23 days ago

https://github.com/uxlfoundation/scikit-learn-intelex

An AI accelerator for scikit-learn workflows on CPUs and GPUs that leverages oneDAL optimizations to provide performance improvements for machine learning on tabular data. It includes the high-level sklearnex module for seamless patching of the scikit-learn API and the low-level daal4py module for accessing oneDAL routines, including distributed and streaming modes.

Tokens
35.6K
Snippets
81
Records
180
Agent score
80%

What's inside scikit-learn-intelex

  1. What is scikit-learn-intelex

    main

    scikit-learn-intelex (sklearnex) is an open-source software accelerator built on top of scikit-learn and the oneDAL libraries. It accelerates machine learning workflows by replacing selected scikit-learn algorithm calls with optimized versions from oneDAL.

    Key features include:

    • Hardware Optimization: Leverages SIMD instructions and cache structures of modern hardware.
    • Linear Algebra Acceleration: Uses the oneMKL library instead of the default OpenBLAS used by scikit-learn.
    • High Compatibility: Instead of just mimicking the API, sklearnex classes inherit directly from scikit-learn classes. This ensures they share the same attributes and methods as the stock version.
    • Version Resilience: It is designed to be compatible with the last 4 minor releases of scikit-learn. It uses runtime checks to adjust its behavior (e.g., setting or omitting attributes) based on the specific scikit-learn version installed to maintain full API compatibility.
  2. How n_jobs works in scikit-learn-intelex

    main

    In scikit-learn-intelex (sklearnex), the n_jobs parameter controls the number of threads used by the underlying oneDAL library. It differs from standard scikit-learn in several ways:

    • Support: n_jobs is supported for all estimators patched by sklearnex, whereas standard scikit-learn only supports it for selected estimators.
    • Default Behavior: If n_jobs is not specified, sklearnex uses all available threads by default. In contrast, standard scikit-learn is single-threaded by default.
    • Implementation: sklearnex does not use joblib for parallelism in patched estimators; instead, it uses oneTBB (via oneDAL and oneMKL).
    • Environment Variables: Standard environment variables like OMP_NUM_THREADS, MKL_NUM_THREADS, or OPENBLAS_NUM_THREADS do not affect sklearnex threading.
    • GPU: n_jobs has no effect if computations are performed on a GPU.

    When using scikit-learn utilities with built-in parallelism (like GridSearchCV or VotingClassifier), sklearnex attempts to determine the optimal number of threads per job using hints from joblib / threadpoolctl.

  3. Use additional machine learning algorithms via daal4py

    main

    While sklearnex provides estimators that follow scikit-learn conventions, the daal4py module provides Python bindings for additional machine learning algorithms available in oneDAL that are not implemented in scikit-learn.

    Note that daal4py uses a lower-level interface and does not follow standard scikit-learn API conventions. Use this module when you need access to specific oneDAL algorithms that are not part of the sklearnex extension.

  4. Accelerate GBT model inference using daal4py

    main

    You can accelerate predictions (inference) for Gradient-Boosted Decision Tree (GBT) models produced by libraries like XGBoost, LightGBM, CatBoost, or TreeLite by converting them to a daal4py.mb.GBTDAALModel. This class provides faster implementations of .predict() and .predict_proba().

    Acceleration is achieved through optimized memory arrangement for modern CPUs and leveraging Intel hardware instruction set extensions, providing significant speedups without loss of numerical precision. It also accelerates SHAP computations for both feature contributions and feature interactions.

    import xgboost as xgb
    import daal4py
    
    # Assuming xgb_model is a trained XGBoost model
    d4p_model = daal4py.mb.convert_model(xgb_model)
    predictions = d4p_model.predict(X)
  5. Understand the difference between sklearnex and daal4py

    main

    The scikit-learn-intelex package provides two main modules for accessing accelerated routines from oneDAL:

    1. sklearnex (Preferred): A high-level, idiomatic module built atop the "oneAPI" interface. It supports modern features like GPU support and is designed to work seamlessly with scikit-learn via patching or direct use.
    2. daal4py (Low-level): A low-level module providing Python bindings over the legacy CPU-only "DAAL" interface.

    When to use daal4py instead of sklearnex:

    • To access algorithms that are outside the scope of scikit-learn.
    • To use Distributed mode on CPU (via MPI).
    • To perform fast serving of gradient boosted decision trees from other libraries (e.g., XGBoost model builders).

    daal4py is included in the scikit-learn-intelex package and can be imported directly after installation.

  6. Understand why configurations are not serializable

    main

    Serializing a model object does not save global or local configurations.

    If you enable a feature like array_api_dispatch=True via a config_context to fit a model, that setting is not part of the model object. When you deserialize the model in a new Python process, you must re-enable the configuration (e.g., enable array API support) before the model can be used correctly. Similarly, process-level internal settings or efficiency parameters are not saved with the model object.

  7. Manage sklearnex configurations using context managers or global settings

    main

    The sklearnex library provides configurable options to control acceleration behaviors, such as target_offload for GPU functionalities or enabling array API.

    Configurations can be managed in two ways:

    1. Locally via a configuration context: Using a context manager to apply settings only to the code block within the with statement. This is recommended for isolated changes.
    2. Globally via process-wide settings: Using set_config to apply settings to all subsequent computations in the process.

    Important Note on Patching: To ensure that configuration contexts from sklearnex propagate correctly when using sklearnex estimators inside sklearn meta-estimators (like GridSearchCV), you must apply patching to the sklearn module. Without patching, options might not propagate correctly through the meta-estimator.

    from sklearnex import config_context
    from sklearnex.cluster import DBSCAN
    import numpy as np
    
    X = np.array([[1., 2.], [2., 2.], [2., 3.],
                  [8., 7.], [8., 8.], [25., 80.]], dtype=np.float32)
    
    # Local context: only affects code inside the block
    with config_context(target_offload="gpu"):
        clustering = DBSCAN(eps=3, min_samples=2).fit(X)
  8. Understand fallback behavior in scikit-learn-intelex

    main

    When using sklearnex patching, not every parameter or combination of parameters from scikit-learn estimators is supported. If an unsupported operation is attempted, the system will by default execute the code from stock sklearn instead of the accelerated version. This is known as a fallback, ensuring that any valid sklearn workflow continues to function even if it cannot be accelerated.

    To verify whether your operations are using accelerated routines or falling back to stock sklearn, enable verbose mode.

  9. Handling unsupported input types in sklearnex

    main

    If you pass an unsupported input type to an sklearnex estimator, the behavior depends on the context:

    1. Conversion: The estimator may attempt to convert the input to a supported class (e.g., PyArrow tables might be converted to NumPy arrays via scikit-learn data validators).
    2. Error: The estimator may throw an error if the data format is not recognized by scikit-learn.
    3. Fallback: If array_api is enabled but the input is unsupported, the estimator will fall back to the stock scikit-learn implementation.
  10. How scikit-learn-intelex acceleration works

    main
    The extension achieves acceleration by replacing standard scikit-learn calls with calls to the oneDAL (oneAPI Data Analytics Library) behind the scenes. This allows the library to leverage vector instructions, AI hardware-specific memory optimizations, and threading to improve performance on CPUs and GPUs.