ML グッドプット測定ライブラリでグッドプットをモニタリングする

ML グッドプット測定ライブラリ(ml-goodput-measurement)は、Cloud TPU VM で実行されている ML トレーニング ワークロードの効率性を測定するのに役立つ Python パッケージです。このライブラリは、ワークロード グッドプットを測定する指標を提供します。これは、生産的なトレーニングの進捗状況を維持するために費やされた TPU 使用時間の割合です。一方、バッドプットは、起動オーバーヘッド、I/O 停止、中断復旧などの非生産的なアクティビティに費やされた合計時間の割合です。

Cloud Monitoring ダッシュボードと TensorBoard を使用して、スループット指標をリアルタイムで可視化できます。これにより、ボトルネックを特定し、リソース使用率を最適化して、最終的にトレーニング費用を削減できます。

詳細については、ML Goodput 測定 GitHub リポジトリをご覧ください。

スループット指標

ML Goodput Measurement ライブラリは、次の指標を提供します。これらの指標は、Cloud Monitoring と TensorBoard で表示することもできます。この表の指標は compute.googleapis.com/workload/ で始まりますが、たとえば、goodput_time の完全な指標名は compute.googleapis.com/workload/goodput_time です。

  • goodput_time: 生産的なトレーニングの合計時間(秒)。これは累積スループットと解釈できます。

  • badput_time: 生産性のないトレーニング時間の合計(秒単位)(起動、停止、復元)。これは、累積バッドプットとして解釈できます。

  • total_elapsed_time: ワークロードの経過時間(実時間)の合計(秒単位)。経過時間は、アプリケーションの開始時刻から現在時刻またはジョブの完了時刻までの時間です。

  • interval_goodput: 指定された期間(過去 24 時間など)のグッドプット率。この指標は、スループットのローリング ウィンドウを提供します。

  • interval_badput: 指定された期間のバッドプット率。この指標は、バッドプットのローリング ウィンドウを提供します。これは、I/O オペレーションの急増などの一時的な問題を特定するのに役立ちます。

  • disruptions: ジョブの中断の累積数。中断とは、トレーニング プロセスが予期せず停止し、再起動が必要になるイベントです。たとえば、ハードウェアの障害やメンテナンス イベントなどです。

  • step_time_deviation: トレーニング ステップ時間のばらつきによる非生産的な時間の量。これは「ジッター」とも呼ばれます。この指標は、スパイクに敏感な安定性を測定します。これは、最近の「最悪のケース」のステップと過去のベースラインとの偏差を秒単位で表したものです。ステップは、トレーニング ループの 1 回のイテレーションです。

  • performance: 最も速い安定したステップ時間の推定値(ベースライン)(秒単位)。理想的なステップ時間は、最適な条件下で、一時的なノイズやジッターの影響を受けずに、1 回のトレーニング ステップにかかる最速の時間です。

  • max_productive_steps: 正常に保存された最大歩数。

インストール

次の手順に沿って、TPU ワークロードで ML Goodput Measurement ライブラリを設定します。

  1. Cloud Logging API と Cloud Monitoring API を有効にします。
  2. Google Kubernetes Engine(GKE)にデプロイする場合は、すべてのノードプールを cloud-platform アクセス スコープで構成します。

トレーニング ホストと分析マシンに ml-goodput-measurement パッケージをインストールします。

pip install ml-goodput-measurement

Goodput ライブラリでモニタリングする

ML グッドプット測定ライブラリを使用するには、GoodputRecorder インスタンスを初期化し、主要なセクションを record_event コンテキスト マネージャーでラップしてトレーニング コードを計測し、GoodputMonitor で指標をリアルタイムでモニタリングします。モニターはバックグラウンド プロセスを実行して、記録されたイベントからスループット指標を定期的に計算し、リアルタイムの分析と可視化のために Cloud Monitoring と TensorBoard にアップロードします。

スループット レコーダーを初期化する

ML グッドプット測定ライブラリのコア コンポーネントである GoodputRecorder を初期化します。

import datetime
import jax
from ml_goodput_measurement import measurement

# Define a unique logger name for this specific run
logger_name = f'goodput_{config.run_name}'

# Instantiate the recorder
goodput_recorder = measurement.GoodputRecorder(
    job_name=config.run_name,
    logger_name=logger_name,
    logging_enabled=(jax.process_index() == 0)
)

イベントを記録する

record_event コンテキスト マネージャーを使用して、トレーニング コードをラップします。

def train_loop(config):

  # 1. Wrap the entire Job (Start/End)
  with goodput_recorder.record_event(measurement.Event.JOB):

    # 2. Record Hardware Initialization
    with goodput_recorder.record_event(measurement.Event.ACCELERATOR_INIT):
      # ... perform device mesh setup ...
      initialize_tpu(config)

    # 3. Record Training Prep
    with goodput_recorder.record_event(measurement.Event.TRAINING_PREP):
      # ... create checkpoint managers, setup model ...
      model = training_prep(config)

    # 4. Main Training Loop
    for step in range(config.steps):

      # Record Data Loading
      with goodput_recorder.record_event(measurement.Event.DATA_LOADING):
        batch = get_next_batch()

      # Record Step Start (CRITICAL: Pass the step number!)
      with goodput_recorder.record_event(measurement.Event.STEP, step):
        output = train_step(model, batch)

      # 5. Record Custom Events (e.g., Evaluation)
      if step % eval_interval == 0:
        with goodput_recorder.record_event(measurement.Event.CUSTOM, "eval_step"):
          run_evaluation()

MaxText を使用してトレーニング コードを Goodput ライブラリと統合する方法の例については、goodput.py をご覧ください。

イベントをモニタリングする

GoodputMonitor を使用して指標をモニタリングします。これにより、ジョブの実行中に指標を計算してアップロードするバックグラウンド プロセスが開始されます。アップローダ プロセスが意図したとおりに開始および停止するように、ロジックをコンテキスト マネージャーでラップします。

構成とライフサイクル管理を処理するヘルパー コンテキスト マネージャーを定義します。

import contextlib
from ml_goodput_measurement import monitoring

@contextlib.contextmanager
def maybe_monitor_goodput(config):
  """Monitor goodput if enabled and on the main process."""
  if not config.monitor_goodput or jax.process_index() != 0:
    yield
    return

  goodput_monitor = None
  try:
    # Configure GCPOptions for Cloud Monitoring
    gcp_options = monitoring.GCPOptions(
      enable_gcp_goodput_metrics=config.enable_gcp_goodput_metrics
    )

    # Instantiate the monitor
    goodput_monitor = monitoring.GoodputMonitor(
      job_name=config.run_name,
      logger_name=f"goodput_{config.run_name}",
      tensorboard_dir=config.tensorboard_dir,
      upload_interval=config.goodput_upload_interval_seconds,
      monitoring_enabled=True,
      pathway_enabled=config.enable_pathways_goodput,
      include_badput_breakdown=True,
      gcp_options=gcp_options,
    )

    # Start the background upload process
    goodput_monitor.start_goodput_uploader()
    print("Started Goodput upload to Tensorboard & GCM in the background!")
    yield

  finally:
    # Ensure clean shutdown of the background process
    if goodput_monitor:
      goodput_monitor.stop_goodput_uploader()
      print("Flushed final metrics and safe exited from Goodput monitoring.")

ローリング ウィンドウのパフォーマンスを測定するには、start_rolling_window_goodput_uploaderstop_rolling_window_goodput_uploader を使用するようにモニターを構成します。

try:
    self._rolling_window_monitor.start_rolling_window_goodput_uploader(
        self.config.rolling_window_size
    )
finally:
    if self._rolling_window_monitor:
        self._rolling_window_monitor.stop_rolling_window_goodput_uploader()

メインのトレーニング エントリ ポイントをコンテキスト マネージャーでラップします。これにより、モニタリングはトレーニングの開始前に開始され、トレーニングの終了時に最終的な指標がクリアされます。

def main():
  # ... Load configuration ...

  # Wrap the entire execution
  with maybe_monitor_goodput(config):
    # Run the training loop (which contains the GoodputRecorder events)
    train_loop(config)

後処理と分析

標準の CPU VM やノートパソコンなど、任意のマシンから完了したジョブのグッドプット指標を計算できます。後処理と分析に TPU を使用する必要はありません。

次のコードは、ジョブの総グッドプットを出力します。

from ml_goodput_measurement import goodput

calculator = goodput.GoodputCalculator(
    job_name="my-run-name",
    logger_name="goodput_my-run-name"
)

goodput, badput, last_step = calculator.get_job_goodput(
    include_badput_breakdown=True
)

print(f"Goodput: {goodput}%")
print(f"Badput (Infra Recovery): {badput[goodput.BadputType.INFRASTRUCTURE_RECOVERY_FROM_DISRUPTION]}%")

バッドプット タイプ(BadputType)については、バッドプットの内訳の詳細をご覧ください。

特定の期間を指定するには、get_job_goodput_interval を使用します。

goodput_pct, badput, _, _, _ = calculator.get_job_goodput_interval(
    start_time_utc,
    end_time_utc
)

開始時間(start_time_utc)と終了時間(end_time_utc)は datetime オブジェクトです。詳細については、goodput.py をご覧ください。

Goodput ダッシュボードでモニタリングする

ML トレーニング ワークロードのモニタリングと可視化に役立つように、Google Cloud には GKE JobSet Goodput ダッシュボードと Cloud ML Goodput ダッシュボードの 2 つの Goodput ダッシュボードが用意されています。GKE JobSet ダッシュボードを使用してインフラストラクチャまたはスケジューリングの問題を診断し、Cloud ML Goodput ダッシュボードを使用してトレーニング コード内のボトルネックを特定します。

Google Cloud ML グッドプット ダッシュボード

ML グッドプット ダッシュボードは、トレーニング スクリプトのアプリケーション レベルの効率を測定します。生産的なトレーニングに費やされた時間や、データ読み込み、初期化、中断からの復元などのバッドプットのソースに関する分析情報を提供します。

Cloud Monitoring で ML Goodput ダッシュボードを表示するには: