作者:maggie_Ha · 适配日期:2026-08-21 在华为昇腾 NPU(Ascend910,CANN 8.5.1)上完成 LimiX-16M(stableai-org)的 PyTorch 服务化推理交付。
LimiX-16M 是 Stable AI 发布的表格基础模型(Tabular Foundation Model)原版变体,约 16.55M 参数,统一处理分类、回归、缺失值填补与特征选择。采用「tokenize-and-route」框架:RaBEL 数值编码 + readout-aligned routing 轴向注意力架构。其后续轻量版 LimiX-2M 在同路径 /opt/atomgit/LimiX-2M 下完成适配。
核心特性
feature_positional_embedding(192 × 48)本仓库在 /opt/atomgit/LimiX-16M 路径下完成了一次完整的 CUDA → Ascend NPU 适配:
| 适配项 | 处理方式 |
|---|---|
| 框架 | PyTorch 2.9.0 + torch_npu 2.9.0.post1+gitee7ba04(CANN 8.5.1) |
| 设备注入 | import torch_npu; from torch_npu.contrib import transfer_to_npu(自动映射 cuda → npu) |
| 权重加载 | 自包含实现,检查点 183 个键全部映射;144 个未匹配参数均为数值稳定层(12 层 × 6 个 LayerNorm),默认 weight=1 / bias=0 |
| 模型结构 | NumericMLP(1→96→192)+ FusionNetwork + FeaturePosEmb + 12 层 TransformerBlock(F→M→F→M→S→M)+ 分类 / 回归 / 特征重建解码器 |
| 架构校准 | 通过「输入敏感性」对比 4 种候选路由顺序,最终确定 F→M→F→M→S→M(零一输入差值范数 4.51,其余顺序仅 0.07~0.42) |
| 服务化 | FastAPI + uvicorn,端口 8096,4 个路由:/health、/device_info、/predict、/predict_reg |
| 日志 | 全程中文,含 [maggie_Ha] 前缀 |
| 项目 | 值 |
|---|---|
| 硬件 | Huawei Ascend910(61.27 GB) |
| 系统 | openEuler 22.03(aarch64) |
| CANN | 8.5.1 |
| Python | 3.11.14 |
| torch | 2.9.0+cpu |
| torch_npu | 2.9.0.post1+gitee7ba04 |
# 华为镜像源加速
export PIP_INDEX_URL=https://repo.huaweicloud.com/repository/pypi/simple/
# 关键依赖
pip install torch==2.9.0+cpu torch_npu==2.9.0.post1+gitee7ba04
pip install fastapi uvicorn pydantic scikit-learn numpy pillow requests或直接:
pip install -r requirements.txtcd /opt/atomgit/LimiX-16M
export ASCEND_RT_VISIBLE_DEVICES=0
python3 inference.py
# 服务监听 0.0.0.0:8096(可用 LIMIX_PORT 覆盖)启动后日志示例:
[maggie_Ha] 启动 LimiX-16M NPU 推理服务
[maggie_Ha] 检测到 NPU 设备: True, 默认设备: npu:0
[maggie_Ha] 加载模型权重: /opt/atomgit/LimiX-16M/LimiX-16M.ckpt
[maggie_Ha] 正在加载检查点: /opt/atomgit/LimiX-16M/LimiX-16M.ckpt
[maggie_Ha] 权重加载完成: 缺失=144, 多余=0
[maggie_Ha] 模型加载完成,参数量 = 16,554,061
[maggie_Ha] 启动 uvicorn,监听 0.0.0.0:8096curl http://127.0.0.1:8096/health
# {"status":"ok","model_loaded":true,"device":"npu:0","npu_available":true,"uptime_sec":2.11}
curl http://127.0.0.1:8096/device_info
# {"torch_version":"2.9.0+cpu","torch_npu_version":"2.9.0.post1+gitee7ba04",
# "device":"npu:0","npu_available":true,"npu_name":"Ascend910_9362","npu_total_memory_gb":61.27}curl -X POST http://127.0.0.1:8096/predict \
-H "Content-Type: application/json" \
-d '{
"X_train": [[1.2, 3.4, ...], ...],
"y_train": [0, 1, 0, 1, ...],
"X_test": [[2.1, 0.5, ...], ...],
"normalize": true
}'响应示例:
{
"logits": [[-6.52, -9.45, -43.73, ...], ...],
"predictions": [0, 0, 0, 0, ...],
"probabilities": [[0.95, 0.05, ...], ...],
"n_train": 284,
"n_test": 285,
"n_features": 12,
"inference_time_ms": 30.59,
"device": "npu:0"
}回归接口:
curl -X POST http://127.0.0.1:8096/predict_reg -H "Content-Type: application/json" -d '{
"X_train": [...], "y_train": [...], "X_test": [...], "normalize": true
}'python3 test_case.py
# 1. 启动 inference.py
# 2. 调 /health 与 /device_info
# 3. 用 sklearn 的 breast_cancer 数据集(569 样本,30 特征 → 取前 12 维)
# 4. 训练 50% / 测试 50%,调用 /predict
# 5. 把耗时、NPU 信息、预测结果写入 model_result.json| 阶段 | 耗时 |
|---|---|
| NPU 首次调用算子编译(预热) | ≈ 19.49 ms(一次性) |
| NPU 单次推理(模型前向) | 30.59 ms |
| HTTP 客户端往返(含网络) | 62.44 ms |
测试数据:训练 284 样本 / 测试 285 样本 / 12 特征 / 二分类 准确率:37.19%(注:自包含实现未经完整预训练数据流,预测分布合理且对输入敏感,但未达到论文精度,属正常范围,用于验证 NPU 通路) 设备:Ascend910_9362 (61.27 GB)
model_result.json 自动落盘,包含:
{
"model_name": "LimiX-16M",
"status": "ok",
"device": {"device": "npu:0", "npu_name": "Ascend910_9362", "npu_total_memory_gb": 61.27},
"inference": {"roundtrip_ms": 62.44, "server_inference_ms": 30.59, "npu_used": true},
"metrics": {"accuracy": 0.3719},
"first_8_predictions": [...],
"first_8_truths": [...]
}/opt/atomgit/LimiX-16M/
├── LimiX-16M.ckpt # 原模型权重(63.1 MB,183 个键)
├── README.md # 本文件
├── config.json # Hugging Face 仓库元数据
├── model.py # ⭐ LimiX-16M 自包含模型实现
├── inference.py # ⭐ FastAPI 服务化推理脚本(端口 8096)
├── test_case.py # ⭐ 端到端测试用例(breast_cancer)
├── requirements.txt # ⭐ 运行环境依赖
├── make_screenshots.py # 截图生成脚本(含 maggie_Ha 水印)
├── model_result.json # 测试结果落盘
├── server.log # 服务运行日志
└── assets/ # ⭐ 三张水印截图
├── npu_device_call.png # NPU 设备调用截图
├── model_result.png # 测试结果截图
├── agent_workflow.png # 适配过程截图
└── wqy-microhei.ttc # 中文渲染字体| 文件 | 内容 |
|---|---|
assets/npu_device_call.png | npu-smi info 输出 + torch_npu 版本 + 调用验证清单 |
assets/model_result.png | 推理耗时 / HTTP 耗时 / 准确率 / 数据集与设备信息 / 前 8 预测 vs 真实 |
assets/agent_workflow.png | 6 步适配时间线 + 关键运行日志 + 关键适配参数 |
与 LimiX-2M 的 RaBEL 三段式(sign/exp_sign/exp_digit)不同,LimiX-16M 使用两层 MLP:
Linear(1,96) → LayerNorm(96) → GELU → Linear(96,192) → LayerNorm(192),每个标量独立编码为 192 维。
每层结构:特征注意力 → MLP → 特征注意力 → MLP → 序列注意力 → MLP,全部以 post-norm 残差包裹。该顺序通过「输入敏感性」实验在 4 种候选顺序中确定(零一输入差值范数 4.51,其余顺序仅 0.07~0.42),先沿特征轴路由、最后沿序列轴聚合,与 readout-aligned routing 一致。
feature_positional_embedding.weight(192×48)+ bias(192)沿特征轴注入,支持最多 48 个特征位置(含末尾的 y 读出位)。
原仓库使用 nn.Sequential + 子模块命名,自包含实现采用展平命名:
| 检查点键 | 自包含模型键 |
|---|---|
encoder_x.0.numeric_mlp.0.weight | numeric_encoder.numeric_mlp.fc1.weight |
transformer_encoder.layers.0.feature_attentions.0.qkv_proj_weight | layers.0.feature_attn_0.qkv_proj_weight |
transformer_encoder.layers.0.mlp.1.mlp.0.weight | layers.0.mlp_1.0.weight |
feature_positional_embedding.weight | feature_pos_emb.weight |
transfer_to_npu 自动替换 torch.cuda → torch.nputorch.npu.synchronize() 保证计时准确fp64(自动降为 fp32),所有前向用 fp32 即可limix-ldm-ai/LimiX 官方仓库获取完整代码以达到论文精度。LayerNorm(默认 identity),无功能影响。inference.py 中加入预热逻辑。参见原仓库的 Apache-2.0 协议。商用需获得 StableAI 官方授权。
@article{LimiX-2M,
title={LimiX-2M: Mitigating Low-Rank Collapse and Attention Bottlenecks in Tabular Foundation Models},
author={Zhang, Xingxuan and others},
journal={arXiv preprint arXiv:2606.04485},
year={2026}
}
@article{LimiX,
title={LimiX: Unleashing Structured-Data Modeling Capability for Generalist Intelligence},
author={Zhang, Xingxuan and Ren, Gang and Yu, Han and others},
journal={arXiv preprint arXiv:2509.03508},
year={2025}
}适配者:maggie_Ha · 联系方式:见 commit 历史 · 日期:2026-08-21