Skip to contents

pliman_onnx_profiling() turns ONNX Runtime's built-in profiler on or off for sessions created afterwards. Each session writes one JSON trace, named after the model and the provider, when it is released, i.e. on pliman_clear_sessions(), LRU eviction, or at the end of the R session. onnx_profile_summary() reads such a trace and reports, per execution provider and operator type, how many nodes ran there and how much time they took.

The main use is to find CPU fallbacks inside a GPU session: operators that the GPU provider cannot execute run on the CPU, with a device-to-host copy around each one. A model with many such nodes (for example dynamically quantized INT8 models on DirectML) may be faster entirely on the CPU, or need an FP16/FP32 variant.

Usage

pliman_onnx_profiling(
  enable = TRUE,
  dir = file.path(tempdir(), "pliman_ort_profile")
)

onnx_profile_summary(file)

Arguments

enable

Logical. TRUE starts profiling new sessions, FALSE stops.

dir

Directory for the JSON traces. Defaults to a temporary directory.

file

Path to a JSON trace produced by the profiler.

Value

pliman_onnx_profiling(): the trace prefix (invisibly). Already cached sessions are not profiled: call pliman_clear_sessions() first. onnx_profile_summary(): a data.frame with columns provider, op_type, nodes, calls, total_ms and share (fraction of the node time), ordered by time, with attribute "providers" summarising time per provider.

Examples

if (FALSE) { # interactive()
library(pliman)
pliman_clear_sessions()
prefix <- pliman_onnx_profiling(TRUE)
img <- image_pliman("soybean_touch.jpg")
res <- image_detect_dl(img, model = "yolo26n", plot = FALSE)
pliman_clear_sessions()                     # writes the trace
pliman_onnx_profiling(FALSE)
f <- list.files(dirname(prefix), "\\.json$", full.names = TRUE)
onnx_profile_summary(f[1])
}