快捷方式

指標

指標 API。

概述:

torchelastic 中的指標 API 用於釋出遙測指標。它旨在供 torchelastic 的內部模組使用,以便為終端使用者釋出指標,從而提高可見性並幫助除錯。但是,您也可以在自己的作業中使用相同的 API 將指標釋出到同一個指標 sink

一個 metric 可以被視為時間序列資料,並透過字串值元組 (metric_group, metric_name) 唯一標識。

torchelastic 不對 metric_group 是什麼以及它與 metric_name 有何關係做出任何假設。使用者可以完全自由地使用這兩個欄位來唯一標識一個指標。

注意

指標組 torchelastic 被 torchelastic 保留,用於其產生的平臺級指標。例如,torchelastic 可以將 agent 進行重新集合(re-rendezvous)操作的延遲(以毫秒為單位)輸出為 (torchelastic, agent.rendezvous.duration.ms)

一種合理的使用指標組的方式是將它們對映到作業中的某個階段或模組。您也可以編碼作業的某些高階屬性,例如區域或階段(開發 vs 生產)。

釋出指標:

使用 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 指標。

指標名稱預設為函式的限定名 (class_name.def_name)。如果函式不屬於某個類,則使用葉子模組名稱代替。

用法

@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 全面的開發者文件

檢視文件

教程

獲取針對初學者和高階開發者的深度教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源