|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "import sys, os\n", |
| 10 | + "from sagemaker import image_uris\n", |
| 11 | + "\n", |
| 12 | + "# Get the absolute path of the root directory\n", |
| 13 | + "root_dir = os.path.abspath(os.path.join(os.getcwd(), \"../../..\"))\n", |
| 14 | + "sys.path.insert(0, root_dir)" |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "markdown", |
| 19 | + "metadata": {}, |
| 20 | + "source": [ |
| 21 | + "## Local Mode ModelTrainer\n", |
| 22 | + "\n", |
| 23 | + "In local mode training, user will train their model in a container that runs in their local machine.\n", |
| 24 | + "You don't need to access any AWS resources unless you want to use data from S3 as input, or pull images from ECR." |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "markdown", |
| 29 | + "metadata": {}, |
| 30 | + "source": [ |
| 31 | + "## Simple Case Minimally Setup Local ModelTrainer and Execute Commands\n", |
| 32 | + "When running model trainer in local mode, you need to have docker engine running in your environment.\n", |
| 33 | + "When you run the following cell for the first time, a SageMaker session will be initiated to pull the image from ECR.\n", |
| 34 | + "Once the image has been pulled, there won't be any AWS API call." |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": null, |
| 40 | + "metadata": {}, |
| 41 | + "outputs": [], |
| 42 | + "source": [ |
| 43 | + "from sagemaker.modules.train.model_trainer import ModelTrainer, Mode\n", |
| 44 | + "from sagemaker.modules.configs import SourceCode\n", |
| 45 | + "from sagemaker.modules.constants import DEFAULT_INSTANCE_TYPE\n", |
| 46 | + "\n", |
| 47 | + "hugging_face_image = \"763104351884.dkr.ecr.us-west-2.amazonaws.com/huggingface-pytorch-training:2.0.0-transformers4.28.1-gpu-py310-cu118-ubuntu20.04\"\n", |
| 48 | + "\n", |
| 49 | + "source_code = SourceCode(\n", |
| 50 | + " command=\"echo 'Hello World' && env\",\n", |
| 51 | + ")\n", |
| 52 | + "model_trainer = ModelTrainer(\n", |
| 53 | + " training_image=hugging_face_image,\n", |
| 54 | + " source_code=source_code,\n", |
| 55 | + " training_input_mode=Mode.LOCAL_CONTAINER,\n", |
| 56 | + ")" |
| 57 | + ] |
| 58 | + }, |
| 59 | + { |
| 60 | + "cell_type": "code", |
| 61 | + "execution_count": null, |
| 62 | + "metadata": {}, |
| 63 | + "outputs": [], |
| 64 | + "source": [ |
| 65 | + "model_trainer.train()" |
| 66 | + ] |
| 67 | + }, |
| 68 | + { |
| 69 | + "cell_type": "markdown", |
| 70 | + "metadata": {}, |
| 71 | + "source": [ |
| 72 | + "## Simple Script Mode Case - 1: Training with Local Data\n", |
| 73 | + "In this example, everything (input, output, training resource) will be in your local environment. You don't need to use your AWS account at all for this." |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "code", |
| 78 | + "execution_count": null, |
| 79 | + "metadata": {}, |
| 80 | + "outputs": [], |
| 81 | + "source": [ |
| 82 | + "from sagemaker.modules.configs import Compute, InputData, SourceCode\n", |
| 83 | + "\n", |
| 84 | + "source_code = SourceCode(\n", |
| 85 | + " source_dir=\"basic-script-mode\",\n", |
| 86 | + " entry_script=\"local_training_script.py\",\n", |
| 87 | + ")\n", |
| 88 | + "\n", |
| 89 | + "compute = Compute(\n", |
| 90 | + " instance_type=\"local_cpu\",\n", |
| 91 | + " instance_count=1,\n", |
| 92 | + ")\n", |
| 93 | + "\n", |
| 94 | + "train_data = InputData(\n", |
| 95 | + " channel_name=\"train\",\n", |
| 96 | + " data_source=\"basic-script-mode/data/train/\",\n", |
| 97 | + ")\n", |
| 98 | + "\n", |
| 99 | + "test_data = InputData(\n", |
| 100 | + " channel_name=\"test\",\n", |
| 101 | + " data_source=\"basic-script-mode/data/test/\",\n", |
| 102 | + ")\n", |
| 103 | + "\n", |
| 104 | + "model_trainer = ModelTrainer(\n", |
| 105 | + " training_image=hugging_face_image,\n", |
| 106 | + " source_code=source_code,\n", |
| 107 | + " compute=compute,\n", |
| 108 | + " input_data_config=[train_data, test_data],\n", |
| 109 | + " base_job_name=\"local_mode_single_container_case_1\",\n", |
| 110 | + " training_mode=Mode.LOCAL_CONTAINER,\n", |
| 111 | + ")" |
| 112 | + ] |
| 113 | + }, |
| 114 | + { |
| 115 | + "cell_type": "code", |
| 116 | + "execution_count": null, |
| 117 | + "metadata": {}, |
| 118 | + "outputs": [], |
| 119 | + "source": [ |
| 120 | + "model_trainer.train()" |
| 121 | + ] |
| 122 | + }, |
| 123 | + { |
| 124 | + "cell_type": "markdown", |
| 125 | + "metadata": {}, |
| 126 | + "source": [ |
| 127 | + "## Simple Script Mode Case - 2: Training with Input Data from S3\n", |
| 128 | + "In this example, the input data is read from S3. You will have to configure your AWS credentials before running this." |
| 129 | + ] |
| 130 | + }, |
| 131 | + { |
| 132 | + "cell_type": "code", |
| 133 | + "execution_count": null, |
| 134 | + "metadata": {}, |
| 135 | + "outputs": [], |
| 136 | + "source": [ |
| 137 | + "train_data = InputData(\n", |
| 138 | + " channel_name=\"train\", data_source=\"s3://morpheus-bugbash/basic-script-mode/data/train/\"\n", |
| 139 | + ")\n", |
| 140 | + "\n", |
| 141 | + "test_data = InputData(\n", |
| 142 | + " channel_name=\"test\", data_source=\"s3://morpheus-bugbash/basic-script-mode/data/test/\"\n", |
| 143 | + ")\n", |
| 144 | + "\n", |
| 145 | + "model_trainer = ModelTrainer(\n", |
| 146 | + " training_image=hugging_face_image,\n", |
| 147 | + " source_code=source_code,\n", |
| 148 | + " compute=compute,\n", |
| 149 | + " input_data_config=[train_data, test_data],\n", |
| 150 | + " base_job_name=\"local_mode_single_container_case_2\",\n", |
| 151 | + " training_mode=Mode.LOCAL_CONTAINER,\n", |
| 152 | + ")" |
| 153 | + ] |
| 154 | + }, |
| 155 | + { |
| 156 | + "cell_type": "code", |
| 157 | + "execution_count": null, |
| 158 | + "metadata": {}, |
| 159 | + "outputs": [], |
| 160 | + "source": [ |
| 161 | + "model_trainer.train()" |
| 162 | + ] |
| 163 | + } |
| 164 | + ], |
| 165 | + "metadata": { |
| 166 | + "kernelspec": { |
| 167 | + "display_name": "py3.10", |
| 168 | + "language": "python", |
| 169 | + "name": "python3" |
| 170 | + }, |
| 171 | + "language_info": { |
| 172 | + "codemirror_mode": { |
| 173 | + "name": "ipython", |
| 174 | + "version": 3 |
| 175 | + }, |
| 176 | + "file_extension": ".py", |
| 177 | + "mimetype": "text/x-python", |
| 178 | + "name": "python", |
| 179 | + "nbconvert_exporter": "python", |
| 180 | + "pygments_lexer": "ipython3", |
| 181 | + "version": "3.10.14" |
| 182 | + } |
| 183 | + }, |
| 184 | + "nbformat": 4, |
| 185 | + "nbformat_minor": 2 |
| 186 | +} |
0 commit comments