Skip to main content

Per-layer latency and memory profiler for transformer inference.

Project description

glasstrace

CI PyPI Python License: MIT

Per-layer latency and memory profiler for transformer inference.

Why

Most LLM inference tools give you total latency and call it a day. That's not enough if you actually want to know what's slow. glasstrace hooks into your model and tells you where the time goes, split by layer and by inference phase.

Install

pip install glasstrace

Quick start

import glasstrace
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch

model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-0.5B").to("cuda")
tokenizer = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-0.5B")
inputs = tokenizer("Hello, world!", return_tensors="pt").to("cuda")

def warmup():
    model.generate(**inputs, max_new_tokens=5, do_sample=False)

with glasstrace.profile(model, warmup=warmup) as p:
    with torch.no_grad():
        model.generate(**inputs, max_new_tokens=20, do_sample=False)

print(p.report())
p.save_html("report.html")  # interactive HTML report

Output:

glasstrace report
  modules profiled: 169
  total events: 3380
  total measured time: 383.48 ms
  device: cuda
  kv-cache growth during decode: 0.2 MB

── prefill (1 pass, 69.7 ms total) ──────────────────────────────────────
Module                         Type    Calls  Total ms  % of phase
model.layers.0.mlp.down_proj   Linear      1      1.78        2.6%
...

── decode (20 passes, 314.7 ms total, 15.7 ms/token avg) ────────────────
Module                         Type    Calls  Total ms  % of phase
lm_head                        Linear     20     37.48       11.9%
model.layers.0.mlp.gate_proj   Linear     19      2.29        0.6%
...

Benchmark

4 models on a T4 GPU — fp16, 20 decode tokens, same prompt:

glasstrace benchmark

Three things stand out from the data:

  • Decode speed scales sub-linearly with size. Qwen 3B is 6x larger than 0.5B but only 2.5x slower per token.
  • KV-cache growth is about architecture, not parameter count. SmolLM2 1.7B grows its cache 6.8x faster than Qwen 1.5B at similar size.
  • lm_head's share of decode shrinks as models get deeper because the body scales faster than the vocab projection.

How it works

glasstrace registers forward hooks on every nn.Linear and nn.LayerNorm in your model. On CUDA it uses torch.cuda.Event for GPU timing — wall-clock time is meaningless for async GPU work. Phase detection is based on the sequence dimension of each layer's input: seq_len > 1 is prefill, seq_len == 1 is decode.

The warmup runs a forward pass before hooks are attached, paying the one-time GPU initialization cost before measurement starts.

Roadmap

  • v0.1 — per-module CUDA timing, text-table report
  • v0.1.1 — warmup phase, cold-start artifact fix
  • v0.2 — prefill/decode split, KV-cache tracking, PyPI release
  • v0.3 — CLI (glasstrace profile --model Qwen/Qwen2.5-0.5B)
  • v0.4 — HTML report with flamegraph
  • v1.0 — extended model coverage, docs site

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

glasstrace-0.4.0.tar.gz (1.4 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

glasstrace-0.4.0-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file glasstrace-0.4.0.tar.gz.

File metadata

  • Download URL: glasstrace-0.4.0.tar.gz
  • Upload date:
  • Size: 1.4 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for glasstrace-0.4.0.tar.gz
Algorithm Hash digest
SHA256 134e9756440ddbc42adaa8a5c393baa15817f37e988fe08d62f2a2b52320bff9
MD5 8168f59bd157b37be5214cda2db07c8b
BLAKE2b-256 b458c84414563fa76c0111865f5615bd7829562a24bf3b4072929845600491a6

See more details on using hashes here.

File details

Details for the file glasstrace-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: glasstrace-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for glasstrace-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e45af6b1d4d0dcc81a56b25d240b2dfbf49b36c80fb94a40f5eaaf86dc7dffc3
MD5 c868fe2cd4c29e73f392fdecb395f887
BLAKE2b-256 a0fe670a1c9fd13a7dba4989d99d09454da7784572824006468e824178d5b9a1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page