Repository navigation
ENH Newton-CD part 4: Add memory buffer for pointwise gradients and hessians to LinearModelLoss and NewtonCholeskySolver - #34337
Conversation
…odelLoss and NewtonCholeskySolver
|
@OmarManzoor ...and part 3 😄 |
OmarManzoor
left a comment
There was a problem hiding this comment.
Thank you for the PR @lorentzenchr
| gradient_out=grad_pointwise_out, | ||
| hessian_out=hess_pointwise_out, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Yea, I think we can use self.grad_pointwise directly instead of defining a new variable gradient_out.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
OmarManzoor
left a comment
There was a problem hiding this comment.
LGTM. Thank you @lorentzenchr
@virchan I think we can merge this but you can have a look before merging.
…essians to LinearModelLoss and NewtonCholeskySolver (scikit-learn#34337)
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_outandhess_pointwise_outin theLinearModelLoss.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?