Skip to main content

lmdec

lmdec is a Python library for implementing and exploring language-model decoding algorithms, with a CLI for estimating model parameters and inference memory. The project currently focuses on correctness and clear implementations, with inference performance optimizations planned as it matures.

The package is available on PyPI.

Installation

lmdec requires Python 3.12 or newer.

pip install lmdec

Analyze a model

lmdec analyze reads a Hugging Face model configuration without loading model weights. It reports estimated parameter counts, weight memory, KV-cache memory, model dimensions, and attention layout.

lmdec analyze Qwen/Qwen2.5-0.5B --context 4096 --batch_size 3

Choose weight and cache dtypes independently, and add --explain to show derivations and assumptions:

lmdec analyze Qwen/Qwen2.5-0.5B \
  --context 4096 \
  --batch_size 3 \
  --dtype fp32 \
  --kv-dtype bf16 \
  --explain
Option Default Meaning
model_id Required Hugging Face model ID or local configuration directory
--context 1 Tokens per sequence; must be positive
--batch_size 1 Number of sequences; must be positive
--dtype bf16 Weight storage dtype: bf16, fp16, or fp32
--kv-dtype bf16 KV-cache storage dtype: bf16, fp16, or fp32
--explain Off Include derivations and memory assumptions

Dedicated model families handle Qwen2/Qwen2.5 (qwen2), GLM-5 (glm_moe_dsa), DeepSeek V3 (deepseek_v3), and DeepSeek V3.2 (deepseek_v32). Other model types use a generic decoder estimate when their configuration exposes the required dimensions; architecture-specific features may not be captured.

Memory estimates count raw parameter storage and theoretical cache tensors. Every sequence is assumed to occupy the full requested context. The total excludes activations, temporary workspaces, allocator overhead, and cache block rounding, so it is not a prediction of peak GPU memory. Parameter estimates omit normalization parameters and biases. MLA estimates assume compressed KV latents; GLM-5 and DeepSeek V3.2 also include indexer keys at the chosen cache dtype. Backends using expanded K/V or quantized indexer keys can use different memory. Quantization scales and metadata are excluded.

You can also inspect the analysis in Python:

from transformers import AutoConfig

from lmdec.model_families import DType, Workload, resolve_family
from lmdec.presentation import render_analysis

model_id = "Qwen/Qwen2.5-0.5B"
family = resolve_family(model_id, AutoConfig.from_pretrained(model_id))
analysis = family.analyze(
    Workload(context=4096, batch_size=3, dtype=DType.FP32, kv_dtype=DType.BF16)
)

print(analysis.params.total)
print(analysis.memory.total_bytes)
print(render_analysis(analysis, explain=True))

Regex-constrained generation

The initial API provides batched greedy generation constrained by a regular expression. At each decoding step, regex_generate masks tokens that would make each generated text violate the expression. The same expression is applied to every prompt in the batch.

from transformers import AutoModelForCausalLM, AutoTokenizer

from lmdec import regex_generate

model_name = "HuggingFaceTB/SmolLM2-135M-Instruct"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

results = regex_generate(
    model=model,
    tokenizer=tokenizer,
    prompts=[
        "The first release date is ",
        "The second release date is ",
    ],
    regex=r"\d{4}-\d{2}-\d{2}",
    max_new_tokens=10,
)

print(results)
regex_generate(
    model,
    tokenizer,
    prompts: list[str],
    regex: str,
    max_new_tokens: int,
) -> list[str]

How it works

For a detailed walkthrough of the implementation—including token masking, finite-state machines, precomputed transitions, KV caching, batching, and the remaining tokenizer edge cases—read Regex-Constrained Generation: Making Output Syntax a Decoding Rule.

Roadmap

Planned areas of work include:

  • Structured generation beyond regular-expression constraints
  • Speculative decoding
  • Paged attention
  • Further inference and memory optimizations

The API is still early and may change as these capabilities are developed.

Development

Install the development dependencies and run the test suite with:

uv sync --dev
uv run pytest

Metadata

Release files for lmdec 0.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for lmdec 0.1.6
File Size Uploaded Provenance Actions
lmdec-0.1.6.tar.gz 11.7 kB With provenance Details for lmdec-0.1.6.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for lmdec 0.1.6
File Interpreter ABI Platform Actions
lmdec-0.1.6-py3-none-any.whl Python 3 none any Details for lmdec-0.1.6-py3-none-any.whl

Total release size: 29.7 kB

Release files / lmdec-0.1.6.tar.gz

Download URL lmdec-0.1.6.tar.gz
Size 11.7 kB
Tags Source
SHA-256 checksum
How to use checksums
61e61f72aff5c94b20f4f7084edf7790fb7be86ff8bd7e9d121e97b92d4ccb1e
BLAKE2b-256 checksum
How to use checksums
6cc35c910689a77ad4f3577fa8571ece3c196c75098a0d504c1a4f786e0a7868
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 7, 2026.

Transparency log

Release files / lmdec-0.1.6-py3-none-any.whl

Download URL lmdec-0.1.6-py3-none-any.whl
Size 17.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7e71829b5972a486351833f5c7b2e11749b6ef31ac641262006770dfe55d757a
BLAKE2b-256 checksum
How to use checksums
a13b51a14b512c9032ea3b6850d44b064240cde6e8b47e9b0cde48495a4fdaa8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 7, 2026.

Transparency log

Security for lmdec 0.1.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for lmdec-0.1.6.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 7, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page