Cactus混合端云AI引擎的量化与端侧工具调用

Cactus是面向移动设备与可穿戴设备的混合端云AI引擎,支持LLM、VLM与语音转写推理,提供多种量化位宽与混合精度方案,并内置26m参数的端侧工具调用模型Needle,通过Swift、Kotlin、Flutter绑定接入。

# 仙人掌 ![Logo](<https://raw.githubusercontent.com/cactus-compute/engine/HEAD/assets/banner.jpg>) [![文档][docs-shield]][docs-url] [![网站][website-shield]][website-url] [![GitHub][github-shield]][github-url] [![HuggingFace][hf-shield]][hf-url] [![编辑][reddit-shield]][reddit-url] [![博客][blog-shield]][blog-url] 用于移动设备及可穿戴设备的混合边云型AI引擎. ``` ┌─────────────────┐ │ Cactus Engine │ ←── OpenAI-compatible APIs for text, speech, and vision. └─────────────────┘ │ ┌─────────────────┐ │ Cactus Graph │ ←── Zero-copy computation graph └─────────────────┘ │ ┌─────────────────┐ │ Cactus Kernels │ ←── CPU/GPU kernels for (Apple, Samsung, Pixel, etc.) └─────────────────┘ │ ┌─────────────────┐ │ Cactus Quants │ ←── Custom rotation-based quantization technique └─────────────────┘ ``` ## 快速演示( mac) - Step 1: `brew install cactus-compute/cactus/cactus` - Step 2: `cactus run` ## 仙人掌引擎( Cactus) ```cpp #include "cactus_engine.h" cactus_model_t model = cactus_init( "path/to/weight/folder", "path to txt or dir of txts for auto-rag", false ); const char* messages = R"([ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "My name is Henry Ndubuaku"} ])"; const char* options = R"({ "max_tokens": 50, "stop_sequences": ["<|im_end|>"] })"; char response[4096]; int result = cactus_complete( model, // model handle messages, // JSON chat messages response, // response buffer sizeof(response), // buffer size options, // generation options nullptr, // tools JSON nullptr, // streaming callback nullptr, // user data nullptr, // pcm audio buffer 0 // pcm buffer size ); ``` Gemma4- E2B 的示例响应 ```json { "success": true, // generation succeeded "error": null, // error details if failed "cloud_handoff": false, // true if cloud model used "response": "Hi there!", "function_calls": [], // parsed tool calls "segments": [], // transcription segments (empty for chat) "confidence": 0.8193, // model confidence "confidence_threshold": 0.7, // resolved handoff threshold (model-dependent) "time_to_first_token_ms": 45.23, "total_time_ms": 163.67, "prefill_tps": 1621.89, "decode_tps": 168.42, "ram_usage_mb": 245.67, "prefill_tokens": 28, "decode_tokens": 50, "total_tokens": 78 } ``` ## 仙人掌图 ```cpp #include "cactus_graph.h" CactusGraph graph; auto a = graph.input({2, 3}, Precision::FP16); auto b = graph.input({3, 4}, Precision::INT8); auto x1 = graph.matmul(a, b, false); auto x2 = graph.transpose(x1); auto result = graph.matmul(b, x2, true); float a_data[6] = {1.1f, 2.3f, 3.4f, 4.2f, 5.7f, 6.8f}; float b_data[12] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; graph.set_input(a, a_data, Precision::FP16); graph.set_input(b, b_data, Precision::INT8); graph.execute(); void* output_data = graph.get_output(result); graph.hard_reset(); ``` ## 推论速度 - LLM: Gemma-4 - E2B - CQ4(1k - 文本预填/解码为100令牌) - VLM : Gemma-4 - E2B - CQ4(256px图像编码时间/解码) - 转写:Parakeet-TDT-0.6B - CQ4(20s 音频 end-to-end 转写时间) - 1k - Context RAM:在LLM基准期内的顶峰 MB - 没有投机解码或MTP,纯解码 命令 : `cactus benchmark` [选项 `--ios` 或 时 间 `--android`] | 设备 | LLM | VLM | 转写 | RAM | |--------|-----|-----|------------|---------------| | 麦克M5 马克斯 | 2964tps / 154tps (英语). | 0.09s / 168tps (百分点) | 0.15s | 1348MB (英语). | | 麦克·M4 Pro 软件 | 1963tps / 101tps (英语). | 0.25s / 112tps (秒数) | 0.21s | 1225MB (英语). | | 麦克·M3 Pro 软件 | 1294tps / 64tps (英语). | 040s / 72tps (秒数) | 0.37s | 735MB (英语). | | iPad/Vision Pro M5 软件 | 1336tps / 71tps (英语). | 0.25s / 80tps (英语). | 0.27s | 703MB (英语). | | iPhone 17 Pro 软件 | 729tps / 37tps (英语). | 0.5s / 39tps (百分点) | 0.51s | 644MB (英语). | | iPhone 15 Pro 软件 | 517tps / 26tps (中文(简体) ). | 1.15s / 27tps (百分点) | 0.82s | 633MB (英语). | 编号/B:使用 1k - 为 100 预装和解码运行于 M5 Max - `LFM2.5-VL-1.6B` = 289吨/秒 - `Qwen3-1.7B` = 155吨/秒 - `LFM2.5-VL-450m` = 472吨/秒,图像编码为43ms - `LFM22.5-VL-230m` = 555吨/秒 ## 产出质量 - Gemma-4 -E2B-it精度跨位宽度,平均超过3种子. - CQ3 26和CQ2 .54是混合精度,CQ2 / CQ3 / CQ4是统一定量的. - 全面结果 [docs/cactus_quants.md](<https://github.com/docs/cactus_quants.md>): | 任务 | F16(原件) | CQ4 | CQ3.26 | CQ2.54 | CQ2 | |-----------|-----|-----|--------|--------|-----| | ARC-E (英语) | 73.80 | 73.73 | 74.20 | 68.20 | 50.80 | | ARC-C 数据交换中心 | 56.47 | 52.47 | 51.53 | 37.20 | 24.73 | | HellaSwag | 46.93 | 47.07 | 45.20 | 40.73 | 35.87 | | WinoGrande | 61.00 | 61.13 | 59.60 | 60.13 | 51.27 | | MMLU | 62.33 | 59.45 | 57.63 | 47.19 | 33.18 | | GPQA | 34.34 | 34.34 | 31.82 | 30.81 | 23.23 | | GSM8K | 73.67 | 71.20 | 66.20 | 22.00 | 0.40 | | HumanEval | 54.88 | 57.11 | 53.66 | 15.24 | 1.02 | | BFCL 简单 | 92.00 | 92.42 | 91.50 | 82.25 | 18.75 | | BFCL 多进制 | 89.00 | 88.33 | 89.00 | 52.50 | 13.67 | | BFCL 并行 | 84.00 | 83.67 | 82.50 | 30.00 | 3.33 | | BFCL 平行-多功能 | 78.00 | 83.33 | 82.00 | 37.00 | 1.33 | ## 支持的模型 - 任何 HuggingFace 模型都可以使用 `cactus convert [HF-Name]`虽然是实验性的。 - 液态,Gemma,低语,鹦鹉和Quen模型家族尤其受到考验. - 一些型号已经预装 [这儿](https://huggingface.co/Cactus-Compute)运行中 `cactus download [HF-Name]`. - `cactus run [HF-Name]` 虽然没有找到, 则会首先下载或转换模型。 ## 针头 [针头](https://github.com/cactus-compute/needle) 是一个用于设备上的工具的 26m 参数模型,它要求: ```bash cactus run Cactus-Compute/needle [--tools my_tools.json] # OpenAI function-calling format; demo toolset by default ``` ## 学习更多 | 参考 | 语言 | 说明 | |-----------|----------|-------------| | [仙人掌引擎( Cactus)](<https://github.com/docs/cactus_engine.md>) | C | 聊天完成、流线、工具调用、转录、嵌入、RAG、视觉、矢量索引、云传递 | | [仙人掌图](<https://github.com/docs/cactus_graph.md>) | C++ | 十进制操作,矩阵乘法,注意,正常化,激活功能 | | [仙人掌内核](<https://github.com/docs/cactus_kernels.md>) | C++ | ARM NEON SIMD 内核用于matmul,注意,演化,量化,DSP,图像处理 | | [仙人掌泉水库](<https://github.com/docs/cactus_quants.md>) | C++ | 旋转和编码本的量化从 4 - bit 到 1 - bit 用于所有重量的 百分数 | | [仙人掌混合物](<https://github.com/docs/cactus_hybrid.md>) | C/Python | 根据本地模型置信度自动将困难查询路径到云 | | [Python 软件包](<https://github.com/python/>) | Python 键 | Python 软件包和 CLI | ## 约束 - [挥动](<https://github.com/bindings/swift/>) - [科特林语Name](<https://github.com/bindings/kotlin/>) - [小蝶,小蝶,小蝶,小蝶,小蝶,宝贝](<https://github.com/bindings/flutter/>) - [原生反应](<https://github.com/bindings/react-native/>) - [Python 键](<https://github.com/bindings/python/>) - [锈](<https://github.com/bindings/rust/>) ## 使用此还原 ``` ┌────────────────────────────────────────────────────────────────────────────────┐ │ │ │ Step 0: if on Linux (Ubuntu/Debian) │ │ sudo apt-get install python3.12 python3.12-venv python3-pip cmake │ │ build-essential libcurl4-openssl-dev │ │ │ │ Step 1: clone and setup │ │ git clone https://github.com/cactus-compute/cactus && cd cactus │ │ source ./setup │ │ │ │ Step 2: use the commands │ │────────────────────────────────────────────────────────────────────────────────│ │ │ │ cactus auth manage cloud API key │ │ --status show key status │ │ --clear remove saved key │ │ │ │ cactus run [model|path] run a model (downloads if needed) │ │ --bits 1|2|3|4|2.54|3.26 CQ quantization (default: 4) │ │ --backend cpu|metal inference backend (default: auto) │ │ --image <path> image file for VLM inference │ │ --audio <path> audio file for audio chat │ │ --system <prompt> system prompt │ │ --prompt <text> send prompt immediately │ │ --tools <json|file> tool definitions for tool calling │ │ --thinking enable thinking/reasoning mode │ │ --token <token> HuggingFace token (gated models) │ │ --reconvert force local rebuild from source │ │ │ │ cactus transcribe [model] live microphone transcription with a model│ │ --file <audio.wav> audio file to transcribe (WAV) │ │ --language <code> language code (default: en) │ │ --bits 1|2|3|4|2.54|3.26 CQ quantization (default: 4) │ │ --token <token> HuggingFace token (gated models) │ │ --reconvert force local rebuild from source │ │ │ │ cactus download [model] get a bundle (prebuilt, else build) │ │ --bits 1|2|3|4|2.54|3.26 CQ quantization (default: 4) │ │ --token <token> HuggingFace token (gated models) │ │ --reconvert refresh cached bundle │ │ │ │ cactus convert <model> [dir] HuggingFace -> Cactus CQ weights │ │ --bits 1|2|3|4 CQ quantization (default: 4) │ │ --token <token> HuggingFace token (gated models) │ │ --reconvert force weight conversion from source │ │ --lora <path> merge a LoRA adapter before converting │ │ --weights-only stop after CQ weights (skip the graph) │ │ --artifact-dir <path> bundle output (default: weights/<model>) │ │ │ │ cactus serve [model] OpenAI-compatible local HTTP server │ │ --host <addr> bind address (default: 127.0.0.1) │ │ --port <port> port (default: 8080) │ │ --bits 1|2|3|4|2.54|3.26 CQ quantization (default: 4) │ │ --backend cpu|metal inference backend (default: auto) │ │ --token <token> HuggingFace token (gated models) │ │ --reconvert force local rebuild from source │ │ --no-cloud-handoff disable automatic cloud handoff │ │ --confidence-threshold <0..1> handoff to cloud below this confidence │ │ --cloud-timeout-ms <n> max wait for cloud handoff │ │ │ │ cactus code run the AI coding agent (TUI / print) │ │ --serve-model <id> auto-start a server with this model │ │ --bits 1|2|3|4|2.54|3.26 CQ quantization (default: 4) │ │ --backend cpu|metal inference backend (default: auto) │ │ --token <token> HuggingFace token (gated models) │ │ --reconvert force local rebuild from source │ │ --host <addr> server address (default: 127.0.0.1) │ │ --port <port> server port (default: 8080) │ │ --no-serve require a running server (no auto-start) │ │ --no-cloud-handoff disable automatic cloud handoff │ │ --confidence-threshold <0..1> handoff to cloud below this confidence │ │ --cloud-timeout-ms <n> max wait for cloud handoff │ │ -- <args...> pass remaining args to the agent │ │ │ │ cactus list list downloaded models │ │ │ │ cactus build build cactus libraries │ │ --apple Apple (iOS/macOS) │ │ --android Android │ │ --python shared lib for Python FFI │ │ │ │ cactus test run the test suite │ │ --component <name> kernels | graph | engine | all │ │ (default: all) │ │ --model <hf-id> default: google/gemma-4-E2B-it │ │ --transcription-model <hf-id> default: nvidia/parakeet-tdt-0.6b-v3 │ │ --bits 1|2|3|4|2.54|3.26 CQ quantization (default: 4) │ │ --backend cpu|metal inference backend (default: auto) │ │ --token <token> HuggingFace token (gated models) │ │ --reconvert force local rebuild of test models │ │ --suite <name> run a single test suite by name │ │ (resolved across components, │ │ e.g. llm → engine) │ │ --list list components and suites │ │ --ios run on connected iPhone │ │ --android run on connected Android │ │ --enable-telemetry send cloud telemetry (off by default) │ │ │ │ cactus benchmark run the engine benchmark suite │ │ --model <hf-id> default: google/gemma-4-E2B-it │ │ --transcription-model <hf-id> default: nvidia/parakeet-tdt-0.6b-v3 │ │ --bits 1|2|3|4|2.54|3.26 CQ quantization (default: 4) │ │ --backend cpu|metal inference backend (default: auto) │ │ --ios run on connected iPhone │ │ --android run on connected Android │ │ │ │ cactus clean delete build artifacts, weights, venv │ │ cactus --help show this help │ │ │ └────────────────────────────────────────────────────────────────────────────────┘ ``` ## 引用 如果您在研究中使用仙人掌,请引用如下: ```bibtex @software{cactus, title = {Cactus: AI Inference Engine for Phones & Wearables}, author = {Ndubuaku, Henry and Cactus Team}, url = {https://github.com/cactus-compute/cactus}, year = {2025} } ``` **N/B:** 向上滚动,点击盾牌链接获取资源! [docs-shield]: https://img.shields.io/badge/Docs-555?style=for-the-badge&logo=readthedocs&logoColor=white [docs-url]: https://cactus-compute.github.io/cactus/ [website-shield]: https://img.shields.io/badge/Website-555?style=for-the-badge&logo=safari&logoColor=white [website-url]: https://cactuscompute.com/ [github-shield]: https://img.shields.io/badge/GitHub-555?style=for-the-badge&logo=github&logoColor=white [github-url]: https://github.com/cactus-compute/cactus [hf-shield]: https://img.shields.io/badge/HuggingFace-555?style=for-the-badge&logo=huggingface&logoColor=white [hf-url]: https://huggingface.co/Cactus-Compute [reddit-shield]: https://img.shields.io/badge/Reddit-555?style=for-the-badge&logo=reddit&logoColor=white [reddit-url]: https://www.reddit.com/r/cactuscompute/ [blog-shield]: https://img.shields.io/badge/Blog-555?style=for-the-badge&logo=hashnode&logoColor=white [blog-url]: https://cactuscompute.com/blog

查看原始来源