xinfer-ai 0.12.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,88 @@
1
+ # Adding New Model Architectures (AI-Assisted)
2
+
3
+ xInfer ships with a **Cursor Agent Skill** that guides AI coding assistants through the full workflow of adapting a new HuggingFace model architecture to this project. It covers safetensors and GGUF formats, Dense and MoE architectures, and quantization formats (MXFP4, FP8, ISQ, etc.).
4
+
5
+ ## Prerequisites
6
+
7
+ - [Cursor IDE](https://cursor.sh/) with Agent mode enabled (for other Agents, mention the skill file manually)
8
+ - The xInfer repository cloned locally
9
+ - (Optional) A local copy of [attention.rs](https://github.com/guoqingbao/attention.rs) if custom kernels are needed
10
+
11
+ ## How It Works
12
+
13
+ The skill lives at `.cursor/skills/add-model/SKILL.md` and is **automatically activated** when you ask the agent to add, port, or support a new model. It walks the agent through six phases:
14
+
15
+ | Phase | What happens |
16
+ |-------|-------------|
17
+ | **0 — Gather info** | Collects model config, weight tensor info, and GGUF metadata. Can read from a local model path, HuggingFace Hub, or user-provided files. |
18
+ | **1 — Analyze** | Compares the new architecture against existing models, identifies closest template, classifies as Dense / MoE / Hybrid / Multimodal. |
19
+ | **2 — Kernels** | Determines if new CUDA + Metal kernels are needed in `attention.rs`. If so, clones the repo locally, adds both backends, and points `Cargo.toml` to the local copy. |
20
+ | **3 — Implement** | Creates the model file (`src/models/<arch>.rs`), handles HF and GGUF weight paths, MoE construction, packed expert layouts. |
21
+ | **4 — Register** | Wires the model into `mod.rs`, `config.rs`, `utils/mod.rs`, `runner.rs`, and `parser.rs`. |
22
+ | **5 — Build** | Compiles with the correct platform features and fixes all errors/warnings. |
23
+ | **6 — Test** | Starts the server, sends OpenAI-compatible requests, debugs common issues (NaN, crashes, wrong output). |
24
+
25
+ ## Quick Start
26
+
27
+ Open the project in Cursor and ask the agent:
28
+
29
+ ```
30
+ Add support for the model google/gemma-4-26B-A4B-it
31
+ ```
32
+
33
+ The agent will read the skill, then ask you for any missing information (config URL, tensor info, etc.). If you have the model downloaded locally, provide the path:
34
+
35
+ ```
36
+ Add support for the model at /path/to/my-model/
37
+ ```
38
+
39
+ The agent will inspect the local weights directly to extract config and tensor info.
40
+
41
+ ## What You Can Provide
42
+
43
+ The skill accepts multiple input forms — provide whichever is convenient:
44
+
45
+ - **HuggingFace model ID** — e.g. `google/gemma-4-26B-A4B-it`
46
+ - **Local safetensors directory (prefered)** — path containing `config.json` + `*.safetensors`
47
+ - **Local GGUF file** — a single `.gguf` file
48
+ - **Manual config + tensor info** — paste `config.json` contents (or the path/url) and weight tensor names/shapes (can be obtained by clicking model weights in Hugginface model/files repo)
49
+
50
+ ## Supported Configurations
51
+
52
+ | Dimension | Supported variants |
53
+ |-----------|--------------------|
54
+ | **Format** | Safetensors (HF), GGUF, GPTQ, AWQ |
55
+ | **Architecture** | Dense, MoE, Hybrid MoE (parallel dense+MoE), Multimodal (vision+language) |
56
+ | **Quantization** | ISQ (in-situ: Q4K, Q8_0, etc.), FP8, MXFP4, NVFP4, GPTQ, AWQ |
57
+ | **Attention** | GQA, MQA, MHA, Sliding Window, Multi-head Latent Attention (MLA) |
58
+ | **Platform** | CUDA (Linux/Windows), Metal (macOS) |
59
+
60
+ ## Multi-GPU Models
61
+
62
+ For models that require multi-GPU inference:
63
+
64
+ ```bash
65
+ xinfer --m <model_id> --port 8000 --d 0,1
66
+ ```
67
+
68
+ When retrying after a failed load, the agent will kill all `xinfer` processes and verify GPU memory is freed before reloading.
69
+
70
+ ## Debugging Tips
71
+
72
+ If the model loads but produces incorrect output, the skill guides the agent through:
73
+
74
+ 1. Comparing logits against the Python HuggingFace reference implementation
75
+ 2. Checking RoPE parameters (`rope_theta`, `partial_rotary_factor`, `head_dim`)
76
+ 3. Verifying activation functions (`Silu` vs `GeluPytorchTanh`)
77
+ 4. Inspecting MoE routing (expert selection, normalization, scaling)
78
+ 5. Validating weight loading prefixes (HF vs GGUF tensor name mappings)
79
+
80
+ ## File Reference
81
+
82
+ | File | Role |
83
+ |------|------|
84
+ | `.cursor/skills/add-model/SKILL.md` | The skill definition (read by the AI agent) |
85
+ | `src/models/<arch>.rs` | Model implementation (created by the agent) |
86
+ | `src/models/layers/moe.rs` | Shared MoE layer (FusedMoe, FusedMoeGGUF, ISQ, MXFP4) |
87
+ | `src/utils/mod.rs` | Config parsing and architecture registration (revised by the agent) |
88
+ | `src/core/runner.rs` | Model enum and dispatch macros (revised by the agent) |
@@ -0,0 +1,95 @@
1
+ # Claude Code + xInfer (Anthropic-compatible endpoint)
2
+
3
+ This guide connects Claude Code directly to xInfer using the built-in
4
+ Anthropic-compatible `/v1/messages` API. No proxy required.
5
+
6
+ ```
7
+ Claude Code -> xInfer (Anthropic-compatible)
8
+ ```
9
+
10
+ ## 1) Start xInfer on port 8000
11
+
12
+ ```bash
13
+ # Rust
14
+ xinfer --m Qwen/Qwen3-Coder-Next-FP8 --server --d 0,1
15
+
16
+ # Different model
17
+ xinfer --m Qwen/Qwen3.5-27B-FP8 --d 0 --server
18
+
19
+ # Python
20
+ python3 -m xinfer.server --m Qwen/Qwen3-Coder-Next-FP8 --d 0,1
21
+ ```
22
+
23
+ ## 2) Configure Claude Code
24
+
25
+ Install claude code
26
+
27
+ ```shell
28
+ npm install -g @anthropic-ai/claude-code
29
+ ```
30
+
31
+ or
32
+ ```shell
33
+ curl -fsSL https://claude.ai/install.sh | bash
34
+ ```
35
+
36
+ Export config
37
+
38
+ ```shell
39
+ export ANTHROPIC_BASE_URL="http://127.0.0.1:8000"
40
+ export ANTHROPIC_AUTH_TOKEN="sk-dummy"
41
+ export CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC=1
42
+ ```
43
+
44
+ Or make it permanent
45
+
46
+ Set `~/.claude/settings.json` (or copy from `example/claude/settings.json`):
47
+
48
+ ```json
49
+ {
50
+ "env": {
51
+ "ANTHROPIC_BASE_URL": "http://127.0.0.1:8000",
52
+ "ANTHROPIC_MODEL": "default",
53
+ "ANTHROPIC_SMALL_FAST_MODEL": "default",
54
+ "ANTHROPIC_AUTH_TOKEN": "sk-dummy",
55
+ "CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": "1"
56
+ }
57
+ }
58
+ ```
59
+
60
+ ## 3) Run Claude Code
61
+
62
+ run claude code
63
+
64
+ ```shell
65
+ claude
66
+ ```
67
+
68
+ or verify with a direct request (optional)
69
+
70
+ ```bash
71
+ curl http://127.0.0.1:8000/v1/messages \
72
+ -H "Content-Type: application/json" \
73
+ -d '{
74
+ "model": "default",
75
+ "max_tokens": 256,
76
+ "messages": [
77
+ {"role": "user", "content": "Hello from Claude Code"}
78
+ ]
79
+ }'
80
+ ```
81
+
82
+ ## Notes
83
+
84
+ - Streaming uses server-sent events (SSE) on `/v1/messages` with `stream: true`.
85
+ - Token counting is available at `POST /v1/messages/count_tokens`.
86
+ - Embeddings are not part of the Anthropic API and are not exposed here.
87
+
88
+ ### Trouble shooting
89
+
90
+ 1. Use the chat logger to monitor detailed interactions between Claude Code and xInfer.
91
+
92
+ ```shell
93
+ # Log into files (in folder ./log)
94
+ export XINFER_CHAT_LOGGER=1
95
+ ```
package/docs/docker.md ADDED
@@ -0,0 +1,73 @@
1
+ # xInfer Docker Image
2
+
3
+ This repository provides a Docker image for **xInfer**, a high-performance inference engine for large language models (LLMs), built using Rust and optimized for NVIDIA GPUs.
4
+
5
+ ---
6
+
7
+ ## Build Options
8
+
9
+ The image is built with the following features enabled by default:
10
+
11
+ ```text
12
+ cuda,nccl,python,flashinfer,cutlass
13
+ ```
14
+
15
+ CUDA graph capture is auto-enabled with `cuda`; use `--disable-cuda-graph` at runtime to skip it.
16
+ `flashinfer` or `flashattn` increases build time but improves long-context prefill/decoding performance.
17
+
18
+ For V100, remove `flashattn` or `flashinfer`, and `cutlass`.
19
+
20
+ For single-GPU machines, you may remove `nccl`.
21
+
22
+ For SM90+ GPUs, add feature `cutlass` will enable hardware FP8 acceleration.
23
+
24
+ ---
25
+
26
+ ## Docker Build
27
+
28
+ ## Build From Dockerfile
29
+
30
+ To build this Docker image locally, choose the feature list, compute capability and CUDA version:
31
+
32
+ Build from script:
33
+
34
+ ```bash
35
+ ./build_docker.sh "cuda,nccl,flashinfer,python" sm_80 12.9.0
36
+ ```
37
+
38
+ Build from command line:
39
+
40
+ ```bash
41
+ docker build --network=host -f "Dockerfile" -t "xinfer:latest" \
42
+ --build-arg CUDA_VERSION="12.9.0" \
43
+ --build-arg UBUNTU_VERSION="22.04" \
44
+ --build-arg CUDA_FLAVOR="cudnn-devel" \
45
+ --build-arg WITH_FEATURES="cuda,nccl,flashinfer,cutlass,python" \
46
+ --build-arg CUDA_COMPUTE_CAP="sm_80" \
47
+ # --build-arg CHINA_MIRROR="0" \ Use Rust crate mirror in Chinese mainland
48
+ .
49
+ ```
50
+
51
+ ## Run xInfer docker service
52
+
53
+ ### xInfer Help:
54
+ ```bash
55
+ docker run --rm -it --gpus all --network host xinfer:latest xinfer --help
56
+ ```
57
+ ### Run API server (make sure `--network host`):
58
+
59
+ ```bash
60
+ docker run --rm -it --gpus all --network host xinfer:latest xinfer --m Qwen/Qwen3-0.6B --server
61
+ ```
62
+
63
+ ### Run UI + API Server:
64
+ Run interactively:
65
+ ```bash
66
+ docker run --rm -it --gpus all --network host -v /home:/home -v /data:/data xinfer:latest bash
67
+ ```
68
+ Start the UI + API server
69
+ ```bash
70
+ xinfer --w /home/path/Qwen3-Coder-30B-A3B-Instruct-FP8 --ui-server
71
+ ```
72
+
73
+ **Note:** if `Ctrl+C` not working in docker, you need `Ctrl+P` then `Ctrl+Q` to stop the server.
@@ -0,0 +1,33 @@
1
+ # Embedding Usage
2
+
3
+ This repository now exposes OpenAI-style embeddings for text-only models (Qwen3, Qwen3-MoE, LLaMa, GLM4, Gemma3). Use the standard server run path and hit `/v1/embeddings`.
4
+
5
+ ## Start the server (embeddings enabled)
6
+ - CUDA example (Qwen3 text):
7
+ ```bash
8
+ target/release/xinfer --server --m Qwen/Qwen2.5-7B-Instruct
9
+ ```
10
+ - Metal example (LLaMa3 text):
11
+ ```bash
12
+ target/release/xinfer --server --m meta-llama/Llama-3-8b --max-model-len 32768
13
+ ```
14
+
15
+ ## Request examples
16
+ - Float embeddings (default) with mean pooling:
17
+ ```bash
18
+ curl -X POST http://localhost:8000/v1/embeddings \
19
+ -H "Content-Type: application/json" \
20
+ -d '{"input":"hello world","model":"default","embedding_type":"mean"}'
21
+ ```
22
+ - Base64-encoded embeddings with last-token pooling:
23
+ ```bash
24
+ curl -X POST http://localhost:8000/v1/embeddings \
25
+ -H "Content-Type: application/json" \
26
+ -d '{"input":["hello","hola"],"embedding_type":"last","encoding_format":"base64"}'
27
+ ```
28
+
29
+ ## Notes
30
+ - `model` defaults to the loaded model id; multiple models per request are not supported.
31
+ - Uses existing tokenizer; long prompts must fit `max_model_len` (same as chat).
32
+ - `embedding_type`: `mean` (default) averages tokens; `last` returns the final token hidden state.
33
+ - Responses mirror OpenAI schema: `data[].embedding`, `usage.prompt_tokens`.
@@ -0,0 +1,93 @@
1
+ # Get Started
2
+
3
+ This guide walks through building and running xInfer across CUDA/Metal, different model formats, multi-rank, PD Disaggregation, and OpenAI-compatible APIs. Commands assume repo root.
4
+
5
+ ## Build & features
6
+ - **Backends**: `--features cuda[,nccl,graph,flashinfer,cutlass]` or `--features metal`. CPU-only is supported but slow.
7
+ - **Quant/accel toggles**: `--kvcache-dtype fp8|turbo8|turbo4|turbo3` (KV cache quantization), `flashattn` or `flashinfer` (Ampere+), `--prefix-cache` (prefix KV reuse).
8
+ - **Python bindings**: add feature `python` when building wheels (`./build.sh --features python`).
9
+
10
+ ### Build (CUDA)
11
+ ```shell
12
+ # Remove `nccl` for single-gpu usage
13
+ # Remove `flashattn`, `flashinfer` and `cutlass` for V100 or older hardware
14
+ ./build.sh --release --features cuda,nccl,flashinfer,cutlass
15
+ ```
16
+
17
+ ### Build (Metal)
18
+ ```shell
19
+ cargo build --release --features metal
20
+ ```
21
+
22
+ ## Model formats
23
+ - **Safetensors (HF layout)**: `--m <hf_id>` for cached download, or `--w <local_dir>` for offline weights + configs.
24
+ - **Safetensors (HF layout) + ISQ**: in-situ quantize into GGUF with args `--isq <q4k|q2k|q6k|...>`.
25
+ - **GGUF**: `--f <gguf_file>`; no configs needed.
26
+ - **Vision-Language** (Qwen3-VL, Gemma3, Mistral3-VL): require image tokens; use `--ui-server` for uploads or send image_url/base64 in the request.
27
+
28
+
29
+ ## 3) Run patterns (single host)
30
+ - **CUDA text model (chat/server)**
31
+ ```bash
32
+ target/release/xinfer --m Qwen/Qwen2.5-7B-Instruct --max-model-len 131072 \
33
+ --kv-fraction 0.6 --ui-server
34
+ ```
35
+ - **Metal (Mac) text model**
36
+ ```bash
37
+ target/release/xinfer --m meta-llama/Llama-3-8b --max-model-len 32768 --ui-server
38
+ ```
39
+ - **GGUF quantized**
40
+ ```bash
41
+ target/release/xinfer --f /path/model-Q4_K_M.gguf --max-model-len 65536 ```
42
+ - **Embeddings** (same server; OpenAI `/v1/embeddings`)
43
+ ```bash
44
+ target/release/xinfer --m Qwen/Qwen2.5-7B-Instruct # curl -d '{"input":"hello","embedding_type":"mean"}' http://localhost:8000/v1/embeddings
45
+ ```
46
+ - **Multimodal**
47
+ ```bash
48
+ # Update image in the Chat UI
49
+ target/release/xinfer --m Qwen/Qwen3-VL-8B-Instruct --ui-server ```
50
+
51
+ Common runtime knobs: `--max-model-len`, `--max-num-seqs`, `--kv-fraction` (CUDA KV share), `--cpu-mem-fold` (CPU swap ratio), `--port`, `--kvcache-dtype` (fp8/turbo8/turbo4/turbo3), `--prefix-cache`, `--prefix-cache-max-tokens`, `--ui-server`, `--batch` (perf test).
52
+
53
+ Reasoning defaults to enabled when a request omits `thinking` / `enable_thinking`. Use `--disable-reasoning` on the Rust CLI to make the default be disabled instead; explicit request values still override the server default.
54
+
55
+ ## 4) Multi-rank (single node)
56
+ - **NCCL multi-GPU**
57
+ ```bash
58
+ target/release/xinfer --m Qwen/Qwen3-30B-A3B-Instruct-2507 --d 0,1 --max-num-seqs 2 --kv-fraction 0.5
59
+ ```
60
+ - **Graph capture**: CUDA graph is auto-enabled with `cuda` feature. Use `--disable-cuda-graph` at runtime to skip graph capture.
61
+
62
+ ## 5) PD Disaggregation (prefill/decoding split)
63
+ - **PD server (prefill host, usually memory-rich)**
64
+ ```bash
65
+ target/release/xinfer --pd-server --port 8000 \
66
+ --m Qwen/Qwen3-30B-A3B-Instruct-2507 ```
67
+ - **PD client (decode host)**
68
+ ```bash
69
+ target/release/xinfer --server --pd-client --pd-url 0.0.0.0:8000 \
70
+ --m Qwen/Qwen3-30B-A3B-Instruct-2507 ```
71
+ - Same weights/config on both ends; Local IPC used automatically on same node CUDA, TCP when `--pd-url` is set. Monitor logs for transfer and swap events.
72
+
73
+ ## Prefix cache
74
+ - Enabled by default (CUDA/Metal). Disable with `--disable-prefix-cache`. Prefix reuse is automatic; no `session_id` required.
75
+ - Use `--prefix-cache-max-tokens` to cap the cache size (rounded down to block size).
76
+ - Tune `--max-model-len`, `--kv-fraction`, `--cpu-mem-fold`; avoid overcommitting KV or cache will swap/evict.
77
+
78
+ ## APIs (OpenAI-style)
79
+ - Chat: `POST /v1/chat/completions` (supports `stream=true`, images for VL models).
80
+ - Embeddings: `POST /v1/embeddings` (`embedding_type=mean|last`, `encoding_format=float|base64`).
81
+ - Models: `GET /v1/models`; Usage: `GET /v1/usage?session_id=...`.
82
+ - UI: add `--ui-server` to expose the built-in web UI on port 8001.
83
+
84
+ ## Troubleshooting & tuning
85
+ - Use `--log` to view loading/progress; watch for “swap” messages (KV pressure).
86
+ - If OOM on Metal, lower `--max-model-len` and batch; on CUDA, reduce `--kv-fraction` or `--max-num-seqs`.
87
+ - For GGUF/ISQ, keep `--max-num-seqs` moderate to avoid bandwidth bottlenecks; `--kvcache-dtype fp8` is supported on all CUDA GPUs (SM70+) and Metal.
88
+ - Use the chat logger to monitor detailed interactions between client and xInfer.
89
+
90
+ ```shell
91
+ # Log into files (in folder ./log)
92
+ export XINFER_CHAT_LOGGER=1
93
+ ```
package/docs/goose.md ADDED
@@ -0,0 +1,83 @@
1
+ # Goose + xInfer (OpenAI-compatible endpoint)
2
+
3
+ This guide connects Goose (Rust `AI Agent`) directly to xInfer using the built-in OpenAI-compatible `/v1/chat/completions` API. No proxy required.
4
+
5
+ ```
6
+ Goose -> xInfer (OpenAI-compatible)
7
+ ```
8
+
9
+ ## 1) Start xInfer on port 8000
10
+
11
+ ```bash
12
+ # Rust
13
+ xinfer --m Qwen/Qwen3-Coder-Next-FP8 --server --d 0,1
14
+ # Python
15
+ python3 -m xinfer.server --m Qwen/Qwen3-30B-A3B-Instruct-2507 --d 0,1 --server
16
+ ```
17
+
18
+ ## 2) Configure Goose
19
+
20
+ ```shell
21
+ # For non-UI system,
22
+ export GOOSE_DISABLE_KEYRING=1
23
+ ```
24
+ Export empty API KEY
25
+
26
+ ```shell
27
+ export VLLM_API_KEY="empty"
28
+ ```
29
+
30
+ ### Download and install Goose: https://block.github.io/goose/docs/getting-started/installation/
31
+
32
+ ### Configure goose with `Custom Providers` and API key `empty`
33
+
34
+ ```shell
35
+ goose configure
36
+
37
+ ┌ goose-configure
38
+ │
39
+ ◇ What would you like to configure?
40
+ │ Custom Providers
41
+ │
42
+ ◇ What would you like to do?
43
+ │ Add A Custom Provider
44
+ │
45
+ ◇ What type of API is this?
46
+ │ OpenAI Compatible
47
+ │
48
+ ◇ What should we call this provider?
49
+ │ xinfer
50
+ │
51
+ ◇ Provider API URL:
52
+ │ http://127.0.0.1:8000/v1/
53
+ │
54
+ ◇ API key:
55
+ │ ▪▪▪▪▪
56
+ │
57
+ ◇ Available models (separate with commas):
58
+ │ default
59
+ │
60
+ ◇ Does this provider support streaming responses?
61
+ │ Yes
62
+ │
63
+ ◇ Does this provider require custom headers?
64
+ │ No
65
+ │
66
+ └ Custom provider added: xinfer
67
+ └ Configuration saved successfully to /root/.config/goose/config.yaml
68
+ ```
69
+
70
+ ### Run `goose` at any folder to work with your local server
71
+
72
+ ```shell
73
+ goose
74
+ ```
75
+
76
+ ### Trouble shooting
77
+
78
+ 1. Use the chat logger to monitor detailed interactions between Goose and xInfer.
79
+
80
+ ```shell
81
+ # Log into files (in folder ./log)
82
+ export XINFER_CHAT_LOGGER=1
83
+ ```