OptiSpec
Renewal-Reward Optimal Control for Grammar-Coupled Self-Speculative Decoding
OptiSpec accelerates open-source Hugging Face causal language models with zero auxiliary draft-model VRAM overhead, combining grammar-forced span jumping, closed-form Ridge early-exit adapters, Dinkelbach optimal depth/stopping control, and provably lossless verification ($\epsilon = 0 \implies \text{TV} = 0$).
Key Features
- Zero Draft-Model VRAM: Reuses hidden states $h^{(k)}$ from intermediate transformer layers $1..k$ directly into verification layers $k+1..N$.
- Works on Any Hugging Face CausalLM:
- Curated Sub-4B Presets:
Qwen/Qwen3-4B,Qwen/Qwen3-1.7B,Qwen/Qwen2.5-3B-Instruct,NousResearch/Hermes-3-Llama-3.2-3B,meta-llama/Llama-3.2-3B-Instruct,google/gemma-2-2b-it,google/gemma-3-1b-it,google/gemma-3-4b-it,LGAI-EXAONE/EXAONE-3.5-2.4B-Instruct,LGAI-EXAONE/EXAONE-4.0-1.2B,microsoft/Phi-3.5-mini-instruct,HuggingFaceTB/SmolLM2-1.7B-Instruct. - 1-Command Auto-Onboarding (
optispec fit): Profile GPU latency (~30s) + fit closed-form Ridge adapters + Theorem 2 optimal exits $K^*$ in ~1–2 minutes for any unseen HF model.
- Curated Sub-4B Presets:
- Interactive Gradio Comparison Demo (
optispec demo): Live side-by-side streaming comparing Standard AR vs OptiSpec with token-level color provenance (Grammar Forced,Early-Exit Draft,Target Bonus), throughput speedup, layer savings, and TV bound. - OpenAI-Compatible HTTP Server (
optispec serve): Drop-in/v1/chat/completionsendpoint supportingtools,response_formatJSON Schema (xgrammar), andstream=True.
Installation
# Core runtime
pip install optispec
# With Gradio live comparison demo UI
pip install "optispec[demo]"
# Everything (Demo UI + OpenAI Server + Training/Distillation + XGrammar + 4-bit)
pip install "optispec[all]"
For local development with uv:
uv sync --all-extras --extra dev
uv run pytest -v
Quickstart
1. Launch the Gradio Live Comparison Demo
optispec demo --model Qwen/Qwen3-4B
Select any curated preset or enter any Hugging Face model ID, click Load / Auto-Fit Model, and compare Standard AR vs OptiSpec side-by-side.
2. Python SDK
from optispec import GenerateConfig, OptiSpecEngine
engine = OptiSpecEngine.from_pretrained("Qwen/Qwen3-4B", adapter="auto", epsilon=0.0)
result = engine.chat(
[{"role": "user", "content": "Check the weather in Seoul in celsius."}],
tools=[...],
config=GenerateConfig(max_new_tokens=192, temperature=0.0),
)
print(f"{result.tokens_per_sec:.1f} tok/s | TV={result.tv_bound_total:.4f}")
print(result.generated_text)
3. Onboard Any New Hugging Face Model (and Push to Hub)
# Fast 1-2 minute closed-form Ridge calibration (zero gradient training)
optispec fit --model google/gemma-3-1b-it --mode ridge-fast
# Optional: full low-rank KL self-distillation & share adapter bundle on HF Hub
optispec fit --model LGAI-EXAONE/EXAONE-3.5-2.4B-Instruct --mode distill --push-to-hub username/exaone-3.5-2.4b-optispec
4. OpenAI-Compatible Server
optispec serve --model Qwen/Qwen3-4B --port 8000
See examples/03_openai_client.py for calling it with the standard openai Python SDK.
Architecture
- Layer 0 (
optispec.core): Pure mathematical solvers — Dinkelbach fractional programming (renewal.py), Exact/Submodular exit selection (layer_selection.py), Theorem 3 lossless/$\epsilon$-bounded verifier (verifier.py). - Layer 1 (
optispec.grammar): UniformGrammarConstraintprotocol, multi-formatToolCallSchemaFSMwith schema-mask precompilation, and optionalXGrammarConstraint. - Layer 2 (
optispec.engine): UniversalBackboneAdapter(Llama/Qwen/Gemma2/Gemma3/EXAONE/GPT-NeoX),PartialLayerRunnerwith exact KV rollback, and unified streaming decoders. - Layer 3 (
optispec.adapters):LowRankExitAdapter, dependency-freeConfidenceCalibrator,safetensors+JSON bundle serialization, hardware profiler cache, andauto_fit. - Layer 4 (
optispec.api,cli,demo,server): Public SDK, Typer CLI, Gradio comparison UI, and FastAPI OpenAI server.
See docs/architecture.md and docs/supported_models.md for details.
Metadata
Release files for optimalspec 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| optimalspec-0.2.0.tar.gz | 497.1 kB | Without provenance | Details for optimalspec-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| optimalspec-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for optimalspec-0.2.0-py3-none-any.whl |
Total release size: 576.2 kB
Release files / optimalspec-0.2.0.tar.gz
| Download URL | optimalspec-0.2.0.tar.gz |
|---|---|
| Size | 497.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
557f1339cb2bcd99167aee82c52908b8a622e49c0b1fc35da693c31bb8e3be9e
|
|
BLAKE2b-256 checksum How to use checksums |
9a8a260ea4c2091ceb16b2a5d8453783f4b088e70a00da29f55bbef029efca18
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / optimalspec-0.2.0-py3-none-any.whl
| Download URL | optimalspec-0.2.0-py3-none-any.whl |
|---|---|
| Size | 79.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
c8b2fded5b20c9da49a23caadfa5529202311ae1593b830e8c7ddb9c13bcc908
|
|
BLAKE2b-256 checksum How to use checksums |
363123cd05e0db1dec659375edcbc090d6234d806485120c0bd4888d2d8c6648
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.13.6 {"installer":{"name":"uv","version":"0.13.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for optimalspec 0.2.0
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.