本頁面說明如何啟用 Cloud Profiler,以便偵錯自訂訓練工作的模型訓練效能。
訓練模型的運算成本可能相當高。Profiler 可協助您瞭解訓練作業的資源耗用情形,進而監控及最佳化模型訓練效能。有了這些資訊,您就能找出並修正效能瓶頸,以更快速且更經濟實惠的方式訓練模型。
事前準備
- 確認您使用的是 TensorFlow 2.4 以上版本。
使用
cloud_profiler
外掛程式安裝 Vertex AI SDK。在本機 Docker 容器中執行:pip install google-cloud-aiplatform[cloud_profiler]
您必須要有 Vertex AI TensorBoard 執行個體。如需操作說明,請參閱「建立 Vertex AI TensorBoard 執行個體」。
您必須具備具有
roles/storage.admin
和roles/aiplatform.user
角色的服務帳戶。如需操作說明,請參閱「建立具備必要權限的服務帳戶」。您必須擁有 Cloud Storage bucket,才能儲存 Vertex AI TensorBoard 記錄。如需操作說明,請參閱「建立 Cloud Storage 值區來儲存 Vertex AI TensorBoard 記錄」。
啟用 Profiler
如要為訓練工作啟用分析器,請在訓練指令碼中加入下列內容:
在頂層匯入項目中新增
cloud_profiler
匯入項目:from google.cloud.aiplatform.training_utils import cloud_profiler
新增下列項目,初始化
cloud_profiler
外掛程式:cloud_profiler.init()
範例
以下是訓練指令碼範例:
#!/usr/bin/env python
import tensorflow as tf
import argparse
import os
from google.cloud.aiplatform.training_utils import cloud_profiler
import time
"""Train an mnist model and use cloud_profiler for profiling."""
def _create_model():
model = tf.keras.models.Sequential(
[
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation="relu"),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10),
]
)
return model
def main(args):
strategy = None
if args.distributed:
strategy = tf.distribute.MultiWorkerMirroredStrategy()
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
if args.distributed:
strategy = tf.distribute.MultiWorkerMirroredStrategy()
with strategy.scope():
model = _create_model()
model.compile(
optimizer="adam",
loss=tf.keras.losses.sparse_categorical_crossentropy,
metrics=["accuracy"],
)
else:
model = _create_model()
model.compile(
optimizer="adam",
loss=tf.keras.losses.sparse_categorical_crossentropy,
metrics=["accuracy"],
)
# Initialize the profiler.
cloud_profiler.init()
# Use AIP_TENSORBOARD_LOG_DIR to update where logs are written to.
tensorboard_callback = tf.keras.callbacks.TensorBoard(
log_dir=os.environ["AIP_TENSORBOARD_LOG_DIR"], histogram_freq=1
)
model.fit(
x_train,
y_train,
epochs=args.epochs,
verbose=0,
callbacks=[tensorboard_callback],
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--epochs", type=int, default=100, help="Number of epochs to run model."
)
parser.add_argument(
"--distributed", action="store_true", help="Use MultiWorkerMirroredStrategy"
)
args = parser.parse_args()
main(args)
存取 Profiler 資訊主頁
設定訓練指令碼以啟用 Profiler 後,請使用 Vertex AI TensorBoard 執行個體執行訓練指令碼。
請在訓練指令碼中確認下列設定:
- 將
BASE_OUTPUT_DIR:
設為要儲存訓練指令碼產生的 Vertex AI TensorBoard 記錄的 Cloud Storage 值區。 - 將
'serviceAccount':
設為您使用roles/storage.admin
和roles/aiplatform.user
角色建立的服務帳戶。 將
'tensorboard':
設為要用於這項訓練工作的 Vertex AI TensorBoard 執行個體完整名稱。完整名稱的格式如下:projects/PROJECT_NUMBER_OR_ID/locations/REGION/tensorboards/TENSORBOARD_INSTANCE_ID
您可以透過以下兩種方式,從 Google Cloud 控制台存取 Profiler 資訊主頁:
- 從「自訂工作」頁面。
- 從「實驗」頁面。
透過「自訂工作」頁面存取 Profiler 資訊主頁
即使訓練工作處於「已完成」狀態,您也可以使用這個方法存取分析器資訊主頁。
前往 Google Cloud 控制台的「Training」(訓練) 頁面,然後點選「Custom jobs」(自訂工作) 分頁標籤。
按一下剛建立的訓練工作名稱,前往工作詳細資料頁面。
按一下「開啟 TensorBoard」。
按一下「個人資料」分頁標籤。
透過「實驗」頁面存取 Profiler 資訊主頁
只有在訓練工作處於「執行中」狀態時,才能使用這個方法存取分析器資訊主頁。
前往 Google Cloud 控制台的 Vertex AI Experiments 頁面。
選取剛建立的訓練工作區域。
按一下訓練工作名稱旁的「開啟 TensorBoard」。
按一下「個人資料」分頁標籤。
擷取剖析工作階段
如要擷取剖析工作階段,訓練工作必須處於「執行中」狀態。在 Vertex AI TensorBoard 執行個體的「Profile」(設定檔) 分頁中,執行下列步驟:
- 按一下「擷取設定檔」。
在「設定檔服務網址或 TPU 名稱」欄位中,輸入:
workerpool0-0
在「地址類型」中,選取「IP 位址」。
按一下「擷取」。
筆記本
後續步驟
- 請參閱 Tensorflow Profiler 說明文件,瞭解分析器工具,以及如何使用這些工具提升模型效能。