{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Train a model locally or remote" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "scrolled": true, "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Note: you may need to restart the kernel to use updated packages.\n", "Looking in indexes: https://pypi.org/simple, https://pip.repos.neuron.amazonaws.com\n", "Collecting ezsmdeploy==1.93.dev0\n", " Using cached ezsmdeploy-1.93.dev0-py3-none-any.whl (29 kB)\n", "Collecting sagemaker==2.171.0 (from ezsmdeploy==1.93.dev0)\n", " Using cached sagemaker-2.171.0-py2.py3-none-any.whl\n", "Requirement already satisfied: yaspin==0.16.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from ezsmdeploy==1.93.dev0) (0.16.0)\n", "Requirement already satisfied: shortuuid==1.0.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from ezsmdeploy==1.93.dev0) (1.0.1)\n", "Requirement already satisfied: sagemaker-studio-image-build==0.5.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from ezsmdeploy==1.93.dev0) (0.5.0)\n", "Requirement already satisfied: boto3>=1.14.12 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from ezsmdeploy==1.93.dev0) (1.26.157)\n", "Requirement already satisfied: attrs<24,>=23.1.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (23.1.0)\n", "Requirement already satisfied: cloudpickle==2.2.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (2.2.1)\n", "Requirement already satisfied: google-pasta in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (0.2.0)\n", "Requirement already satisfied: numpy<2.0,>=1.9.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (1.22.3)\n", "Requirement already satisfied: protobuf<4.0,>=3.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (3.20.3)\n", "Requirement already satisfied: smdebug-rulesconfig==1.0.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (1.0.1)\n", "Requirement already satisfied: importlib-metadata<5.0,>=1.4.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (4.13.0)\n", "Requirement already satisfied: packaging>=20.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (21.3)\n", "Requirement already satisfied: pandas in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (2.0.1)\n", "Requirement already satisfied: pathos in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (0.3.0)\n", "Requirement already satisfied: schema in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (0.7.5)\n", "Requirement already satisfied: PyYAML==6.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (6.0)\n", "Requirement already satisfied: jsonschema in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (4.17.3)\n", "Requirement already satisfied: platformdirs in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (3.5.0)\n", "Requirement already satisfied: tblib==1.7.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (1.7.0)\n", "Requirement already satisfied: botocore<1.30.0,>=1.29.157 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from boto3>=1.14.12->ezsmdeploy==1.93.dev0) (1.29.157)\n", "Requirement already satisfied: jmespath<2.0.0,>=0.7.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from boto3>=1.14.12->ezsmdeploy==1.93.dev0) (1.0.1)\n", "Requirement already satisfied: s3transfer<0.7.0,>=0.6.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from boto3>=1.14.12->ezsmdeploy==1.93.dev0) (0.6.1)\n", "Requirement already satisfied: python-dateutil<3.0.0,>=2.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from botocore<1.30.0,>=1.29.157->boto3>=1.14.12->ezsmdeploy==1.93.dev0) (2.8.2)\n", "Requirement already satisfied: urllib3<1.27,>=1.25.4 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from botocore<1.30.0,>=1.29.157->boto3>=1.14.12->ezsmdeploy==1.93.dev0) (1.26.14)\n", "Requirement already satisfied: zipp>=0.5 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from importlib-metadata<5.0,>=1.4.0->sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (3.15.0)\n", "Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from packaging>=20.0->sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (3.0.9)\n", "Requirement already satisfied: six in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from google-pasta->sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (1.16.0)\n", "Requirement already satisfied: pyrsistent!=0.17.0,!=0.17.1,!=0.17.2,>=0.14.0 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from jsonschema->sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (0.19.3)\n", "Requirement already satisfied: pytz>=2020.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from pandas->sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (2023.3)\n", "Requirement already satisfied: tzdata>=2022.1 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from pandas->sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (2023.3)\n", "Requirement already satisfied: ppft>=1.7.6.6 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from pathos->sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (1.7.6.6)\n", "Requirement already satisfied: dill>=0.3.6 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from pathos->sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (0.3.6)\n", "Requirement already satisfied: pox>=0.3.2 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from pathos->sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (0.3.2)\n", "Requirement already satisfied: multiprocess>=0.70.14 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from pathos->sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (0.70.14)\n", "Requirement already satisfied: contextlib2>=0.5.5 in /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages (from schema->sagemaker==2.171.0->ezsmdeploy==1.93.dev0) (21.6.0)\n", "Installing collected packages: sagemaker, ezsmdeploy\n", " Attempting uninstall: sagemaker\n", " Found existing installation: sagemaker 2.173.0\n", " Uninstalling sagemaker-2.173.0:\n", " Successfully uninstalled sagemaker-2.173.0\n", "Successfully installed ezsmdeploy-1.93.dev0 sagemaker-2.171.0\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "%pip uninstall -y ezsmdeploy --quiet\n", "# !pip install --upgrade pip\n", "%pip install ezsmdeploy==2.0.0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Note: you may need to restart the kernel to use updated packages." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Testing serverless huggingface models" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "scrolled": true, "tags": [] }, "outputs": [], "source": [ "# %pip uninstall -y sagemaker\n", "# %pip install --upgrade sagemaker==2.148.0 --no-cache-dir" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'2.171.0'" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import sagemaker\n", "sagemaker.__version__" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Name: ezsmdeploy\n", "Version: 1.93.dev0\n", "Summary: SageMaker custom deployments made easy\n", "Home-page: https://pypi.python.org/pypi/ezsmdeploy\n", "Author: Shreyas Subramanian\n", "Author-email: subshrey@amazon.com\n", "License: MIT\n", "Location: /home/ec2-user/anaconda3/envs/python3/lib/python3.10/site-packages\n", "Requires: boto3, sagemaker, sagemaker-studio-image-build, shortuuid, yaspin\n", "Required-by: \n" ] } ], "source": [ "!pip show ezsmdeploy" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "tags": [] }, "outputs": [], "source": [ "import ezsmdeploy" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[K0:00:00.401097 | created model(s). Now deploying on Serverless!\n", "\u001b[K0:02:32.945616 | deployed model\n", "\u001b[K\u001b[32m0:02:32.945715 | Done! ✔\u001b[0m \n" ] } ], "source": [ "ezonsm = ezsmdeploy.Deploy(model = \"Intel/dynamic_tinybert\",\n", " huggingface_model=True,\n", " huggingface_model_task='question-answering',\n", " serverless=True, \n", " serverless_memory=6144\n", " )" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'score': 0.628618061542511, 'start': 107, 'end': 112, 'answer': 'Leizu'}\n" ] } ], "source": [ "# Example for most models\n", "# payload = {\"inputs\":\"The symbol *** marks the separator between the \\\n", "# # prescribed beginning and the student’s completion:\\nOnce upon a time, in an ancient house, there lived a girl named Lily. She loved\"} \n", " # # example for distilbert-base-uncased-finetuned-sst-2-english\n", "# payload = {\n", "# \"inputs\": \"the mesmerizing performances of the leads keep the film grounded and keep the audience riveted .\",\n", "# }\n", "\n", "# # Example for distilbert / dynamic-tinybert / deepset/tinyroberta-squad2\n", "payload = {\"inputs\": {\n", " \"question\": \"Who discovered silk?\",\n", " \"context\": \"Legend has it that the process for making silk cloth was first invented by the wife of the Yellow Emperor, Leizu, around the year 2696 BC. The idea for silk first came to Leizu while she was having tea in the imperial gardens.\" + \"The production of silk originates in China in the Neolithic (Yangshao culture, 4th millennium BCE). Silk remained confined to China until the Silk Road opened at some point during the later half of the first millennium BCE. \"\n", "}}\n", "\n", "response = ezonsm.predictor.predict(payload)\n", "\n", "print(response)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "tags": [] }, "outputs": [], "source": [ "ezonsm.predictor.delete_endpoint()" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "tags": [] }, "outputs": [ { "data": { "text/plain": [ "'ezsm-hf-endpoint-akejahze3ahiqpnyzyvnjp'" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ezonsm.predictor.endpoint_name" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "conda_python3", "language": "python", "name": "conda_python3" }, "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.10" } }, "nbformat": 4, "nbformat_minor": 4 }