

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 SageMaker Profiler 準備並執行訓練任務
<a name="profiler-prepare"></a>

使用 SageMaker Profiler 設定執行訓練任務包含兩個步驟：調整訓練指令碼及設定 SageMaker 訓練任務啟動器。

**Topics**
+ [步驟 1：使用 SageMaker Profiler Python 模組調整訓練指令碼](#profiler-prepare-training-script)
+ [步驟 2：建立 SageMaker AI 架構估算器並啟用 SageMaker Profiler](#profiler-profilerconfig)
+ [(選用) 安裝 SageMaker Profiler Python 套件](#profiler-install-python-package)

## 步驟 1：使用 SageMaker Profiler Python 模組調整訓練指令碼
<a name="profiler-prepare-training-script"></a>

若要在訓練任務執行時開始擷取 GPU 執行的核心，請使用 SageMaker Profiler Python 模組修改訓練指令碼。匯入程式庫並新增 `start_profiling()` 及 `stop_profiling()` 方法以定義分析的開始與結束。您也可以使用選擇性的自訂註釋，在訓練指令碼新增標記，以便在每個步驟的特定作業期間視覺化硬體活動。

請注意，註釋器會從 GPU 擷取作業。對於 CPU 的分析作業，您不需要新增任何其他的註釋。當您指定分析設定時，也會啟用 CPU 分析，您將在 [步驟 2：建立 SageMaker AI 架構估算器並啟用 SageMaker Profiler](#profiler-profilerconfig) 練習該設定。

**注意**  
分析整個訓練任務並不是資源的最有效利用方式。我們建議對訓練任務的最多 300 個步驟進行分析。

**重要**  
[2023 年 12 月 14 日](profiler-release-notes.md#profiler-release-notes-20231214) 上的版本有重大變更。SageMaker Profiler Python 套件名稱已從 `smppy` 變更為 `smprof`。這在 TensorFlow v2.12 和更新版本的 [SageMaker AI 架構容器](https://github.com/aws/deep-learning-containers/blob/master/available_images.md#sagemaker-framework-containers-sm-support-only)中很有效。  
如果您使用舊版 [SageMaker AI 架構容器](https://github.com/aws/deep-learning-containers/blob/master/available_images.md#sagemaker-framework-containers-sm-support-only)，例如 TensorFlow v2.11.0，則 SageMaker Profiler Python 套件仍可作為 `smppy` 使用。如果您不確定應使用哪個版本或套件名稱，請以下列程式碼片段取代 SageMaker Profiler 套件的匯入陳述式。  

```
try:
    import smprof 
except ImportError:
    # backward-compatability for TF 2.11 and PT 1.13.1 images
    import smppy as smprof
```

**方法 1.** 使用關聯內容管理工具 `smprof.annotate` 來註釋完整的函式

您可以透過 `smprof.annotate()` 關聯內容管理工具包裝完整的函式。如果您想按函式而不是程式碼行進行分析，建議使用此包裝函式。下列範例指令碼顯示如何實作上下文管理工具，以便在每次反覆運算包裝訓練循環及完整函式。

```
import smprof

SMProf = smprof.SMProfiler.instance()
config = smprof.Config()
config.profiler = {
    "EnableCuda": "1",
}
SMProf.configure(config)
SMProf.start_profiling()

for epoch in range(args.epochs):
    if world_size > 1:
        sampler.set_epoch(epoch)
    tstart = time.perf_counter()
    for i, data in enumerate(trainloader, 0):
        with smprof.annotate("step_"+str(i)):
            inputs, labels = data
            inputs = inputs.to("cuda", non_blocking=True)
            labels = labels.to("cuda", non_blocking=True)
    
            optimizer.zero_grad()
    
            with smprof.annotate("Forward"):
                outputs = net(inputs)
            with smprof.annotate("Loss"):
                loss = criterion(outputs, labels)
            with smprof.annotate("Backward"):
                loss.backward()
            with smprof.annotate("Optimizer"):
                optimizer.step()

SMProf.stop_profiling()
```

**方法 2.** 使用 `smprof.annotation_begin()` 及 `smprof.annotation_end()` 註釋函式的特定程式碼行

您也可以定義註釋以分析特定的程式碼行。您可以在個別程式碼行的層級 (而不是函式) 設定準確的分析起點與終點。例如，在下面的指令碼，`step_annotator` 在每次反覆運算開始時定義，並在反覆運算結束時結束。同時，定義每個操作的其他詳細註釋器，並在每次反覆運算過程圍繞目標操作。

```
import smprof

SMProf = smprof.SMProfiler.instance()
config = smprof.Config()
config.profiler = {
    "EnableCuda": "1",
}
SMProf.configure(config)
SMProf.start_profiling()

for epoch in range(args.epochs):
    if world_size > 1:
        sampler.set_epoch(epoch)
    tstart = time.perf_counter()
    for i, data in enumerate(trainloader, 0):
        step_annotator = smprof.annotation_begin("step_" + str(i))

        inputs, labels = data
        inputs = inputs.to("cuda", non_blocking=True)
        labels = labels.to("cuda", non_blocking=True)
        optimizer.zero_grad()

        forward_annotator = smprof.annotation_begin("Forward")
        outputs = net(inputs)
        smprof.annotation_end(forward_annotator)

        loss_annotator = smprof.annotation_begin("Loss")
        loss = criterion(outputs, labels)
        smprof.annotation_end(loss_annotator)

        backward_annotator = smprof.annotation_begin("Backward")
        loss.backward()
        smprof.annotation_end(backward_annotator)

        optimizer_annotator = smprof.annotation_begin("Optimizer")
        optimizer.step()
        smprof.annotation_end(optimizer_annotator)

        smprof.annotation_end(step_annotator)

SMProf.stop_profiling()
```

註釋並設定分析工具啟動模組後，請在以下步驟 2 使用 SageMaker 訓練任務啟動器儲存要提交的指令碼。範例啟動器假設訓練指令碼已命名為 `train_with_profiler_demo.py`。

## 步驟 2：建立 SageMaker AI 架構估算器並啟用 SageMaker Profiler
<a name="profiler-profilerconfig"></a>

以下程序說明如何準備 SageMaker AI 架構估算器以使用 SageMaker Python SDK 進行訓練。

1. 使用 `ProfilerConfig` 與 `Profiler` 模組設定 `profiler_config` 物件，如下所示。

   ```
   from sagemaker import ProfilerConfig, Profiler
   profiler_config = ProfilerConfig(
       profile_params = Profiler(cpu_profiling_duration=3600)
   )
   ```

   以下是 `Profiler` 模組及其參數的描述。
   +  `Profiler`：用於透過訓練任務啟動 SageMaker Profiler 的模組。
     +  `cpu_profiling_duration` (int)：指定在 CPU 進行分析的持續時間 (以秒為單位)。預設為 3600 秒。

1. 使用在上一步建立的 `profiler_config` 物件建立 SageMaker AI 架構估算器。下列程式碼顯示建立 PyTorch 估算器的範例。如果您要建立 TensorFlow 估算器，請匯入 `sagemaker.tensorflow.TensorFlow`，然後指定 SageMaker Profiler支援的其中一個 [TensorFlow 版本](profiler-support.md#profiler-support-frameworks-tensorflow)。如需支援的架構及執行個體類型詳細資訊，請參閱[預先安裝 SageMaker Profiler 的 SageMaker AI 架構映像](profiler-support.md#profiler-support-frameworks)。

   ```
   import sagemaker
   from sagemaker.pytorch import PyTorch
   
   estimator = PyTorch(
       framework_version="2.0.0",
       role=sagemaker.get_execution_role(),
       entry_point="train_with_profiler_demo.py", # your training job entry point
       source_dir=source_dir, # source directory for your training script
       output_path=output_path,
       base_job_name="sagemaker-profiler-demo",
       hyperparameters=hyperparameters, # if any
       instance_count=1, # Recommended to test with < 8
       instance_type=ml.p4d.24xlarge,
       profiler_config=profiler_config
   )
   ```

1. 透過執行 `fit` 方法以啟動訓練任務。使用 `wait=False`，您可以靜音訓練任務日誌，並讓它在背景執行。

   ```
   estimator.fit(wait=False)
   ```

執行訓練任務時或作業完成後，您可以前往 [開啟 SageMaker Profiler 使用者介面應用程式](profiler-access-smprofiler-ui.md) 的下一個主題，並開始探索並視覺化儲存的設定檔。

如果您想要直接存取儲存在 Amazon S3 儲存貯體的設定檔資料，請使用下列指令碼擷取 S3 URI。

```
import os
# This is an ad-hoc function to get the S3 URI
# to where the profile output data is saved
def get_detailed_profiler_output_uri(estimator):
    config_name = None
    for processing in estimator.profiler_rule_configs:
        params = processing.get("RuleParameters", dict())
        rule = config_name = params.get("rule_to_invoke", "")
        if rule == "DetailedProfilerProcessing":
            config_name = processing.get("RuleConfigurationName")
            break
    return os.path.join(
        estimator.output_path, 
        estimator.latest_training_job.name, 
        "rule-output",
        config_name,
    )

print(
    f"Profiler output S3 bucket: ", 
    get_detailed_profiler_output_uri(estimator)
)
```

## (選用) 安裝 SageMaker Profiler Python 套件
<a name="profiler-install-python-package"></a>

若要在未列於 [預先安裝 SageMaker Profiler 的 SageMaker AI 架構映像](profiler-support.md#profiler-support-frameworks) 的 PyTorch 或 TensorFlow 架構映像上使用 SageMaker Profiler，或在自有自訂 Docker 容器上用於訓練，您可以使用其中一個 [SageMaker Profiler Python 套件二進位檔案](profiler-support.md#profiler-python-package) 來安裝 SageMaker Profiler。

**選項 1：啟動訓練任務時安裝 SageMaker Profiler 套件**

如果您要使用未列於 [預先安裝 SageMaker Profiler 的 SageMaker AI 架構映像](profiler-support.md#profiler-support-frameworks) 的 PyTorch 或 TensorFlow 映像，來使用 SageMaker Profiler 進行訓練任務，請建立 `requirements.txt` 檔案，並在[步驟 2](#profiler-profilerconfig) 中指定 SageMaker AI 架構估算器的 `source_dir` 參數路徑下找到該檔案。如需更多設定 `requirements.txt` 檔案的一般詳細資訊，請參閱 *SageMaker Python SDK 文件*中的[使用第三方程式庫](https://sagemaker.readthedocs.io/en/stable/frameworks/pytorch/using_pytorch.html#using-third-party-libraries)。在 `requirements.txt` 檔案中，為 [SageMaker Profiler Python 套件二進位檔案](profiler-support.md#profiler-python-package) 新增其中一個 S3 儲存貯體路徑。

```
# requirements.txt
https://smppy.s3.amazonaws.com/tensorflow/cu112/smprof-0.3.332-cp39-cp39-linux_x86_64.whl
```

**選項 2：在您的自訂 Docker 容器中安裝 SageMaker Profiler 套件**

如果您使用自訂 Docker 容器進行訓練，請將其中一個 [SageMaker Profiler Python 套件二進位檔案](profiler-support.md#profiler-python-package) 新增至您的 Docker 檔案。

```
# Install the smprof package version compatible with your CUDA version
RUN pip install https://smppy.s3.amazonaws.com/tensorflow/cu112/smprof-0.3.332-cp39-cp39-linux_x86_64.whl
```

如需了解如何在 SageMaker AI 上執行自訂 Docker 容器進行訓練，請參閱[調整自有訓練容器](https://docs.aws.amazon.com/sagemaker/latest/dg/adapt-training-container.html)。