Skip redundant inverse transforms in _predict when return_all_rounds=False#241
Open
Skip redundant inverse transforms in _predict when return_all_rounds=False#241
Conversation
…False Summary: `_predict` previously called `_inverse_transform_predictions` (the logistic/sigmoid function over the full prediction array) on every boosting round, even when `return_all_rounds=False` and only the final round's result was needed. The first R-1 inverse transforms were computed and discarded. This is especially costly during early stopping, where `_predict` is called once per fold per round. At early stopping round r, the fold model has r boosters, so `_predict` was making r logistic calls but using only the last. Over all R rounds and F folds, total logistic calls were F·R·(R+1)/2 (quadratic in R). After this change they are F·R (linear in R), saving F·R·(R-1)/2 calls. Benchmarking `utils.logistic` (median of 20 runs, `buck test`) shows ~26 ns/element, scaling linearly with array size: - n=100K: 2.6 ms/call - n=1M: 26 ms/call - n=10M: 277 ms/call Estimated wall-clock savings during early stopping (F=5 folds, 26 ns/element): | Rounds (R) | Saved calls | n_val=100K | n_val=1M | n_val=10M | |------------|-------------|------------|-----------|-----------| | 10 | 225 | 0.6s | 5.9s | 62s | | 20 | 950 | 2.5s | 25s | 4.4 min | | 50 | 6,125 | 15.9s | 2.7 min | 28 min | The 2D `predictions_per_round` allocation is also skipped when not needed. No change in behavior — `RegressionMCGrad` is unaffected (its inverse transform is the identity). Differential Revision: D96827345
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #241 +/- ##
=======================================
Coverage 95.81% 95.82%
=======================================
Files 9 9
Lines 1887 1891 +4
=======================================
+ Hits 1808 1812 +4
Misses 79 79
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
_predictpreviously called_inverse_transform_predictions(the logistic/sigmoid function over the full prediction array) on every boosting round, even whenreturn_all_rounds=Falseand only the final round's result was needed. The first R-1 inverse transforms were computed and discarded.This is especially costly during early stopping, where
_predictis called once per fold per round. At early stopping round r, the fold model has r boosters, so_predictwas making r logistic calls but using only the last. Over all R rounds and F folds, total logistic calls were F·R·(R+1)/2 (quadratic in R). After this change they are F·R (linear in R), saving F·R·(R-1)/2 calls.Benchmarking
utils.logistic(median of 20 runs,buck test) shows ~26 ns/element, scaling linearly with array size:Estimated wall-clock savings during early stopping (F=5 folds, 26 ns/element):
The 2D
predictions_per_roundallocation is also skipped when not needed.No change in behavior —
RegressionMCGradis unaffected (its inverse transform is the identity).Differential Revision: D96827345