ALISY/LimiX-2M-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

LimiX-2M · 昇腾 NPU 适配推理工程

本仓库将 stableai-org/LimiX-2M(表格基础模型,TabPFN 类 Transformer)适配迁移到 昇腾 NPU(Ascend 910),提供 CLI + FastAPI 双模式推理,全程运行在 npu:0,无任何 CUDA 调用。

  • 原始权重:https://hf-mirror.com/stable-ai/LimiX-2M
  • 模型类型:表格(tabular)基础模型,输入 CSV 表格,输出分类/回归预测
  • 适配设备:Ascend910 (64GB) · torch 2.9.0+cpu · torch_npu · CANN 8.5.1

环境依赖

pip install torch==2.9.0+cpu torch_npu einops pandas numpy scikit-learn scipy \
            fastapi uvicorn python-multipart kditransform pillow
export HF_ENDPOINT=https://hf-mirror.com

目录结构

.
├── inference.py            # 主入口:CLI + FastAPI 双模式,模型跑在 npu:0
├── limix_inference/        # 源码包(CUDA 已全量替换为 NPU)
│   ├── predictor.py
│   ├── inference_method.py
│   └── preprocess.py
├── model/                  # 模型结构(transformer/encoder/layer)
├── utils/                  # 加载与数据工具
├── config/                 # 推理配置(noretrieval,规避 DDP/检索)
├── model_weights/stable-ai/LimiX-2M/   # 权重本地缓存(LimiX-2M.ckpt + config.json)
├── demo_data.csv           # 演示用分类 CSV(最后一列为标签)
└── assets/                 # 截图(agent_workflow / npu_device_call / model_result)

用法

CLI 模式

python inference.py --img demo_data.csv
# 自动识别任务类型;也可显式指定
python inference.py --img demo_data.csv --task Classification
python inference.py --img demo_data.csv --task Regression

输出为 JSON:model / task_type / device / num_train / num_test / classes / predictions。

FastAPI 模式

uvicorn inference:app --host 0.0.0.0 --port 8000
curl -s -F "file=@demo_data.csv" http://127.0.0.1:8000/predict

/predict 接收 multipart/form-data 文件字段 file(CSV),返回 JSON 预测结果。

CSV 约定

  • CSV 最后一列为目标标签 y,其余列为特征 X;
  • 数据自动按 7:3 划分为 train/test(与官方 predictor 约定一致);
  • 分类型特征(str/object)自动转为有序整数编码;目标标签缺失行自动丢弃。

NPU 迁移说明

  • 全局删除/替换所有 CUDA 调用(.cuda()、torch.cuda.*、device='cuda'、to("cuda") 等);
  • 模型与输入 tensor 全部 .to(torch.device('npu:0'));
  • import torch_npu 注册 NPU 后端,torch.npu.set_device(0) 初始化,失败 try-except 报错退出;
  • torch.autocast(device_type='npu') 开启混合精度推理;
  • 推理配置采用 config/*_noretrieval.json,规避 DDP/NCCL 与检索路径中的 CUDA 依赖;
  • 源码包重命名为 limix_inference/,避免脚本 inference.py 与包目录同名冲突。