Use LiteRT telemetry hooks

A telemetry hook is an extensible lifecycle callback mechanism embedded within the LiteRT (formerly TensorFlow Lite) runtime and compiler pipeline. This helps developers automatically collect, monitor, and process hardware accelerator performance metrics during standard model execution, without modifying low-level runtime code, or relying on external vendor-specific profiling binaries.

Data Accessible through Telemetry Hooks

When running on supported hardware such as Google Tensor, using a telemetry hook can fetch you on-device metrics including the following:

  • TPU Energy Consumption: Measure overall energy usage (in µJ) and calculate exact average energy consumption per inference.
  • TPU Hardware Tile Execution Time: Isolate pure TPU compute duration (in µs) from CPU host-side overhead and scheduling delays.

Configure and use telemetry hooks

You can configure and execute LiteRT telemetry hooks on Google Tensor devices by passing configuration arguments directly or using a textproto file. Follow these steps to get started:

Pass hook configurations

Google Tensor hooks are configured through the LITERT_VENDOR_HOOK_ARGS environment variable, which you can pass as a plain-text configuration in two ways:

Using a textproto file

Create a plain-text configuration file (hook_config.textproto) specifying which metrics to enable and where to write the output reports:

```proto
power_config {
  dump_power_metrics: true
  power_dump_path: "/data/local/tmp/power_bench.txt"
}
tpu_tile_time_metrics_config {
  dump_tpu_tile_time_metrics: true
  tpu_tile_time_metrics_dump_path: "/data/local/tmp/tpu_bench.txt"
}
```

Push the plain-text file directly to the device and point LITERT_VENDOR_HOOK_ARGS to its path:

```sh
adb push hook_config.textproto /data/local/tmp/hook_config.textproto

adb shell 'LITERT_VENDOR_HOOK_ARGS=/data/local/tmp/hook_config.textproto \
  /data/local/tmp/benchmark_model \
  --graph=/data/local/tmp/model.bin \
  --use_npu=true \
  --dispatch_library_path=/data/local/tmp'
```

Using a configuration string

You can also pass the configuration key-value pairs directly as an inline string in LITERT_VENDOR_HOOK_ARGS without creating or pushing any file:

adb shell 'LITERT_VENDOR_HOOK_ARGS="
  dump_power_metrics:true
  power_dump_path:\"/data/local/tmp/power_bench.txt\"
  dump_tpu_tile_time_metrics:true
  tpu_tile_time_metrics_dump_path:\"/data/local/tmp/tpu_bench.txt\"
" /data/local/tmp/benchmark_model \
  --graph=/data/local/tmp/model.bin \
  --use_npu=true \
  --dispatch_library_path=/data/local/tmp'

View the generated reports

Once the execution completes, you can pull or inspect the output files specified in your configuration:

adb shell cat /data/local/tmp/power_bench.txt
adb shell cat /data/local/tmp/tpu_bench.txt

Sample Power Report (power_bench.txt):

Average TPU Energy consumed per inference(uJ): 20.681
Total Energy (uJ): 2399
Inference count: 116

Sample TPU Tile Time Report (tpu_bench.txt):

==== Google Tensor TPU Tile Time Metrics ====
Total inferences: 116
Total TPU tile time (us): 12270
Average TPU tile time (us): 105
==================================