-
Notifications
You must be signed in to change notification settings - Fork 1
use inplace_predict for xgboost to bypass dmatrix #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
This should with a bit of change in parameters prevent mainly the xgboost model to get enormously big with much improvement.
Summary of ChangesHello @nikromen, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the RPMeta project by optimizing XGBoost prediction performance through the use of Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces two main improvements: it optimizes XGBoost prediction performance by using inplace_predict to avoid DMatrix creation overhead, and it adds a mechanism to penalize large models during hyperparameter tuning to encourage smaller, more efficient models. The refactoring of the Model class to encapsulate the loaded regressor is a good architectural improvement that makes the code cleaner and more object-oriented. The changes are well-tested, including new tests to verify the correctness of the optimized prediction path. I have a few suggestions to improve robustness and fix a potential bug.
| def compute_size_penalty(self, regressor: Any, trial: Any = None) -> float: | ||
| if not self.config.model.lightgbm.size_penalty_enabled: | ||
| return 0.0 | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method call booster.num_leaves() will likely raise an AttributeError at runtime because the lightgbm.Booster object does not have this method. To get the total number of leaves, you would need to parse the model structure from booster.dump_model().
Additionally, for consistency with the XGBoost implementation and the configuration comments which refer to nodes, you might consider calculating the penalty based on the total number of nodes instead of leaves.
| model_dump = booster.dump_model() | |
| n_leaves = sum(tree_info["num_leaves"] for tree_info in model_dump["tree_info"]) |
rpmeta/model.py
Outdated
| raw_pred = booster.inplace_predict(data) | ||
| # lost the inverse transformation here because of the inplace_predict :( | ||
| # https://xgboost.readthedocs.io/en/stable/prediction.html#in-place-prediction | ||
| return np.expm1(raw_pred) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hardcoding np.expm1 here makes the prediction logic brittle. If the transformation function (func=np.log1p in create_regressor) is ever changed, this will silently produce incorrect predictions. It's more robust to use the inverse_func stored on the TransformedTargetRegressor instance, which is available via self.regressor.
| return np.expm1(raw_pred) | |
| return self.regressor.inverse_func(raw_pred) |
rpmeta/trainer/base.py
Outdated
| model_size = sys.getsizeof(best_regressor) / (1024 * 1024) | ||
| logger.info("Model size is %.2f MB", model_size) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using sys.getsizeof() to determine the model size is likely inaccurate. This function doesn't account for the memory used by the objects the model contains, so the logged size will be misleadingly small. A more reliable method is to measure the size of the model once it's serialized to disk, for example by summing the sizes of the files created by save_regressor.
|
/gemini review |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request introduces a significant performance improvement for XGBoost predictions by using inplace_predict, bypassing the overhead of DMatrix creation. It also refactors the model loading logic for better encapsulation, making the Model class stateful. Additionally, it adds a mechanism to penalize large models during hyperparameter tuning to encourage smaller, more efficient models. The changes are well-structured and accompanied by thorough tests, including new tests to validate the optimized prediction path. A new shell command is also added for easier debugging. My review found a minor issue with a duplicated test case, which I've commented on.
test/unit/test_predictor.py
Outdated
| def test_predict_uses_model_predict(example_config): | ||
| category_maps = {"package_name": ["pkg1"], "feature": ["a"]} | ||
| mock_input = MagicMock() | ||
| mock_input.package_name = "pkg1" | ||
| df_dict = { | ||
| "feature": "a", | ||
| "package_name": "pkg1", | ||
| "ram": 1000, | ||
| "swap": 2000, | ||
| } | ||
| mock_input.to_data_frame.return_value = pd.DataFrame([df_dict]) | ||
| mock_model = MagicMock() | ||
| mock_model.predict.return_value = np.array([42]) | ||
|
|
||
| predictor = Predictor(mock_model, category_maps, example_config) | ||
| result = predictor.predict(mock_input, example_config.model.behavior) | ||
|
|
||
| assert result == 42 | ||
| mock_model.predict.assert_called_once() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bypass sklearn's predict() which creates a new DMatrix on every call, causing expensive memmove operations. Use Booster.inplace_predict() directly with manual expm1() inverse transformation.
Fix #45