Skip to main content

Per-layer latency and memory profiler for transformer inference.

Project description

glasstrace

CI PyPI Python License: MIT

Go to the Documentation

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-1.0.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-1.0.0-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: glasstrace-1.0.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-1.0.0.tar.gz
Algorithm Hash digest
SHA256 5a6fcf5a81f3a96004feec2c9d88a7177a35fbea423c9a3c13f51f76d034024e
MD5 04cac804e569c05831023825332bfae6
BLAKE2b-256 b26f28cb56927e19a5e35e782a7c0a5d202593632a58b7b0c1b72c49611ad296

See more details on using hashes here.

File details

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

File metadata

  • Download URL: glasstrace-1.0.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-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 50ece26e730699c6992912afc30bae9567e361a42427838d9924d5f3bf2543bb
MD5 3de9874f7dfcdabb5a5910caab3ba415
BLAKE2b-256 6d28606522182747a68d3efa0d8d64921637abe3183f9c75c1c149953c097187

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