使用 Amazon SageMaker 偵錯工具 Python 模組設定具有基本效能分析參數的估算器 - Amazon SageMaker

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

使用 Amazon SageMaker 偵錯工具 Python 模組設定具有基本效能分析參數的估算器

依預設, SageMaker 偵錯工具基本分析預設為開啟,並監控使用 Amazon SageMaker Python SDK 提交之所有 SageMaker 訓練任務的資源使用率指標,例如 CPU 使用率、GPU 使用率、GPU 記憶體使用率、網路和 I/O 等待時間。 SageMaker 調試器收集這些資源使用率指標每 500 毫秒。您不需要在程式碼、訓練指令碼或任務啟動器中進行任何其他變更,即可追蹤基本資源使用率。如果您想要在 SageMaker Studio 中存取訓練工作的資源使用率指標儀表板,可以跳至Amazon SageMaker 工作室經典實驗 Amazon SageMaker 調試器 UI.

如果您想要變更基本效能分析的指標收集間隔,可以在使用 SageMaker Python SDK、或 AWS Command Line Interface (CLI) 建立 SageMaker 訓練工作啟動器時指定除錯器特定的參數。 AWS SDK for Python (Boto3)在本指南中,我們專注於如何使用 Amazon SageMaker Python 開發套件變更效能分析選項。

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

重要

若要使用最新的 SageMaker 除錯程式功能,您需要升級 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)

使用 Python SDK 中的除錯程式 Python 模組設定 SageMaker 估算 SageMaker 器物件的程式碼範本 SageMaker

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

注意

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

PyTorch
# An example of constructing a SageMaker 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 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 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 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 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 參數,請參閱為系統資源使用率的基本分析進行設定配置架構分析

  • rules— 設定此參數以啟動您要並行執 parallel 的 SageMaker 偵錯工具內建規則。請確定您的訓練任務可存取此 S3 儲存貯體。這些規則會在處理容器上執行,並自動分析您的訓練工作,以找出運算和作業效能問題。ProfilerReport 規則是最完整的規則,可執行所有內建分析規則,並將分析結果以報告的形式儲存至安全的 S3 儲存貯體。若要了解如何設定 rules 參數,請參閱設定由 Amazon 偵錯工具管理的內建效能分析工具規則 SageMaker

注意

偵錯工具將輸出資料安全地儲存在預設的 S3 儲存貯體的子資料夾內。例如,預設 S3 儲存貯體 URI 的格式為 s3://sagemaker-<region>-<12digit_account_id>/<base-job-name>/<debugger-subfolders>/。偵錯工具建立的子資料夾有三個:debug-outputprofiler-outputrule-output。您也可以使用SageMaker 估算器類別方法擷取預設的 S3 儲存貯體 URI。

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