From 3ec195ac8b3ebdae522158353aef2095827688bd Mon Sep 17 00:00:00 2001 From: Safoine El Khabich Date: Mon, 10 Apr 2023 09:09:09 +0000 Subject: [PATCH] add model registry blog post --- _posts/2023-04-07-model-registry.md | 171 ++++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 _posts/2023-04-07-model-registry.md diff --git a/_posts/2023-04-07-model-registry.md b/_posts/2023-04-07-model-registry.md new file mode 100644 index 00000000..46cf0b4d --- /dev/null +++ b/_posts/2023-04-07-model-registry.md @@ -0,0 +1,171 @@ +--- +layout: post +author: "Safoine El khabich" +title: "Productionalizing LangChain and LlamaIndex with a ZenML MLOps Pipeline to Help Community Slack Support" +description: "We decided to explore how the emerging technologies around Large Language Models (LLMs) could seamlessly fit into ZenML's MLOps workflows and standards. We created and deployed a Slack bot to provide community support." +category: zenml +tags: zenml evergreen model-registry production +publish_date: April 10, 2023 +date: 2023-03-31T00:02:00Z +thumbnail: /assets/posts/slackbot/slackbot-small.png +image: + path: /assets/posts/slackbot/slackbot.png +--- + +**Last updated:** March 31, 2023 + +![*Image generated by [Midjourney v5](https://www.midjourney.com/)*](/assets/posts/slackbot/slackbot-small.png) + +Machine learning is revolutionizing various industries, but managing and deploying ML models can be a challenging task. In the world of MLOps, a model registry is a key component that streamlines the management, deployment, and lifecycle of machine learning models. This blog post discusses the importance of model registries, how they fit into the ZenML MLOps stack, and the benefits they bring to your workflow. Additionally, we explore the integration of MLflow Model Registry with ZenML and how to deploy and utilize it effectively in your projects. + +## Why Model Registries Are Important + +Model registries are centralized storage solutions for managing and tracking machine learning models throughout their development and deployment stages. They help track different model versions and configurations and enable reproducibility. By storing metadata such as version, configuration, and metrics, model registries streamline the management of trained models. + +In ZenML, model registries are Stack Components that allow for easy retrieval, loading, and deployment of trained models. They also provide information on the pipeline in which the model was trained and how to reproduce it. +Model registries offer a visual way to manage and track model metadata, especially when using a remote orchestrator. + +## How Model Registries Fit within the ZenML MLOps Stack + +ZenML provides a unified abstraction for model registries, enabling consistent handling and management of model groups, versions, and stages, regardless of the underlying registry tool or platform. Model registries are an optional component in the ZenML stack that are tied to the experiment tracker. To use model registries in your stack, you need to register a model registry with the same flavor as your experiment tracker. + +In ZenML, model registries simplify model lifecycle management by making it easier to register, track, and version your trained, deployed, and retired models in a central, organized, and searchable repository. They also store metadata and runtime dependencies for trained models, which makes deployment processes more straightforward. + +## Benefits of Model Registries in Your Workflow + +**Fast Deployment:** Model registries bridge the gap between experimentation and production activities, allowing for faster rollout of production models. They store trained models for quick and easy retrieval by any integrated application or service. + +**Simplified Model Lifecycle Management:** Model registries enable you to register, track, and version your models in a central repository, store metadata and runtime dependencies, and build automated pipelines for continuous integration, delivery, and training. + +**Improved Collaboration:** Model registries provide a central UI for teams to collaborate on models, bridging the gap between machine learning and operations. This unifying component reduces friction in the hand-off of production-ready models from experimentation to production environments. + +**Model Governance:** Model registries make it easier to compare models running in production (champion models) to freshly trained models (or challenger models) in the staging environment, facilitating better model governance. + +## Integrating the MLflow Model Registry with ZenML + +The MLflow Model Registry is a powerful integration within the ZenML stack that facilitates the management and tracking of machine learning models and their artifacts. It is typically employed during the experimentation, quality assurance, and production phases. The MLflow Model Registry enhances collaboration on model development and deployment, tracks model usage in various environments, and monitors performance over time. It streamlines the deployment process to both production and staging environments. + +To deploy the MLflow Model Registry, first, install the MLflow integration on your local machine: + +```python +zenml integration install mlflow -y +``` + +After installing the MLflow integration, register an MLflow Model Registry component in your stack: + +```python +zenml model-registry register mlflow_model_registry --flavor=mlflow +``` + +Utilizing MLflow Model Registry with ZenML + +There are several ways to use the MLflow Model Registry in your ZenML pipelines. You can use the built-in step, the ZenML CLI to register your model manually, or call the Model Registry API within a custom step in your pipeline. + +### Using the Built-in Step + +The MLflow Model Registry is a built-in step in ZenML pipelines. To use it, simply add the `model_register` step to your pipeline: + +```python +from zenml.integrations.mlflow.steps.mlflow_registry import ( + MLFlowRegistryParameters, + mlflow_register_model_step, +) + +mlflow_training_pipeline( + importer=loader_mnist(), + normalizer=normalizer(), + trainer=tf_trainer(params=TrainerParameters(epochs=5, lr=0.003)), + evaluator=tf_evaluator(), + model_register=mlflow_register_model_step( + params=MLFlowRegistryParameters( + name="tensorflow-mnist-model", + description="A simple MNIST model trained with ZenML", + tags={"framework": "tensorflow", "dataset": "mnist"}, + tags={"lr": 0.003}, + description=f"The 1st run of the mlflow_training_pipeline.", + ) + ), +).run() + +# The list of parameters that can be passed to the mlflow_model_register_step: +""" + name: Name of the registered model. + experiment_name: Name of the MLflow experiment to be used for the run. + run_name: Name of the MLflow run to be created. + run_id: ID of the MLflow run to be used. + model_source_uri: URI of the model source. If not provided, the model + will be fetched from the MLflow tracking server. + description: Description of the model. + metadata: Tags to be added to the model. +""" +``` + +### Model Registry CLI Commands: + +The `zenml model-registry models list` command will list all registered models in the +configured model registry. + +```shell +$ zenml model-registry models list + +┏━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━┯━━━━━━━━━━┓ +┃ NAME │ DESCRIPTION │ METADATA ┃ +┠────────────────────────┼─────────────┼──────────┨ +┃ tensorflow-mnist-model │ │ ┃ +┗━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━┷━━━━━━━━━━┛ +``` + +To list all versions of a specific model, you can use the `zenml model-registry models list-versions REGISTERED_MODEL_NAME` command: + + +```shell +$ zenml model-registry models list-versions tensorflow-mnist-model +┏━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ +┃ NAME │ MODEL_VERSION │ VERSION_DESCRIPTION │ METADATA ┃ +┠────────────────────────┼───────────────┼─────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┨ +┃ tensorflow-mnist-model │ 3 │ Run #3 of the mlflow_training_pipeline. │ {'zenml_version': '0.34.0', 'zenml_run_name': 'mlflow_training_pipeline-2023_03_01-08_09_23_672599', 'zenml_pipeline_name': 'mlflow_training_pipeline', ┃ +┃ │ │ │ 'zenml_pipeline_run_uuid': 'a5d4faae-ef70-48f2-9893-6e65d5e51e98', 'zenml_workspace': '10e060b3-2f7e-463d-9ec8-3a211ef4e1f6', 'epochs': '5', 'optimizer': 'Adam', 'lr': '0.005'} ┃ +┠────────────────────────┼───────────────┼─────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┨ +┃ tensorflow-mnist-model │ 2 │ Run #2 of the mlflow_training_pipeline. │ {'zenml_version': '0.34.0', 'zenml_run_name': 'mlflow_training_pipeline-2023_03_01-08_09_08_467212', 'zenml_pipeline_name': 'mlflow_training_pipeline', ┃ +┃ │ │ │ 'zenml_pipeline_run_uuid': '11858dcf-3e47-4b1a-82c5-6fa25ba4e037', 'zenml_workspace': '10e060b3-2f7e-463d-9ec8-3a211ef4e1f6', 'epochs': '5', 'optimizer': 'Adam', 'lr': '0.003'} ┃ +┠────────────────────────┼───────────────┼─────────────────────────────────────────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┨ +┃ tensorflow-mnist-model │ 1 │ Run #1 of the mlflow_training_pipeline. │ {'zenml_version': '0.34.0', 'zenml_run_name': 'mlflow_training_pipeline-2023_03_01-08_08_52_398499', 'zenml_pipeline_name': 'mlflow_training_pipeline', ┃ +┃ │ │ │ 'zenml_pipeline_run_uuid': '29fb22c1-6e0b-4431-9e04-226226506d16', 'zenml_workspace': '10e060b3-2f7e-463d-9ec8-3a211ef4e1f6', 'epochs': '5', 'optimizer': 'Adam', 'lr': '0.001'} ┃ +┗━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ +``` + +For more details on a specific model version, you can use the `zenml model-registry models get-version REGISTERED_MODEL_NAME -v VERSION` command: + +```shell +$ zenml model-registry models get-version tensorflow-mnist-model -v 1 + +┏━━━━━━━━━━━━━━━━━━━━━━━━┯━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ +┃ MODEL VERSION PROPERTY │ VALUE ┃ +┠────────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┨ +┃ REGISTERED_MODEL_NAME │ tensorflow-mnist-model ┃ +┠────────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┨ +┃ VERSION │ 1 ┃ +┠────────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┨ +┃ VERSION_DESCRIPTION │ Run #1 of the mlflow_training_pipeline. ┃ +┠────────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┨ +┃ CREATED_AT │ 2023-03-01 09:09:06.899000 ┃ +┠────────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┨ +┃ UPDATED_AT │ 2023-03-01 09:09:06.899000 ┃ +┠────────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┨ +┃ METADATA │ {'zenml_version': '0.34.0', 'zenml_run_name': 'mlflow_training_pipeline-2023_03_01-08_08_52_398499', 'zenml_pipeline_name': 'mlflow_training_pipeline', 'zenml_pipeline_run_uuid': '29fb22c1-6e0b-4431-9e04-226226506d16', ┃ +┃ │ 'zenml_workspace': '10e060b3-2f7e-463d-9ec8-3a211ef4e1f6', 'lr': '0.001', 'epochs': '5', 'optimizer': 'Adam'} ┃ +┠────────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┨ +┃ MODEL_SOURCE_URI │ file:///Users/safoine-zenml/Library/Application Support/zenml/local_stores/0902a511-117d-4152-a098-b2f1124c4493/mlruns/489728212459131640/293a0d2e71e046999f77a79639f6eac2/artifacts/model ┃ +┠────────────────────────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┨ +┃ STAGE │ None ┃ +┗━━━━━━━━━━━━━━━━━━━━━━━━┷━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ +``` + +Finally to delete a registered model or a specific model version, +you can use the `zenml model-registry models delete REGISTERED_MODEL_NAME` and +`zenml model-registry models delete-version REGISTERED_MODEL_NAME -v VERSION` +commands respectively. + +## Conclusion + +Incorporating a model registry into your ZenML MLOps stack offers numerous benefits. It can speed up deployment, simplify model lifecycle management, enhance collaboration, and improve model governance. By integrating the MLflow Model Registry with ZenML, you can further streamline the management and deployment of your machine learning models. Embrace the power of model registries in your machine learning workflows and take advantage of their comprehensive capabilities. \ No newline at end of file