Skip to content

ENH Newton-CD part 4: Add memory buffer for pointwise gradients and hessians to LinearModelLoss and NewtonCholeskySolver - #34337

Merged
virchan merged 1 commit into
scikit-learn:mainfrom
lorentzenchr:logistic_lasso_newton_part_3
Jun 23, 2026
Merged

virchan merged 1 commit into
scikit-learn:mainfrom
lorentzenchr:logistic_lasso_newton_part_3

Conversation

@lorentzenchr

Copy link
Copy Markdown
Member

Reference Issues/PRs

Part 3 of #33160.

What does this implement/fix? Explain your changes.

This PR adds explicit memory buffer parameters for grad_pointwise_out and hess_pointwise_out in the LinearModelLoss.gradient_hessian.
It then makes use of those in NewtonCholeskySolver.

This has no user impact (maybe tiny tiny better memory footprint), the current behavior is kept (functionality only for future purposes).

AI usage disclosure

None

Any other comments?

@lorentzenchr

Copy link
Copy Markdown
Member Author

@OmarManzoor ...and part 3 😄

@OmarManzoor OmarManzoor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR @lorentzenchr

Comment on lines +559 to +560
gradient_out=grad_pointwise_out,
hessian_out=hess_pointwise_out,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why exactly do we need this? This just seems like we need to update these within pre constructed arrays instead of allowing them to be constructed within the loss method.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The CD solvers do not construct the full hessian, but they need access to the pointwise hessian and pointwise gradient.
Do you have an alternative suggestion on how to return/access them?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But aren't the return values themselves grad_pointwise, hess_pointwise? I mean why do we have duplication here since we are getting them from the function but also passing them in the function.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I think we can use self.grad_pointwise directly instead of defining a new variable gradient_out.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The signature is

def gradient_hessian(self, coef, X, y, sample_weight=None, l2_reg_strength=0.0, n_threads=1,
    gradient_out=None,        # <- this one is already there
    hessian_out=None,         # <- this one is already there
    grad_pointwise_out=None,  # <- this one is new
    hess_pointwise_out=None,  # <- this one is new
    raw_prediction=None,
): -> (gradient_out, hessian_out, bool)

So it returns the full gradient and full hessian (X.T @ diag(hess_poinwise) @X). It already allows to pass the buffers for the full gradient and hessian (gradient_out, hessian_out). This PR just extends it and also allows to pass the buffers for the pointwise versions. This has 2 advantages:

  • We avoid memory allocation in each call of gradien_hessian.
    As this method is used in Newton solver, this happens roughly ~10 times, so not that much of a deal.
  • Gives access to the pointwise versions without changing the return values. Changing the return values would require more code changes.

Correction to what I wrote earlier: The Gram version of the CD solver uses the full hessian. Both variants of CD solver use the full gradient.

@OmarManzoor OmarManzoor Jun 22, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay I think I checked the overall flow and this looks fine. So basically the base loss method already returns the pointwise gradient and pointwise hessian. We just allocate arrays for them beforehand both to avoid allocating them again and also to give us access to the attributes that you defined above in case we need them. These attributes are passed to the base loss method to just use these for storing the results instead of allocating new arrays.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

@OmarManzoor OmarManzoor left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thank you @lorentzenchr

@virchan I think we can merge this but you can have a look before merging.

@lorentzenchr lorentzenchr changed the title ENH Newton-CD part 3: Add memory buffer for pointwise gradients and hessians to LinearModelLoss and NewtonCholeskySolver ENH Newton-CD part 4: Add memory buffer for pointwise gradients and hessians to LinearModelLoss and NewtonCholeskySolver Jun 22, 2026

@virchan virchan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thanks, everyone!

@virchan
virchan merged commit 5c99cbe into scikit-learn:main Jun 23, 2026
38 checks passed
@lorentzenchr
lorentzenchr deleted the logistic_lasso_newton_part_3 branch June 23, 2026 14:57
prady0t pushed a commit to prady0t/scikit-learn that referenced this pull request Sep 2, 2026
@jeremiedbb jeremiedbb mentioned this pull request Sep 8, 2026
14 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants