

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

# 使用 Amazon SageMaker Debugger Python 設定基本分析的估算器組態及參數
<a name="debugger-configuration-for-profiling"></a>

根據預設值，SageMaker Debugger 基本分析預設為開啟，並監控使用 [Amazon SageMaker Python SDK](https://sagemaker.readthedocs.io/en/stable) 提交的所有 SageMaker 訓練工作的資源使用率指標，例如 CPU 利用率、GPU 使用率、GPU 記憶體使用率、網路和 I/O 等待時間。SageMaker Debugger 會每 500 毫秒收集一次這些資源使用率指標。您不需要在程式碼、訓練指令碼或任務啟動器中進行任何其他變更，即可追蹤基本資源使用率。如果您想要變更基本分析的指標收集間隔，您可以在使用 SageMaker Python SDK 適用於 Python (Boto3) 的 AWS SDK或 AWS Command Line Interface (CLI) 建立 SageMaker 訓練任務啟動器時指定 Debugger 特定參數。在本指南中，我們聚焦在如何使用 [Amazon SageMaker Python SDK](https://sagemaker.readthedocs.io/en/stable) 變更分析選項。此頁面提供用於設定此估算器物件的參考範本。

如果您想要在 SageMaker Studio 中存取訓練工作的資源使用率指標儀表板，您可以跳至 [Amazon SageMaker Studio Classic 實驗中的 Amazon SageMaker Debugger 使用者介面](debugger-on-studio.md)。

如果您想要自動啟動偵測系統資源使用率問題的規則，您可以在估算器物件中新增 `rules` 參數以啟動規則。

**重要**  
若要使用最新的 SageMaker Debugger 功能，您需要升級 SageMaker Python SDK 和 `SMDebug` 用戶端程式庫。在您的 IPython 核心內，Jupyter 筆記本或 JupyterLab 環境中，執行以下程式碼以安裝最新版本的程式庫並重新啟動核心。  

```
import sys
import IPython
!{sys.executable} -m pip install -U sagemaker smdebug
IPython.Application.instance().kernel.do_shutdown(True)
```

## 程式碼範本，用於以 SageMaker AI Python SDK 內的 SageMaker Debugger Python 模組設定 SageMaker AI 估算器物件
<a name="debugger-configuration-structure-profiler"></a>

若要調整基本設定檔組態 (`profiler_config`) 或新增剖析工具規則 (`rules`)，請選擇一個標籤以取得用於設定 SageMaker AI 估計器的範本。在後續頁面中，您可以找到如何設定這兩個參數的更多相關資訊。

**注意**  
下列程式碼範例無法直接執行。繼續閱讀下一節，了解如何設定每個參數。

------
#### [ PyTorch ]

```
# An example of constructing a SageMaker AI PyTorch estimator
import boto3
import sagemaker
from sagemaker.pytorch import PyTorch
from sagemaker.debugger import ProfilerConfig, ProfilerRule, rule_configs

session=boto3.session.Session()
region=session.region_name

profiler_config={{ProfilerConfig(...)}}
rules=[
    {{ProfilerRule.sagemaker(rule_configs.BuiltInRule())}}
]

estimator=PyTorch(
    entry_point="{{directory/to/your_training_script.py}}",
    role=sagemaker.get_execution_role(),
    base_job_name="{{debugger-profiling-demo}}",
    instance_count={{1}},
    instance_type="{{ml.p3.2xlarge}}",
    framework_version="{{1.12.0}}",
    py_version="{{py37}}",
    
    # SageMaker Debugger parameters
    profiler_config=profiler_config,
    rules=rules
)

estimator.fit(wait=False)
```

------
#### [ TensorFlow ]

```
# An example of constructing a SageMaker AI TensorFlow estimator
import boto3
import sagemaker
from sagemaker.tensorflow import TensorFlow
from sagemaker.debugger import ProfilerConfig, ProfilerRule, rule_configs

session=boto3.session.Session()
region=session.region_name

profiler_config={{ProfilerConfig(...)}}
rules=[
    {{ProfilerRule.sagemaker(rule_configs.BuiltInRule())}}
]

estimator=TensorFlow(
    entry_point="{{directory/to/your_training_script.py}}",
    role=sagemaker.get_execution_role(),
    base_job_name="{{debugger-profiling-demo}}",
    instance_count={{1}},
    instance_type="{{ml.p3.2xlarge}}",
    framework_version="{{2.8.0}}",
    py_version="{{py37}}",
    
    # SageMaker Debugger parameters
    profiler_config=profiler_config,
    rules=rules
)

estimator.fit(wait=False)
```

------
#### [ MXNet ]

```
# An example of constructing a SageMaker AI MXNet estimator
import sagemaker
from sagemaker.mxnet import MXNet
from sagemaker.debugger import ProfilerConfig, ProfilerRule, rule_configs

profiler_config={{ProfilerConfig(...)}}
rules=[
    {{ProfilerRule.sagemaker(rule_configs.BuiltInRule())}}
]

estimator=MXNet(
    entry_point="{{directory/to/your_training_script.py}}",
    role=sagemaker.get_execution_role(),
    base_job_name="{{debugger-profiling-demo}}",
    instance_count={{1}},
    instance_type="{{ml.p3.2xlarge}}",
    framework_version="{{1.7.0}}",
    py_version="{{py37}}",
    
    # SageMaker Debugger parameters
    profiler_config=profiler_config,
    rules=rules
)

estimator.fit(wait=False)
```

**注意**  
對於 MXNet，在設定 `profiler_config` 參數時，您只能針對系統監控進行設定。MXNet 不支援分析架構指標。

------
#### [ XGBoost ]

```
# An example of constructing a SageMaker AI XGBoost estimator
import sagemaker
from sagemaker.xgboost.estimator import XGBoost
from sagemaker.debugger import ProfilerConfig, ProfilerRule, rule_configs

profiler_config={{ProfilerConfig(...)}}
rules=[
    {{ProfilerRule.sagemaker(rule_configs.BuiltInRule())}}
]

estimator=XGBoost(
    entry_point="{{directory/to/your_training_script.py}}",
    role=sagemaker.get_execution_role(),
    base_job_name="{{debugger-profiling-demo}}",
    instance_count={{1}},
    instance_type="{{ml.p3.2xlarge}}",
    framework_version="{{1.5-1}}",

    # Debugger-specific parameters
    profiler_config=profiler_config,
    rules=rules
)

estimator.fit(wait=False)
```

**注意**  
對於 XGBoost，在設定 `profiler_config` 參數時，您只能針對設定系統監控進行設定。XGBoost 不支援分析架構指標。

------
#### [ Generic estimator ]

```
# An example of constructing a SageMaker AI generic estimator using the XGBoost algorithm base image
import boto3
import sagemaker
from sagemaker.estimator import Estimator
from sagemaker import image_uris
from sagemaker.debugger import ProfilerConfig, DebuggerHookConfig, Rule, ProfilerRule, rule_configs

profiler_config={{ProfilerConfig(...)}}
rules=[
    {{ProfilerRule.sagemaker(rule_configs.BuiltInRule())}}
]

region=boto3.Session().region_name
xgboost_container=sagemaker.image_uris.retrieve("xgboost", region, "1.5-1")

estimator=Estimator(
    role=sagemaker.get_execution_role()
    image_uri=xgboost_container,
    base_job_name="{{debugger-demo}}",
    instance_count={{1}},
    instance_type="{{ml.m5.2xlarge}}",
    
    # Debugger-specific parameters
    profiler_config=profiler_config,
    rules=rules
)

estimator.fit(wait=False)
```

------

以下提供參數的簡短描述。
+ `profiler_config`—設定偵錯工具，以從訓練任務收集系統指標和架構指標，並儲存至安全的 S3 儲存貯體 URI 或本機機器中。您可以設定收集系統指標的頻率頻繁與否。若要了解如何設定 `profiler_config` 參數，請參閱[為系統資源使用率的基本分析進行設定](debugger-configure-system-monitoring.md)和[架構分析的估算器組態](debugger-configure-framework-profiling.md)。
+ `rules`—設定此參數以啟動您要平行執行的 SageMaker Debugger 內建規則。請確定您的訓練任務可存取此 S3 儲存貯體。這些規則會在處理容器上執行，並自動分析您的訓練工作，以找出運算和作業效能問題。[ProfilerReport](debugger-built-in-profiler-rules.md#profiler-report) 規則是最完整的規則，可執行所有內建分析規則，並將分析結果以報告的形式儲存至安全的 S3 儲存貯體。若要了解如何設定 `rules` 參數，請參閱[使用透過 Amazon SageMaker Debugger 管理的內建剖析工具規則](use-debugger-built-in-profiler-rules.md)。

**注意**  
偵錯工具將輸出資料安全地儲存在預設的 S3 儲存貯體的子資料夾內。例如，預設 S3 儲存貯體 URI 的格式為 `s3://sagemaker-<region>-<12digit_account_id>/<base-job-name>/<debugger-subfolders>/`。偵錯工具建立的子資料夾有三個：`debug-output`、`profiler-output` 和 `rule-output`。您還可以使用 [SageMaker AI 估算器類別方法](debugger-estimator-classmethods.md)擷取預設的 S3 儲存貯體 URI。

請參閱下列主題，深入了解如何設定偵錯工具特定參數的詳細資訊。

**Topics**
+ [程式碼範本，用於以 SageMaker AI Python SDK 內的 SageMaker Debugger Python 模組設定 SageMaker AI 估算器物件](#debugger-configuration-structure-profiler)
+ [為系統資源使用率的基本分析進行設定](debugger-configure-system-monitoring.md)
+ [架構分析的估算器組態](debugger-configure-framework-profiling.md)
+ [訓練工作正在執行時更新偵錯工具系統監控和架構分析組態](debugger-update-monitoring-profiling.md)
+ [關閉偵錯工具](debugger-turn-off-profiling.md)