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
Autologging functionality for scikit-learn integration with LightGBM (Part 1) #5130
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ce9c5e3
init commit
jwyyy af4bcf1
restore test
jwyyy c804a81
fix doc
jwyyy aa4337b
address review: use cloudpickle
jwyyy 7397fce
remove prev folders
jwyyy c212f55
address review
jwyyy 2b31029
a better soln
jwyyy File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -33,6 +33,7 @@ | |||||||||||
from mlflow.models.signature import ModelSignature | ||||||||||||
from mlflow.models.utils import ModelInputExample, _save_example | ||||||||||||
from mlflow.tracking.artifact_utils import _download_artifact_from_uri | ||||||||||||
from mlflow.utils import _get_fully_qualified_class_name | ||||||||||||
from mlflow.utils.environment import ( | ||||||||||||
_mlflow_conda_env, | ||||||||||||
_validate_env_arguments, | ||||||||||||
|
@@ -73,7 +74,7 @@ def get_default_pip_requirements(): | |||||||||||
Calls to :func:`save_model()` and :func:`log_model()` produce a pip environment | ||||||||||||
that, at minimum, contains these requirements. | ||||||||||||
""" | ||||||||||||
return [_get_pinned_requirement("lightgbm")] | ||||||||||||
return [_get_pinned_requirement("lightgbm"), _get_pinned_requirement("cloudpickle")] | ||||||||||||
|
||||||||||||
|
||||||||||||
def get_default_conda_env(): | ||||||||||||
|
@@ -132,7 +133,10 @@ def save_model( | |||||||||||
path = os.path.abspath(path) | ||||||||||||
if os.path.exists(path): | ||||||||||||
raise MlflowException("Path '{}' already exists".format(path)) | ||||||||||||
model_data_subpath = "model.lgb" | ||||||||||||
if isinstance(lgb_model, lgb.Booster): | ||||||||||||
model_data_subpath = "model.lgb" | ||||||||||||
else: | ||||||||||||
model_data_subpath = "model.pkl" | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
nit |
||||||||||||
model_data_path = os.path.join(path, model_data_subpath) | ||||||||||||
os.makedirs(path) | ||||||||||||
if mlflow_model is None: | ||||||||||||
|
@@ -143,15 +147,21 @@ def save_model( | |||||||||||
_save_example(mlflow_model, input_example, path) | ||||||||||||
|
||||||||||||
# Save a LightGBM model | ||||||||||||
lgb_model.save_model(model_data_path) | ||||||||||||
_save_model(lgb_model, model_data_path) | ||||||||||||
|
||||||||||||
lgb_model_class = _get_fully_qualified_class_name(lgb_model) | ||||||||||||
pyfunc.add_to_model( | ||||||||||||
mlflow_model, | ||||||||||||
loader_module="mlflow.lightgbm", | ||||||||||||
data=model_data_subpath, | ||||||||||||
env=_CONDA_ENV_FILE_NAME, | ||||||||||||
) | ||||||||||||
mlflow_model.add_flavor(FLAVOR_NAME, lgb_version=lgb.__version__, data=model_data_subpath) | ||||||||||||
mlflow_model.add_flavor( | ||||||||||||
FLAVOR_NAME, | ||||||||||||
lgb_version=lgb.__version__, | ||||||||||||
data=model_data_subpath, | ||||||||||||
model_class=lgb_model_class, | ||||||||||||
) | ||||||||||||
mlflow_model.save(os.path.join(path, MLMODEL_FILE_NAME)) | ||||||||||||
|
||||||||||||
if conda_env is None: | ||||||||||||
|
@@ -186,6 +196,20 @@ def save_model( | |||||||||||
write_to(os.path.join(path, _REQUIREMENTS_FILE_NAME), "\n".join(pip_requirements)) | ||||||||||||
|
||||||||||||
|
||||||||||||
def _save_model(lgb_model, model_path): | ||||||||||||
# LightGBM Boosters are saved using the built-in method `save_model()`, | ||||||||||||
# whereas LightGBM scikit-learn models are serialized using Cloudpickle. | ||||||||||||
import lightgbm as lgb | ||||||||||||
|
||||||||||||
if isinstance(lgb_model, lgb.Booster): | ||||||||||||
lgb_model.save_model(model_path) | ||||||||||||
else: | ||||||||||||
import cloudpickle | ||||||||||||
|
||||||||||||
with open(model_path, "wb") as out: | ||||||||||||
cloudpickle.dump(lgb_model, out) | ||||||||||||
|
||||||||||||
|
||||||||||||
@format_docstring(LOG_MODEL_PARAM_DOCS.format(package_name=FLAVOR_NAME)) | ||||||||||||
def log_model( | ||||||||||||
lgb_model, | ||||||||||||
|
@@ -251,9 +275,31 @@ def log_model( | |||||||||||
|
||||||||||||
|
||||||||||||
def _load_model(path): | ||||||||||||
import lightgbm as lgb | ||||||||||||
""" | ||||||||||||
Load Model Implementation. | ||||||||||||
:param path: Local filesystem path to | ||||||||||||
the MLflow Model with the ``lightgbm`` flavor (MLflow < 1.23.0) or | ||||||||||||
the top-level MLflow Model directory (MLflow >= 1.23.0). | ||||||||||||
""" | ||||||||||||
|
||||||||||||
model_dir = os.path.dirname(path) if os.path.isfile(path) else path | ||||||||||||
flavor_conf = _get_flavor_configuration(model_path=model_dir, flavor_name=FLAVOR_NAME) | ||||||||||||
|
||||||||||||
model_class = flavor_conf.get("model_class", "lightgbm.basic.Booster") | ||||||||||||
lgb_model_path = os.path.join(model_dir, flavor_conf.get("data")) | ||||||||||||
|
||||||||||||
if model_class == "lightgbm.basic.Booster": | ||||||||||||
import lightgbm as lgb | ||||||||||||
|
||||||||||||
model = lgb.Booster(model_file=lgb_model_path) | ||||||||||||
else: | ||||||||||||
# LightGBM scikit-learn models are deserialized using Cloudpickle. | ||||||||||||
import cloudpickle | ||||||||||||
|
||||||||||||
with open(lgb_model_path, "rb") as f: | ||||||||||||
model = cloudpickle.load(f) | ||||||||||||
|
||||||||||||
return lgb.Booster(model_file=path) | ||||||||||||
return model | ||||||||||||
|
||||||||||||
|
||||||||||||
def _load_pyfunc(path): | ||||||||||||
|
@@ -286,9 +332,7 @@ def load_model(model_uri, dst_path=None): | |||||||||||
:return: A LightGBM model (an instance of `lightgbm.Booster`_). | ||||||||||||
""" | ||||||||||||
local_model_path = _download_artifact_from_uri(artifact_uri=model_uri, output_path=dst_path) | ||||||||||||
flavor_conf = _get_flavor_configuration(model_path=local_model_path, flavor_name=FLAVOR_NAME) | ||||||||||||
lgb_model_file_path = os.path.join(local_model_path, flavor_conf.get("data", "model.lgb")) | ||||||||||||
return _load_model(path=lgb_model_file_path) | ||||||||||||
return _load_model(path=local_model_path) | ||||||||||||
|
||||||||||||
|
||||||||||||
class _LGBModelWrapper: | ||||||||||||
|
This file contains 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
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.
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.
Can we add
cloudpickle
conditionally because users who don't use scikit-learn estimators don't needcloudpickle
?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.
Hi @harupy, thank you for your suggestion! Does that mean we also need to provide an option to turn on / off autologging for scikit-learn estimators? I assumed
mlflow.lightgbm.autolog()
enables autologging for all models.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.
Hi @harupy, I found a simple way to add
cloudpickle
conditionally (and automatically) based on what model is saved (please see L169-171). Please let me know your feedback and comments. Thanks a lot!(This comment is also addressed in the latest commit.)