mizorewww /laya-mlx

Native MLX runtime for Laya typed decision models — 7–14 ms short decisions on M3 Max. No text generation, PyTorch, or cloud API.

★ 7,086⑂ 574👁 21⑃ 17● 10▤ 4◈ 0
PythonApache-2.0Created 2026-09-19pypi.org/project/laya-mlx/
7,086
stars
+7+24+307
574
forks
21
watchers
17
prs
10
issues
4
releases
0
discussions

README

Laya-MLX

开放权重的类型化决策模型,在 Apple Silicon 上原生运行。

English · 使用参考 · 基准测试 · Hugging Face 权重 · 更新日志

Laya 专门回答有固定答案范围的问题:从几个选项里挑一个、按评分标准打分、判断一句话真不真。
它只做一次双向前向计算,直接给出校准后的概率,不逐个生成 token,也不需要解析输出的 JSON。
本项目用 MLX 运行 Laya
原版权重,不依赖 PyTorch、Transformers 或任何云端 API。

M3 Max、FP16 下,一个简短的英文问题中位延迟 13.4 ms,多语言模型 7.4 ms。

!Laya MLX 玩贪吃蛇:真实决策,原速录制

 快速开始

需要 Apple Silicon、macOS 14+ 和 Python 3.11+。

bash
pip install laya-mlx


python
import laya_mlx as laya

agent = laya.load("aac6fef/laya-mlx")
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


像上面这样给每个选项写上说明,效果远好于只给标签名。首次加载会下载模型,之后的推理完全在本地进行。

 问题类型

| 类型 | 你提供 | 你得到 |
|---|---|---|
| choice | 若干选项:列表,或「标签 → 说明」字典 | 每个选项的概率 |
| score | 有序的评分档位 | 每档的概率和期望档位 |
| noul | 一个是非命题 | 为真的概率 |

输入状态可以是文本、类 JSON 字典或对话列表。每个答案都附带校准概率和置信度;
在新任务上,置信度高不代表一定答对。

 模型

| 预转换 MLX 版(FP16) | 原版 | 参数量 | 上下文 | 用途 |
|---|---|---:|---:|---|
| aac6fef/laya-mlx | convaiinnovations/laya | 421M | 512 | 英文 |
| aac6fef/laya-multilingual-mlx | convaiinnovations/laya-multilingual | 322M | 1,024 | 多语言输入 |
| aac6fef/laya-typed-decisions-mlx | convaiinnovations/laya-typed-decisions | 421M | 1,024 | 上游 typed-decisions 工作流 |

laya.load 两列的 ID 都能用。上下文长度把指令、选项和状态算在一起。
每个发布的模型都带有模型卡、验证结果、来源说明和校验和(固定版本与哈希)。

 其他功能

详见使用参考:

- 语言路由:Router() 自动识别语言、选择模型,默认最多常驻两个;也可以注册自己的模型。
- 批量与长文档:predict_batch 对问题结构相同的请求共用前向计算;长文本扫描会保留答案出自哪一段。
- 置信度门槛:min_confidence= 把低置信度答案标记出来供人工复核,答案本身不变。
- 超多选项:predict_shortlist(先用向量筛选)和 predict_tournament(分组淘汰,不需要嵌入模型)。
- 吞吐优化:重复负载可开启 compile=True、pad_to_multiple=16、cache_prompts=True。
- 工具与服务:命令行(laya-mlx predict)、HTTP(laya-mlx-serve)、MCP(laya-mlx-mcp-server)、评估与校准,以及 LangChain/LlamaIndex/CrewAI/Agno 集成,按需安装 extras。
- 导出:laya-mlx convert 生成独立的 MLX 模型目录。

 性能

M3 Max(40 核 GPU,128 GiB),FP16,端到端:

| | Laya 421M | 多语言 322M |
|---|---:|---:|
| 单个短问题 P50 / P95 | 13.42 / 13.92 ms | 7.39 / 7.79 ms |
| 50 个问题吞吐 | 146.8 问/秒 | 395.0 问/秒 |
| 单个问题 MLX 峰值内存 | 943.6 MiB | 687.6 MiB |

计时包含提示词构造、分词、推理、校准和结果格式化,不含模型加载。吞吐一行使用
batch_size=64(默认 16)。实际延迟随输入长度、问题数量和系统负载变化。

三个模型在 FP32 和 FP16 下都与上游在 63/63 道验证题上选出相同答案(共 378/378 次比对),
重复调用结果一致,未测到内存增长。这衡量的是移植是否忠实,不是任务准确率。
测量方法、全部计时样本与概率误差。

 贪吃蛇演示

上面的 GIF 是一次真实的本地运行,原速录制:每一步都向 Laya 提三个问题,
画面上可见的安全层会拦下危险的走法。

bash
pip install 'laya-mlx[demo]'
hf download aac6fef/laya-multilingual-mlx
laya-snake                       按节奏运行;加 --max-speed 取消节奏限制
laya-snake --optimize --max-sp

Latest release

laya-mlx v0.6.0 v0.6.0 2026-10-29
What's Changed
 feat: port upstream Laya v0.4.17 behavior to MLX by @Omicron0314 in https://github.com/mizorewww/laya-mlx/pull/25
 Fix typed batch grouping and hook timeout serialization by @JurhoRr in https://github.com/mizorewww/laya-mlx/pull/26

 New Contributors
 @Omicron0314 made their first contribution in https://github.com/mizorewww/laya-mlx/pull/25
 @JurhoRr made their first contribution in https://github.com/mizorewww/laya-mlx/pull/26

Full Changelog: https://github.com/mizorewww/laya-mlx/compare/v0.5.0...v0.6.0

Snapshot: 2026-11-10T09:35:25Z · Data license Apache-2.0 · JSON