-
Notifications
You must be signed in to change notification settings - Fork 169
feat: MLflow Apps for experiment tracking #102
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| """Shared MLflow helper for pipeline steps. | ||
|
|
||
| Provides setup/teardown for MLflow runs. Each pipeline step creates its own | ||
| run under a shared experiment (the pipeline name). When MLFLOW_TRACKING_ARN | ||
| is not set, all functions are no-ops. | ||
| """ | ||
| import logging | ||
| import os | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def _install_mlflow(): | ||
| """Install MLflow dependencies at runtime if not already available.""" | ||
| try: | ||
| import mlflow # noqa: F401 | ||
| return True | ||
| except ImportError: | ||
| pass | ||
| try: | ||
| import subprocess | ||
| import sys | ||
| subprocess.check_call( | ||
| [sys.executable, "-m", "pip", "install", "mlflow", "sagemaker-mlflow==0.2.0", "-q"] | ||
|
Collaborator
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. Why do we need to install MLflow packages when its already present in the requirements.txt
Contributor
Author
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. Because I was using |
||
| ) | ||
| return True | ||
| except Exception as e: | ||
| logger.warning("Failed to install MLflow: %s", e) | ||
| return False | ||
|
|
||
|
|
||
| def setup_mlflow(step_name): | ||
| """Set up MLflow tracking for a pipeline step. | ||
|
|
||
| Args: | ||
| step_name: Name for this run (e.g. "PreprocessAbaloneData") | ||
|
|
||
| Returns: | ||
| True if MLflow tracking is active, False otherwise. | ||
| """ | ||
| tracking_arn = os.environ.get("MLFLOW_TRACKING_ARN", "") | ||
| if not tracking_arn: | ||
| logger.info("MLFLOW_TRACKING_ARN not set. MLflow tracking disabled.") | ||
| return False | ||
|
|
||
| if not _install_mlflow(): | ||
| return False | ||
|
|
||
| try: | ||
| import mlflow | ||
|
|
||
| mlflow.set_tracking_uri(tracking_arn) | ||
|
|
||
| experiment_name = os.environ.get("MLFLOW_EXPERIMENT_NAME", "Default") | ||
| mlflow.set_experiment(experiment_name) | ||
| mlflow.start_run(run_name=step_name) | ||
|
|
||
| logger.info("MLflow run started: %s (experiment=%s)", step_name, experiment_name) | ||
| return True | ||
| except Exception as e: | ||
| logger.warning("Failed to set up MLflow: %s. Continuing without tracking.", e) | ||
| return False | ||
|
|
||
|
|
||
| def end_mlflow(mlflow_enabled): | ||
| """End the current MLflow run. | ||
|
|
||
| Args: | ||
| mlflow_enabled: Return value from setup_mlflow(). | ||
| """ | ||
| if not mlflow_enabled: | ||
| return | ||
| try: | ||
| import mlflow | ||
| mlflow.end_run() | ||
|
Collaborator
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. If you start the mlflow run with a python loop it automatically terminates the mlflow run.
Contributor
Author
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. Ack |
||
| except Exception as e: | ||
| logger.warning("Failed to end MLflow run: %s", e) | ||
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.
Keeping in line with MLflow best practices: You need to set the experiment and for parent and child run chaining, pass the parent run when calling the child run.
See a similar example here which can be adopted for new version.
Uh oh!
There was an error while loading. Please reload this page.
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.
Ack. The approach that you are referring to, works best with @step decorator way of creating pipeline since Data flows between steps can be directly return as values (not S3 properties). I have rewritten the example to use the @step decorator approach and submitted a new PR feat: MLflow Apps steps for experiment tracking