Buckets:

hf-doc-build/doc-dev / sagemaker /pr_2188 /en /examples /sagemaker-sdk-fine-tune-trl-cli.html
rtrm's picture
download
raw
75.4 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Fine-Tuning LLMs with TRL CLI on SageMaker&quot;,&quot;local&quot;:&quot;fine-tuning-llms-with-trl-cli-on-sagemaker&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Prerequisites&quot;,&quot;local&quot;:&quot;prerequisites&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;⚙️ Setup&quot;,&quot;local&quot;:&quot;-setup&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;⚙️ Configuration&quot;,&quot;local&quot;:&quot;-configuration&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;🔧 TRL CLI with YAML Config&quot;,&quot;local&quot;:&quot;-trl-cli-with-yaml-config&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;📝 Create Training Files&quot;,&quot;local&quot;:&quot;-create-training-files&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;📊 Training Logging with Trackio&quot;,&quot;local&quot;:&quot;-training-logging-with-trackio&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;🚀 Launch Training Job&quot;,&quot;local&quot;:&quot;-launch-training-job&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;📊 Monitor Training&quot;,&quot;local&quot;:&quot;-monitor-training&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;🧪 Load and Test the Model&quot;,&quot;local&quot;:&quot;-load-and-test-the-model&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;🧹 Cleanup&quot;,&quot;local&quot;:&quot;-cleanup&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}">
<link href="/docs/sagemaker/pr_2188/en/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload">
<link rel="modulepreload" href="/docs/sagemaker/pr_2188/en/_app/immutable/entry/start.48a18f09.js">
<link rel="modulepreload" href="/docs/sagemaker/pr_2188/en/_app/immutable/chunks/scheduler.aec39e6a.js">
<link rel="modulepreload" href="/docs/sagemaker/pr_2188/en/_app/immutable/chunks/singletons.8e7a9ddc.js">
<link rel="modulepreload" href="/docs/sagemaker/pr_2188/en/_app/immutable/chunks/paths.a4e52f32.js">
<link rel="modulepreload" href="/docs/sagemaker/pr_2188/en/_app/immutable/entry/app.8481cdda.js">
<link rel="modulepreload" href="/docs/sagemaker/pr_2188/en/_app/immutable/chunks/preload-helper.382cad4e.js">
<link rel="modulepreload" href="/docs/sagemaker/pr_2188/en/_app/immutable/chunks/index.4ee0a2d0.js">
<link rel="modulepreload" href="/docs/sagemaker/pr_2188/en/_app/immutable/nodes/0.52c1f0fb.js">
<link rel="modulepreload" href="/docs/sagemaker/pr_2188/en/_app/immutable/chunks/each.e59479a4.js">
<link rel="modulepreload" href="/docs/sagemaker/pr_2188/en/_app/immutable/nodes/9.65431425.js">
<link rel="modulepreload" href="/docs/sagemaker/pr_2188/en/_app/immutable/chunks/Tip.e2132029.js">
<link rel="modulepreload" href="/docs/sagemaker/pr_2188/en/_app/immutable/chunks/MermaidChart.svelte_svelte_type_style_lang.3c60bfa3.js">
<link rel="modulepreload" href="/docs/sagemaker/pr_2188/en/_app/immutable/chunks/CodeBlock.543f5448.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Fine-Tuning LLMs with TRL CLI on SageMaker&quot;,&quot;local&quot;:&quot;fine-tuning-llms-with-trl-cli-on-sagemaker&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Prerequisites&quot;,&quot;local&quot;:&quot;prerequisites&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;⚙️ Setup&quot;,&quot;local&quot;:&quot;-setup&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;⚙️ Configuration&quot;,&quot;local&quot;:&quot;-configuration&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;🔧 TRL CLI with YAML Config&quot;,&quot;local&quot;:&quot;-trl-cli-with-yaml-config&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;📝 Create Training Files&quot;,&quot;local&quot;:&quot;-create-training-files&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;📊 Training Logging with Trackio&quot;,&quot;local&quot;:&quot;-training-logging-with-trackio&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;🚀 Launch Training Job&quot;,&quot;local&quot;:&quot;-launch-training-job&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;📊 Monitor Training&quot;,&quot;local&quot;:&quot;-monitor-training&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;🧪 Load and Test the Model&quot;,&quot;local&quot;:&quot;-load-and-test-the-model&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;🧹 Cleanup&quot;,&quot;local&quot;:&quot;-cleanup&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="fine-tuning-llms-with-trl-cli-on-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="#fine-tuning-llms-with-trl-cli-on-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>Fine-Tuning LLMs with TRL CLI on SageMaker</span></h1> <p data-svelte-h="svelte-10rirhv">This notebook shows how to fine-tune language models on AWS SageMaker using the <strong><code>trl sft</code> CLI</strong> - the same command used by <a href="https://github.com/huggingface/trl-jobs" rel="nofollow">trl-jobs</a> on HuggingFace.</p> <p data-svelte-h="svelte-asbjt7"><strong>Hardware:</strong> <code>ml.g6e.12xlarge</code> (4x L40S) or <code>ml.p4de.24xlarge</code> (8x A100)</p> <p data-svelte-h="svelte-1gdx2t4"><strong>Why TRL CLI?</strong></p> <ul data-svelte-h="svelte-1xzrglb"><li>Zero Python code - just config and run</li> <li>Multi-GPU training via <code>accelerate</code></li> <li>Compatible with <a href="https://github.com/huggingface/trl-jobs/tree/main/trl_jobs/configs" rel="nofollow">trl-jobs configs</a></li></ul> <h2 class="relative group"><a id="prerequisites" 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="#prerequisites"><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>Prerequisites</span></h2> <ul data-svelte-h="svelte-shr2a1"><li>AWS credentials configured</li> <li>SageMaker execution role with S3 access</li> <li>HuggingFace token</li></ul> <h2 class="relative group"><a id="-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="#-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>⚙️ Setup</span></h2> <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=""><!-- HTML_TAG_START -->&gt;&gt; %pip install sagemaker --upgrade --quiet<!-- HTML_TAG_END --></pre></div> <pre data-svelte-h="svelte-17iqp4r">WARNING: Ignoring invalid distribution ~ackaging (/opt/pytorch/lib/python3.12/site-packages)
WARNING: Ignoring invalid distribution ~ackaging (/opt/pytorch/lib/python3.12/site-packages)
WARNING: Ignoring invalid distribution ~ackaging (/opt/pytorch/lib/python3.12/site-packages)
WARNING: Ignoring invalid distribution ~ackaging (/opt/pytorch/lib/python3.12/site-packages)
WARNING: Ignoring invalid distribution ~ackaging (/opt/pytorch/lib/python3.12/site-packages)
WARNING: Ignoring invalid distribution ~ackaging (/opt/pytorch/lib/python3.12/site-packages)

[notice] A new release of pip is available: 25.1.1 -&gt; 25.3
[notice] To update, run: pip install --upgrade pip
Note: you may need to restart the kernel to use updated packages.
</pre> <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=""><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> sagemaker<!-- 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=""><!-- HTML_TAG_START -->&gt;&gt; <span class="hljs-comment"># Authenticate with Hugging Face</span>
&gt;&gt; <span class="hljs-keyword">import</span> os
&gt;&gt; <span class="hljs-keyword">from</span> huggingface_hub <span class="hljs-keyword">import</span> login, get_token
&gt;&gt; login()
&gt;&gt; HF_TOKEN = get_token()
&gt;&gt; os.environ[<span class="hljs-string">&quot;HF_TOKEN&quot;</span>] = HF_TOKEN
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;HF_TOKEN set: <span class="hljs-subst">{HF_TOKEN[:<span class="hljs-number">8</span>]}</span>...&quot;</span>)<!-- HTML_TAG_END --></pre></div> <pre data-svelte-h="svelte-12puo0c">HF_TOKEN set: hf_MICph...
</pre> <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=""><!-- HTML_TAG_START -->&gt;&gt; <span class="hljs-keyword">import</span> json
&gt;&gt; <span class="hljs-keyword">import</span> time
&gt;&gt; <span class="hljs-keyword">import</span> uuid
&gt;&gt; <span class="hljs-keyword">import</span> tempfile
&gt;&gt; <span class="hljs-keyword">import</span> shutil
&gt;&gt; <span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path
&gt;&gt; <span class="hljs-keyword">import</span> boto3
&gt;&gt; <span class="hljs-keyword">import</span> sagemaker
&gt;&gt; <span class="hljs-keyword">from</span> sagemaker.train.model_trainer <span class="hljs-keyword">import</span> ModelTrainer
&gt;&gt; <span class="hljs-keyword">from</span> sagemaker.train.configs <span class="hljs-keyword">import</span> SourceCode, Compute, StoppingCondition, OutputDataConfig
&gt;&gt; <span class="hljs-keyword">from</span> sagemaker.core.helper.session_helper <span class="hljs-keyword">import</span> Session<!-- HTML_TAG_END --></pre></div> <pre data-svelte-h="svelte-1k0tiap">sagemaker.config INFO - Not applying SDK defaults from location: /etc/xdg/sagemaker/config.yaml
sagemaker.config INFO - Not applying SDK defaults from location: /home/ec2-user/.config/sagemaker/config.yaml
</pre> <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=""><!-- HTML_TAG_START -->&gt;&gt; <span class="hljs-comment"># Initialize SageMaker session</span>
&gt;&gt; sagemaker_session = Session()
&gt;&gt; iam = boto3.client(<span class="hljs-string">&#x27;iam&#x27;</span>)
&gt;&gt; role = iam.get_role(RoleName=<span class="hljs-string">&#x27;sagemaker-dlcs&#x27;</span>)[<span class="hljs-string">&#x27;Role&#x27;</span>][<span class="hljs-string">&#x27;Arn&#x27;</span>]
&gt;&gt; region = sagemaker_session.boto_region_name
&gt;&gt; account_id = boto3.client(<span class="hljs-string">&quot;sts&quot;</span>).get_caller_identity()[<span class="hljs-string">&quot;Account&quot;</span>]
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Region: <span class="hljs-subst">{region}</span>&quot;</span>)
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Account: <span class="hljs-subst">{account_id}</span>&quot;</span>)
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Role: <span class="hljs-subst">{role}</span>&quot;</span>)<!-- HTML_TAG_END --></pre></div> <pre data-svelte-h="svelte-blti5a">Region: us-east-1
Account: 754289655784
Role: arn:aws:iam::754289655784:role/sagemaker-dlcs
</pre> <h2 class="relative group"><a id="-configuration" 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="#-configuration"><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>⚙️ Configuration</span></h2> <p data-svelte-h="svelte-uk8vqv">We use <strong>YAML config files</strong> exactly like <a href="https://github.com/huggingface/trl-jobs/blob/main/trl_jobs/configs/Qwen3-4B-a100-large.yaml" rel="nofollow">trl-jobs</a>.</p> <p data-svelte-h="svelte-r7hp5o"><strong>Hardware:</strong> 4x L40S GPUs on <code>ml.g6e.12xlarge</code> (192GB total VRAM)</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=""><!-- HTML_TAG_START -->&gt;&gt; <span class="hljs-comment"># =============================================================================</span>
&gt;&gt; <span class="hljs-comment"># MODEL &amp; INSTANCE CONFIGURATION</span>
&gt;&gt; <span class="hljs-comment"># =============================================================================</span>
&gt;&gt; <span class="hljs-comment"># Based on trl-jobs optimal configs:</span>
&gt;&gt; <span class="hljs-comment"># https://github.com/huggingface/trl-jobs/blob/main/trl_jobs/configs/</span>
&gt;&gt; MODEL_NAME = <span class="hljs-string">&quot;Qwen/Qwen3-4B-Instruct-2507&quot;</span> <span class="hljs-comment"># trl-jobs optimal config</span>
&gt;&gt; DATASET_NAME = <span class="hljs-string">&quot;OpenMed/Medical-Reasoning-SFT-GPT-OSS-120B&quot;</span>
&gt;&gt; <span class="hljs-comment"># Instance: ml.g6e.12xlarge = 4x L40S GPUs (48GB each = 192GB total)</span>
&gt;&gt; <span class="hljs-comment"># Note: g6e.16xlarge only has 1 GPU, g6e.12xlarge/g6e.24xlarge have 4 GPUs</span>
&gt;&gt; INSTANCE_TYPE = <span class="hljs-string">&quot;ml.p4de.24xlarge&quot;</span>
&gt;&gt; NUM_GPUS = <span class="hljs-number">8</span>
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Model: <span class="hljs-subst">{MODEL_NAME}</span>&quot;</span>)
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Dataset: <span class="hljs-subst">{DATASET_NAME}</span>&quot;</span>)
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Instance: <span class="hljs-subst">{INSTANCE_TYPE}</span> (<span class="hljs-subst">{NUM_GPUS}</span>x L40S GPUs)&quot;</span>)<!-- HTML_TAG_END --></pre></div> <pre data-svelte-h="svelte-8uq4u2">Model: Qwen/Qwen3-4B-Instruct-2507
Dataset: OpenMed/Medical-Reasoning-SFT-GPT-OSS-120B
Instance: ml.p4de.24xlarge (8x L40S GPUs)
</pre> <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=""><!-- HTML_TAG_START -->&gt;&gt; <span class="hljs-comment"># Generate unique output model name for hub_model_id and run_name</span>
&gt;&gt; timestamp = time.strftime(<span class="hljs-string">&quot;%Y%m%d%H%M%S&quot;</span>, time.gmtime())
&gt;&gt; OUTPUT_MODEL_NAME = <span class="hljs-string">f&quot;Qwen3-4B-Base-SFT-<span class="hljs-subst">{timestamp}</span>&quot;</span>
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Output model name: <span class="hljs-subst">{OUTPUT_MODEL_NAME}</span>&quot;</span>)
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;(Copy this to hub_model_id and run_name in the YAML config cell)&quot;</span>)<!-- HTML_TAG_END --></pre></div> <pre data-svelte-h="svelte-v6xiiq">Output model name: Qwen3-4B-Base-SFT-20260120162518
(Copy this to hub_model_id and run_name in the YAML config cell)
</pre> <h2 class="relative group"><a id="-trl-cli-with-yaml-config" 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="#-trl-cli-with-yaml-config"><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>🔧 TRL CLI with YAML Config</span></h2> <p data-svelte-h="svelte-vgee4u">TRL CLI has <strong>native accelerate support</strong> - just put <code>num_processes</code> in the YAML config:</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=""><!-- HTML_TAG_START -->trl sft --config sft_config.yaml<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1bu7p6r">No need for <code>accelerate launch</code> wrapper! See <a href="https://huggingface.co/docs/trl/en/clis#scaling-up-with-accelerate" rel="nofollow">TRL CLI docs</a>.</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=""><!-- HTML_TAG_START -->&gt;&gt; <span class="hljs-comment"># Preview the command that will be executed</span>
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Command:&quot;</span>)
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot; trl sft --config sft_config.yaml&quot;</span>)
&gt;&gt; <span class="hljs-built_in">print</span>()
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Config: scripts/sft_config.yaml&quot;</span>)
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Model: Qwen/Qwen3-4B-Base&quot;</span>)
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Dataset: trl-lib/Capybara&quot;</span>)
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;GPUs: 4 (via num_processes in YAML)&quot;</span>)<!-- HTML_TAG_END --></pre></div> <pre data-svelte-h="svelte-1f2b3x9">Command:
trl sft --config sft_config.yaml
Config: scripts/sft_config.yaml
Model: Qwen/Qwen3-4B-Base
Dataset: trl-lib/Capybara
GPUs: 4 (via num_processes in YAML)
</pre> <h2 class="relative group"><a id="-create-training-files" 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-training-files"><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 Training Files</span></h2> <p data-svelte-h="svelte-u05ibi">We create:</p> <ol data-svelte-h="svelte-1x0qyyz"><li><strong><code>sft_config.yaml</code></strong> - TRL SFT config (same format as trl-jobs)</li> <li><strong><code>run_sft.py</code></strong> - Entry script that calls <code>trl sft --config</code></li> <li><strong><code>requirements.txt</code></strong> - Python dependencies</li></ol> <h3 class="relative group"><a id="-training-logging-with-trackio" 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-logging-with-trackio"><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 Logging with Trackio</span></h3> <p data-svelte-h="svelte-heatfk">This notebook uses <a href="https://huggingface.co/docs/trl/en/trackio_integration" rel="nofollow">Trackio</a> for real-time training metrics visualization. Trackio is TRL’s native integration for logging training runs to a Hugging Face Space.</p> <p data-svelte-h="svelte-fx8nq4"><strong>To use Trackio:</strong></p> <ol data-svelte-h="svelte-1d3wvnx"><li>Set <code>report_to: trackio</code> in your config</li> <li>Configure <code>TRACKIO_SPACE_ID</code> (your HF Space) and <code>TRACKIO_PROJECT</code> (project name) as environment variables</li></ol> <p data-svelte-h="svelte-gec9i3"><strong>To disable:</strong> Change <code>report_to: trackio</code> to <code>report_to: none</code> in the config and remove the <code>TRACKIO_*</code> environment variables.</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=""><!-- HTML_TAG_START -->&gt;&gt; <span class="hljs-comment"># Create scripts directory</span>
&gt;&gt; <span class="hljs-keyword">import</span> shutil
&gt;&gt; source_dir = Path(<span class="hljs-string">&quot;scripts&quot;</span>)
&gt;&gt; <span class="hljs-keyword">if</span> source_dir.exists():
<span class="hljs-meta">... </span> shutil.rmtree(source_dir)
&gt;&gt; source_dir.mkdir(exist_ok=<span class="hljs-literal">True</span>)
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;✓ Created <span class="hljs-subst">{source_dir}</span>/&quot;</span>)<!-- HTML_TAG_END --></pre></div> <pre data-svelte-h="svelte-15dm47y">✓ Created scripts/
</pre> <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=""><!-- HTML_TAG_START -->&gt;&gt; %%writefile scripts/run_sft.py
&gt;&gt; <span class="hljs-comment">#!/usr/bin/env python3</span>
&gt;&gt; <span class="hljs-string">&quot;&quot;&quot;Entry script for TRL SFT training on SageMaker.
<span class="hljs-meta">... </span>Runs: trl sft --config sft_config.yaml
<span class="hljs-meta">... </span>TRL CLI natively supports accelerate - num_processes is in the YAML config.
<span class="hljs-meta">... </span>See: https://huggingface.co/docs/trl/en/clis#scaling-up-with-accelerate
<span class="hljs-meta">... </span>&quot;&quot;&quot;</span>
&gt;&gt; <span class="hljs-keyword">import</span> os
&gt;&gt; <span class="hljs-keyword">import</span> sys
&gt;&gt; <span class="hljs-keyword">import</span> subprocess
&gt;&gt; <span class="hljs-keyword">from</span> pathlib <span class="hljs-keyword">import</span> Path
&gt;&gt; <span class="hljs-keyword">def</span> <span class="hljs-title function_">main</span>():
<span class="hljs-meta">... </span> <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;=&quot;</span> * <span class="hljs-number">60</span>)
<span class="hljs-meta">... </span> <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;TRL SFT Training on SageMaker&quot;</span>)
<span class="hljs-meta">... </span> <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;=&quot;</span> * <span class="hljs-number">60</span>)
<span class="hljs-meta">... </span> <span class="hljs-comment"># Environment info</span>
<span class="hljs-meta">... </span> hf_token = os.environ.get(<span class="hljs-string">&quot;HF_TOKEN&quot;</span>, <span class="hljs-string">&quot;&quot;</span>)
<span class="hljs-meta">... </span> <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;HF_TOKEN: <span class="hljs-subst">{hf_token[:<span class="hljs-number">8</span>]}</span>...&quot;</span> <span class="hljs-keyword">if</span> hf_token <span class="hljs-keyword">else</span> <span class="hljs-string">&quot;HF_TOKEN: NOT SET&quot;</span>)
<span class="hljs-meta">... </span> <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Working directory: <span class="hljs-subst">{os.getcwd()}</span>&quot;</span>)
<span class="hljs-meta">... </span> <span class="hljs-comment"># Find config file</span>
<span class="hljs-meta">... </span> config_path = Path(<span class="hljs-string">&quot;/opt/ml/input/data/code/sft_config.yaml&quot;</span>)
<span class="hljs-meta">... </span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> config_path.exists():
<span class="hljs-meta">... </span> config_path = Path(<span class="hljs-string">&quot;sft_config.yaml&quot;</span>)
<span class="hljs-meta">... </span> <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Config file: <span class="hljs-subst">{config_path}</span>&quot;</span>)
<span class="hljs-meta">... </span> <span class="hljs-built_in">print</span>()
<span class="hljs-meta">... </span> <span class="hljs-comment"># Print config contents</span>
<span class="hljs-meta">... </span> <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Configuration:&quot;</span>)
<span class="hljs-meta">... </span> <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;-&quot;</span> * <span class="hljs-number">40</span>)
<span class="hljs-meta">... </span> <span class="hljs-built_in">print</span>(config_path.read_text())
<span class="hljs-meta">... </span> <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;-&quot;</span> * <span class="hljs-number">40</span>)
<span class="hljs-meta">... </span> <span class="hljs-built_in">print</span>()
<span class="hljs-meta">... </span> <span class="hljs-comment"># TRL CLI has native accelerate support - just pass the config</span>
<span class="hljs-meta">... </span> <span class="hljs-comment"># num_processes, mixed_precision, etc. are read from the YAML</span>
<span class="hljs-meta">... </span> cmd = [<span class="hljs-string">&quot;trl&quot;</span>, <span class="hljs-string">&quot;sft&quot;</span>, <span class="hljs-string">&quot;--config&quot;</span>, <span class="hljs-built_in">str</span>(config_path)]
<span class="hljs-meta">... </span> <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Running: <span class="hljs-subst">{<span class="hljs-string">&#x27; &#x27;</span>.join(cmd)}</span>&quot;</span>)
<span class="hljs-meta">... </span> <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;=&quot;</span> * <span class="hljs-number">60</span>)
<span class="hljs-meta">... </span> <span class="hljs-comment"># Execute</span>
<span class="hljs-meta">... </span> result = subprocess.run(cmd, check=<span class="hljs-literal">False</span>)
<span class="hljs-meta">... </span> sys.exit(result.returncode)
&gt;&gt; <span class="hljs-keyword">if</span> __name__ == <span class="hljs-string">&quot;__main__&quot;</span>:
<span class="hljs-meta">... </span> main()<!-- HTML_TAG_END --></pre></div> <pre data-svelte-h="svelte-hfy09j">Overwriting scripts/run_sft.py
</pre> <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=""><!-- HTML_TAG_START -->&gt;&gt; %%writefile scripts/requirements.txt
&gt;&gt; trl&gt;=<span class="hljs-number">0.12</span><span class="hljs-number">.0</span>
&gt;&gt; transformers&gt;=<span class="hljs-number">4.45</span><span class="hljs-number">.0</span>
&gt;&gt; datasets&gt;=<span class="hljs-number">3.0</span><span class="hljs-number">.0</span>
&gt;&gt; peft&gt;=<span class="hljs-number">0.13</span><span class="hljs-number">.0</span>
&gt;&gt; accelerate&gt;=<span class="hljs-number">1.0</span><span class="hljs-number">.0</span>
&gt;&gt; huggingface_hub&gt;=<span class="hljs-number">0.26</span><span class="hljs-number">.0</span>
&gt;&gt; liger-kernel&gt;=<span class="hljs-number">0.4</span><span class="hljs-number">.0</span>
&gt;&gt; flash-attn&gt;=<span class="hljs-number">2.0</span><span class="hljs-number">.0</span>
&gt;&gt; trackio
&gt;&gt; hf_transfer<!-- HTML_TAG_END --></pre></div> <pre data-svelte-h="svelte-1jtnupv">Overwriting scripts/requirements.txt
</pre> <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=""><!-- HTML_TAG_START -->&gt;&gt; %%writefile scripts/sft_config.yaml
&gt;&gt; <span class="hljs-comment"># TRL SFT Config - Based on trl-jobs</span>
&gt;&gt; <span class="hljs-comment"># https://github.com/huggingface/trl-jobs/blob/main/trl_jobs/configs/Qwen3-4B-a100-large.yaml</span>
&gt;&gt; <span class="hljs-comment"># Adapted for 4x L40S GPUs (ml.g6e.12xlarge)</span>
&gt;&gt; <span class="hljs-comment"># Accelerate arguments</span>
&gt;&gt; num_processes: <span class="hljs-number">8</span>
&gt;&gt; num_machines: <span class="hljs-number">1</span>
&gt;&gt; mixed_precision: <span class="hljs-string">&quot;no&quot;</span>
&gt;&gt; dynamo_backend: <span class="hljs-string">&quot;no&quot;</span>
&gt;&gt; <span class="hljs-comment"># Model arguments</span>
&gt;&gt; model_name_or_path: Qwen/Qwen3-4B-Instruct-<span class="hljs-number">2507</span>
&gt;&gt; model_revision: main
&gt;&gt; torch_dtype: bfloat16
&gt;&gt; attn_implementation: kernels-community/flash-attn2
&gt;&gt; use_peft: true
&gt;&gt; <span class="hljs-comment"># Data arguments</span>
&gt;&gt; dataset_name: OpenMed/Medical-Reasoning-SFT-GPT-OSS-120B
&gt;&gt; dataset_num_proc: <span class="hljs-number">96</span>
&gt;&gt; <span class="hljs-comment"># Training arguments</span>
&gt;&gt; bf16: true
&gt;&gt; do_eval: false
&gt;&gt; eval_strategy: <span class="hljs-string">&quot;no&quot;</span>
&gt;&gt; gradient_accumulation_steps: <span class="hljs-number">8</span>
&gt;&gt; gradient_checkpointing: true
&gt;&gt; gradient_checkpointing_kwargs:
<span class="hljs-meta">... </span> use_reentrant: false
&gt;&gt; learning_rate: <span class="hljs-number">2.0e-04</span>
&gt;&gt; log_level: info
&gt;&gt; logging_strategy: steps
&gt;&gt; logging_steps: <span class="hljs-number">10</span>
&gt;&gt; lr_scheduler_type: cosine_with_min_lr
&gt;&gt; lr_scheduler_kwargs:
<span class="hljs-meta">... </span> min_lr_rate: <span class="hljs-number">0.1</span>
&gt;&gt; max_grad_norm: <span class="hljs-number">0.2</span>
&gt;&gt; max_length: <span class="hljs-number">24576</span>
&gt;&gt; num_train_epochs: <span class="hljs-number">1</span>
&gt;&gt; output_dir: /opt/ml/model
&gt;&gt; overwrite_output_dir: true
&gt;&gt; packing: true
&gt;&gt; pad_to_multiple_of: <span class="hljs-number">4096</span>
&gt;&gt; per_device_eval_batch_size: <span class="hljs-number">1</span>
&gt;&gt; per_device_train_batch_size: <span class="hljs-number">1</span>
&gt;&gt; seed: <span class="hljs-number">42</span>
&gt;&gt; use_liger_kernel: true
&gt;&gt; warmup_ratio: <span class="hljs-number">0.03</span>
&gt;&gt; <span class="hljs-comment"># Checkpointing</span>
&gt;&gt; save_steps: <span class="hljs-number">100</span>
&gt;&gt; save_strategy: steps
&gt;&gt; save_total_limit: <span class="hljs-number">2</span>
&gt;&gt; <span class="hljs-comment"># Hub &amp; Logging</span>
&gt;&gt; push_to_hub: true
&gt;&gt; hub_strategy: every_save
&gt;&gt; hub_model_id: Qwen3-4B-Instruct-OpenMed <span class="hljs-comment"># &lt;-- UPDATE with OUTPUT_MODEL_NAME from Cell 10</span>
&gt;&gt; run_name: Qwen3-4B-Instruct-OpenMed-SFT <span class="hljs-comment"># &lt;-- UPDATE with OUTPUT_MODEL_NAME from Cell 10</span>
&gt;&gt; report_to: trackio<!-- HTML_TAG_END --></pre></div> <pre data-svelte-h="svelte-1yslspo">Overwriting scripts/sft_config.yaml
</pre> <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=""><!-- HTML_TAG_START -->&gt;&gt; <span class="hljs-comment"># Verify all files created</span>
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Contents of scripts/:&quot;</span>)
&gt;&gt; <span class="hljs-keyword">for</span> f <span class="hljs-keyword">in</span> <span class="hljs-built_in">sorted</span>(source_dir.iterdir()):
<span class="hljs-meta">... </span> <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;<span class="hljs-subst">{f.name}</span>&quot;</span>)<!-- HTML_TAG_END --></pre></div> <pre data-svelte-h="svelte-18v1uj6">Contents of scripts/:
✓ requirements.txt
✓ run_sft.py
✓ sft_config.yaml
</pre> <h2 class="relative group"><a id="-launch-training-job" 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="#-launch-training-job"><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>🚀 Launch Training Job</span></h2> <p data-svelte-h="svelte-4ynnjy">We use the SageMaker <code>ModelTrainer</code> with <code>SourceCode</code> to launch the training job.</p> <p data-svelte-h="svelte-1rg2oxc">The TRL CLI arguments are embedded directly in the entry script to avoid SageMaker/accelerate argument parsing conflicts.</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=""><!-- HTML_TAG_START -->&gt;&gt; <span class="hljs-comment"># Job configuration</span>
&gt;&gt; PROJECT_NAME = <span class="hljs-string">&quot;trl-sft-yaml&quot;</span>
&gt;&gt; BUCKET_NAME = sagemaker_session.default_bucket()
&gt;&gt; S3_OUTPUT_URI = <span class="hljs-string">f&quot;s3://<span class="hljs-subst">{BUCKET_NAME}</span>/<span class="hljs-subst">{PROJECT_NAME}</span>&quot;</span>
&gt;&gt; unique_id = uuid.uuid4().<span class="hljs-built_in">hex</span>[:<span class="hljs-number">8</span>]
&gt;&gt; base_job_name = <span class="hljs-string">f&quot;<span class="hljs-subst">{PROJECT_NAME}</span>-<span class="hljs-subst">{unique_id}</span>&quot;</span>
&gt;&gt; <span class="hljs-comment"># Training image - PyTorch DLC with GPU support</span>
&gt;&gt; TRAINING_IMAGE = <span class="hljs-string">f&quot;763104351884.dkr.ecr.us-east-1.amazonaws.com/huggingface-pytorch-training:2.8.0-transformers4.56.2-gpu-py312-cu129-ubuntu22.04-v1.2&quot;</span>
&gt;&gt; <span class="hljs-comment"># Create ModelTrainer with SourceCode</span>
&gt;&gt; trainer = ModelTrainer(
<span class="hljs-meta">... </span> sagemaker_session=sagemaker_session,
<span class="hljs-meta">... </span> role=role,
<span class="hljs-meta">... </span> training_mode=<span class="hljs-string">&quot;SAGEMAKER_TRAINING_JOB&quot;</span>,
<span class="hljs-meta">... </span> source_code=SourceCode(
<span class="hljs-meta">... </span> source_dir=<span class="hljs-built_in">str</span>(source_dir),
<span class="hljs-meta">... </span> entry_script=<span class="hljs-string">&quot;run_sft.py&quot;</span>,
<span class="hljs-meta">... </span> requirements=<span class="hljs-string">&quot;requirements.txt&quot;</span>,
<span class="hljs-meta">... </span> ),
<span class="hljs-meta">... </span> compute=Compute(
<span class="hljs-meta">... </span> instance_type=INSTANCE_TYPE,
<span class="hljs-meta">... </span> instance_count=<span class="hljs-number">1</span>,
<span class="hljs-meta">... </span> volume_size_in_gb=<span class="hljs-number">200</span>, <span class="hljs-comment"># Larger for 4B model + checkpoints</span>
<span class="hljs-meta">... </span> ),
<span class="hljs-meta">... </span> stopping_condition=StoppingCondition(
<span class="hljs-meta">... </span> max_runtime_in_seconds=<span class="hljs-number">6</span> * <span class="hljs-number">60</span> * <span class="hljs-number">60</span>, <span class="hljs-comment"># 6 hours</span>
<span class="hljs-meta">... </span> ),
<span class="hljs-meta">... </span> output_data_config=OutputDataConfig(
<span class="hljs-meta">... </span> s3_output_path=<span class="hljs-string">f&quot;<span class="hljs-subst">{S3_OUTPUT_URI}</span>/output/&quot;</span>,
<span class="hljs-meta">... </span> ),
<span class="hljs-meta">... </span> base_job_name=base_job_name,
<span class="hljs-meta">... </span> environment={
<span class="hljs-meta">... </span> <span class="hljs-string">&quot;HF_TOKEN&quot;</span>: HF_TOKEN,
<span class="hljs-meta">... </span> <span class="hljs-string">&quot;TRANSFORMERS_VERBOSITY&quot;</span>: <span class="hljs-string">&quot;info&quot;</span>,
<span class="hljs-meta">... </span> <span class="hljs-string">&quot;NCCL_DEBUG&quot;</span>: <span class="hljs-string">&quot;WARN&quot;</span>, <span class="hljs-comment"># For multi-GPU debugging</span>
<span class="hljs-meta">... </span> <span class="hljs-comment"># Trackio logging - https://huggingface.co/docs/trl/en/trackio_integration</span>
<span class="hljs-meta">... </span> <span class="hljs-string">&quot;TRACKIO_SPACE_ID&quot;</span>: <span class="hljs-string">&quot;florentgbelidji/trackio&quot;</span>,
<span class="hljs-meta">... </span> <span class="hljs-string">&quot;TRACKIO_PROJECT&quot;</span>: <span class="hljs-string">&quot;trl-sft&quot;</span>,
<span class="hljs-meta">... </span> <span class="hljs-string">&quot;HF_HUB_ENABLE_HF_TRANSFER&quot;</span>: <span class="hljs-string">&quot;1&quot;</span>,
<span class="hljs-meta">... </span> },
<span class="hljs-meta">... </span> training_image=TRAINING_IMAGE,
<span class="hljs-meta">... </span>)
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Job name: <span class="hljs-subst">{base_job_name}</span>&quot;</span>)
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Instance: <span class="hljs-subst">{INSTANCE_TYPE}</span> (<span class="hljs-subst">{NUM_GPUS}</span>x L40S GPUs)&quot;</span>)
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Output: <span class="hljs-subst">{S3_OUTPUT_URI}</span>&quot;</span>)
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Image: <span class="hljs-subst">{TRAINING_IMAGE.split(<span class="hljs-string">&#x27;/&#x27;</span>)[-<span class="hljs-number">1</span>]}</span>&quot;</span>)
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;\nReady to launch!&quot;</span>)<!-- HTML_TAG_END --></pre></div> <pre data-svelte-h="svelte-1dmol27">Job name: trl-sft-yaml-97d3b6c5
Instance: ml.p4de.24xlarge (8x L40S GPUs)
Output: s3://sagemaker-us-east-1-754289655784/trl-sft-yaml
Image: huggingface-pytorch-training:2.8.0-transformers4.56.2-gpu-py312-cu129-ubuntu22.04-v1.2
Ready to launch!
</pre> <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=""><!-- HTML_TAG_START -->&gt;&gt; <span class="hljs-comment"># Launch the training job</span>
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Launching training job: <span class="hljs-subst">{base_job_name}</span>&quot;</span>)
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Model: <span class="hljs-subst">{MODEL_NAME}</span>&quot;</span>)
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Dataset: <span class="hljs-subst">{DATASET_NAME}</span>&quot;</span>)
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Instance: <span class="hljs-subst">{INSTANCE_TYPE}</span> (<span class="hljs-subst">{NUM_GPUS}</span>x L40S)&quot;</span>)
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;\nUsing: trl sft --config sft_config.yaml&quot;</span>)
&gt;&gt; <span class="hljs-built_in">print</span>()
&gt;&gt; <span class="hljs-comment"># Set wait=True to stream logs, wait=False to run in background</span>
&gt;&gt; trainer.train(wait=<span class="hljs-literal">True</span>)<!-- HTML_TAG_END --></pre></div> <pre data-svelte-h="svelte-1ow53zd">Launching training job: trl-sft-yaml-97d3b6c5
Model: Qwen/Qwen3-4B-Instruct-2507
Dataset: OpenMed/Medical-Reasoning-SFT-GPT-OSS-120B
Instance: ml.p4de.24xlarge (8x L40S)
Using: trl sft --config sft_config.yaml
</pre> <h2 class="relative group"><a id="-monitor-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="#-monitor-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>📊 Monitor Training</span></h2> <p data-svelte-h="svelte-12wqj5">If you launched with <code>wait=False</code>, you can monitor the job here.</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=""><!-- HTML_TAG_START -->&gt;&gt; <span class="hljs-comment"># Get the job name</span>
&gt;&gt; training_job_name = trainer.latest_training_job.name <span class="hljs-keyword">if</span> <span class="hljs-built_in">hasattr</span>(trainer, <span class="hljs-string">&#x27;latest_training_job&#x27;</span>) <span class="hljs-keyword">else</span> base_job_name
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Job name: <span class="hljs-subst">{training_job_name}</span>&quot;</span>)
&gt;&gt; <span class="hljs-comment"># View in console</span>
&gt;&gt; console_url = <span class="hljs-string">f&quot;https://<span class="hljs-subst">{region}</span>.console.aws.amazon.com/sagemaker/home?region=<span class="hljs-subst">{region}</span>#/jobs/<span class="hljs-subst">{training_job_name}</span>&quot;</span>
&gt;&gt; <span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Console: <span class="hljs-subst">{console_url}</span>&quot;</span>)<!-- HTML_TAG_END --></pre></div> <pre data-svelte-h="svelte-lmlvhp">Job name: trl-sft-yaml-dfe9c258
Console: https://us-east-1.console.aws.amazon.com/sagemaker/home?region=us-east-1#/jobs/trl-sft-yaml-dfe9c258
</pre> <h2 class="relative group"><a id="-load-and-test-the-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="#-load-and-test-the-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>🧪 Load and Test the Model</span></h2> <p data-svelte-h="svelte-69dbxo">After training completes, the model is pushed to the Hugging Face Hub.</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=""><!-- HTML_TAG_START --><span class="hljs-comment"># Load the fine-tuned model from Hub</span>
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> pipeline
<span class="hljs-comment"># Your HF username</span>
HF_USERNAME = <span class="hljs-string">&quot;florentgbelidji&quot;</span> <span class="hljs-comment"># Change this!</span>
finetuned_model_id = <span class="hljs-string">f&quot;<span class="hljs-subst">{HF_USERNAME}</span>/<span class="hljs-subst">{OUTPUT_MODEL_NAME}</span>&quot;</span>
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Model will be available at: <span class="hljs-subst">{finetuned_model_id}</span>&quot;</span>)
<span class="hljs-comment"># Uncomment after training completes</span>
<span class="hljs-comment"># pipe = pipeline(</span>
<span class="hljs-comment"># &quot;text-generation&quot;,</span>
<span class="hljs-comment"># model=finetuned_model_id,</span>
<span class="hljs-comment"># torch_dtype=&quot;auto&quot;,</span>
<span class="hljs-comment"># device_map=&quot;auto&quot;,</span>
<span class="hljs-comment"># )</span>
<span class="hljs-comment"># messages = [{&quot;role&quot;: &quot;user&quot;, &quot;content&quot;: &quot;What is the capital of France?&quot;}]</span>
<span class="hljs-comment"># output = pipe(messages, max_new_tokens=100)</span>
<span class="hljs-comment"># print(output[0][&quot;generated_text&quot;][-1][&quot;content&quot;])</span><!-- HTML_TAG_END --></pre></div> <h2 class="relative group"><a id="-cleanup" 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="#-cleanup"><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>🧹 Cleanup</span></h2> <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=""><!-- HTML_TAG_START -->shutil.rmtree(source_dir, ignore_errors=<span class="hljs-literal">True</span>)
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Cleaned up temporary files.&quot;</span>)<!-- HTML_TAG_END --></pre></div> <hr> <blockquote class="tip"><p data-svelte-h="svelte-12riiza">📍 Find the complete example on GitHub <a href="https://github.com/huggingface/hub-docs/tree/main/notebooks/sagemaker-sdk/fine-tune-trl-cli/sagemaker-notebook.ipynb" rel="nofollow">here</a>!</p></blockquote> <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/examples/sagemaker-sdk-fine-tune-trl-cli.mdx" 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_16a821l = {
assets: "/docs/sagemaker/pr_2188/en",
base: "/docs/sagemaker/pr_2188/en",
env: {}
};
const element = document.currentScript.parentElement;
const data = [null,null];
Promise.all([
import("/docs/sagemaker/pr_2188/en/_app/immutable/entry/start.48a18f09.js"),
import("/docs/sagemaker/pr_2188/en/_app/immutable/entry/app.8481cdda.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 9],
data,
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
75.4 kB
·
Xet hash:
3e37c9e457911baa87b49a7b165f73a67a4543d379c95cce30407e6e24299aa2

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