The metrics API in torchelastic is used to publish telemetry metrics.
It is designed to be used by torchelastic’s internal modules to
publish metrics for the end user with the goal of increasing visibility
and helping with debugging. However you may use the same API in your
jobs to publish metrics to the same metrics sink.
A metric can be thought of as timeseries data
and is uniquely identified by the string-valued tuple
(metric_group,metric_name).
torchelastic makes no assumptions about what a metric_group is
and what relationship it has with metric_name. It is totally up
to the user to use these two fields to uniquely identify a metric.
A sensible way to use metric groups is to map them to a stage or module
in your job. You may also encode certain high level properties
the job such as the region or stage (dev vs prod).
Publish Metrics:
Using torchelastic’s metrics API is similar to using python’s logging
framework. You first have to configure a metrics handler before
trying to add metric data.
The example below measures the latency for the calculate() function.
importtimeimporttorch.distributed.elastic.metricsasmetrics# makes all metrics other than the one from "my_module" to go /dev/nullmetrics.configure(metrics.NullMetricsHandler())metrics.configure(metrics.ConsoleMetricsHandler(),"my_module")defmy_method():start=time.time()calculate()end=time.time()metrics.put_metric("calculate_latency",int(end-start),"my_module")
You may also use the torch.distributed.elastic.metrics.prof` decorator
to conveniently and succinctly profile functions
# -- in module examples.foobar --importtorch.distributed.elastic.metricsasmetricsmetrics.configure(metrics.ConsoleMetricsHandler(),"foobar")metrics.configure(metrics.ConsoleMetricsHandler(),"Bar")@metrics.profdeffoo():passclassBar:@metrics.profdefbaz():pass
torch.distributed.elastic.metrics.MetricHandler is responsible for emitting
the added metric values to a particular destination. Metric groups can be
configured with different metric handlers.
By default torchelastic emits all metrics to /dev/null.
By adding the following configuration metrics,
torchelastic and my_app metric groups will be printed out to
console.
If you want your metrics to be emitted to a custom location, implement
the torch.distributed.elastic.metrics.MetricHandler interface
and configure your job to use your custom metric handler.
Below is a toy example that prints the metrics to stdout
@profile decorator publishes duration.ms, count, success, failure metrics for the function that it decorates.
The metric name defaults to the qualified name (class_name.def_name) of the function.
If the function does not belong to a class, it uses the leaf module name instead.