a fast, efficient inference engine for moondream
Project description
Kestrel
High-performance inference engine for the Moondream vision-language model.
Kestrel is the inference engine behind Photon, Moondream's on-device deployment option. Most users should install via pip install moondream — this repo is the internal engine for those who need direct access.
Kestrel provides async, micro-batched inference with streaming support, paged KV caching, and optimized CUDA and Metal kernels. It's designed for production deployments where throughput and latency matter.
Features
- Async micro-batching — Cooperative scheduler batches heterogeneous requests without compromising per-request latency
- Streaming — Real-time token streaming for query and caption tasks
- Multi-task — Visual Q&A, captioning, point detection, object detection, and segmentation
- Paged KV cache — Efficient memory management for high concurrency
- Prefix caching — Radix tree-based caching for repeated prompts and images
- LoRA adapters — Parameter-efficient fine-tuning support with automatic cloud loading
Requirements
- Python 3.10+ (3.12 on macOS arm64).
- One of:
- NVIDIA GPU on Linux x86_64 / aarch64 or Windows x86_64. Optimized kernels for SM80 (A100), SM86 (A10, RTX 30-series), SM87 (Jetson Orin), SM89 (L4, L40S, RTX 4090), SM90 (H100, H200, GH200), SM100 (B200), SM110 (Jetson Thor), SM120 (RTX PRO 6000). Other CUDA GPUs may work but have not been tested.
- Apple Silicon Mac (M-series) on macOS 13 (Ventura) or later, with native Metal kernels.
MOONDREAM_API_KEYenvironment variable orapi_keyparameter (get a key from moondream.ai)
Installation
pip install kestrel
For Jetson Orin (JetPack 6) or Jetson Thor (JetPack 7), see the Jetson setup guide.
Model Access
Kestrel supports both Moondream 3 and Moondream 2:
| Model | Repository | Notes |
|---|---|---|
| Moondream 2 | vikhyatk/moondream2 | Public, no approval needed |
| Moondream 3 | moondream/moondream3-preview | Requires access approval |
For Moondream 3, request access (automatically granted) then authenticate with huggingface-cli login or set HF_TOKEN.
Quick Start
import asyncio
from kestrel.config import RuntimeConfig
from kestrel.engine import InferenceEngine
async def main():
# Weights are automatically downloaded from HuggingFace on first run.
# Use model="moondream2" or model="moondream3-preview".
cfg = RuntimeConfig(model="moondream2")
# Create the engine (loads model and warms up)
engine = await InferenceEngine.create(cfg, api_key="your-key-here")
# Load an image (JPEG, PNG, or WebP bytes)
image = open("photo.jpg", "rb").read()
# Visual question answering
result = await engine.query(
image=image,
question="What's in this image?",
settings={"temperature": 0.2, "max_tokens": 512},
)
print(result.output["answer"])
# Clean up
await engine.shutdown()
asyncio.run(main())
Tasks
Kestrel supports several vision-language tasks through dedicated methods on the engine.
Query (Visual Q&A)
Ask questions about an image:
result = await engine.query(
image=image,
question="How many people are in this photo?",
settings={
"temperature": 0.2, # Lower = more deterministic
"top_p": 0.9,
"max_tokens": 512,
},
)
print(result.output["answer"])
Caption
Generate image descriptions:
result = await engine.caption(
image,
length="normal", # "short", "normal", or "long"
settings={"temperature": 0.2, "max_tokens": 512},
)
print(result.output["caption"])
Point
Locate objects as normalized (x, y) coordinates:
result = await engine.point(image, "person")
print(result.output["points"])
# [{"x": 0.5, "y": 0.3}, {"x": 0.8, "y": 0.4}]
Coordinates are normalized to [0, 1] where (0, 0) is top-left.
Detect
Detect objects as bounding boxes:
result = await engine.detect(
image,
"car",
settings={"max_objects": 10},
)
print(result.output["objects"])
# [{"x_min": 0.1, "y_min": 0.2, "x_max": 0.5, "y_max": 0.6}, ...]
Bounding box coordinates are normalized to [0, 1].
Segment
Generate a segmentation mask (Moondream 3 only):
result = await engine.segment(image, "dog")
seg = result.output["segments"][0]
print(seg["svg_path"]) # SVG path data for the mask
print(seg["bbox"]) # {"x_min": ..., "y_min": ..., "x_max": ..., "y_max": ...}
Note: Segmentation requires Moondream 3 and separate model weights. Contact moondream.ai for access.
Streaming
For longer responses, you can stream tokens as they're generated:
image = open("photo.jpg", "rb").read()
stream = await engine.query(
image=image,
question="Describe this scene in detail.",
stream=True,
settings={"max_tokens": 1024},
)
# Print tokens as they arrive
async for chunk in stream:
print(chunk.text, end="", flush=True)
# Get the final result with metrics
result = await stream.result()
print(f"\n\nGenerated {result.metrics.output_tokens} tokens")
Streaming is supported for query and caption methods.
Response Format
All methods return an EngineResult with these fields:
result.output # Dict with task-specific output ("answer", "caption", "points", etc.)
result.finish_reason # "stop" (natural end) or "length" (hit max_tokens)
result.metrics # Timing and token counts
The metrics object contains:
result.metrics.input_tokens # Number of input tokens (including image)
result.metrics.output_tokens # Number of generated tokens
result.metrics.prefill_time_ms # Time to process input
result.metrics.decode_time_ms # Time to generate output
result.metrics.ttft_ms # Time to first token
Using Finetunes
If you've created a finetuned model through the Moondream API, you can use it by passing the adapter ID:
result = await engine.query(
image=image,
question="What's in this image?",
settings={"adapter": "01J5Z3NDEKTSV4RRFFQ69G5FAV@1000"},
)
The adapter ID format is {finetune_id}@{step} where:
finetune_idis the ID of your finetune jobstepis the training step/checkpoint to use
Adapters are automatically downloaded and cached on first use.
Configuration
RuntimeConfig
RuntimeConfig(
model="moondream3-preview", # or "moondream2"
max_batch_size=4, # Max concurrent requests
)
Environment Variables
| Variable | Description |
|---|---|
MOONDREAM_API_KEY |
Required. Get this from moondream.ai. |
HF_HOME |
Override HuggingFace cache directory for downloaded weights (default: ~/.cache/huggingface). |
HF_TOKEN |
HuggingFace token for gated models like Moondream 3. Alternatively, run huggingface-cli login. |
Triton Inference Server
Kestrel can be deployed as a Triton Inference Server backend. See the Triton setup guide.
Benchmarks
Throughput and latency for the query skill are tracked in PERFORMANCE.md, with results broken out by GPU.
License
Kestrel requires a Moondream API key for billing. See moondream.ai/pricing for plans.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kestrel-0.3.1.tar.gz.
File metadata
- Download URL: kestrel-0.3.1.tar.gz
- Upload date:
- Size: 140.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8a24c1c216b21fb52ddb58ba40acf393721eb89ff36837888f43bc0e2fbc2aed
|
|
| MD5 |
69e59269f9173ac7eb98ef1c9f8ea60e
|
|
| BLAKE2b-256 |
c2839c9bb11fa6f60f3bf375f3fda093b01227d15a3b1de518a81ae22deb4699
|
File details
Details for the file kestrel-0.3.1-py3-none-any.whl.
File metadata
- Download URL: kestrel-0.3.1-py3-none-any.whl
- Upload date:
- Size: 163.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b9323b8aa51f35ca9f9e8762e2d1b2e72a336a3fbd4c616ed7aa3510d7b835a1
|
|
| MD5 |
56cee0091726cb02d0194b348747a21e
|
|
| BLAKE2b-256 |
ecfb341ecbf7d22c9ea84a84ae860de90c0f795470d7b4a20723317f31e8ab81
|