Skip to content

Array API support for LogisticRegression with LBFGS #32611

Closed

Description

@ogrisel

Even if scipy.optimize does not support array API yet, we can probably work around it to perform the computation of the gradient using the input data array namespace and convert the gradient values to numpy prior to feeding them back to the solver at each iteration.

Assuming n_samples >> n_features, this is likely to be a worthwhile way to get a large speed-up on GPU devices for that estimator despite back and forth conversion to and from device for the parameters array.

To compute the gradient we perform X @ coef + intercept which leads to "raw predictions". Those raw predictions are fed along with y to the Cython loss. This will be an (n_samples, n_classes) array. For a first iteration, we can convert that to NumPy and continue using Cython. Alternatively, we can write pure Python array API capable version of the gradient method of the loss functions. We should start with log loss for which we can already refactor and move the sklearn.utils._array_api._half_multinomial_loss function (introduced for metrics) into the sklearn._loss package to add its gradient/Hessian counterparts.

We can leave the other losses as raising NotImplementError for now.

Activity

  1. converted this from a draft issue on Oct 30, 2025
  2. added
    Needs Triage
    on Oct 30, 2025
  3. added
    High Priority
    and removed
    Needs Triage
    on Oct 30, 2025
  4. OmarManzoor commented on Oct 30, 2025

    @OmarManzoor
    Contributor

    For a start maybe I can try adding the gradient part of HalfMultinomialLoss as a dedicated PR? Then we can add array API support for LogisticRegression with lbfgs in another PR. What do you think?
    But I think we would also need HalfBinomialLoss refactored with respect to the array API.

  5. ogrisel commented on Oct 31, 2025

    @ogrisel
    MemberAuthor

    I find it weird to merge a PR that implements some private functions/methods that are never called in any public counterparts. I would rather introduce such new functions/methods only when actually needed. Otherwise we might not realize that they have a design defect of some kind.

  6. OmarManzoor commented on Oct 31, 2025

    @OmarManzoor
    Contributor

    I understand and agree.

  7. OmarManzoor commented on Nov 3, 2025

    @OmarManzoor
    Contributor

    @ogrisel How about first adding array API support for LogisticRegression with LBFGS without modifying the loss or gradient and simply converting to numpy when calling the optimization related code. Then maybe later we can see if we want to refactor the loss and gradient as well. This might help in working on this systematically.

  8. ogrisel commented on Nov 3, 2025

    @ogrisel
    MemberAuthor

    It sounds like a plan (focusing on first array API support in sklearn/linear_model/_linear_loss.py without touching sklearn/_loss)! Once this first iteration is implemented, we should profile it with py-spy or similar to check how much we have to gain from array API compatible alternatives to our Cython loss function.

  9. moved this from Todo to In Progress in Array APIon Nov 13, 2025
  10. moved this to In Progress in Array APIon Nov 18, 2025
  11. lorentzenchr commented on Feb 21, 2026

    @lorentzenchr
    Member

    Shall we close as #32644 is merged, or should we first add it to LogisticRegressionCV, too?

  12. OmarManzoor commented on Feb 21, 2026

    @OmarManzoor
    Contributor

    From my opinion I think we can close this and create a separate issue for LogisticRegressionCV when required.

  13. lorentzenchr commented on Feb 21, 2026

    @lorentzenchr
    Member

    #33346 for LogisticRegressionCV, so I close this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Array APIHigh Priority

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions