<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Felix Schaumann on Medium]]></title>
        <description><![CDATA[Stories by Felix Schaumann on Medium]]></description>
        <link>https://medium.com/@felix.schaumann?source=rss-37f636bb9742------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*A_QIM2NzULIksl-G</url>
            <title>Stories by Felix Schaumann on Medium</title>
            <link>https://medium.com/@felix.schaumann?source=rss-37f636bb9742------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 22:12:49 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@felix.schaumann/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Vertex AI Tips and Tricks: Using Exit Handlers to Create Robust ML Pipelines in Production]]></title>
            <link>https://blog.montrealanalytics.com/vertex-ai-tips-and-tricks-using-exit-handlers-to-create-robust-ml-pipelines-in-production-2d0d8d1d8318?source=rss-37f636bb9742------2</link>
            <guid isPermaLink="false">https://medium.com/p/2d0d8d1d8318</guid>
            <category><![CDATA[kubeflow]]></category>
            <category><![CDATA[chatgpt]]></category>
            <category><![CDATA[vertex-ai]]></category>
            <category><![CDATA[kubeflow-pipelines]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <dc:creator><![CDATA[Felix Schaumann]]></dc:creator>
            <pubDate>Wed, 21 Jun 2023 09:29:33 GMT</pubDate>
            <atom:updated>2023-06-30T12:28:46.217Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://cloud.google.com/vertex-ai/docs/pipelines/introduction">Vertex AI Pipelines</a> offer a scalable way to automate and orchestrate your ML workflows in a serverless manner. Vertex AI supports <a href="https://github.com/kubeflow/pipelines/tree/release-1.8">Kubeflow Pipelines SDK v1.8.9</a> or later and thus, you can use a variety of open source features available in Kubeflow in Vertex AI Pipelines, too.</p><p>While some Kubeflow features are commonly used in Vertex AI Pipelines, such as breaking your pipeline into individual tasks or controlling your workflow using conditions, some often overlooked Kubeflow concepts are crucial for creating robust production-ready ML pipelines. One of these features is certainly the <strong>Exit Handler</strong> and as such, this post explains its usage in the context of deploying <a href="https://huggingface.co/gpt2">GPT-2 from HuggingFace</a> and testing its performance using Vertex AI Pipelines and <a href="https://locust.io/">Locust</a>.</p><h3>Introducing Exit Handlers</h3><p>When creating a robust product, error handling in your code must be considered. Python offers <a href="https://docs.python.org/3/tutorial/errors.html#handling-exceptions"><em>“try-except”</em></a> statements for catching errors encountered at run-time. Additionally, Python’s <em>“try”</em> statement has another optional clause <a href="https://docs.python.org/3/tutorial/errors.html#defining-clean-up-actions"><em>“finally”</em></a><em> </em>which is intended to define clean-up actions that must be executed under all circumstances.</p><p>Kubeflow offers a similar control flow by introducing <a href="https://www.kubeflow.org/docs/components/pipelines/v2/pipelines/control-flow/#exit-handling-dslexithandler">Exit Handlers</a>. The example below (see Figure 1) ensures that you clean up resources during model training in a Kubeflow pipeline similar to what you would achieve with a <em>“finally”</em> statement locally.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/872/1*XDHv5lKs_gxiK2g6fXIIqw.png" /><figcaption>Figure 1: Comparing clean-up actions between Python and Kubeflow</figcaption></figure><p>You might notice that Kubeflow requires you to define your clean-up task before creating the dataset and training the model. Nevertheless, your exit handler will still run <strong>after</strong> your business logic. Consider the following scenarios in which your pipeline will execute Kubeflow’s exit handler:</p><ul><li>Pipeline tasks complete successfully</li><li>Any of the pipeline tasks within the exit handler’s block fail</li><li>The pipeline is cancelled</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/410/1*JoJc0zEuZXVsh-B3rH0uYg.png" /><figcaption>Figure 2: A Kubeflow pipeline with an Exit Handler</figcaption></figure><h3>Creating Robust ML Pipelines</h3><p>Common AI +ML use cases require a training and prediction pipeline to serve predictions offline. However, some use cases (for example in the context of a chatbot) will often require an API to provide ad-hoc predictions such as answers, summaries or recommendations based on user input. In Vertex AI, you can deploy <a href="https://cloud.google.com/vertex-ai/docs/predictions/get-predictions">model endpoints</a> to wrap your model in an API which can be queried upon user requests. As an example, we’ll deploy the open-source Large-Language Model (LLM) <a href="https://huggingface.co/gpt2">GPT-2 from HuggingFace</a> to provide predictions based on a user prompt.</p><p>Regardless of how you automate the deployment of your model, it’s important to ensure that your API serves predictions correctly and satisfies latency requirements to keep your end users happy! Based on what we know about Kubeflow and exit handlers, let’s implement a robust deployment workflow in a Vertex AI pipeline:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/676/1*cHKe7zY-IsouvaIpYgjTxA.png" /><figcaption>Figure 3: A robust deployment pipeline</figcaption></figure><p>The deployment pipeline is broken down into three parts:</p><ol><li><strong>Import model and tests: </strong>Import your pre-trained open-source model and tests from <a href="https://cloud.google.com/vertex-ai/docs/model-registry/introduction">Vertex AI Model Registry</a> and performance tests from Cloud Storage.</li><li><strong>Deploy + test model: </strong>Deploy a Vertex AI endpoint + model and run performance tests against the deployed model API.</li><li><strong>Clean up resources: </strong>Delete the deployed model and endpoint.</li></ol><p>This pipeline structure allows us to declare the clean-up operation as an exit handler which runs even if any of the tasks fail or the pipeline is cancelled. This is especially important to keep your costs low if you deploy a model with GPU acceleration:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/652/1*rrOepnKkCPDb8y370hXM-g.png" /><figcaption>Figure 4: The exit handler executes regardless of task failures</figcaption></figure><p>The following code shows a low-code implementation of the ML pipeline which uses <a href="https://cloud.google.com/vertex-ai/docs/pipelines/components-introduction">Google Cloud Pipeline Components</a> and <a href="https://locust.io/">Locust</a>:</p><pre>from kfp.dsl import pipeline, importer, ExitHandler<br>from google_cloud_pipeline_components.v1.endpoint import EndpointCreateOp, ModelDeployOp<br>from .locust_component import LocustTestsOp<br><br>@pipeline<br>def deploy_and_test_model_pipeline():<br>  # 1. Import model and tests<br>  import_model_op = importer(...).set_display_name(&quot;Import model&quot;)<br>  import_tests_op = importer(...).set_display_name(&quot;Import tests&quot;)<br><br>  # 3. Clean up resources<br>  exit_handler_op = EndpointDeleteOp(...).set_display_name(&quot;Clean up&quot;)<br>  with ExitHandler(exit_handler_op, name=&quot;Deploy &amp; test model&quot;):<br><br>    # 2. Deploy and test models<br>    create_endpoint_op = EndpointCreateOp(...).set_display_name(&quot;Create endpoint&quot;)<br>    deploy_model_op = ModelDeployOp(...).set_display_name(&quot;Deploy model&quot;)<br>    locust_tests_op = LocustTestsOp(...).set_display_name(&quot;Test model performance&quot;)</pre><h3>1. Import model and tests</h3><p>The pipeline imports a pre-trained model from the Model Registry using an <a href="https://cloud.google.com/vertex-ai/docs/pipelines/use-components#use_an_importer_node">importer node</a> which ensures that the model is available for downstream tasks. Concurrently, tests are imported from Cloud Storage in the form of a <a href="https://docs.locust.io/en/stable/writing-a-locustfile.html">locustfile.py</a> as we’ll be testing the model’s performance using Locust. A test in Locust which monitors the latency of your deployed model can be as simple as:</p><pre>import google.auth<br>import google.auth.transport.requests<br>from os import environ as env<br>from locust import HttpUser, task<br><br>class VertexEndpointUser(HttpUser):<br>  host = env[&quot;VERTEX_AI_ENDPOINT_HOST&quot;]<br>  path = env[&quot;VERTEX_AI_ENDPOINT_PATH&quot;]<br><br>  def on_start(self):<br>    # authenticate using default credentials<br>    creds, _ = google.auth.default()<br>    creds.refresh(google.auth.transport.requests.Request()<br>    self.client.headers = {&quot;Authorization&quot;: f&quot;Bearer {creds.token}&quot;}<br><br>  @task<br>  def request_prediction(self):<br>    # send a prediction request to the Vertex AI endpoint<br>    prompt = &quot;Write a dialog using Shakespeare language.&quot;<br>    body = {&quot;instances&quot;: [{&quot;text&quot;: prompt}]}<br>    with self.client.post(self.path, json=body, catch_response=True) as res:<br>      if res.status_code == 200:<br>        res.success()<br>      else:<br>        res.failure(f&quot;Status code: {res.status_code} Reason: {res.reason}&quot;)</pre><h3>2. Deploy and test model</h3><p>We use the tasks <em>“EndpointCreateOp”</em> and <em>“ModelDeployOp”</em> from <a href="https://cloud.google.com/vertex-ai/docs/pipelines/model-endpoint-component">Model and Endpoint Components</a> to create a model endpoint and deploy the model as an API. For running performance tests, we implement a custom task which wraps Locust in a Kubeflow component to execute performance tests defined in the importer node previously.</p><p>The Locust component provides the test results as an HTML artifact output on Google Cloud Storage. Since Vertex AI Pipelines supports several visualisation types such as <a href="https://cloud.google.com/vertex-ai/docs/pipelines/output-html-md#output_html">HTML or Markdown outputs</a>, you can open the results natively via the UI:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*J3_9oBkmq4NWFGjMdOhpDA.gif" /><figcaption>Figure 7: Results of the model performance test</figcaption></figure><p>As shown in Figure 7, the Locust test report summarises the latency results of your model over time and the success rate of requests. These results and other useful information provided by Locust will help you to understand the performance of your model <strong>before</strong> making it available to your end-users in production.</p><h3>3. Clean up resources</h3><p>At the end of your pipeline, the deployed endpoint is deleted to ensure that you won’t incur further cloud costs. Going back to the advantages of using an exit handler in Kubeflow, your ML pipeline will be robust to task failures or pipeline cancellations as the exit handler will be executed under all circumstances.</p><h3>Where to go from here</h3><p>If you’re interested in running model performance tests yourself, follow us on <a href="https://medium.com/datatonic">Medium</a> for an in-depth story on using Locust in Vertex AI Pipelines soon and more Vertex AI Tips + Tricks.</p><p>Are you looking for scalable training and prediction pipelines for XGBoost or TensorFlow instead? Check out our <a href="https://datatonic.com/insights/vertex-ai-mlops-turbo-templates/">MLOps Turbo Template based on Google Cloud and Vertex AI</a>; we appreciate your contribution to our <a href="https://github.com/GoogleCloudPlatform/vertex-pipelines-end-to-end-samples">open-source GitHub repository</a>!</p><blockquote>Datatonic is <a href="https://datatonic.com/insights/datatonic-google-cloud-specialization-partner-award-machine-learning/">Google Cloud’s Machine Learning Partner of the Year</a> with a wealth of experience developing and deploying impactful Machine Learning models and MLOps Platform builds. Need help with developing an ML model, or deploying your Machine Learning models fast? Have a look at our <a href="https://datatonic.com/events/mlops-101-ml-at-scale/">MLOps 101 webinar</a>, where our experts talk you through how to get started with Machine Learning at scale or<strong> </strong><a href="https://datatonic.com/contact">get in touch</a> to discuss your ML or MLOps requirements!</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d0d8d1d8318" width="1" height="1" alt=""><hr><p><a href="https://blog.montrealanalytics.com/vertex-ai-tips-and-tricks-using-exit-handlers-to-create-robust-ml-pipelines-in-production-2d0d8d1d8318">Vertex AI Tips and Tricks: Using Exit Handlers to Create Robust ML Pipelines in Production</a> was originally published in <a href="https://blog.montrealanalytics.com/">${around_the_block}</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>