Buckets:

hf-doc-build/doc-dev / sagemaker /pr_2585 /en /tutorials /sagemaker-sdk /training-sagemaker-sdk.html
download
raw
74.6 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;local&quot;:&quot;run-training-on-amazon-sagemaker&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Installation and setup&quot;,&quot;local&quot;:&quot;installation-and-setup&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Prepare a 🤗 Transformers fine-tuning script&quot;,&quot;local&quot;:&quot;prepare-a--transformers-fine-tuning-script&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Training Output Management&quot;,&quot;local&quot;:&quot;training-output-management&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Create a ModelTrainer&quot;,&quot;local&quot;:&quot;create-a-modeltrainer&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Execute training&quot;,&quot;local&quot;:&quot;execute-training&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Access trained model&quot;,&quot;local&quot;:&quot;access-trained-model&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Distributed training&quot;,&quot;local&quot;:&quot;distributed-training&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Data parallelism&quot;,&quot;local&quot;:&quot;data-parallelism&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Model parallelism&quot;,&quot;local&quot;:&quot;model-parallelism&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Spot instances&quot;,&quot;local&quot;:&quot;spot-instances&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Git repository&quot;,&quot;local&quot;:&quot;git-repository&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;SageMaker metrics&quot;,&quot;local&quot;:&quot;sagemaker-metrics&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}">
<link href="/docs/sagemaker/pr_2585/en/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload">
<link rel="modulepreload" href="/docs/sagemaker/pr_2585/en/_app/immutable/entry/start.8bec021f.js">
<link rel="modulepreload" href="/docs/sagemaker/pr_2585/en/_app/immutable/chunks/scheduler.aec39e6a.js">
<link rel="modulepreload" href="/docs/sagemaker/pr_2585/en/_app/immutable/chunks/singletons.dbd6b9f0.js">
<link rel="modulepreload" href="/docs/sagemaker/pr_2585/en/_app/immutable/chunks/paths.98b9d0e7.js">
<link rel="modulepreload" href="/docs/sagemaker/pr_2585/en/_app/immutable/entry/app.2d6e6bb7.js">
<link rel="modulepreload" href="/docs/sagemaker/pr_2585/en/_app/immutable/chunks/preload-helper.a4df83e9.js">
<link rel="modulepreload" href="/docs/sagemaker/pr_2585/en/_app/immutable/chunks/index.4ee0a2d0.js">
<link rel="modulepreload" href="/docs/sagemaker/pr_2585/en/_app/immutable/nodes/0.a7dfdf8a.js">
<link rel="modulepreload" href="/docs/sagemaker/pr_2585/en/_app/immutable/chunks/each.e59479a4.js">
<link rel="modulepreload" href="/docs/sagemaker/pr_2585/en/_app/immutable/nodes/20.b2119262.js">
<link rel="modulepreload" href="/docs/sagemaker/pr_2585/en/_app/immutable/chunks/MermaidChart.svelte_svelte_type_style_lang.c07de156.js">
<link rel="modulepreload" href="/docs/sagemaker/pr_2585/en/_app/immutable/chunks/CodeBlock.22855481.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Run training on Amazon SageMaker&quot;,&quot;local&quot;:&quot;run-training-on-amazon-sagemaker&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Installation and setup&quot;,&quot;local&quot;:&quot;installation-and-setup&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Prepare a 🤗 Transformers fine-tuning script&quot;,&quot;local&quot;:&quot;prepare-a--transformers-fine-tuning-script&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Training Output Management&quot;,&quot;local&quot;:&quot;training-output-management&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Create a ModelTrainer&quot;,&quot;local&quot;:&quot;create-a-modeltrainer&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Execute training&quot;,&quot;local&quot;:&quot;execute-training&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Access trained model&quot;,&quot;local&quot;:&quot;access-trained-model&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Distributed training&quot;,&quot;local&quot;:&quot;distributed-training&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Data parallelism&quot;,&quot;local&quot;:&quot;data-parallelism&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Model parallelism&quot;,&quot;local&quot;:&quot;model-parallelism&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Spot instances&quot;,&quot;local&quot;:&quot;spot-instances&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Git repository&quot;,&quot;local&quot;:&quot;git-repository&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;SageMaker metrics&quot;,&quot;local&quot;:&quot;sagemaker-metrics&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"><!-- HEAD_svelte-u9bgzb_END --> <p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"><div class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg></span> <span>Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg></button></div> </div> <h1 class="relative group"><a id="run-training-on-amazon-sagemaker" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#run-training-on-amazon-sagemaker"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Run training on Amazon SageMaker</span></h1> <iframe width="700" height="394" src="https://www.youtube.com/embed/ok3hetb42gU" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen=""></iframe> <p data-svelte-h="svelte-1rb0wi">This guide will show you how to train a 🤗 Transformers model with the SageMaker Python SDK. Learn how to:</p> <ul data-svelte-h="svelte-1goonzi"><li><a href="#installation-and-setup">Install and setup your training environment</a>.</li> <li><a href="#prepare-a-transformers-fine-tuning-script">Prepare a training script</a>.</li> <li><a href="#create-a-modeltrainer">Create a ModelTrainer</a>.</li> <li><a href="#execute-training">Run training with the <code>train</code> method</a>.</li> <li><a href="#access-trained-model">Access your trained model</a>.</li> <li><a href="#distributed-training">Perform distributed training</a>.</li> <li><a href="#spot-instances">Create a spot instance</a>.</li> <li><a href="#git-repository">Load a training script from a GitHub repository</a>.</li> <li><a href="#sagemaker-metrics">Collect training metrics</a>.</li></ul> <h2 class="relative group"><a id="installation-and-setup" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#installation-and-setup"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Installation and setup</span></h2> <p data-svelte-h="svelte-9mb7jq">Before you can train a 🤗 Transformers model with SageMaker, you need to sign up for an AWS account. If you don’t have an AWS account yet, learn more <a href="https://docs.aws.amazon.com/sagemaker/latest/dg/gs-set-up.html" rel="nofollow">here</a>.</p> <p data-svelte-h="svelte-14pz1nf">Once you have an AWS account, get started using one of the following:</p> <ul data-svelte-h="svelte-nu9uzs"><li><a href="https://docs.aws.amazon.com/sagemaker/latest/dg/gs-studio-onboard.html" rel="nofollow">SageMaker Studio</a></li> <li><a href="https://docs.aws.amazon.com/sagemaker/latest/dg/gs-console.html" rel="nofollow">SageMaker notebook instance</a></li> <li>Local environment</li></ul> <p data-svelte-h="svelte-10vylvb">To start training locally, you need to setup an appropriate <a href="https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-roles.html" rel="nofollow">IAM role</a>.</p> <p data-svelte-h="svelte-1q1gn32">Upgrade to the latest <code>sagemaker</code> version:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-bash "><!-- HTML_TAG_START -->pip install <span class="hljs-string">&#x27;sagemaker&gt;=3.0.0&#x27;</span><!-- HTML_TAG_END --></pre></div> <blockquote class="note" data-svelte-h="svelte-17avxnn"><p>These docs and examples use the <a href="https://github.com/aws/sagemaker-python-sdk" rel="nofollow">SageMaker Python SDK v3</a>, which introduces a new framework-agnostic API built around <code>ModelTrainer</code> (training) and <code>ModelBuilder</code> (inference), replacing the v2 <code>HuggingFace</code> and <code>HuggingFaceModel</code> classes. Install it with <code>pip install &quot;sagemaker&gt;=3.0.0&quot;</code>.</p></blockquote> <p data-svelte-h="svelte-aqpf90"><strong>SageMaker environment</strong></p> <p data-svelte-h="svelte-1n86nit">Setup your SageMaker environment as shown below:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-python "><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> sagemaker.core.helper.session_helper <span class="hljs-keyword">import</span> Session, get_execution_role
sess = Session()
role = get_execution_role()<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-bmcgfj"><em>Note: The execution role is only available when running a notebook within SageMaker. If you run <code>get_execution_role</code> in a notebook not on SageMaker, expect a <code>region</code> error.</em></p> <p data-svelte-h="svelte-12o7543"><strong>Local environment</strong></p> <p data-svelte-h="svelte-qjt50s">Setup your local environment as shown below:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-python "><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> boto3
<span class="hljs-keyword">from</span> sagemaker.core.helper.session_helper <span class="hljs-keyword">import</span> Session
iam_client = boto3.client(<span class="hljs-string">&#x27;iam&#x27;</span>)
role = iam_client.get_role(RoleName=<span class="hljs-string">&#x27;role-name-of-your-iam-role-with-right-permissions&#x27;</span>)[<span class="hljs-string">&#x27;Role&#x27;</span>][<span class="hljs-string">&#x27;Arn&#x27;</span>]
sess = Session()<!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="prepare-a--transformers-fine-tuning-script" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#prepare-a--transformers-fine-tuning-script"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Prepare a 🤗 Transformers fine-tuning script</span></h2> <p data-svelte-h="svelte-1u3xeug">Our training script is very similar to a training script you might run outside of SageMaker. However, you can access useful properties about the training environment through various environment variables (see <a href="https://github.com/aws/sagemaker-training-toolkit/blob/master/ENVIRONMENT_VARIABLES.md" rel="nofollow">here</a> for a complete list), such as:</p> <ul data-svelte-h="svelte-hopda0"><li><p><code>SM_MODEL_DIR</code>: A string representing the path to which the training job writes the model artifacts. After training, artifacts in this directory are uploaded to S3 for model hosting. <code>SM_MODEL_DIR</code> is always set to <code>/opt/ml/model</code>.</p></li> <li><p><code>SM_NUM_GPUS</code>: An integer representing the number of GPUs available to the host.</p></li> <li><p><code>SM_CHANNEL_XXXX:</code> A string representing the path to the directory that contains the input data for the specified channel. For example, when you specify <code>train</code> and <code>test</code> channels in the <code>ModelTrainer</code> via <code>input_data_config</code>, the environment variables are set to <code>SM_CHANNEL_TRAIN</code> and <code>SM_CHANNEL_TEST</code>.</p></li></ul> <p data-svelte-h="svelte-43864j">The <code>hyperparameters</code> defined in the <a href="#create-a-modeltrainer">ModelTrainer</a> are passed as named arguments and processed by <code>ArgumentParser()</code>.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-python "><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> transformers
<span class="hljs-keyword">import</span> datasets
<span class="hljs-keyword">import</span> argparse
<span class="hljs-keyword">import</span> os
<span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&quot;__main__&quot;</span>:
parser = argparse.ArgumentParser()
<span class="hljs-comment"># hyperparameters sent by the client are passed as command-line arguments to the script</span>
parser.add_argument(<span class="hljs-string">&quot;--epochs&quot;</span>, <span class="hljs-built_in">type</span>=<span class="hljs-built_in">int</span>, default=<span class="hljs-number">3</span>)
parser.add_argument(<span class="hljs-string">&quot;--per_device_train_batch_size&quot;</span>, <span class="hljs-built_in">type</span>=<span class="hljs-built_in">int</span>, default=<span class="hljs-number">32</span>)
parser.add_argument(<span class="hljs-string">&quot;--model_name_or_path&quot;</span>, <span class="hljs-built_in">type</span>=<span class="hljs-built_in">str</span>)
<span class="hljs-comment"># data, model, and output directories</span>
parser.add_argument(<span class="hljs-string">&quot;--model-dir&quot;</span>, <span class="hljs-built_in">type</span>=<span class="hljs-built_in">str</span>, default=os.environ[<span class="hljs-string">&quot;SM_MODEL_DIR&quot;</span>])
parser.add_argument(<span class="hljs-string">&quot;--training_dir&quot;</span>, <span class="hljs-built_in">type</span>=<span class="hljs-built_in">str</span>, default=os.environ[<span class="hljs-string">&quot;SM_CHANNEL_TRAIN&quot;</span>])
parser.add_argument(<span class="hljs-string">&quot;--test_dir&quot;</span>, <span class="hljs-built_in">type</span>=<span class="hljs-built_in">str</span>, default=os.environ[<span class="hljs-string">&quot;SM_CHANNEL_TEST&quot;</span>])<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-x146s4"><em>Note that SageMaker doesn’t support argparse actions. For example, if you want to use a boolean hyperparameter, specify <code>type</code> as <code>bool</code> in your script and provide an explicit <code>True</code> or <code>False</code> value.</em></p> <p data-svelte-h="svelte-1v5ow9s">Look <a href="https://github.com/huggingface/notebooks/blob/main/sagemaker/01_getting_started_pytorch/scripts/train.py" rel="nofollow">train.py file</a> for a complete example of a 🤗 Transformers training script.</p> <h2 class="relative group"><a id="training-output-management" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#training-output-management"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Training Output Management</span></h2> <p data-svelte-h="svelte-1iromrn">If <code>output_dir</code> in the <code>TrainingArguments</code> is set to ‘/opt/ml/model’ the Trainer saves all training artifacts, including logs, checkpoints, and models. Amazon SageMaker archives the whole ‘/opt/ml/model’ directory as <code>model.tar.gz</code> and uploads it at the end of the training job to Amazon S3. Depending on your Hyperparameters and <code>TrainingArguments</code> this could lead to a large artifact (&gt; 5GB), which can slow down deployment for Amazon SageMaker Inference.
You can control how checkpoints, logs, and artifacts are saved by customization the <a href="https://huggingface.co/docs/transformers/main/en/main_classes/trainer#transformers.TrainingArguments" rel="nofollow">TrainingArguments</a>. For example by providing <code>save_total_limit</code> as <code>TrainingArgument</code> you can control the limit of the total amount of checkpoints. Deletes the older checkpoints in <code>output_dir</code> if new ones are saved and the maximum limit is reached.</p> <p data-svelte-h="svelte-2xadtt">In addition to the options already mentioned above, there is another option to save the training artifacts during the training session. Amazon SageMaker supports <a href="https://docs.aws.amazon.com/sagemaker/latest/dg/model-checkpoints.html" rel="nofollow">Checkpointing</a>, which allows you to continuously save your artifacts during training to Amazon S3 rather than at the end of your training. To enable <a href="https://docs.aws.amazon.com/sagemaker/latest/dg/model-checkpoints.html" rel="nofollow">Checkpointing</a> you need to provide a <code>CheckpointConfig(s3_uri=...)</code> pointing to an Amazon S3 location on the <code>ModelTrainer</code> and set <code>output_dir</code> to <code>/opt/ml/checkpoints</code>.
<em>Note: If you set <code>output_dir</code> to <code>/opt/ml/checkpoints</code> make sure to call <code>trainer.save_model(&quot;/opt/ml/model&quot;)</code> or model.save_pretrained(“/opt/ml/model”)/<code>tokenizer.save_pretrained(&quot;/opt/ml/model&quot;)</code> at the end of your training to be able to deploy your model seamlessly to Amazon SageMaker for Inference.</em></p> <h2 class="relative group"><a id="create-a-modeltrainer" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#create-a-modeltrainer"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Create a ModelTrainer</span></h2> <p data-svelte-h="svelte-21obxn">Run 🤗 Transformers training scripts on SageMaker by creating a <a href="https://sagemaker.readthedocs.io/en/stable/" rel="nofollow"><code>ModelTrainer</code></a>. The <code>ModelTrainer</code> handles end-to-end SageMaker training. There are several parameters you should define:</p> <ol data-svelte-h="svelte-li674k"><li><code>source_code</code> specifies the fine-tuning script (<code>entry_script</code>) and its directory (<code>source_dir</code>).</li> <li><code>compute</code> specifies the Amazon instance(s) to launch. Refer <a href="https://aws.amazon.com/sagemaker/pricing/" rel="nofollow">here</a> for a complete list of instance types.</li> <li><code>training_image</code> is the training container image, retrieved with <code>image_uris.retrieve</code>.</li> <li><code>hyperparameters</code> specifies training hyperparameters. View additional available hyperparameters in <a href="https://github.com/huggingface/notebooks/blob/main/sagemaker/01_getting_started_pytorch/scripts/train.py" rel="nofollow">train.py file</a>.</li></ol> <p data-svelte-h="svelte-129am2x">The following code sample shows how to train with a custom script <code>train.py</code> with three hyperparameters (<code>epochs</code>, <code>per_device_train_batch_size</code>, and <code>model_name_or_path</code>):</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-python "><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> sagemaker.train.model_trainer <span class="hljs-keyword">import</span> ModelTrainer
<span class="hljs-keyword">from</span> sagemaker.train.configs <span class="hljs-keyword">import</span> SourceCode, Compute
<span class="hljs-keyword">from</span> sagemaker.core <span class="hljs-keyword">import</span> image_uris
<span class="hljs-keyword">from</span> sagemaker.core.helper.session_helper <span class="hljs-keyword">import</span> Session, get_execution_role
<span class="hljs-comment"># set up the SageMaker session and execution role</span>
sess = Session()
role = get_execution_role()
<span class="hljs-comment"># hyperparameters which are passed to the training job (as `--key value` CLI args)</span>
hyperparameters = {
<span class="hljs-string">&#x27;epochs&#x27;</span>: <span class="hljs-number">1</span>,
<span class="hljs-string">&#x27;per_device_train_batch_size&#x27;</span>: <span class="hljs-number">32</span>,
<span class="hljs-string">&#x27;model_name_or_path&#x27;</span>: <span class="hljs-string">&#x27;distilbert-base-uncased&#x27;</span>,
}
instance_type = <span class="hljs-string">&#x27;ml.g6.12xlarge&#x27;</span>
<span class="hljs-comment"># Retrieve the Hugging Face PyTorch training DLC image URI</span>
training_image = image_uris.retrieve(
framework=<span class="hljs-string">&quot;huggingface&quot;</span>,
region=sess.boto_region_name,
version=<span class="hljs-string">&quot;4.49.0&quot;</span>,
base_framework_version=<span class="hljs-string">&quot;pytorch2.5.1&quot;</span>,
py_version=<span class="hljs-string">&quot;py311&quot;</span>,
image_scope=<span class="hljs-string">&quot;training&quot;</span>,
instance_type=instance_type,
)
<span class="hljs-comment"># create the ModelTrainer</span>
huggingface_estimator = ModelTrainer(
sagemaker_session=sess,
role=role,
training_image=training_image,
source_code=SourceCode(
source_dir=<span class="hljs-string">&#x27;./scripts&#x27;</span>,
entry_script=<span class="hljs-string">&#x27;train.py&#x27;</span>,
),
compute=Compute(
instance_type=instance_type,
instance_count=<span class="hljs-number">1</span>,
),
hyperparameters=hyperparameters,
)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1dwzx5g">If you are running a <code>TrainingJob</code> locally, define <code>instance_type=&#39;local&#39;</code> or <code>instance_type=&#39;local_gpu&#39;</code> for GPU usage. Note that this will not work with SageMaker Studio.</p> <h2 class="relative group"><a id="execute-training" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#execute-training"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Execute training</span></h2> <p data-svelte-h="svelte-6qnu8e">Start your <code>TrainingJob</code> by calling <code>train</code> on a <code>ModelTrainer</code>. Specify your input training data as channels via <code>input_data_config</code>. Each channel’s <code>data_source</code> can be a:</p> <ul data-svelte-h="svelte-pw3ckd"><li>S3 URI such as <code>s3://my-bucket/my-training-data</code>.</li> <li><code>FileSystemInput</code> for Amazon Elastic File System or FSx for Lustre.</li></ul> <p data-svelte-h="svelte-1xfp6ro">Each channel is mounted inside the container at <code>/opt/ml/input/data/&lt;channel_name&gt;</code>. Call <code>train</code> to begin training:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-python "><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> sagemaker.train.configs <span class="hljs-keyword">import</span> InputData
huggingface_estimator.train(
input_data_config=[
InputData(channel_name=<span class="hljs-string">&quot;train&quot;</span>, data_source=<span class="hljs-string">&quot;s3://&lt;your-bucket&gt;/imdb/train&quot;</span>),
InputData(channel_name=<span class="hljs-string">&quot;test&quot;</span>, data_source=<span class="hljs-string">&quot;s3://&lt;your-bucket&gt;/imdb/test&quot;</span>),
]
)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-pdxcpx">SageMaker starts and manages all the required EC2 instances and initiates the <code>TrainingJob</code> by running:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-bash "><!-- HTML_TAG_START -->/opt/conda/bin/python train.py --epochs 1 --model_name_or_path distilbert-base-uncased --per_device_train_batch_size 32<!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="access-trained-model" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#access-trained-model"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Access trained model</span></h2> <p data-svelte-h="svelte-5qtnyn">Once training is complete, you can access your model through the <a href="https://console.aws.amazon.com/console/home?nc2=h_ct&src=header-signin" rel="nofollow">AWS console</a> or download it directly from S3. The S3 URI of the trained model artifacts is available on the completed training job:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-python "><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> boto3
<span class="hljs-keyword">from</span> urllib.parse <span class="hljs-keyword">import</span> urlparse
<span class="hljs-comment"># S3 URI where the trained model artifacts (model.tar.gz) are located</span>
model_data = huggingface_estimator._latest_training_job.model_artifacts.s3_model_artifacts
parsed = urlparse(model_data)
boto3.client(<span class="hljs-string">&quot;s3&quot;</span>).download_file(
parsed.netloc, <span class="hljs-comment"># bucket</span>
parsed.path.lstrip(<span class="hljs-string">&quot;/&quot;</span>), <span class="hljs-comment"># key</span>
<span class="hljs-string">&quot;model.tar.gz&quot;</span>, <span class="hljs-comment"># local path where the artifact is saved</span>
)<!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="distributed-training" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#distributed-training"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Distributed training</span></h2> <p data-svelte-h="svelte-8hyjxi">SageMaker provides two strategies for distributed training: data parallelism and model parallelism. Data parallelism splits a training set across several GPUs, while model parallelism splits a model across several GPUs.</p> <h3 class="relative group"><a id="data-parallelism" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#data-parallelism"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Data parallelism</span></h3> <p data-svelte-h="svelte-ovxam8">The Hugging Face <a href="https://huggingface.co/docs/transformers/main_classes/trainer" rel="nofollow">Trainer</a> supports distributed data parallel training. With <code>ModelTrainer</code> you launch your script with <code>torchrun</code> by passing a <code>Torchrun</code> config to the <code>distributed</code> parameter. Set <code>process_count_per_node</code> to the number of GPUs per instance (<code>ml.p3dn.24xlarge</code> has 8):</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-python "><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> sagemaker.train.model_trainer <span class="hljs-keyword">import</span> ModelTrainer
<span class="hljs-keyword">from</span> sagemaker.train.configs <span class="hljs-keyword">import</span> SourceCode, Compute
<span class="hljs-keyword">from</span> sagemaker.train.distributed <span class="hljs-keyword">import</span> Torchrun
<span class="hljs-keyword">from</span> sagemaker.core <span class="hljs-keyword">import</span> image_uris
<span class="hljs-keyword">from</span> sagemaker.core.helper.session_helper <span class="hljs-keyword">import</span> Session, get_execution_role
<span class="hljs-comment"># set up the SageMaker session and execution role</span>
sess = Session()
role = get_execution_role()
<span class="hljs-comment"># hyperparameters which are passed to the training job (as `--key value` CLI args)</span>
hyperparameters = {
<span class="hljs-string">&#x27;epochs&#x27;</span>: <span class="hljs-number">1</span>,
<span class="hljs-string">&#x27;per_device_train_batch_size&#x27;</span>: <span class="hljs-number">32</span>,
<span class="hljs-string">&#x27;model_name_or_path&#x27;</span>: <span class="hljs-string">&#x27;distilbert-base-uncased&#x27;</span>,
}
instance_type = <span class="hljs-string">&#x27;ml.p3dn.24xlarge&#x27;</span>
training_image = image_uris.retrieve(
framework=<span class="hljs-string">&quot;huggingface&quot;</span>,
region=sess.boto_region_name,
version=<span class="hljs-string">&quot;4.49.0&quot;</span>,
base_framework_version=<span class="hljs-string">&quot;pytorch2.5.1&quot;</span>,
py_version=<span class="hljs-string">&quot;py311&quot;</span>,
image_scope=<span class="hljs-string">&quot;training&quot;</span>,
instance_type=instance_type,
)
<span class="hljs-comment"># create the ModelTrainer with torchrun for distributed data parallelism</span>
huggingface_estimator = ModelTrainer(
sagemaker_session=sess,
role=role,
training_image=training_image,
source_code=SourceCode(source_dir=<span class="hljs-string">&#x27;./scripts&#x27;</span>, entry_script=<span class="hljs-string">&#x27;train.py&#x27;</span>),
compute=Compute(instance_type=instance_type, instance_count=<span class="hljs-number">2</span>),
distributed=Torchrun(process_count_per_node=<span class="hljs-number">8</span>),
hyperparameters=hyperparameters,
)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-oj7kbb">📓 Open the <a href="https://github.com/huggingface/notebooks/blob/main/sagemaker/07_tensorflow_distributed_training_data_parallelism/sagemaker-notebook.ipynb" rel="nofollow">sagemaker-notebook.ipynb notebook</a> for an example of how to run the data parallelism library with TensorFlow.</p> <h3 class="relative group"><a id="model-parallelism" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#model-parallelism"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Model parallelism</span></h3> <p data-svelte-h="svelte-1l2kzh0">The Hugging Face [Trainer] also supports model parallelism through the SageMaker Model Parallelism library (SMP). With <code>ModelTrainer</code> you enable it by passing an <code>SMP</code> config to <code>Torchrun</code>. SMP provides tensor parallelism, context parallelism and sharded data parallelism:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-python "><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> sagemaker.train.model_trainer <span class="hljs-keyword">import</span> ModelTrainer
<span class="hljs-keyword">from</span> sagemaker.train.configs <span class="hljs-keyword">import</span> SourceCode, Compute
<span class="hljs-keyword">from</span> sagemaker.train.distributed <span class="hljs-keyword">import</span> Torchrun, SMP
<span class="hljs-keyword">from</span> sagemaker.core <span class="hljs-keyword">import</span> image_uris
<span class="hljs-keyword">from</span> sagemaker.core.helper.session_helper <span class="hljs-keyword">import</span> Session, get_execution_role
<span class="hljs-comment"># set up the SageMaker session and execution role</span>
sess = Session()
role = get_execution_role()
<span class="hljs-comment"># hyperparameters which are passed to the training job (as `--key value` CLI args)</span>
hyperparameters = {
<span class="hljs-string">&#x27;epochs&#x27;</span>: <span class="hljs-number">1</span>,
<span class="hljs-string">&#x27;per_device_train_batch_size&#x27;</span>: <span class="hljs-number">32</span>,
<span class="hljs-string">&#x27;model_name_or_path&#x27;</span>: <span class="hljs-string">&#x27;distilbert-base-uncased&#x27;</span>,
}
instance_type = <span class="hljs-string">&#x27;ml.p3dn.24xlarge&#x27;</span>
training_image = image_uris.retrieve(
framework=<span class="hljs-string">&quot;huggingface&quot;</span>,
region=sess.boto_region_name,
version=<span class="hljs-string">&quot;4.49.0&quot;</span>,
base_framework_version=<span class="hljs-string">&quot;pytorch2.5.1&quot;</span>,
py_version=<span class="hljs-string">&quot;py311&quot;</span>,
image_scope=<span class="hljs-string">&quot;training&quot;</span>,
instance_type=instance_type,
)
<span class="hljs-comment"># create the ModelTrainer with torchrun + SMP for model parallelism</span>
huggingface_estimator = ModelTrainer(
sagemaker_session=sess,
role=role,
training_image=training_image,
source_code=SourceCode(source_dir=<span class="hljs-string">&#x27;./scripts&#x27;</span>, entry_script=<span class="hljs-string">&#x27;train.py&#x27;</span>),
compute=Compute(instance_type=instance_type, instance_count=<span class="hljs-number">2</span>),
distributed=Torchrun(
process_count_per_node=<span class="hljs-number">8</span>,
smp=SMP(
tensor_parallel_degree=<span class="hljs-number">2</span>,
hybrid_shard_degree=<span class="hljs-number">1</span>,
),
),
hyperparameters=hyperparameters,
)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-16yf61l">📓 Open the <a href="https://github.com/huggingface/notebooks/blob/main/sagemaker/04_distributed_training_model_parallelism/sagemaker-notebook.ipynb" rel="nofollow">sagemaker-notebook.ipynb notebook</a> for an example of how to run the model parallelism library.</p> <h2 class="relative group"><a id="spot-instances" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#spot-instances"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Spot instances</span></h2> <p data-svelte-h="svelte-1yk874x">The Hugging Face extension for the SageMaker Python SDK means we can benefit from <a href="https://docs.aws.amazon.com/sagemaker/latest/dg/model-managed-spot-training.html" rel="nofollow">fully-managed EC2 spot instances</a>. This can help you save up to 90% of training costs!</p> <p data-svelte-h="svelte-11t96qn"><em>Note: Unless your training job completes quickly, we recommend you use <a href="https://docs.aws.amazon.com/sagemaker/latest/dg/model-checkpoints.html" rel="nofollow">checkpointing</a> with managed spot training. In this case, you need to define the <code>checkpoint_s3_uri</code>.</em></p> <p data-svelte-h="svelte-1mkah2h">Set <code>enable_managed_spot_training=True</code> on <code>Compute</code> and define <code>max_wait_time_in_seconds</code> and <code>max_runtime_in_seconds</code> on <code>StoppingCondition</code> to use spot instances:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-python "><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> sagemaker.train.model_trainer <span class="hljs-keyword">import</span> ModelTrainer
<span class="hljs-keyword">from</span> sagemaker.train.configs <span class="hljs-keyword">import</span> SourceCode, Compute, StoppingCondition, CheckpointConfig
<span class="hljs-keyword">from</span> sagemaker.core <span class="hljs-keyword">import</span> image_uris
<span class="hljs-keyword">from</span> sagemaker.core.helper.session_helper <span class="hljs-keyword">import</span> Session, get_execution_role
<span class="hljs-comment"># set up the SageMaker session and execution role</span>
sess = Session()
role = get_execution_role()
<span class="hljs-comment"># hyperparameters which are passed to the training job</span>
hyperparameters = {
<span class="hljs-string">&#x27;epochs&#x27;</span>: <span class="hljs-number">1</span>,
<span class="hljs-string">&#x27;train_batch_size&#x27;</span>: <span class="hljs-number">32</span>,
<span class="hljs-string">&#x27;model_name&#x27;</span>: <span class="hljs-string">&#x27;distilbert-base-uncased&#x27;</span>,
<span class="hljs-string">&#x27;output_dir&#x27;</span>: <span class="hljs-string">&#x27;/opt/ml/checkpoints&#x27;</span>,
}
instance_type = <span class="hljs-string">&#x27;ml.g6.12xlarge&#x27;</span>
training_image = image_uris.retrieve(
framework=<span class="hljs-string">&quot;huggingface&quot;</span>,
region=sess.boto_region_name,
version=<span class="hljs-string">&quot;4.49.0&quot;</span>,
base_framework_version=<span class="hljs-string">&quot;pytorch2.5.1&quot;</span>,
py_version=<span class="hljs-string">&quot;py311&quot;</span>,
image_scope=<span class="hljs-string">&quot;training&quot;</span>,
instance_type=instance_type,
)
<span class="hljs-comment"># create the ModelTrainer</span>
huggingface_estimator = ModelTrainer(
sagemaker_session=sess,
role=role,
training_image=training_image,
source_code=SourceCode(source_dir=<span class="hljs-string">&#x27;./scripts&#x27;</span>, entry_script=<span class="hljs-string">&#x27;train.py&#x27;</span>),
compute=Compute(
instance_type=instance_type,
instance_count=<span class="hljs-number">1</span>,
enable_managed_spot_training=<span class="hljs-literal">True</span>, <span class="hljs-comment"># use fully-managed spot instances</span>
),
<span class="hljs-comment"># max_wait_time_in_seconds should be equal to or greater than max_runtime_in_seconds</span>
stopping_condition=StoppingCondition(
max_runtime_in_seconds=<span class="hljs-number">1000</span>,
max_wait_time_in_seconds=<span class="hljs-number">3600</span>,
),
checkpoint_config=CheckpointConfig(s3_uri=<span class="hljs-string">f&#x27;s3://<span class="hljs-subst">{sess.default_bucket()}</span>/checkpoints&#x27;</span>),
hyperparameters=hyperparameters,
)
<span class="hljs-comment"># Training seconds: 874</span>
<span class="hljs-comment"># Billable seconds: 262</span>
<span class="hljs-comment"># Managed Spot Training savings: 70.0%</span><!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1384417">📓 Open the <a href="https://github.com/huggingface/notebooks/blob/main/sagemaker/05_spot_instances/sagemaker-notebook.ipynb" rel="nofollow">sagemaker-notebook.ipynb notebook</a> for an example of how to use spot instances.</p> <h2 class="relative group"><a id="git-repository" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#git-repository"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Git repository</span></h2> <p data-svelte-h="svelte-pqe8g0">The v2 <code>git_config</code> parameter is not available in <code>ModelTrainer</code>. To run a training script that lives in a GitHub repository (such as the <a href="https://github.com/huggingface/transformers/tree/main/examples" rel="nofollow">🤗 Transformers example scripts</a>), clone the repository locally first and point <code>source_dir</code>/<code>entry_script</code> at the checked-out files. Choose a branch that matches the Transformers version of your training image.</p> <p data-svelte-h="svelte-hhyk3e"><em>Tip: Save your model to S3 by setting <code>output_dir=/opt/ml/model</code> in the hyperparameter of your training script.</em></p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-bash "><!-- HTML_TAG_START --><span class="hljs-comment"># clone the repo locally, matching the transformers version of your training image</span>
git <span class="hljs-built_in">clone</span> --branch v4.49.0 https://github.com/huggingface/transformers.git<!-- HTML_TAG_END --></pre></div> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-python "><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> sagemaker.train.model_trainer <span class="hljs-keyword">import</span> ModelTrainer
<span class="hljs-keyword">from</span> sagemaker.train.configs <span class="hljs-keyword">import</span> SourceCode, Compute
<span class="hljs-keyword">from</span> sagemaker.core <span class="hljs-keyword">import</span> image_uris
<span class="hljs-keyword">from</span> sagemaker.core.helper.session_helper <span class="hljs-keyword">import</span> Session, get_execution_role
<span class="hljs-comment"># set up the SageMaker session and execution role</span>
sess = Session()
role = get_execution_role()
<span class="hljs-comment"># hyperparameters which are passed to the training job (as `--key value` CLI args)</span>
hyperparameters = {
<span class="hljs-string">&#x27;epochs&#x27;</span>: <span class="hljs-number">1</span>,
<span class="hljs-string">&#x27;per_device_train_batch_size&#x27;</span>: <span class="hljs-number">32</span>,
<span class="hljs-string">&#x27;model_name_or_path&#x27;</span>: <span class="hljs-string">&#x27;distilbert-base-uncased&#x27;</span>,
}
instance_type = <span class="hljs-string">&#x27;ml.g6.12xlarge&#x27;</span>
<span class="hljs-comment"># Retrieve the Hugging Face PyTorch training DLC image URI</span>
training_image = image_uris.retrieve(
framework=<span class="hljs-string">&quot;huggingface&quot;</span>,
region=sess.boto_region_name,
version=<span class="hljs-string">&quot;4.49.0&quot;</span>,
base_framework_version=<span class="hljs-string">&quot;pytorch2.5.1&quot;</span>,
py_version=<span class="hljs-string">&quot;py311&quot;</span>,
image_scope=<span class="hljs-string">&quot;training&quot;</span>,
instance_type=instance_type,
)
<span class="hljs-comment"># create the ModelTrainer pointing at the cloned example directory</span>
huggingface_estimator = ModelTrainer(
sagemaker_session=sess,
role=role,
training_image=training_image,
source_code=SourceCode(
source_dir=<span class="hljs-string">&#x27;transformers/examples/pytorch/text-classification&#x27;</span>,
entry_script=<span class="hljs-string">&#x27;run_glue.py&#x27;</span>,
requirements=<span class="hljs-string">&#x27;requirements.txt&#x27;</span>,
),
compute=Compute(instance_type=<span class="hljs-string">&#x27;ml.g6.12xlarge&#x27;</span>, instance_count=<span class="hljs-number">1</span>),
hyperparameters=hyperparameters,
)<!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="sagemaker-metrics" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#sagemaker-metrics"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>SageMaker metrics</span></h2> <p data-svelte-h="svelte-1i0p3wr"><a href="https://docs.aws.amazon.com/sagemaker/latest/dg/training-metrics.html#define-train-metrics" rel="nofollow">SageMaker metrics</a> automatically parses training job logs for metrics and sends them to CloudWatch. If you want SageMaker to parse the logs, you must specify the metric’s name and a regular expression for SageMaker to use to find the metric. With <code>ModelTrainer</code> you attach them using <code>with_metric_definitions</code>:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class="language-python "><!-- HTML_TAG_START --><span class="hljs-keyword">from</span> sagemaker.train.model_trainer <span class="hljs-keyword">import</span> ModelTrainer
<span class="hljs-keyword">from</span> sagemaker.train.configs <span class="hljs-keyword">import</span> SourceCode, Compute, MetricDefinition
<span class="hljs-keyword">from</span> sagemaker.core <span class="hljs-keyword">import</span> image_uris
<span class="hljs-keyword">from</span> sagemaker.core.helper.session_helper <span class="hljs-keyword">import</span> Session, get_execution_role
<span class="hljs-comment"># set up the SageMaker session and execution role</span>
sess = Session()
role = get_execution_role()
<span class="hljs-comment"># hyperparameters which are passed to the training job (as `--key value` CLI args)</span>
hyperparameters = {
<span class="hljs-string">&#x27;epochs&#x27;</span>: <span class="hljs-number">1</span>,
<span class="hljs-string">&#x27;per_device_train_batch_size&#x27;</span>: <span class="hljs-number">32</span>,
<span class="hljs-string">&#x27;model_name_or_path&#x27;</span>: <span class="hljs-string">&#x27;distilbert-base-uncased&#x27;</span>,
}
instance_type = <span class="hljs-string">&#x27;ml.g6.12xlarge&#x27;</span>
training_image = image_uris.retrieve(
framework=<span class="hljs-string">&quot;huggingface&quot;</span>,
region=sess.boto_region_name,
version=<span class="hljs-string">&quot;4.49.0&quot;</span>,
base_framework_version=<span class="hljs-string">&quot;pytorch2.5.1&quot;</span>,
py_version=<span class="hljs-string">&quot;py311&quot;</span>,
image_scope=<span class="hljs-string">&quot;training&quot;</span>,
instance_type=instance_type,
)
<span class="hljs-comment"># define metrics definitions</span>
metric_definitions = [
MetricDefinition(name=<span class="hljs-string">&quot;train_runtime&quot;</span>, regex=<span class="hljs-string">&quot;train_runtime.*=\D*(.*?)$&quot;</span>),
MetricDefinition(name=<span class="hljs-string">&quot;eval_accuracy&quot;</span>, regex=<span class="hljs-string">&quot;eval_accuracy.*=\D*(.*?)$&quot;</span>),
MetricDefinition(name=<span class="hljs-string">&quot;eval_loss&quot;</span>, regex=<span class="hljs-string">&quot;eval_loss.*=\D*(.*?)$&quot;</span>),
]
<span class="hljs-comment"># create the ModelTrainer</span>
huggingface_estimator = ModelTrainer(
sagemaker_session=sess,
role=role,
training_image=training_image,
source_code=SourceCode(source_dir=<span class="hljs-string">&#x27;./scripts&#x27;</span>, entry_script=<span class="hljs-string">&#x27;train.py&#x27;</span>),
compute=Compute(instance_type=instance_type, instance_count=<span class="hljs-number">1</span>),
hyperparameters=hyperparameters,
).with_metric_definitions(metric_definitions)<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1y5r5uo">📓 Open the <a href="https://github.com/huggingface/notebooks/blob/main/sagemaker/06_sagemaker_metrics/sagemaker-notebook.ipynb" rel="nofollow">notebook</a> for an example of how to capture metrics in SageMaker.</p> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/hub-docs/blob/main/docs/sagemaker/source/tutorials/sagemaker-sdk/training-sagemaker-sdk.md" target="_blank"><svg class="mr-1" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg> <span data-svelte-h="svelte-zjs2n5"><span class="underline">Update</span> on GitHub</span></a> <p></p>
<script>
{
__sveltekit_fg6pm8 = {
assets: "/docs/sagemaker/pr_2585/en",
base: "/docs/sagemaker/pr_2585/en",
env: {}
};
const element = document.currentScript.parentElement;
const data = [null,null];
Promise.all([
import("/docs/sagemaker/pr_2585/en/_app/immutable/entry/start.8bec021f.js"),
import("/docs/sagemaker/pr_2585/en/_app/immutable/entry/app.2d6e6bb7.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 20],
data,
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
74.6 kB
·
Xet hash:
b5d9e80e792e5529b55584d9317644c1456550aa9ce428af2cb4976cc1c7f90d

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.