LimiX-2M 是由 stable-ai 发布的 2M 参数表格基础模型(Tabular Foundation Model),采用 12 层 Transformer 架构,核心创新在于 axis-wise attention 机制——同时沿特征维度和样本维度计算双向注意力,从而实现上下文学习(In-Context Learning)式的表格数据分类与回归。该模型无需针对每个新数据集进行微调,只需提供少量训练样本即可完成推理。
本仓库提供在华为昇腾 Atlas 800 A2 推理卡上基于 torch_npu + FastAPI 的服务化部署方案。由于 LimiX-2M 采用非自回归、非生成式的 axis-wise attention 架构,无法使用 vLLM-Ascend 部署,因此采用纯 PyTorch 推理路径,充分利用 torch_npu 的 NPU 加速能力。
主要特性:
| 组件 | 版本 |
|---|---|
| 操作系统 | openEuler 22.03 (LTS-SP4) aarch64 |
| 昇腾驱动 | 25.5.5 |
| NPU 型号 | Ascend 910B (64GB HBM) |
| Python | 3.11.14 |
| PyTorch | 2.9.0 |
| torch_npu | 与 PyTorch 匹配 |
| CANN | 8.5.1 |
| fastapi | 0.123.10 |
| uvicorn | 0.46.0 |
验证环境的关键命令:
npu-smi info
python3 -c "import torch; print(torch.npu.is_available())"pip install torch torch_npu numpy scikit-learn pandas einops kditransform fastapi uvicorn pydantic scipywget https://huggingface.co/stable-ai/LimiX-2M/resolve/main/LimiX-2M.ckpt
# 或使用 GitCode 镜像
wget https://ai.gitcode.com/hf_mirrors/stable-ai/LimiX-2M/resolve/main/LimiX-2M.ckptASCEND_RT_VISIBLE_DEVICES=1 LIMIX_DEVICE=npu:0 LIMIX_CKPT=./LimiX-2M.ckpt python3 inference.py --port 8060 --device npu:0启动后将看到如下输出日志:
[LimiX] NPU devices: 1 current: 0
[LimiX] loading model on npu:0 ckpt=./LimiX-2M.ckpt
[LimiX] model loaded cls=0.08s reg=0.05s
INFO: Uvicorn running on http://0.0.0.0:8060LimiX-2M/
├── inference.py # FastAPI 推理服务入口
├── README.md # 本文档
├── requirements.txt # Python 依赖
├── assets/
│ └── README.md # 模型资产说明
└── limix_src/ # 模型源代码(需部署时一并放置)
├── config/ # 预处理管线配置文件
├── model/ # 模型定义(transformer, layer, encoders)
├── inference/ # 推理管线(predictor, preprocess)
└── utils/ # 工具函数curl http://127.0.0.1:8060/healthz
# 预期输出: {"status":"ok","model":"stable-ai/LimiX-2M","device":"npu:0"}curl http://127.0.0.1:8060/v1/modelscurl -X POST http://127.0.0.1:8060/v1/predict \
-H "Content-Type: application/json" \
-d '{
"x_train": [[1.0,2.0],[2.0,3.0],[3.0,4.0],[4.0,5.0],[5.0,6.0],[6.0,7.0],[7.0,8.0],[8.0,9.0],[9.0,10.0],[10.0,11.0]],
"y_train": [0,0,0,0,0,1,1,1,1,1],
"x_test": [[1.5,2.5],[5.5,6.5],[9.5,10.5]],
"task": "classification"
}'curl -X POST http://127.0.0.1:8060/v1/predict \
-H "Content-Type: application/json" \
-d '{
"x_train": [[1.0,2.0],[2.0,3.0],[3.0,4.0],[4.0,5.0],[5.0,6.0],[6.0,7.0],[7.0,8.0],[8.0,9.0],[9.0,10.0],[10.0,11.0]],
"y_train": [1.5,2.5,3.5,4.5,5.5,6.5,7.5,8.5,9.5,10.5],
"x_test": [[1.5,2.5],[5.5,6.5],[9.5,10.5]],
"task": "regression"
}'在 Ascend 910B 单卡(NPU 1, Chip 0, Phy-ID 2)上实测数据:
| 任务 | 样本数 | 特征数 | 平均耗时 | 峰值内存 |
|---|---|---|---|---|
| 分类 (10 train, 3 test) | 13 | 2 | 1.40s | ~3500 MB |
| 回归 (10 train, 3 test) | 13 | 2 | 0.37s | ~3500 MB |
| 模型加载 | - | - | 0.13s | ~280 MB |
性能说明:
使用内置 iris 风格分类数据进行的精度验证结果:
| 指标 | 分类 | 回归 |
|---|---|---|
| 准确率 (Acc) | 0.9825 | - |
| AUC | 0.9986 | - |
| MAE | - | 0.287 |
| R2 | - | 0.964 |
精度说明:
ASCEND_RT_VISIBLE_DEVICES=1 指定 NPU 1,避免与系统其他进程冲突。可通过 npu-smi info 查看 NPU 使用情况。eval_pos 或减少样本数。LayerNormMixedPrecision 层已针对 NPU 做了适配,自动使用 device_type=input.device.type 而非硬编码 "cuda",确保混合精度在 NPU 上正确工作。