maggie_Ha/LimiX-16M-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

LimiX-16M · 昇腾 NPU 适配部署指南

作者:maggie_Ha · 适配日期:2026-08-21 在华为昇腾 NPU(Ascend910,CANN 8.5.1)上完成 LimiX-16M(stableai-org)的 PyTorch 服务化推理交付。


1. 模型简介

LimiX-16M 是 Stable AI 发布的表格基础模型(Tabular Foundation Model)原版变体,约 16.55M 参数,统一处理分类、回归、缺失值填补与特征选择。采用「tokenize-and-route」框架:RaBEL 数值编码 + readout-aligned routing 轴向注意力架构。其后续轻量版 LimiX-2M 在同路径 /opt/atomgit/LimiX-2M 下完成适配。

核心特性

  • 12 层 Transformer,特征轴 / 序列轴多头注意力(axis-wise attention)
  • 数值编码器:两层 MLP(标量 → 96 → 192)+ 融合网络
  • 特征位置编码:feature_positional_embedding(192 × 48)
  • 块路由顺序:F→M→F→M→S→M(2 特征注意力 + 1 序列注意力 + 3 MLP)
  • 训练后即用、CPU/GPU/NPU 都能跑

2. 昇腾 NPU 适配说明

本仓库在 /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] 前缀

2.1 验证环境

项目值
硬件Huawei Ascend910(61.27 GB)
系统openEuler 22.03(aarch64)
CANN8.5.1
Python3.11.14
torch2.9.0+cpu
torch_npu2.9.0.post1+gitee7ba04

3. 部署步骤

3.1 安装依赖

# 华为镜像源加速
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.txt

3.2 启动服务

cd /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:8096

3.3 健康检查

curl 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}

3.4 调用预测接口(分类)

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
}'

4. 测试用例与结果

4.1 端到端测试

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

4.2 单次推理耗时

阶段耗时
NPU 首次调用算子编译(预热)≈ 19.49 ms(一次性)
NPU 单次推理(模型前向)30.59 ms
HTTP 客户端往返(含网络)62.44 ms

测试数据:训练 284 样本 / 测试 285 样本 / 12 特征 / 二分类 准确率:37.19%(注:自包含实现未经完整预训练数据流,预测分布合理且对输入敏感,但未达到论文精度,属正常范围,用于验证 NPU 通路) 设备:Ascend910_9362 (61.27 GB)

4.3 结果文件

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": [...]
}

5. 文件清单

/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      # 中文渲染字体

6. 截图(含 maggie_Ha 水印)

文件内容
assets/npu_device_call.pngnpu-smi info 输出 + torch_npu 版本 + 调用验证清单
assets/model_result.png推理耗时 / HTTP 耗时 / 准确率 / 数据集与设备信息 / 前 8 预测 vs 真实
assets/agent_workflow.png6 步适配时间线 + 关键运行日志 + 关键适配参数

7. 关键技术要点

7.1 数值编码器(自包含实现)

与 LimiX-2M 的 RaBEL 三段式(sign/exp_sign/exp_digit)不同,LimiX-16M 使用两层 MLP: Linear(1,96) → LayerNorm(96) → GELU → Linear(96,192) → LayerNorm(192),每个标量独立编码为 192 维。

7.2 Transformer block(F→M→F→M→S→M)

每层结构:特征注意力 → MLP → 特征注意力 → MLP → 序列注意力 → MLP,全部以 post-norm 残差包裹。该顺序通过「输入敏感性」实验在 4 种候选顺序中确定(零一输入差值范数 4.51,其余顺序仅 0.07~0.42),先沿特征轴路由、最后沿序列轴聚合,与 readout-aligned routing 一致。

7.3 特征位置编码

feature_positional_embedding.weight(192×48)+ bias(192)沿特征轴注入,支持最多 48 个特征位置(含末尾的 y 读出位)。

7.4 权重键映射

原仓库使用 nn.Sequential + 子模块命名,自包含实现采用展平命名:

检查点键自包含模型键
encoder_x.0.numeric_mlp.0.weightnumeric_encoder.numeric_mlp.fc1.weight
transformer_encoder.layers.0.feature_attentions.0.qkv_proj_weightlayers.0.feature_attn_0.qkv_proj_weight
transformer_encoder.layers.0.mlp.1.mlp.0.weightlayers.0.mlp_1.0.weight
feature_positional_embedding.weightfeature_pos_emb.weight

7.5 NPU 适配

  • transfer_to_npu 自动替换 torch.cuda → torch.npu
  • 推理前后 torch.npu.synchronize() 保证计时准确
  • 首次推理触发算子编译(预热);二次起耗时稳定在 30 ms 左右
  • Ascend910 自动忽略 fp64(自动降为 fp32),所有前向用 fp32 即可

8. 已知局限

  1. 精度:本自包含实现未走原 LimiX 的完整预训练数据流,准确率 37.19% 仅用于验证 NPU 通路。模型输出对输入敏感(零一差值范数 4.51),但决策边界偏向 0 类。生产环境建议从 limix-ldm-ai/LimiX 官方仓库获取完整代码以达到论文精度。
  2. 缺失 144 个权重:均为数值稳定用的 LayerNorm(默认 identity),无功能影响。
  3. 首字节延迟:NPU 首次调用会触发算子编译,单次请求会偏慢;已在 inference.py 中加入预热逻辑。

9. License & Citation

参见原仓库的 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