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

Input

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())

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.3.0.tar.gz (1.9 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.3.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for glasstrace-0.3.0.tar.gz
Algorithm Hash digest
SHA256 c0dededf5f782dc2b6129616ca3c5a5ada611fba15e672bc73816874bbbacfe5
MD5 a89be742f8acede787b37f9e4570c551
BLAKE2b-256 337d420a4466c0b085acf2350882b83d314149517ade986f0b4de1551e36afa1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: glasstrace-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 10.3 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.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8c8c03c1e0630accd1f0b4d29295f8da0d568f91dfb0e43a800ca04e490a6599
MD5 bab787e238dbd1879901a808af189ef1
BLAKE2b-256 76d11d839df7f4a39e0d65bfa1efdee08c4e2c6127ca3500cb1c1b443f60fdf5

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