LimiX-16M 是一个 tabular foundation model(TFM,表格基础模型),采用 "tokenize-and-route" 框架与 RaBEL 数值 token 化,统一支持分类 / 回归 / 缺失值插补 三类表格任务。本项目在 昇腾 Ascend910 NPU(npu:0) 上完成推理适配。
stable-ai/LimiX-16M(Apache-2.0)./model_weights/stable-ai__LimiX-16M/(config.json + LimiX-16M.ckpt,约 66MB)npu:0(Ascend910)npu:0。LimiX-16M 是 LimiX 系列中的 16M 参数表格基础模型。核心设计:
features_per_group 分组后用 fusion_network 融合,得到特征组 token。feature_attentions:沿特征维度(token 维)做注意力;sequence_attentions:沿样本维度做注意力(跨样本检索/路由)。本 checkpoint 的 config 超参:
| 配置 | 值 |
|---|---|
| nlayers | 12 |
| nhead | 6 |
| embed_dim | 192 |
| hid_dim | 768 |
| features_per_group | 2 |
| num_features(encoder_config_x) | 2 |
| max_num_classes(encoder_config_y) | 10 |
| dropout / recompute_attn | 0.0 / False |
参数量:16.53M(与 LimiX-16M 一致)。
官方 LimiX 推理依赖 GitHub 源码(本环境 github.com 不可达,vendored/ 目录为空),
因此按 OmTrackVLA 重建法 从 checkpoint 自重建模型结构:
torch.load(LimiX-16M.ckpt, map_location='cpu', weights_only=False)
读出完整 config dict(11 个超参键)与 state_dict(183 个键)。state_dict 的 key/shape 重建:
encoder_x.0.{mask_embedding,numeric_mlp,fusion_network}cls_y_encoder.2.{y_embedding,y_mask}、reg_y_encoder.1.layertransformer_encoder.layers.N.{feature_attentions(2),sequence_attentions(1),mlp(3)}cls_y_decoder / reg_y_decoder / feature_decoder / feature_positional_embeddingload_state_dict(..., strict=True) 严格对齐(missing=[],unexpected=[])。forward 在 npu:0 上真实执行并输出数值。重建中的两个关键点:
state_dict 中 mlp.*.mlp.{0,2} 只有 weight,
因此重建为 nn.Linear(..., bias=False)。nn.LayerNorm(embed_dim, elementwise_affine=False)(无参数、不影响严格对齐),
实测 forward 稳定。说明:本项目为演示级(demo)重建。模型结构 / 权重与官方 checkpoint 严格对齐,且 forward 在 NPU 上真实执行并输出数值;但未复现官方完整的 分布式检索(retrieval)管线,推理方式为"知识库(前 70% 样本,带标签)+ 查询样本"的简化单次前向。
pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple/ -r requirements.txt权重已缓存于 ./model_weights/stable-ai__LimiX-16M/,命中即跳过下载。
如需重新下载:
export HF_ENDPOINT=https://hf-mirror.com
python3 -c "from huggingface_hub import snapshot_download; \
snapshot_download('stable-ai/LimiX-16M', local_dir='./model_weights/stable-ai__LimiX-16M', max_workers=2)"--csv)# 分类(默认):输出每个查询样本的类别与概率,并给出准确率
python3 inference.py --csv sample.csv --task classification
# 回归:输出预测值,并给出 RMSE
python3 inference.py --csv sample.csv --task regressionsample.csv 由 sklearn.datasets.load_breast_cancer 生成(2 特征 + 二分类标签 +
连续回归标签,共 569 行)。CSV 前 70% 作为知识库(已知标签),后 30% 作为查询样本。
uvicorn inference:app --host 0.0.0.0 --port 8000POST /predict:
curl -s -X POST http://127.0.0.1:8000/predict \
-H "Content-Type: application/json" \
-d '{"features": [[13.54, 14.36], [11.42, 20.38]], "task": "classification"}'返回 JSON:
{
"model": "stable-ai/LimiX-16M",
"device": "npu:0",
"task": "classification",
"predicted_class": [1, 1],
"probabilities": [[...], [...]],
"num_classes": 10,
"feature_reconstruction": [[...], [...]]
}GET /health 返回服务与 NPU 设备状态。
.cuda() / cuda: / CUDA 调用。npu:0。try-except 捕获 NPU 初始化异常(torch_npu import 失败或 npu:0
不可用时打印清晰错误并优雅退出)。qkv_proj_weight / out_proj_weight),兼容 torch_npu。RANK/WORLD_SIZE/MASTER_ADDR/MASTER_PORT
已在 inference.py 顶部设置。LimiX-16M-npu/
├── inference.py # 双模式入口(CLI + FastAPI)
├── model.py # 从 checkpoint 重建的模型结构
├── sample.csv # 示例表格数据(breast_cancer 2 特征)
├── requirements.txt
├── README.md
├── assets/ # 运行截图
└── model_weights/ # 权重缓存(.gitignore,不上传)npu:0 实测通过。