{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# SageMaker Inference Recommender for Instant Recommendations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "---\n", "\n", "This notebook's CI test result for us-west-2 is as follows. CI test results in other regions can be found at the end of the notebook. \n", "\n", "![This us-west-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/us-west-2/sagemaker-inference-recommender|deployment-recommendations|instant-recommendations.ipynb)\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This notebook demonstrates how to get instant deployment recommendations from the DescribeModel API\n", "\n", "The DescribeModel API can be found here: https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_DescribeModel.html\n", "\n", "**This sample is provided for demonstration purposes, make sure to conduct appropriate testing if deriving this code for your own use-cases!**" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 0: Set Up" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "!pip install boto3 --upgrade" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "import time\n", "import boto3\n", "import sagemaker\n", "import pandas as pd\n", "from uuid import uuid4\n", "from datetime import datetime, timedelta" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "sm_client = boto3.client(service_name=\"sagemaker\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 1: Create the Model\n", "\n", "For this sample notebook, we will use a simple model specifying only a Tensorflow DLC" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "model_name = \"instant-rec-demo-nb-model-\" + str(uuid4())\n", "print(model_name)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "region = boto3.Session().region_name\n", "image = sagemaker.image_uris.retrieve(\n", " framework=\"tensorflow\",\n", " region=region,\n", " version=\"1.12.0\",\n", " image_scope=\"inference\",\n", " instance_type=\"ml.c5.4xlarge\",\n", ")\n", "print(image)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "execution_role = sagemaker.get_execution_role()\n", "\n", "create_model_response = sm_client.create_model(\n", " ModelName=model_name, ExecutionRoleArn=execution_role, Containers=[{\"Image\": image}]\n", ")\n", "\n", "print(create_model_response)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 2: Describe the Model\n", "\n", "Describe the Model until `DeploymentRecommendations` goes into a `COMPLETED` state. On average, you should expect to have DeploymentRecommendations available within a second" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "completed = timed_out = False\n", "\n", "start = datetime.today()\n", "max_duration = start + timedelta(minutes=1)\n", "\n", "while not completed and not timed_out:\n", " describe_model_response = sm_client.describe_model(ModelName=model_name)\n", " deployment_recommendation = describe_model_response.get(\"DeploymentRecommendation\")\n", "\n", " completed = (\n", " deployment_recommendation is not None\n", " and \"COMPLETED\" == deployment_recommendation.get(\"RecommendationStatus\")\n", " )\n", "\n", " time.sleep(0.5)\n", " timed_out = datetime.today() > max_duration\n", "\n", " print(datetime.today(), \"DeploymentRecommendation:\", deployment_recommendation)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 3: Visualize the DeploymentRecommendations" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true, "tags": [] }, "outputs": [], "source": [ "pd.set_option(\"display.max_colwidth\", None)\n", "df = pd.DataFrame(\n", " deployment_recommendation.get(\"RealTimeInferenceRecommendations\"),\n", " columns=[\"RecommendationId\", \"InstanceType\", \"Environment\"],\n", ")\n", "display(df)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 4: Cleanup" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [] }, "outputs": [], "source": [ "sm_client.delete_model(ModelName=model_name)" ] }, { "cell_type": "markdown", "metadata": { "tags": [] }, "source": [ "## Notebook CI Test Results\n", "\n", "This notebook was tested in multiple regions. The test results are as follows, except for us-west-2 which is shown at the top of the notebook.\n", "\n", "\n", "![This us-east-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/us-east-1/sagemaker-inference-recommender|deployment-recommendations|instant-recommendations.ipynb)\n", "\n", "![This us-east-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/us-east-2/sagemaker-inference-recommender|deployment-recommendations|instant-recommendations.ipynb)\n", "\n", "![This us-west-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/us-west-1/sagemaker-inference-recommender|deployment-recommendations|instant-recommendations.ipynb)\n", "\n", "![This ca-central-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/ca-central-1/sagemaker-inference-recommender|deployment-recommendations|instant-recommendations.ipynb)\n", "\n", "![This sa-east-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/sa-east-1/sagemaker-inference-recommender|deployment-recommendations|instant-recommendations.ipynb)\n", "\n", "![This eu-west-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/eu-west-1/sagemaker-inference-recommender|deployment-recommendations|instant-recommendations.ipynb)\n", "\n", "![This eu-west-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/eu-west-2/sagemaker-inference-recommender|deployment-recommendations|instant-recommendations.ipynb)\n", "\n", "![This eu-west-3 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/eu-west-3/sagemaker-inference-recommender|deployment-recommendations|instant-recommendations.ipynb)\n", "\n", "![This eu-central-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/eu-central-1/sagemaker-inference-recommender|deployment-recommendations|instant-recommendations.ipynb)\n", "\n", "![This eu-north-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/eu-north-1/sagemaker-inference-recommender|deployment-recommendations|instant-recommendations.ipynb)\n", "\n", "![This ap-southeast-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/ap-southeast-1/sagemaker-inference-recommender|deployment-recommendations|instant-recommendations.ipynb)\n", "\n", "![This ap-southeast-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/ap-southeast-2/sagemaker-inference-recommender|deployment-recommendations|instant-recommendations.ipynb)\n", "\n", "![This ap-northeast-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/ap-northeast-1/sagemaker-inference-recommender|deployment-recommendations|instant-recommendations.ipynb)\n", "\n", "![This ap-northeast-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/ap-northeast-2/sagemaker-inference-recommender|deployment-recommendations|instant-recommendations.ipynb)\n", "\n", "![This ap-south-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable](https://h75twx4l60.execute-api.us-west-2.amazonaws.com/sagemaker-nb/ap-south-1/sagemaker-inference-recommender|deployment-recommendations|instant-recommendations.ipynb)\n" ] } ], "metadata": { "instance_type": "ml.t3.medium", "kernelspec": { "display_name": "conda_tensorflow2_p310", "language": "python", "name": "conda_tensorflow2_p310" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.8" } }, "nbformat": 4, "nbformat_minor": 4 }