捷徑

指標

指標 API。

概覽:

torchelastic 中的指標 API 用於發布遙測指標。它設計用於 torchelastic 的內部模組,以便為最終用戶發布指標,目標是提高可見性並協助偵錯。但是,您可以在您的作業中使用相同的 API 將指標發布到相同的指標 接收器

可以將 指標 視為時間序列資料,並由字串值元組 (指標群組, 指標名稱) 唯一識別。

torchelastic 不會對 指標群組 是什麼,以及它與 指標名稱 的關係做出任何假設。完全由用戶使用這兩個欄位來唯一識別指標。

注意

指標群組 torchelastic 由 torchelastic 保留,用於其產生的平台級指標。例如,torchelastic 可以從代理程式輸出重新協調作業的延遲(以毫秒為單位)作為 (torchelastic, agent.rendezvous.duration.ms)

使用指標群組的明智方法是將它們映射到作業中的階段或模組。您也可以編碼作業的某些高階屬性,例如區域或階段(開發與生產)。

發布指標:

使用 torchelastic 的指標 API 類似於使用 Python 的日誌記錄框架。您必須先設定指標處理常式,才能嘗試新增指標資料。

以下範例測量 calculate() 函式的延遲。

import time
import torch.distributed.elastic.metrics as metrics

# makes all metrics other than the one from "my_module" to go /dev/null
metrics.configure(metrics.NullMetricsHandler())
metrics.configure(metrics.ConsoleMetricsHandler(), "my_module")

def my_method():
  start = time.time()
  calculate()
  end = time.time()
  metrics.put_metric("calculate_latency", int(end-start), "my_module")

您也可以使用 torch.distributed.elastic.metrics.prof` 裝飾器來方便且簡潔地分析函式

# -- in module examples.foobar --

import torch.distributed.elastic.metrics as metrics

metrics.configure(metrics.ConsoleMetricsHandler(), "foobar")
metrics.configure(metrics.ConsoleMetricsHandler(), "Bar")

@metrics.prof
def foo():
  pass

class Bar():

  @metrics.prof
  def baz():
      pass

@metrics.prof 將發布以下指標

<leaf_module or classname>.success - 1 if the function finished successfully
<leaf_module or classname>.failure - 1 if the function threw an exception
<leaf_module or classname>.duration.ms - function duration in milliseconds

設定指標處理常式:

torch.distributed.elastic.metrics.MetricHandler 負責將新增的指標值發送到特定目的地。可以使用不同的指標處理常式設定指標群組。

根據預設,torchelastic 會將所有指標發送到 /dev/null。透過新增以下設定指標,torchelasticmy_app 指標群組將列印到主控台。

import torch.distributed.elastic.metrics as metrics

metrics.configure(metrics.ConsoleMetricHandler(), group = "torchelastic")
metrics.configure(metrics.ConsoleMetricHandler(), group = "my_app")

撰寫自訂指標處理常式:

如果您希望將指標發送到自訂位置,請實作 torch.distributed.elastic.metrics.MetricHandler 介面,並將您的作業設定為使用您的自訂指標處理常式。

以下是一個將指標列印到 stdout 的簡單範例

import torch.distributed.elastic.metrics as metrics

class StdoutMetricHandler(metrics.MetricHandler):
   def emit(self, metric_data):
       ts = metric_data.timestamp
       group = metric_data.group_name
       name = metric_data.name
       value = metric_data.value
       print(f"[{ts}][{group}]: {name}={value}")

metrics.configure(StdoutMetricHandler(), group="my_app")

現在,群組 my_app 中的所有指標都將列印到 stdout,如下所示

[1574213883.4182858][my_app]: my_metric=<value>
[1574213940.5237644][my_app]: my_metric=<value>

指標處理常式

以下是 torchelastic 隨附的指標處理常式。

class torch.distributed.elastic.metrics.api.MetricHandler[來源]
class torch.distributed.elastic.metrics.api.ConsoleMetricHandler[來源]
class torch.distributed.elastic.metrics.api.NullMetricHandler[來源]

方法

torch.distributed.elastic.metrics.configure(handler, group=None)[來源]
torch.distributed.elastic.metrics.prof(fn=None, group='torchelastic')[來源]

@profile 裝飾器會發布其裝飾之函式的 duration.ms、count、success、failure 指標。

指標名稱預設為函式的限定名稱 (類別名稱.定義名稱)。如果函式不屬於類別,則改用葉模組名稱。

用法

@metrics.prof
def x():
    pass

@metrics.prof(group="agent")
def y():
    pass
torch.distributed.elastic.metrics.put_metric(metric_name, metric_value, metric_group='torchelastic')[原始碼]

發佈一個指標資料點。

用法

put_metric("metric_name", 1)
put_metric("metric_name", 1, "metric_group_name")

文件

取得 PyTorch 的完整開發人員文件

查看文件

教學

取得針對初學者和進階開發人員的深入教學

查看教學

資源

尋找開發資源並獲得問題解答

查看資源