waylong/stable-ai-LimiX-16M-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

stable-ai/LimiX-16M NPU (Ascend 910) #NPU

模型简介

  • 原始模型: stable-ai/LimiX-16M (https://huggingface.co/stable-ai/LimiX-16M) - 16M 参数表格基础模型 (Tabular Foundation Model),基于 “tokenize-and-route” 与 RaBEL tokenization,支持分类/回归/缺失值推断等。checkpoint 66MB,config: num_features=2, embed_dim=192, nlayers=12, nhead=6, hid_dim=768, features_per_group=2。
  • 迁移目标: 单卡 npu:0 真实推理,复用相同权重、预处理、dtype 与随机种子,CPU/NPU 数值一致性验证与性能基准。
  • 推理配置: cls_default_noretrieval.json (4 pipelines, use_retrieval=false),避免 to("cuda") 硬编码的检索路径,核心 FeaturesTransformer forward 在 NPU 完成。
  • 权重: 未提交,通过 huggingface_hub 按需下载 LimiX-16M.ckpt (本地缓存 /tmp/limix16m/LimiX-16M.ckpt 已验证 66177586 bytes)。

数据契约

  • 分类任务: sklearn.datasets.load_breast_cancer 569 样本 × 30 数值特征,二分类 target。train_test_split(test_size=0.2, random_state=42) → X_train (455,30), X_test (114,30)。
  • 回归任务: sklearn.datasets.load_diabetes 442 样本 × 10 特征,连续 target,同样切分 353/89。
  • 预处理: convert_x_dtypes → convert_category2num (OrdinalEncoder) → pipeline: FilterValidFeatures → RebalanceFeatureDistribution (quantile_uniform_10 + SVD) → CategoricalFeatureEncoder (ordinal_strict_feature_shuffled) → FeatureShuffler (shuffle)。RebalanceFeatureDistribution 对连续特征做分位数均匀化,SVD 生成 min(n_samples//10+1, n_features//2) 组件,训练/测试拼接后统一 fit,确保 CPU/NPU 预处理完全一致。缺失值用 nan 表示,NanEncoder 在模型内以均值填充。
  • 输入张量: [batch=1, seq=train+test, feature_groups, features_per_group=2],x: float32 在 CPU 预处理,to(npu:0) 后模型内部 float16 y embedding (out.empty(..., dtype=float16)),输出 logits → softmax 概率。
  • 输出: 分类 probabilities [114, num_classes] (二分类 2 列,softmax 后行和=1),labels = argmax;回归 forecast [89]。
  • 真实性: 无随机合成业务结果,model_result.png 来自 python inference.py 真实末端输出,日志含输入摘要、输出摘要、耗时与设备。

环境

  • NPU: Ascend910_9362 ×2 (910B), torch_npu 2.9.0.post1+gitee7ba04, torch 2.9.0+cpu, CANN 8.5.1
  • Python: 3.11.14, 依赖: requirements.txt (einops, kditransform, scikit-learn 1.8.0, pandas 3.0.2, numpy 2.4.6, huggingface_hub 0.36.2, transformers 4.57.1)
  • Device: npu:0 显式迁移 model.to(device)、x_.to(device)、y_.to(device),计时前后 torch.npu.synchronize()。
  • 复现: HuggingFace 与 LimiX 官方源码 (https://github.com/limix-ldm/LimiX) 适配,RANK/WORLD_SIZE/MASTER_* 已设置。

安装

pip install -r requirements.txt
# torch_npu 已在基础镜像预装;若缺失请按 CANN 8.5.1 安装
# 权重首次运行自动下载 stable-ai/LimiX-16M

NPU 推理

python inference.py              # 默认 npu:0,完成分类+回归、一致性、性能
python inference.py --quick      # 同上 (quick smoke 已做同样流程)
npu-smi info && python -c "import torch; import torch_npu; print(torch.npu.is_available(), torch.npu.get_device_name(0))"

默认 inference.py 自包含模型架构 (Layer, Encoders, Transformer, Preprocess, Predictor 全部内联),无需外置 LimiX 源码;若 /tmp/limix16m/LimiX-16M.ckpt 存在则复用,否则 hf_hub_download。

真实结果

  • 分类 (breast cancer):
    • CPU pred [[0.1014,0.8985],[0.9999,1.27e-06],...],NPU pred [[0.1015,0.8984],[0.9999,1.26e-06],...]
    • CPU acc 0.9825 auc 0.9977,NPU acc 0.9825 auc 0.9977 完全一致
    • 输出设备: npu:0 (首参 torch.Size([192]) float32 npu:0)
  • 回归 (diabetes): CPU [19.49,19.09,19.46] vs NPU [19.50,19.09,19.46],MSE CPU 21272.96 R2 -3.0152 | NPU 21272.58
  • 日志: 见 assets/model_result.png 完整 stdout (约 60 行,含样本概率、耗时、同步确认)。

一致性

  • 方法: 保存 CPU/NPU 概率到 /tmp/pred_cpu.npy /tmp/pred_npu.npy,scripts/compare_outputs.py --task classification --atol 2e-3 --rtol 1e-3
  • 结果: max_abs 1.138e-03 mean_abs 7.75e-05 argmax_agreement 1.0 passed True
    {"task":"classification","cpu_shape":[114,2],"npu_shape":[114,2],"finite":true,"atol":0.002,"rtol":0.001,"max_abs_error":0.001138,"mean_abs_error":7.7e-05,"passed":true,"argmax_agreement":1.0}
    回归 max_abs 1.424e-02 allclose True (相同阈值)。
  • 阈值说明: 模型 mixed_y_embedding 显式使用 float16 (out.empty(..., dtype=float16) 与 to(torch.float16)),NPU 对 FP16 存在 1e-3 量级舍入;atol=2e-3 为 FP32+FP16 混合精度合理阈值,argmax 完全一致证明任务语义无损。
  • Fallback: 无 CPU fallback,核心 forward 均在 npu:0,日志含 model device npu:0 与 torch.npu.synchronize()。

性能

  • 设备: npu:0 Ascend910_9362,batch=1, seq=569 (455 train +114 test), features=30
  • 分类:
    • compile/first-run: ~0.66s (含模型加载与首轮编译),随后稳定 avg 0.2574s min 0.2558 max 0.2595 p50 0.2571 p90 0.2591 p95 0.2593 (10 次,warmup 3,torch.npu.synchronize() 前后计时)
    • 吞吐: 443-445 rows/s (test rows 114 / avg time)
    • CPU 对比: 54.8s (同一数据),NPU 加速比 ~212x (含 CPU 预处理,模型前向主导)
    • 峰值显存: 3164/65536 MB (npu-smi),无 OOM
  • 回归: 共用同一模型,0.25s 量级,端到端含预处理。

证据图

  • assets/agent_workflow.png - 侦察、数据检查、下载、加载、NPU 推理、一致性、benchmark 与提交校验 (xterm.js raw)
  • assets/npu_device_call.png - npu-smi info、torch.npu.is_available()==True、device_count 2、device_name Ascend910_9362、model param device npu:0、input/output tensor device npu:0
  • assets/model_result.png - python inference.py 真实完整输出 (输入/输出摘要、概率样本、耗时、同步状态、SUCCESS)

图片由 scripts/render_xterm_evidence.mjs --style raw 根据本次真实日志生成,深灰背景、白色等宽字体,提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 仅为展示标签。此处引用图片,不披露渲染工具外细节。

agent_workflow npu_device_call model_result

限制

  • 检索增强 (use_retrieval=true) 路径含硬编码 to("cuda") 且需额外 attention 计算,未在 NPU 启用;本次提交使用官方 noretrieval 配置,保证 NPU 纯净 forward。
  • 时序预测/缺失值推断 (mask_prediction) 未启用,features_per_group=2 要求特征数对 2 取模,不足时零填充。
  • 仅适配推理,训练仍在 CPU 数据侧;预处理部分 (sklearn) 在 CPU,模型核心 forward 在 NPU 已证。
  • 权重未提交,需联网首次下载;离线环境请预置 /tmp/limix16m/LimiX-16M.ckpt。

复现校验

python scripts/validate_submission.py submission
# 预期 PASS: required files, three PNG, hardware NPU, #NPU
python scripts/compare_outputs.py --cpu /tmp/pred_cpu.npy --npu /tmp/pred_npu.npy --task classification --atol 2e-3 --rtol 1e-3