Skip to main content

Python binding for UniRT — multi-backend on-device LLM inference with an extensible VLM API

Project description

unirt (Python binding)

Python binding for the UniRT SDK — run LLMs locally through a single API with interchangeable backends:

runtime models hardware in the published wheels
llama_cpp GGUF (LLM, VLM, embeddings, rerank) CPU, Metal on macOS every wheel
mlx HF safetensors (SmolLM2-style Llama/ByteLevel-BPE layout; dense or MLX-quantized) Apple Silicon Metal GPU macOS wheel (Apple-only)
onnxruntime ONNX encoder embeddings CPU, Apple Core ML every wheel

unirt devices lists what your install actually has. The llama_cpp runtime is built for a generic CPU baseline plus Metal on macOS — no CUDA, no Vulkan.

The bundled llama_cpp runtime supports GGUF VLMs through libmtmd when an mmproj is present. MLX remains text-only and fails explicitly for VLM models. MLX also requires a usable Metal device; if none is visible, model loading fails before native model allocation.

Install

pip install unirt

Wheels ship the native libraries — no toolchain, no build step — for macOS 14+ arm64, Linux x86_64 and arm64 (manylinux_2_31, glibc 2.31+), and Windows 10+ x86_64 and arm64. Python 3.10+.

Quickstart (CLI)

unirt chat bartowski/SmolLM2-135M-Instruct-GGUF   # download + interactive chat
unirt serve <model> --port 8080                   # OpenAI-compatible server
unirt embed <encoder> "some text"                 # one vector per argument
unirt rerank <cross-encoder> "a query" "doc one" "doc two"
unirt pull <hf-repo>                              # download only
unirt ls                                          # cached models
unirt devices                                     # plugins + devices

Usage

from unirt.auto import AutoModelForCausalLM

model = AutoModelForCausalLM.from_pretrained(
    'bartowski/SmolLM2-135M-Instruct-GGUF',
    precision='Q4_K_M',
    device_map='llama_cpp',
)
out = model.generate(prompt, max_new_tokens=128, temperature=0.7)
print(out.text)
model.close()

Embedding repositories select an ONNX variant and tokenizer sidecars without downloading the PyTorch checkpoint:

from unirt import AutoModelForEmbedding

with AutoModelForEmbedding.from_pretrained(
    'sentence-transformers/all-MiniLM-L6-v2',
    device_map='cpu',  # or 'coreml' on Apple Silicon
) as model:
    vectors = model.encode(['a cat on a mat', 'a kitten on a rug'])
    print(len(vectors), len(vectors[0]))  # 2, 384

Repository ids are inspected and downloaded with huggingface_hub. GGUF repositories download only the selected quantization (including all of its shards) plus tokenizer/config sidecars. The default cache is ~/.cache/unirt; set UNIRT_DATADIR to move it and UNIRT_HFTOKEN for gated or private repositories.

Generation is stateless by default (n_past=0 clears prior KV state before prefilling the supplied prompt). To continue from a known cached prefix, pass the exact prefix length through n_past; invalid values are rejected rather than silently duplicating context.

Structured output

Constrain decoding so the reply is guaranteed to parse — the grammar masks invalid tokens at every step, which makes even small models reliable JSON emitters (llama_cpp backend; MLX rejects these options):

schema = {'type': 'object',
          'properties': {'city': {'type': 'string'}, 'country': {'type': 'string'}},
          'required': ['city', 'country']}
out = model.generate('Facts about the capital of France as JSON.',
                     json_schema=schema)      # dict or serialized JSON string
data = json.loads(out.text)                    # always parses

model.generate(prompt, json_mode=True)         # any syntactically valid JSON
model.generate(prompt, grammar='root ::= ...')  # raw GBNF

The server accepts the OpenAI response_format field with types json_object and json_schema. Note a length finish can still truncate mid-object — budget max_tokens accordingly.

OpenAI-compatible server

python3 -m unirt.server --model bartowski/SmolLM2-135M-Instruct-GGUF \
  --backend llama_cpp --port 8080

Then point any OpenAI client (or plain curl) at http://localhost:8080/v1/chat/completions — streaming SSE included, and GGUF VLMs accept image content parts when loaded with an mmproj.

--embedding-model <encoder> adds /v1/embeddings and --rerank-model <cross-encoder> adds /v1/rerank; either may be given without --model, so a retrieval sidecar needs no chat model at all.

Sampling takes temperature, top_p, top_k, min_p, seed, repetition_penalty, presence_penalty and frequency_penalty. --api-key (or UNIRT_API_KEY) requires Authorization: Bearer <key> on every /v1 endpoint, leaving /health open for probes — worth setting whenever --host is not loopback.

The native library is closed-source and ships prebuilt. A wheel already bundles it under unirt/lib/; a source checkout is discovered at <repo>/sdk/pkg-unirt/lib after building the SDK, and installing from source without building means populating unirt/lib/ yourself from a Release's native-libs archive before pip install .. UNIRT_LIB_PATH / UNIRT_PLUGIN_PATH override the search.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

unirt-0.3.0-py3-none-win_arm64.whl (9.4 MB view details)

Uploaded Python 3Windows ARM64

unirt-0.3.0-py3-none-win_amd64.whl (9.6 MB view details)

Uploaded Python 3Windows x86-64

unirt-0.3.0-py3-none-manylinux_2_31_x86_64.whl (13.9 MB view details)

Uploaded Python 3manylinux: glibc 2.31+ x86-64

unirt-0.3.0-py3-none-manylinux_2_31_aarch64.whl (12.4 MB view details)

Uploaded Python 3manylinux: glibc 2.31+ ARM64

unirt-0.3.0-py3-none-macosx_14_0_arm64.whl (20.0 MB view details)

Uploaded Python 3macOS 14.0+ ARM64

File details

Details for the file unirt-0.3.0-py3-none-win_arm64.whl.

File metadata

  • Download URL: unirt-0.3.0-py3-none-win_arm64.whl
  • Upload date:
  • Size: 9.4 MB
  • Tags: Python 3, Windows ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for unirt-0.3.0-py3-none-win_arm64.whl
Algorithm Hash digest
SHA256 b847fb8a666e509fa6a523b8f87b57fa5047f11793c164b074acb9dcbf0c5bb5
MD5 3707353eec7b3879652197807001e75d
BLAKE2b-256 7aaff17903c015f9991a65c66f01ab4464f667215940c62cd20aa2e76cabf011

See more details on using hashes here.

File details

Details for the file unirt-0.3.0-py3-none-win_amd64.whl.

File metadata

  • Download URL: unirt-0.3.0-py3-none-win_amd64.whl
  • Upload date:
  • Size: 9.6 MB
  • Tags: Python 3, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for unirt-0.3.0-py3-none-win_amd64.whl
Algorithm Hash digest
SHA256 a9ef9d244fdf52b9c828657303e287b98048e9e54ca1d828fe94324d585a398d
MD5 895257d04ae9dc4fcbb9ddd87edd49a2
BLAKE2b-256 0f9f9bf46f33d4a532444ea4d8816deee1d9e5601ccc27e2f5e73ac5c35d7d84

See more details on using hashes here.

File details

Details for the file unirt-0.3.0-py3-none-manylinux_2_31_x86_64.whl.

File metadata

File hashes

Hashes for unirt-0.3.0-py3-none-manylinux_2_31_x86_64.whl
Algorithm Hash digest
SHA256 6070ae1c96e4f24c966954816baf0953eebc0d5d34e62575645aa2b649fde70d
MD5 5cd4e381467faab9ee27294f99c56011
BLAKE2b-256 9a84c6492fc29b7487bf0b40ca8ba512d3ad58335c7ed4cbd1b7181948514984

See more details on using hashes here.

File details

Details for the file unirt-0.3.0-py3-none-manylinux_2_31_aarch64.whl.

File metadata

File hashes

Hashes for unirt-0.3.0-py3-none-manylinux_2_31_aarch64.whl
Algorithm Hash digest
SHA256 801f896512710fab03ee1983619c2fea6571dc8ed06cf434c2daea0d53c6a67f
MD5 90fb116ba8a5efdb274073fbe257fb91
BLAKE2b-256 00abb20a49e0001c10479af307d61533109168d9ad0a83d9f7e666739e268f05

See more details on using hashes here.

File details

Details for the file unirt-0.3.0-py3-none-macosx_14_0_arm64.whl.

File metadata

  • Download URL: unirt-0.3.0-py3-none-macosx_14_0_arm64.whl
  • Upload date:
  • Size: 20.0 MB
  • Tags: Python 3, macOS 14.0+ ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.0

File hashes

Hashes for unirt-0.3.0-py3-none-macosx_14_0_arm64.whl
Algorithm Hash digest
SHA256 b41a682ccbe0fd53be16c1a498fc28923989acafc7b73b35477be27f3d6e4a07
MD5 d7fd0ce003c706c366e0b5f2c8841172
BLAKE2b-256 0686cc3723457ea0e33dd99b694cad8031e896a0c0d72d3255d1fe1eed32045e

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