Brodhead
← Work

ggml-viz

A tracer and desktop viewer for programs built on ggml, the tensor library under llama.cpp and whisper.cpp. Preload a hook into the binary you already have and it records every compute graph, every op and how long it took, and what memory ggml allocated, without rebuilding anything or changing the program’s output.

The ggml-viz viewer with a TinyLlama trace loaded: compute graph, tensor inspector, memory panel and timeline
TinyLlama 1.1B (Q4_K_M) generating 32 tokens through llama.cpp on CPU: 32 graphs, 26,240 timed ops, 1.5 s.

The Graph

Each forward pass is a ggml compute graph. The hook records every node with its op and inputs when the graph starts, so the viewer redraws the real data flow: one graph at a time, top to bottom in execution order, every node placed just above the first op that uses it. Zoomed out, a transformer block reads off the screen: the norm, the Q, K and V projections fanning out, their writes into the KV cache, attention, then the feed-forward chain.

Graph View zoomed out on the first transformer block of the prompt graph
Graph View, prompt graph, first block. Purple is matrix multiplication; the slider steps through the 32 recorded graphs.

Where the Time Goes

Per-op timing comes from ggml’s own scheduler callback, so the times are the scheduler’s real node boundaries rather than estimates. The timeline shows them in order; the Op Timings tab ranks them. On this run the answer is plain: the feed-forward matmuls of each layer dominate the prompt.

Timeline zoomed to 100x showing one layer's ops: Qcur, attn_out, ffn_gate, ffn_up, ffn_out
Timeline at 100x: one layer of the prompt, the feed-forward matmuls taking most of it.
Op Timings tab listing the slowest ops: ffn_out, ffn_gate and ffn_up of each layer at 2.5 to 3.6 ms
Op Timings: the slowest ops of the whole run.

How It Works

Capture
LD_PRELOAD on Linux, DYLD_INTERPOSE on macOS; the hook wraps ggml’s graph compute, scheduler and tensor allocation calls.
Ops
The scheduler’s eval callback, chained with any callback the program already set.
Memory
Every tensor ggml places in a buffer, including weights llama.cpp maps from the model file. Recorded in the trace; the viewer’s memory panel is not built yet.
Format
Events pass through a lock-free ring buffer into GGMLVIZ, a small binary format the viewer reads after the run or live as it grows.
Cost
Timing every op makes the scheduler run one node at a time: this run took 1.8 s traced against 1.3 s untraced, with identical output.
Tested
llama.cpp on CPU, Linux and macOS. Metal, CUDA and Windows are untried.

Run It

# build
git clone --recursive https://github.com/brodheadw/ggml-viz.git
cd ggml-viz && cmake -B build -DCMAKE_BUILD_TYPE=Release && cmake --build build -j

# trace any llama.cpp binary, unmodified
GGML_VIZ_OUTPUT=trace.ggmlviz GGML_VIZ_MEMORY_TRACKING=1 \
  LD_PRELOAD=build/src/libggml_viz_hook.so \
  ./llama-simple -m model.gguf -n 32 "The capital of France is"
# macOS: DYLD_INSERT_LIBRARIES=build/src/libggml_viz_hook.dylib

# view it (or --live trace.ggmlviz to follow a run as it writes)
./build/bin/ggml-viz trace.ggmlviz

Source on GitHub →