laya-coreml README

项目的 README,来自 github.com/mizorewww/laya-coreml。合并为一个 markdown 文件:README.md。

Laya-CoreML

Open-weight typed decisions on Apple Silicon: Core ML, Neural Engine, zero generated tokens.

中文 · Usage reference · Benchmarks · PyPI · Hugging Face weights · Changelog

Laya answers constrained questions (pick an option, score against a rubric, judge true or false) with calibrated probabilities from one bidirectional forward pass. It generates no tokens and returns no JSON to parse. This package runs the original Laya weights as Core ML programs on the CPU, GPU or Neural Engine. Inference needs no PyTorch, Transformers or MLX.

4.98 ms median for one short multilingual decision on the M3 Max Neural Engine, using 2.78× less system energy than compiled MLX.

Laya Core ML playing Snake with real local model probabilities

Quick start

Requires Apple Silicon, macOS 15+ and Python 3.11–3.13.

pip install laya-coreml
import laya_coreml as laya

agent = laya.load("aac6fef/laya-multilingual-coreml-adaptive-fp16")
result = agent.predict(
    "I was billed twice. Please refund the duplicate.",
    {
        "department": {
            "type": "choice",
            "instructions": "Which department should handle this request?",
            "criteria": {
                "billing": "Payments, invoices, refunds, and duplicate charges.",
                "technical": "Broken features, errors, and product troubleshooting.",
                "sales": "Pricing, upgrades, and new purchases.",
            },
        },
        "refund": {
            "type": "noul",
            "instructions": "Does the customer ask for money back?",
        },
    },
)
print(result["answers"]["department"]["choice"])  # billing

Describing each option, as above, works much better than bare labels. The bundle downloads before first use; predictions after that are local. Pass local_files_only=True to forbid network access.

Question types

Type You provide You get
choice Named options, as a list or a label → description dict Probability per option
score Ordered rubric levels Probability per level and the expected level
noul A yes/no proposition P(true)

States can be text, JSON-like dicts or conversation lists. Each answer carries calibrated probabilities and confidence fields; confidence does not guarantee accuracy on a new task.

Model bundles

All bundles are under aac6fef/ on Hugging Face.

Bundle Engine Capacity Use
laya-multilingual-coreml-adaptive-fp16 (also -fp32) CPU + GPU 1,024 tokens, native batch 1/2/4/16 Recommended general multilingual model
laya-coreml-adaptive-fp16 / -fp32 CPU + GPU 512 tokens, native batch 1/2/4/16 English, 421M
laya-typed-decisions-coreml-adaptive-fp16 / -fp32 CPU + GPU 1,024 tokens, native batch 1/2/4/16 Upstream typed-decisions workflows
laya-coreml, laya-multilingual-coreml, laya-typed-decisions-coreml CPU + GPU 512 / 1,024 tokens, batch 1 Original single-graph bundles; Router() defaults
laya-multilingual-coreml-ane CPU + Neural Engine 96 tokens total, batch 1 Fastest short decisions, FP16
laya-multilingual-coreml-ane-w8 CPU + Neural Engine 96 tokens total, batch 1 Approximate 8-bit palette weights
laya-multilingual-coreml-snake CPU + GPU 64 tokens, batch 3 The Snake demo's three questions

The ANE bundles' 96-token limit covers question, options and state together. Longer state is truncated, while oversized questions or option sets raise. Every bundle includes its tokenizer, model card, provenance, checksums and validation results. Choosing a bundle, pinned revisions and offline use.

What else it does

All of these are covered in the usage reference:

Performance

M3 Max (40-core GPU, 128 GiB). One 91-token question padded to 96, end to end, from 65,598 stable calls in alternating 20-second blocks:

Compiled MLX FP16 Core ML ANE FP16 Core ML ANE W8
P50 / P95 6.94 / 7.39 ms 4.98 / 5.31 ms 4.88 / 5.23 ms
System energy per decision 0.4288 J 0.1540 J 0.1344 J
Speed / energy vs MLX 1× / 1× 1.39× / 2.78× 1.42× / 3.19×

Timings include prompt preparation, tokenization, inference, calibration and formatting; loading and warm-up are excluded. Energy comes from SMC power-sensor readings and is an estimate. The ANE advantage is for short inputs: a 1,024-token ANE graph takes about 91.7 ms, and a full Snake game is not consistently faster than compiled MLX. Speed, energy and hardware evidence · CPU/GPU benchmarks.

Port fidelity: the general bundles match upstream's selected answer on all 63 validation questions per checkpoint in FP32 and FP16, with 100 stable repeated calls each. The ANE FP16 bundle matches all 59 questions that fit in 96 tokens. These are conversion-fidelity checks, not task accuracy.

Snake demo

The GIF above replays a recorded Core ML run at 1× speed, with live probabilities, latency and a visible safety layer.

pip install 'laya-coreml[demo]'
hf download aac6fef/laya-multilingual-coreml-ane --local-dir models/snake
laya-coreml-snake --model ./models/snake

The terminal must be at least 104 × 35. Space pauses, ↑/↓ changes speed, R resets, Q quits. First Core ML initialization can take tens of seconds. The full loop sustained 49.1–50.0 decisions/s over three uncapped 600-step episodes with zero deaths. Demo guide · measured rates.

Relationship to upstream

This is an independent port, not an official Convai Innovations or Apple release. Prompt formatting, calibration, routing, output schema and host APIs follow NandhaKishorM/laya v0.5.10. Only the tensor, artifact, device and resource layers are Core ML-specific. Core ML bundles do not expose encoder embeddings; the vegaml engine (upstream's default since v0.5.0, and its Learner and Escalator) and training stay upstream; Agent() and load() here load the classic checkpoint. A sibling port for MLX is laya-mlx. Native boundaries · upstream compatibility record · all documentation.

Contributing

Bug reports and pull requests are welcome. Host behavior must stay traceable to upstream. See CONTRIBUTING.md for the development setup, tests, conversion and release process.

License

Apache-2.0; see LICENSE and NOTICE. Laya and its pretrained weights are by Convai Innovations and upstream contributors.

laya-coreml (mizorewww/laya-coreml)

正在搜索数据库…

这是什么东西?它是 Laya 的开放权重 Python 移植版,用来回答选择题——从选项里挑一个、给出等级评分,或回答是/否——在 Apple Silicon 上通过 Core ML 和神经网络引擎本地运行,完全不生成文本 token。
那它是我写代码调用的 Python 库,还是我直接运行的应用?是一个 Python 库:从 PyPI 安装 laya-coreml,然后在代码里调用它的 Agent、ANEAgent 和 Router 类(也附带一个命令行工具)。还有一个可选的终端演示,用模型玩贪吃蛇。
我该怎么把它跑起来?在一台 Apple Silicon Mac(macOS 15+,Python 3.11–3.13)上运行 pip install laya-coreml;模型权重会从 Hugging Face 下载一次。可选的贪吃蛇演示需要 104 列 × 35 行的终端窗口。
我还需要另外装 PyTorch 或 Transformers 吗?不需要。推理本身不依赖 PyTorch、Transformers 或 MLX——这些重型框架只用于可选的附加功能,比如温度拟合、训练辅助工具,以及转换你自己的检查点。
那个高速的神经网络引擎模式有什么限制?ANE 的模型包把单次请求限制在总共 96 个 token——包括问题、选项和状态——超长的输入会直接失败。长文本要用 1024 token 的 CPU+GPU 模型包;1024 token 的 ANE 导出版每次请求大约耗时 91.7 毫秒,所以 ANE 适合短决策。
所以它只能在 Mac 上运行吗?实际上是的:推理走的是 Core ML 和神经网络引擎,所以需要一台运行 macOS 15+ 的 Apple Silicon Mac。仓库的可移植测试能在 Linux CI 上跑,但真正的 Core ML 推理只能在 macOS 上进行。
它能离线工作吗,还是会把我的问题发送到别处?首次从 Hugging Face 下载模型之后就能完全离线运行;之后的每一次预测都留在你自己的电脑上。传入 localfilesonly=True 可以强制只使用已有的缓存。
许可证宽松吗?这是 Apple 或 Convai 的官方发布吗?是 Apache-2.0,可以商用。它是 Laya(原作者是 Convai Innovations 及其贡献者)的独立社区移植版,并非 Convai 或 Apple 的官方发布。
它到底有多快?在 M3 Max 上,ANE FP16 模型回答一个短问题的耗时为 4.98 毫秒 P50 / 5.31 毫秒 P95,W8 版本为 4.88 毫秒,而编译后的 MLX FP16 是 6.94 毫秒。每次决策的整机能耗比 MLX 低 2.78 倍(W8 版为 3.19 倍)。这些是单个问题的数据,不是贪吃蛇整帧的耗时。
我可以从 Hugging Face 拉取哪些模型包?一共六个:原始英文 421M(512 token 上下文)、一个 322M 多语言模型和一个 421M 类型化决策模型(各 1024 token)、一个贪吃蛇 GPU 包,以及两个神经网络引擎多语言版本——FP16 和一个近似 W8 调色版。每个包都自带分词器、配置、模型卡、校验和以及所需的张量。
Core ML 移植版的回答和原模型完全一致吗?在验证测试集上是的:三个通用 FP16 检查点在 189 道题上与上游结果完全一致,各自还通过了 100 次相同的重复调用。ANE FP16 版本在校准概率上最多偏差约 0.003,W8 约 0.014,都在 0.02 的门槛之内——不过这只是测试集上的保真度,并不代表真实任务准确率已得到验证。
Router 类有什么用?它会把多个 Core ML 模型包常驻内存,并把每个请求路由到合适的那一个;语言未确定时默认用多语言模型;route() 可以在不加载权重的情况下检查请求。调用方可以设置一个 minconfidence 门槛(默认 0.7),把低置信度的结果标记为弃权,而庞大的选项集则通过锦标赛淘汰法处理。
作者
mizorewww
大小
6.8 MB
星标
1.6K
时长
2个月
提交 · 90 天