End-to-End Latency Profiling on Neuron

Inference latency on Neuron can be measured at three levels. The PyTorch profiler records what the host CPU does, including framework code, input preparation, and the calls that dispatch work to the device. A Neuron system profile records runtime activity across the whole run, such as each model execution on a NeuronCore. neuron-profile records what happens inside the device during a single execution of a compiled model (NEFF). An investigation usually uses them in that order.

Choosing a profiler

The PyTorch profiler captures:

  • CPU activity, including PyTorch operators and framework overhead
  • Input preparation on the host
  • The duration of each call to the Neuron device, including the wait for the result
  • The split between context encoding, which processes the prompt, and token generation, which produces one token per step

The system profile captures:

  • Neuron Runtime API calls and the ML framework trace for the whole run
  • How device executions are spaced across the run and across NeuronCores
  • Idle gaps between executions

The device profile captures:

  • Activity inside one NEFF execution
  • Per-engine activity, DMA transfers, collectives and semaphores

When time to first token or per-token latency is higher than expected, the PyTorch profile shows whether the time goes to the host or the device. The system profile shows how device executions are spaced across the run and across NeuronCores, which exposes gaps, stalls and long tails that a single execution does not show. The device profile then explains a slow execution operator by operator.

Capturing a PyTorch profile

A minimal capture around a transformers-neuronx generation call:

from torch.profiler import profile, ProfilerActivity

# Warm-up run, excluded from the profile.
model.sample(input_ids=input_ids, sequence_length=input_length + output_size, top_k=1)

with profile(activities=[ProfilerActivity.CPU]) as prof:
    model.sample(
        input_ids=input_ids,
        sequence_length=input_length + output_size,
        top_k=1,
    )

prof.export_chrome_trace("torch_profile.json")

ProfilerActivity.CPU records the host-side operators. The warm-up call keeps one-time costs, such as loading the model onto the device and filling caches, out of the measurement. The exported file uses the Chrome trace JSON format.

Each call into the Neuron runtime appears in the trace as a host-side operator. In the transformers-neuronx traces used here, the call is named neuron::_parallel_executor_run, and its duration covers dispatching the work and waiting for the device to finish. Operator names can differ between SDK versions, so the names in the queries below should be checked against the trace first.

Viewing the PyTorch trace

Perfetto opens Chrome trace files directly in the browser and handles large traces better than the older chrome://tracing viewer. Each operator appears as a slice on a timeline. Hovering over a slice shows its duration, and selecting a range of slices shows aggregate statistics.

Querying the PyTorch trace with SQL

Perfetto loads the trace into a database and accepts SQL in its query tab. The slice table holds one row per slice, with the start time ts and duration dur in nanoseconds.

Duration of each device call. Dividing by 1e6 converts nanoseconds to milliseconds:

SELECT ts, dur / 1e6 AS duration_ms
FROM slice
WHERE name = 'neuron::_parallel_executor_run'
ORDER BY ts;

Context encoding latency. The first device call of a generation processes the prompt, so its duration approximates the context encoding latency:

SELECT dur / 1e6 AS context_encoding_ms
FROM slice
WHERE name = 'neuron::_parallel_executor_run'
ORDER BY ts
LIMIT 1;

Per-token latency. The time between the starts of two consecutive device calls is the latency of one generated token as the application sees it, including any host-side gap between calls. LAG() returns the previous row’s timestamp, and the first call has no predecessor:

SELECT (ts - LAG(ts) OVER (ORDER BY ts)) / 1e6 AS token_latency_ms
FROM slice
WHERE name = 'neuron::_parallel_executor_run'
ORDER BY ts;

Average token generation latency. Averaging the gaps and dropping the first row gives the mean per-token latency:

SELECT AVG(token_latency_ms) AS avg_token_latency_ms
FROM (
    SELECT (ts - LAG(ts) OVER (ORDER BY ts)) / 1e6 AS token_latency_ms
    FROM slice
    WHERE name = 'neuron::_parallel_executor_run'
)
WHERE token_latency_ms IS NOT NULL;

These queries assume one slice per device call. If several threads each record the call, the queries should filter on a single thread first. Percentiles are easiest to compute by exporting the query result and processing it with NumPy.

Capturing a system profile

In the PyTorch profile, each device call is a single host-side slice. A system profile, introduced with Neuron Profiler 2.0, records the Neuron Runtime API calls and the ML framework trace for the whole run on one timeline. It shows how runtime calls and device executions are spaced across the run, but not the instructions inside an execution.

The runtime captures a system profile for an unmodified application when two environment variables are set (Neuron Profiler 2.0 guide):

export NEURON_RT_INSPECT_ENABLE=1
export NEURON_RT_INSPECT_OUTPUT_DIR=./output
python run_inference.py

The capture covers the whole lifetime of the process and writes its files to the output directory. A PyTorch or JAX profiler context in the same script should write to the same directory, so that all profile data for the run stays together. The guide describes how to view the result.

Typical analyses of a system profile include:

  • The distribution of NEFF execution durations across the run
  • The gaps between consecutive executions, which include any host-side work between them
  • The time each NeuronCore spends waiting in collective operations
  • Executions that take much longer than the rest, which point to the source of long tails

Capturing a device profile

The PyTorch profile and the system profile show which NEFF execution is slow. neuron-profile shows what happens inside that execution:

neuron-profile capture -n file.neff -s profile.ntff

The capture runs the NEFF on the device and writes the trace to an NTFF file. Models that run across several NeuronCores need the multi-worker options described in the Neuron Profile user guide.

Viewing the device profile

The web viewer stores the processed trace in InfluxDB 2.x, which must be installed and initialized once on the instance. The user guide lists the installation steps. After that, the following command processes the profile and prints a local URL, usually on port 3001:

neuron-profile view -n file.neff -s profile.ntff

When the profile runs on a remote instance, SSH port forwarding makes the viewer reachable from a laptop:

ssh my-instance -L 3001:localhost:3001 -L 8086:localhost:8086

The viewer is then available at http://localhost:3001.

A worked example

Consider a generation of 100 tokens that takes 800 ms when the expected latency is 400 ms.

  1. PyTorch profile. Capture one generation after a warm-up run and open it in Perfetto.
  2. Context encoding. The context encoding query returns 150 ms. Context encoding accounts for less than a fifth of the total.
  3. Token generation. The average token latency query returns 6.5 ms. Over 100 tokens, token generation accounts for 650 ms, so it is the phase to investigate.
  4. Host or device. The durations of the token generation calls are close to the gaps between them, so little time is spent on the host between tokens. The time goes to the device.
  5. System profile. The token generation executions take about the same time on every NeuronCore, and each core spends a large share of every step waiting in collective operations. Most of each step goes to collectives, and no single core is slower than the rest.
  6. Device profile. A neuron-profile capture of the token generation NEFF shows which engines wait on DMA or collectives, which narrows the problem to specific operators.

Practices

Reuse the queries. Running the same SQL queries and scripts on every profile gives a record of normal values for a model, and a regression shows up as a departure from those values.

Exclude warm-up. The first execution includes one-time costs such as loading the model and filling caches. A warm-up run before the profiled run keeps these costs out of the measurement.

Script long traces. For long captures, scripted analysis with the Perfetto trace processor or a Python script over the system profile is faster than scrolling through a UI.


If you found this useful, please cite this post using

Senthilkumar Gopal. (Nov 2025). End-to-End Latency Profiling on Neuron. sengopal.me. https://sengopal.me/posts/end-to-end-latency-profiling-on-neuron

or

@article{gopal2025neuronprofiling,
  title   = {End-to-End Latency Profiling on Neuron},
  author  = {Senthilkumar Gopal},
  journal = {sengopal.me},
  year    = {2025},
  month   = {Nov},
  url     = {https://sengopal.me/posts/end-to-end-latency-profiling-on-neuron}
}