Metrics API


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.


The metric group torchelastic is reserved by torchelastic for platform level metrics that it produces. For instance torchelastic may output the latency (in milliseconds) of a re-rendezvous operation from the agent as (torchelastic,

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.

import time
import torchelastic.metrics as metrics

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

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

You may also use the` decorator to conveniently and succinctly profile functions

# -- in module examples.foobar --

import torchelastic.metrics as metrics

metrics.configure(metrics.ConsoleMetricsHandler(), "foobar")
metrics.configure(metrics.ConsoleMetricsHandler(), "Bar")
def foo():

class Bar():
  def baz():
      pass will publish the following metrics

<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> - function duration in milliseconds

Configuring Metrics Handler:

torchelastic.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.

import torchelastic.metrics as metrics

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

Writing a Custom Metric Handler:

If you want your metrics to be emitted to a custom location, implement the torchelastic.metrics.MetricHandler interface and configure your job to use your custom metric handler.

Below is a toy example that prints the metrics to stdout

import torchelastic.metrics as metrics

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

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

Now all metrics in the group my_app will be printed to stdout as:

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

Metric Handlers

Below are the metric handlers that come included with torchelastic.

class torchelastic.metrics.api.MetricHandler[source]
class torchelastic.metrics.api.ConsoleMetricHandler[source]
class torchelastic.metrics.api.NullMetricHandler[source]


torchelastic.metrics.configure(handler: torchelastic.metrics.api.MetricHandler, group: str = None)[source], group: str = 'torchelastic')[source]

@profile decorator publishes, 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.

def x():
def y():
torchelastic.metrics.put_metric(metric_name: str, metric_value: int, metric_group: str = 'torchelastic')[source]

Publishes a metric data point.


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


Access comprehensive developer documentation for PyTorch

View Docs


Get in-depth tutorials for beginners and advanced developers

View Tutorials


Find development resources and get your questions answered

View Resources