Metadata-Version: 2.4
Name: unlimited-svg-mlx
Version: 0.2.0
Summary: Convert images to editable SVGs locally on Apple Silicon with BF16 or Q8 MLX inference
Author: Anionex
License-Expression: MIT
Project-URL: Source, https://github.com/Anionex/unlimited-svg-mlx
Project-URL: Issues, https://github.com/Anionex/unlimited-svg-mlx/issues
Project-URL: Benchmarks, https://github.com/Anionex/unlimited-svg-mlx/tree/main/benchmarks
Project-URL: BF16 weights, https://huggingface.co/anionex/Unlimited-SVG-MLX-BF16
Project-URL: Q8 weights, https://huggingface.co/anionex/Unlimited-SVG-MLX-Q8
Keywords: svg,vectorization,mlx,apple-silicon,image-to-svg
Classifier: Operating System :: MacOS :: MacOS X
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Multimedia :: Graphics :: Graphics Conversion
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: mlx<0.33,>=0.32.5
Requires-Dist: mlx-vlm==0.8.1
Requires-Dist: Pillow>=10
Requires-Dist: numpy>=1.26
Requires-Dist: huggingface-hub<3,>=1
Requires-Dist: tokenizers<0.24,>=0.22
Requires-Dist: modelscope-hub<0.8,>=0.7.2
Requires-Dist: socksio<2,>=1
Dynamic: license-file

# Unlimited-SVG MLX

Convert an image to editable SVG locally on an Apple Silicon Mac. The Python API and CLI run Baidu's Unlimited-SVG vision encoder and mixture-of-experts decoder on the Metal GPU with MLX.

**Fastest tested MLX engine on our four-image Apple M5 Pro benchmark in BF16 and Q8.** With a warm model and fresh image encoding, the four images took **12.86 s in BF16** and **10.10 s in Q8**. The next-fastest implementations took 14.87 s and 10.40 s, respectively. Median decode speed was 192.0 and 278.4 tokens/s. All 32 measured Anionex outputs parsed as SVG. [Hardware, comparison engines, image quality and raw timings](https://github.com/Anionex/unlimited-svg-mlx/tree/main/benchmarks)

## Quick start

Use an Apple Silicon Mac and Python 3.11 or later (tested with Python 3.12).

```sh
python3 -m pip install unlimited-svg-mlx
unlimited-svg-mlx generate input.png -o output.svg
unlimited-svg-mlx generate input.png -o output-q8.svg --precision q8
```

The first invocation downloads the selected native MLX checkpoint from Hugging Face. BF16 weights occupy 6.67 GB and Q8 weights occupy 3.56 GB. Download ahead of time with `unlimited-svg-mlx fetch --precision q8`. For offline inference, add `--model-dir /path/to/checkpoint`. The loader also accepts the original Baidu checkpoint directory.

| Precision | Hugging Face | ModelScope |
|---|---|---|
| BF16 | [anionex/Unlimited-SVG-MLX-BF16](https://huggingface.co/anionex/Unlimited-SVG-MLX-BF16) | [Anionex/Unlimited-SVG-MLX-BF16](https://modelscope.cn/models/Anionex/Unlimited-SVG-MLX-BF16) |
| Q8 | [anionex/Unlimited-SVG-MLX-Q8](https://huggingface.co/anionex/Unlimited-SVG-MLX-Q8) | [Anionex/Unlimited-SVG-MLX-Q8](https://modelscope.cn/models/Anionex/Unlimited-SVG-MLX-Q8) |

For a direct ModelScope download, use `--source modelscope` on the fetch, generate or batch command, or pass `source="modelscope"` to the Python constructor. The downloader bypasses proxies in its own process. Both hubs are pinned to published checkpoint revisions.

```sh
unlimited-svg-mlx generate input.png -o output.svg --precision q8 --source modelscope
```

The [demo input](https://github.com/Anionex/unlimited-svg-mlx/blob/main/benchmarks/inputs/sample.png) produces this [editable Q8 example](https://github.com/Anionex/unlimited-svg-mlx/blob/main/examples/mountain-q8.svg) with the default greedy settings.

## Local Web UI

Run the browser interface on your Apple Silicon Mac (no additional web dependencies):

```sh
python -m pip install -e .
unlimited-svg-mlx web --port 7860
```

Open <http://127.0.0.1:7860>. Select or drop an image, set BF16/Q8, checkpoint source or local model directory, temperature, token budget, seed and screened/full vocabulary head. During generation, see the real token count, average decode speed, time to first token, SVG source and progressive preview. The token-budget bar is not an estimate of completion. Download the completed SVG. Stop requests are observed on the next progress update; partial generations are archived using the existing archive behavior. Only one generation runs at a time and the model is reused for matching settings. The server listens on loopback only; the model and images stay local except for checkpoint downloads.

## Python

```python
from pathlib import Path
from unlimited_svg_mlx import UnlimitedSVG

model = UnlimitedSVG(precision="q8")
svg = model.generate("input.png", temperature=0)
Path("output.svg").write_text(svg, encoding="utf-8")
```

Pass a path or a Pillow image. Keep one model instance for sequential requests. Repeated inputs reuse vision embeddings. Greedy decoding is the default. For sampling, set `temperature` and optionally `seed`. The default output budget is 30,000 tokens. Completed SVGs are also archived individually under `~/.local/share/unlimited-svg-mlx/generations/` (or `$XDG_DATA_HOME/unlimited-svg-mlx/generations/`). To choose another directory, pass `archive_dir=` to the constructor. Press Ctrl-C during generation to save a viewable `.partial.svg` and its exact raw prefix as `.partial.txt`. Reaching the token limit with an incomplete SVG also saves both files and raises an error. Python callers can read `model.last_saved_path` after generation or interruption.

## Batch inference

For several inputs, the decoder shares weight reads across active images on the GPU. Finished rows leave the batch, and each result is saved immediately. The command prints each input and its archived SVG path in input order.

```sh
unlimited-svg-mlx batch diagrams/*.png --precision q8 --batch-size 4 --output-dir results
```

In Python, `model.generate_batch([image_a, image_b])` returns a list of paths. A truncated item gets a `.partial.svg` and the generated text. A batch can produce different paths from individual greedy runs due to floating-point reductions.

## Precision and coordinates

- **BF16** uses the original model weights. Routing scores and the weighted expert reduction use FP32, matching the upstream routing precision.
- **Q8** uses 8-bit MLX affine weights (groups of 64) for eligible linear projections, routed experts and the token embedding. Activations, convolutions and positional embeddings retain their floating-point precision. Q8 can change the generated paths.
- Both modes use a single 2048 × 2048 global view. Images are EXIF-oriented, composited onto white, resized to fit and centered on gray. The exported SVG uses the coordinates emitted by the model, which can include the padded area.

Single-image greedy decoding computes a Q4 score estimate and uses per-row weight residual norms to screen vocabulary candidates. Metal then rescores the surviving rows with the selected BF16 or Q8 weights. The Q4 table is an auxiliary index built in memory. Sampling uses the full vocabulary projection. To use the standard MLX projection for greedy decoding, pass `--full-head` or `UnlimitedSVG(screened_head=False)`.

The runtime reuses [MLX-VLM's DeepSeek-OCR components](https://github.com/Neywa-Labs/mlx-vlm/tree/main/mlx_vlm/models/deepseekocr), applies the checkpoint-specific routing and image layout, chunks global SAM attention, and overlaps next-token GPU execution with CPU readback.

[Official checkpoint and model license](https://huggingface.co/baidu/Unlimited-SVG/tree/b75458ab68df9088cbb3ad64db7ea59ac6d062d6) · [Baidu reference implementation](https://github.com/baidu/Unlimited-SVG)
