jsjs1258/alana89-TabSTAR-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

alana89/TabSTAR — Ascend NPU

#NPU

alana89/TabSTAR 是 TabSTAR 表格基础模型(基于 intfloat/e5-small-v2 的语义目标感知表示),在 400 个表格数据集上预训练,6 层 tabular encoder,d_model 384。本仓将其适配到单卡 Ascend 910 NPU (npu:0) 通过 torch_npu 真实推理。

  • Model: alana89/TabSTAR (revision 73560fb23d34307f4b6fa849f38f8299a8980a4a, pipeline tabular-classification)
  • Task: 表格分类 / 回归(tabular-classification / tabular-regression),支持数值 + 文本列
  • Architecture: E5-small-v2 文本编码器 + NumericalFusion (attention) + InteractionEncoder (6 层) + cls/reg head
  • Weight: 189 MB (model.safetensors), float32, 参数量 ~47M (文本编码器 ~33M + tabular ~14M)
  • License: CC-BY-4.0 (base e5) + MIT (TabSTAR code)

数据契约

合成演示数据(固定 seed=42,保证可复现,不含隐私):

  • 行列: batch 4 × 5 列 (age, income, score, city, job)
  • 列顺序: ["age","income","score","city","job"] 不得改变
  • dtype: age int64, income int64, score float64, city object, job object; x_num float32 归一化 (age/100, income/100000, score raw; 文本列 0)
  • Target: d_output=2 二分类 logits → softmax 概率;回归时 d_output=1 数值输出
  • Verbalization: "{col} is {value}" 字符串数组 x_txt [B, Seq] + x_num [B, Seq] float32
  • 真实来源: 代码内随机生成(np.random.seed 42),对应 train/test 划分示例;用户自有 CSV 需保持相同列顺序、dtype、缺失值策略、类别映射
  • SHA: 代码与 config 固定,无外部数据集依赖

表格预处理器需复用同一 fitted state(TabSTARVerbalizer),不得在 CPU/NPU 上分别重新 fit。列缺失/多列/类型错误时推理脚本校验并报错。

环境

  • NPU: Ascend910_9362 ×2, CANN 8.5.1, 驱动 npu-smi 25.5.5
  • PyTorch: 2.9.0+cpu, torch-npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, tabstar 1.1.16
  • Python: 3.11.14
  • Device: npu:0 (显式 model.to("npu:0"), tokenizer 输入 .to("npu:0"), 首参 device 校验)

安装

pip install -r requirements.txt
# 权重已通过 HF mirror 缓存,无需手动下载;首次运行会自动从 https://huggingface.co/alana89/TabSTAR 与 intfloat/e5-small-v2 拉取

离线环境可预先 snapshot_download("alana89/TabSTAR") 与 snapshot_download("intfloat/e5-small-v2") 到本地,再设置 E5_SMALL_LOCAL_PATH。

NPU 推理

python inference.py
# --batch-size 4 --device npu:0 (默认)

日志打印:模型名、任务、backend、device、dtype、首参 device、输入摘要 (x_txt/x_num shape/sample)、输出摘要 (logits/probs/pred)、同步耗时、结果 PASS/FAIL。

真实结果

以下为本次在 npu:0 的真实完整输出(python inference.py):

  • CPU baseline: time 163ms, logits shape (4,2), sample [0.6335, -0.6559], probs [0.7840, 0.2159] pred 0
  • NPU first/compile: 280-321ms (含 text encoder 分批 128 与表格编译)
  • NPU stable avg: 45-48ms (10 次, torch.npu.synchronize 前后计时)
  • NPU logits sample: [0.63136, -0.65319], probs [0.78322, 0.21677] pred 0
  • 通过率: argmax 一致 1.0

注意:aten::_transformer_encoder_layer_fwd 在 NPU 上回退到 CPU (warning 已保留),其余算子在 NPU 执行;模型核心前向已在 npu:0 验证,首参 device=npu:0,输出 device=npu:0。

一致性

  • 权重: 同一本地 model.safetensors (189 MB) 与同一 E5 权重
  • 输入: 同一 x_txt/x_num (seed 42) 与相同预处理
  • 比较: compare_outputs.py 分类 logits, atol 1e-2 rtol 1e-3 (FP32 表格模型实测误差 within)
  • 结果: max_abs 0.00496, mean_abs 0.00226, argmax_agreement 1.0, finite true, PASS
  • 说明: 单样本 smoke consistency,非全量数据集评测;文本与数值融合导致微小数值差异但分类一致
python /tmp/npu-adapt-tabular/scripts/compare_outputs.py --cpu cpu_logits.npy --npu npu_logits.npy --task classification --atol 1e-2 --rtol 1e-3

性能

  • 编译/首轮: 280-321 ms (首次分词 + E5 编码 + tabular 前向)
  • 稳定: avg 45-48ms, min 44.7ms, max 48.7ms, p50 47.8ms, p90 48.5ms, p95 48.6ms (10 次, batch 4)
  • 吞吐: 83-88 rows/s (batch 4), 不含 CSV 读取,仅模型前向 + 同步
  • 显存: HBM 3391/65536 MB (npu-smi), 模型常驻 <1GB, 单卡充足
  • dtype: float32, batch: 4, shape: [4,5] → [4,2] logits

计时均使用 torch.npu.synchronize() 包围。

证据图

图片说明
assets/agent_workflow.png侦察、下载、加载、NPU 推理完整工作流
assets/npu_device_call.pngnpu-smi、NPU 可用性、设备名、模型参数 device/dtype
assets/model_result.png默认 python inference.py 的真实完整输出

agent workflow npu device call model result

三图由 xterm.js 根据本次真实日志生成(scripts/render_xterm_evidence.mjs --style raw),深灰背景、白色等宽字体、固定提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$。

限制

  • 本仓为 smoke consistency 与推理验证,未在全量真实业务 CSV 上评测 accuracy/F1/MAE
  • aten::_transformer_encoder_layer_fwd 回退 CPU,非纯 NPU;若后续支持可改等价 PyTorch 标准算子
  • 文本编码分批 128,OOM 时自动减半;当前 batch 4 Seq 5 无 OOM
  • 预处理中的 verbalize 仍在 CPU,模型核心 forward 在 NPU,已在 README 边界说明