本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 SageMaker Training Compiler 執行 PyTorch 訓練任務
您可以使用任何 SageMaker AI 介面,透過 SageMaker Training Compiler 執行訓練任務:Amazon SageMaker Studio Classic AWS SDK for Python (Boto3)、Amazon SageMaker 筆記本執行個體,以及 AWS Command Line Interface。
使用 SageMaker Python SDK
SageMaker Training Compiler for PyTorch 可透過 SageMaker AI PyTorch
和HuggingFace
架構估算器類別取得。若要開啟 SageMaker Training Compiler,請將 compiler_config
參數新增至 SageMaker AI 估算器。匯入 TrainingCompilerConfig
類別並將其執行個體傳遞至 compiler_config
參數。下列程式碼範例顯示開啟 SageMaker Training Compiler 的 SageMaker AI 估算器類別結構。
如要開始使用 PyTorch 或轉換器提供的預建置模型,請嘗試使用 測試過的模型 參考表中提供的批次大小。
原生 PyTorch 支援可在 SageMaker Python SDK 版本 2.121.0 及較新版本中取得。請確認您已就此更新 SageMaker Python SDK。
從 PyTorch 版本 1.12.0 開始,可以使用適用於 PyTorch 的 SageMaker Training Compiler 容器。請注意,適用於 PyTorch 的 SageMaker Training Compiler 容器不會與 Hugging Face 轉換器一起預先包裝。如需在容器中安裝程式庫,請務必在提交訓練任務時將 requirements.txt
檔案新增至來源目錄下。
對於 PyTorch 版本 1.11.0 和之前的版本,請針對 Hugging Face 和 PyTorch 使用先前版本的 SageMaker Training Compiler 容器。
如需架構版本和相應容器資訊的完整清單,請參閱支援的架構。
如需符合您使用案例的資訊,請參閱下列其中一個選項。
- PyTorch v1.12.0 and later
-
若要編譯和訓練 PyTorch 模型,請使用 SageMaker Training Compiler 設定 SageMaker AI PyTorch 估算器,如下列程式碼範例所示。
此原生 PyTorch 支援可在 SageMaker AI Python SDK 2.120.0 版及更新版本中取得。請務必更新 SageMaker AI Python SDK。
from sagemaker.pytorch import PyTorch, TrainingCompilerConfig
# the original max batch size that can fit into GPU memory without compiler
batch_size_native=12
learning_rate_native=float('5e-5
')
# an updated max batch size that can fit into GPU memory with compiler
batch_size=64
# update learning rate
learning_rate=learning_rate_native/batch_size_native*batch_size
hyperparameters={
"n_gpus": 1,
"batch_size": batch_size,
"learning_rate": learning_rate
}
pytorch_estimator=PyTorch(
entry_point='train.py
',
source_dir='path-to-requirements-file
', # Optional. Add this if need to install additional packages.
instance_count=1,
instance_type='ml.p3.2xlarge
',
framework_version='1.13.1
',
py_version='py3',
hyperparameters=hyperparameters,
compiler_config=TrainingCompilerConfig(),
disable_profiler=True,
debugger_hook_config=False
)
pytorch_estimator.fit()
- Hugging Face Transformers with PyTorch v1.11.0 and before
-
若要使用 PyTorch 編譯和訓練轉換器模型,請使用 SageMaker Training Compiler 設定 SageMaker AI Hugging Face 估算器,如下列程式碼範例所示。
from sagemaker.huggingface import HuggingFace, TrainingCompilerConfig
# the original max batch size that can fit into GPU memory without compiler
batch_size_native=12
learning_rate_native=float('5e-5
')
# an updated max batch size that can fit into GPU memory with compiler
batch_size=64
# update learning rate
learning_rate=learning_rate_native/batch_size_native*batch_size
hyperparameters={
"n_gpus": 1,
"batch_size": batch_size,
"learning_rate": learning_rate
}
pytorch_huggingface_estimator=HuggingFace(
entry_point='train.py
',
instance_count=1,
instance_type='ml.p3.2xlarge
',
transformers_version='4.21.1
',
pytorch_version='1.11.0
',
hyperparameters=hyperparameters,
compiler_config=TrainingCompilerConfig(),
disable_profiler=True,
debugger_hook_config=False
)
pytorch_huggingface_estimator.fit()
如需準備訓練指令碼的說明,請參閱下列頁面。
如需端對端範例,請參閱下列筆記本:
- PyTorch v1.12
-
對於 PyTorch 1.12 版,您可以透過將指定pytorch_xla
選項新增至 SageMaker AI PyTorch 估算器類別的 distribution
參數,以使用 SageMaker Training Compiler 執行分散式訓練。
此原生 PyTorch 支援可在 SageMaker AI Python SDK 2.121.0 版及更新版本中取得。請務必更新 SageMaker AI Python SDK。
from sagemaker.pytorch import PyTorch, TrainingCompilerConfig
# choose an instance type, specify the number of instances you want to use,
# and set the num_gpus variable the number of GPUs per instance.
instance_count=1
instance_type='ml.p3.8xlarge
'
num_gpus=4
# the original max batch size that can fit to GPU memory without compiler
batch_size_native=16
learning_rate_native=float('5e-5
')
# an updated max batch size that can fit to GPU memory with compiler
batch_size=26
# update learning rate
learning_rate=learning_rate_native/batch_size_native*batch_size*num_gpus*instance_count
hyperparameters={
"n_gpus": num_gpus,
"batch_size": batch_size,
"learning_rate": learning_rate
}
pytorch_estimator=PyTorch(
entry_point='your_training_script.py
',
source_dir='path-to-requirements-file
', # Optional. Add this if need to install additional packages.
instance_count=instance_count,
instance_type=instance_type,
framework_version='1.13.1
',
py_version='py3',
hyperparameters=hyperparameters,
compiler_config=TrainingCompilerConfig(),
distribution ={'pytorchxla' : { 'enabled': True }},
disable_profiler=True,
debugger_hook_config=False
)
pytorch_estimator.fit()
- Transformers v4.21 with PyTorch v1.11
-
對於 PyTorch 版本 1.11 及較新版本,SageMaker Training Compiler 可用於使用指定給 distribution
參數的 pytorch_xla
選項進行分散式訓練。
from sagemaker.huggingface import HuggingFace, TrainingCompilerConfig
# choose an instance type, specify the number of instances you want to use,
# and set the num_gpus variable the number of GPUs per instance.
instance_count=1
instance_type='ml.p3.8xlarge
'
num_gpus=4
# the original max batch size that can fit to GPU memory without compiler
batch_size_native=16
learning_rate_native=float('5e-5
')
# an updated max batch size that can fit to GPU memory with compiler
batch_size=26
# update learning rate
learning_rate=learning_rate_native/batch_size_native*batch_size*num_gpus*instance_count
hyperparameters={
"n_gpus": num_gpus,
"batch_size": batch_size,
"learning_rate": learning_rate
}
pytorch_huggingface_estimator=HuggingFace(
entry_point='your_training_script.py
',
instance_count=instance_count,
instance_type=instance_type,
transformers_version='4.21.1
',
pytorch_version='1.11.0
',
hyperparameters=hyperparameters,
compiler_config=TrainingCompilerConfig(),
distribution ={'pytorchxla' : { 'enabled': True }},
disable_profiler=True,
debugger_hook_config=False
)
pytorch_huggingface_estimator.fit()
- Transformers v4.17 with PyTorch v1.10.2 and before
-
對於支援的 PyTorch 版本 1.10.2 及之前版本,SageMaker Training Compiler 需要一個替代機制來啟動分散式訓練任務。若要執行分散式訓練,SageMaker Training Compiler 會要求您將 SageMaker AI 分散式訓練啟動器指令碼傳遞至entry_point
引數,並將訓練指令碼傳遞至hyperparameters
引數。下列程式碼範例示範如何設定套用所需變更的 SageMaker AI Hugging Face 估算器。
from sagemaker.huggingface import HuggingFace, TrainingCompilerConfig
# choose an instance type, specify the number of instances you want to use,
# and set the num_gpus variable the number of GPUs per instance.
instance_count=1
instance_type='ml.p3.8xlarge
'
num_gpus=4
# the original max batch size that can fit to GPU memory without compiler
batch_size_native=16
learning_rate_native=float('5e-5
')
# an updated max batch size that can fit to GPU memory with compiler
batch_size=26
# update learning rate
learning_rate=learning_rate_native/batch_size_native*batch_size*num_gpus*instance_count
training_script="your_training_script.py
"
hyperparameters={
"n_gpus": num_gpus,
"batch_size": batch_size,
"learning_rate": learning_rate,
"training_script": training_script # Specify the file name of your training script.
}
pytorch_huggingface_estimator=HuggingFace(
entry_point='distributed_training_launcher.py
', # Specify the distributed training launcher script.
instance_count=instance_count,
instance_type=instance_type,
transformers_version='4.17.0
',
pytorch_version='1.10.2
',
hyperparameters=hyperparameters,
compiler_config=TrainingCompilerConfig(),
disable_profiler=True,
debugger_hook_config=False
)
pytorch_huggingface_estimator.fit()
啟動器指令碼看起來應該如下所示。它會封裝您的訓練指令碼,並根據您所選的訓練執行個體大小來設定分散式訓練環境。
# distributed_training_launcher.py
#!/bin/python
import subprocess
import sys
if __name__ == "__main__":
arguments_command = " ".join([arg for arg in sys.argv[1:]])
"""
The following line takes care of setting up an inter-node communication
as well as managing intra-node workers for each GPU.
"""
subprocess.check_call("python -m torch_xla.distributed.sm_dist " + arguments_command, shell=True)
下列清單是使用編譯器執行 SageMaker 訓練任務所需的最小參數集。
使用 SageMaker AI Hugging Face 估算器時,您必須指定 transformers_version
、hyperparameters
、 pytorch_version
和 compiler_config
參數,以啟用 SageMaker Training Compiler。您無法使用 image_uri
手動指定列於 支援的架構 的 Training Compiler 整合式深度學習容器。
-
entry_point
(str) — 必要條件。指定訓練指令碼的檔案名稱。
若要使用 SageMaker Training Compiler 和 PyTorch 版本 1.10.2 及之前版本執行分散式訓練,請為此參數指定啟動器指令碼的檔案名稱。您應準備好啟動器指令碼,以包裝您的訓練指令碼並配置分散式訓練環境。如需詳細資訊,請參閱下列範例筆記本:
-
source_dir
(str) — 選用。如需安裝其他套件,請新增此項目。如要安裝套件,您需要在此目錄下備妥一個 requirements.txt
檔案。
-
instance_count
(int) — 必要條件。指定執行個體數目。
-
instance_type
(str) — 必要條件。指定執行個體類型。
-
transformers_version
(str) – 只有在使用 SageMaker AI Hugging Face 估算器時才需要。指定 SageMaker Training Compiler 支援的 Hugging Face 轉換器程式庫版本。若要尋找可用版本,請參閱支援的架構。
-
framework_version
或 pytorch_version
(str) — 必要條件。指定 SageMaker Training Compiler 支援的 PyTorch 版本。若要尋找可用版本,請參閱支援的架構。
使用 SageMaker AI Hugging Face 估算器時,您必須同時指定 transformers_version
和 pytorch_version
。
-
hyperparameters
(dict) — 選用。指定訓練任務的超參數,例如 n_gpus
、batch_size
和 learning_rate
。啟用 SageMaker Training Compiler 時,請嘗試較大的批次大小並相應地調整學習速率。若要尋找使用編譯器和調整批次大小以改善訓練速度的案例研究,請參閱測試過的模型和SageMaker Training Compiler 範例筆記本與部落格。
若要使用 SageMaker Training Compiler 和 PyTorch 版本 1.10.2 及之前版本執行分散式訓練,您需要新增其他參數 ("training_script"
),以指定訓練指令碼,如前面的程式碼範例所示。
-
compiler_config
(TrainingCompilerConfig 物件) — 啟動 SageMaker Training Compiler 時所需。納入此參數,以開啟 SageMaker Training Compiler。下列是 TrainingCompilerConfig
類型的參數。
-
distribution
(dict) — 選用。若要使用 SageMaker Training Compiler 執行分散式訓練任務,請新增 distribution = { 'pytorchxla' : {
'enabled': True }}
。
若開啟 SageMaker Debugger,可能會影響 SageMaker Training Compiler 的效能。我們建議您在執行 SageMaker Training Compiler 時關閉偵錯工具,以確保不會影響效能。如需詳細資訊,請參閱考量事項。若要關閉偵錯工具功能,請將下列兩個引數新增至估算器:
disable_profiler=True,
debugger_hook_config=False
如果成功啟動使用編譯器的訓練任務,您會在任務初始化階段接收到下列日誌:
-
搭配 TrainingCompilerConfig(debug=False)
Found configuration for Training Compiler
Configuring SM Training Compiler...
-
搭配 TrainingCompilerConfig(debug=True)
Found configuration for Training Compiler
Configuring SM Training Compiler...
Training Compiler set to debug mode
使用 SageMaker AI CreateTrainingJob
API 操作
SageMaker Training Compiler 組態選項必須透過 CreateTrainingJob
API 作業的請求語法中之 AlgorithmSpecification
和 HyperParameters
欄位來指定。
"AlgorithmSpecification": {
"TrainingImage": "<sagemaker-training-compiler-enabled-dlc-image>
"
},
"HyperParameters": {
"sagemaker_training_compiler_enabled": "true",
"sagemaker_training_compiler_debug_mode": "false",
"sagemaker_pytorch_xla_multi_worker_enabled": "false" // set to "true" for distributed training
}
如要尋找已實作 SageMaker Training Compiler 的深度學習容器映像 URI 的完整清單,請參閱支援的架構。