Skip to content

Example on how to use array API to accelerate a complex pipeline on a GPU #35420

Open

Description

@ogrisel

Warning

This issue is not yet ready for a PR. If you are interested in contributing to scikit-learn, please have a look at our contributing guidelines, and in particular the sections for new contributors and the "Needs triage" label.

Our existing array API documentation would benefit from being complemented by a link to a running example that shows the benefits of using array API to hardware-accelerate the cross-validation (or even the hyper-parameter tuning) of a non-trivial pipeline.

I think we could include the following estimators:

  • OneHotEncoder or (TargetEncoder followed by MinMaxScaler) for categorical features and SplineTransformer for numerical features in columntransformer on CPU.
  • FunctionTransformer to move the results to an automatically detected device using PyTorch
  • Nystroem transformer (with an RBF or a polynomial kernel)
  • RidgeCV or PoissonRegressor

For information, I demoed the usefulness of GPU acceleration via array API on google colab on a synthetic datasets here: https://colab.research.google.com/github/ogrisel/notebooks/blob/master/poly_reg_array_api.ipynb

But contrary to this colab notebook, I think our new usage example should:

  • use a real regression dataset with a mix of categorical and numerical features. This would justify the need to run the first step of the pipeline on CPU because array API does not accept non-numerical columns.
  • make the FunctionTransformer implement the logic to automatically detect the best hardware available on the host. This would make it easier to pickle and load this pipeline on a different host with different accelerators without changing its structure.

Objectives

Challenge

The existing examples of the scikit-learn documentation are rendered on Circle CI with a CPU-only runner. Therefore, if we use our existing infra to render the example, parts of its educational value will not be visible in the rendered outputs and the example might be a bit confusing as a result.

Possible solutions

  • a. render the example on the CPU as usual and assume that part of the example runs on the CPU rather than on GPU. Explain in the body of the text of example that to the user needs to run the notebook on a different host to see the benefits of using a GPU (e.g. on machine with a NVIDIA GPU using a CUDA device, Apple GPU via MPS or Intel GPU via XPU). The text of the example could include a Google Colab link to make it easy to test the first case.
  • b. find a way to use our GitHub Actions CI to render this particular example with a CUDA GPU.
  • c. migrate away from circle ci and render the full doc on a GPU accelerated machine.
  • d. develop a sphinx-gallery extension to render a single notebook on a GPU machine (e.g. using modal or similar).

The problem with point b. is that we would need to coordinate two CI jobs running on different infrastructures to commit the rendered HTML doc on github pages. That sounds challenging to setup and painful to maintain.

Point c. would be easier to maintain but more expensive and maybe a waste of GPU resources.

I think that solution a. is enough in the short term and we can follow-up later with one of the others solutions.

I marked this issue as "Needs Decision" because I don't want to attract a flux of semi-automated PRs before we have a chance to roughly agree that such an example is needed, that the proposed contents is meaningful and how we plan to address the CI related challenge.

Activity

  1. github-actions commented on Nov 16, 2026

    @github-actions

    Thanks for the work you've done so far. The goal of this comment is to set expectations.

    Deciding on new features or substantial changes is a lengthy process. It frequently happens that no maintainer is available to take on this task right now.

    Please do not create a Pull Request before a decision has been made regarding the proposed work. Making this decision can often take a significant amount of time and effort.

  2. moved this to Todo in Labson Nov 16, 2026
  3. lesteve commented on Nov 17, 2026

    @lesteve
    Member

    I would start with the lowest tech possible i.e. option a: the example use PyTorch-CPU and contains a Colab link. I think you could use the scikit-learn.github.io generated .ipynb notebook e.g.

    I thought it was possible to set the runtime to "T4 GPU" in the Colab link but I am not finding it right now, maybe I misremember it.

    xref #32886 which was only about adding a realistic array API example to the gallery.

  4. ogrisel commented on Nov 17, 2026

    @ogrisel
    MemberAuthor

    xref #32886 which was only about adding a realistic array API example to the gallery.

    Indeed. I close the older issue in favor of this one to avoid parallel discussions.

  5. ogrisel commented on Nov 17, 2026

    @ogrisel
    MemberAuthor

    Also xref #33564 that drafts several examples for different estimators.

  6. lesteve commented on Nov 18, 2026

    @lesteve
    Member

    One tiny thing is that the Colab notebook will likely need modifications on top of on our .github.io notebook e.g. !pip install commands to install packages and control the scikit-learn version. I think the simplest solution would be to add a cell as commented out code with a comment saying "Uncomment if you are trying to run this inside Colab".

  7. moved this from Todo to Issue in progress in Labson Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    • Status
      No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions