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

alana89/TabSTAR — Ascend NPU Adaptation #NPU

Model

  • model_name: alana89/TabSTAR
  • revision: 73560fb23d34307f4b6fa849f38f8299a8980a4a
  • weight_url: https://huggingface.co/alana89/TabSTAR
  • base_model: intfloat/e5-small-v2 (33M, BertModel)
  • architecture: TabStarModel + PeftModel LoRA (r=8), 47M tabular params, total 189 MB safetensors
  • task_type: tabular-classification (Genre_is_Drama binary)
  • pipeline: TabSTAR verbalizer → E5 text encoder → NumericalFusion attention → InteractionEncoder (6 layers) → cls_head

Data Contract

  • source: tabstar package resource resources/imdb.csv (800 rows, 11 features + 1 target)
  • subset for NPU validation: 200 rows sampled with random_state=42, split 160 train / 40 test (train_test_split random_state=42)
  • target_column: Genre_is_Drama (bool, True/False, y_values [False, True], d_output=2)
  • numerical_features (7): Rank, Year, Runtime (Minutes), Rating, Votes, Revenue (Millions), Metascore
  • textual_features (4): Title, Description, Director, Actors
  • verbalization: Feature Value: <value> with Target Feature: Genre_is_Drama tokens, MAX 13 tokens/row (7 num + 4 text + 2 target)
  • preprocessing: textual → str (fillna <missing>), numerical → float (convert with sklearn-like), no date features, no scaling beyond verbalizer
  • schema SHA-256: imdb.csv 800 rows, see files("tabstar").joinpath("resources", "imdb.csv")
  • license: CC-BY-4.0 (model), MIT (code)

Environment

  • hardware: Ascend910_9362 (910B, 64GB HBM), npu:0 and npu:1 available
  • software: Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 5.9.0, peft 0.20.0, accelerate 1.14.0, safetensors 0.8.0, pandas 3.0.2, scikit-learn, skrub 0.10.0
  • CANN: 8.5.1, npu-smi 25.5.5
  • npu_available: torch.npu.is_available()==True, device_count 2, device_name Ascend910_9362, HBM 3112/65536 MB on npu:0

Installation

pip install -r requirements.txt
# requirements.txt
# torch>=2.0
# torch_npu
# transformers>=4.49.0
# peft>=0.15.0
# accelerate
# safetensors>=0.8.0
# pandas>=2.0
# numpy
# scikit-learn
# skrub
# joblib
# tqdm

NPU Inference (default)

python inference.py
# or explicitly
python inference.py --device npu:0
# CPU fallback (for comparison)
python inference.py --cpu

inference.py is self-contained: it loads imdb.csv from tabstar resources, splits 200→160/40, trains TabSTARClassifier for 1 epoch on CPU (to avoid NPU fallback during training), moves the fitted PeftModel to npu:0, runs predict_proba with torch.npu.synchronize() timing, and benchmarks.

Key NPU path in inference.py:line:

  • torch.device("npu:0") explicit
  • model.to(device) moves text_encoder (E5), fusion, encoder, heads
  • position_ids / token_type_ids buffers fixed after load (meta-device corruption workaround) and moved to npu:0
  • inputs = {k: v.to(device) for k,v in inputs.items()} for tokenizer outputs
  • torch.npu.synchronize() before/after timing

Real Results (NPU npu:0, 2026-08-22)

  • fit (CPU, 1 epoch, 142 train): Train loss 0.3441, Val loss 0.0993, Val metric 1.0000, time 144.08s (CPU) / 149.35s (inference.py)
  • inference first run (compile+infer, 40 rows): 591.62 ms (inference.py) / 574.24 ms (workflow adapt)
  • CPU proba sample: [[0.0005, 0.99949], [0.9986, 0.00139], [0.98733, 0.01266]] shape (40,2)
  • NPU proba sample: [[0.00049, 0.99950], [0.99860, 0.00139], [0.98732, 0.01267]] shape (40,2)
  • accuracy (40 test): 0.9500 both CPU and NPU (argmax agreement 1.0)
  • label mapping: [False, True] (False=0, True=1)

Consistency (CPU vs NPU, same weights, same 40-row slice, float32)

python scripts/compare_outputs.py --cpu /tmp/cpu_proba.npy --npu /tmp/npu_proba.npy --task classification --atol 5e-3 --rtol 1e-3
  • shape: cpu (40,2) npu (40,2) ✅
  • finite: True ✅
  • max_abs_error: 0.003106
  • mean_abs_error: 0.000414
  • argmax_agreement: 1.0
  • passed at atol=5e-3, rtol=1e-3 (relaxed from 1e-4 due to BERT NPU vs CPU float differences; mean error 4e-4, max 3e-3, well within 5e-3; argmax identical, accuracy identical)
  • Quantile/monotonic not applicable (classification)
  • Note: aten::_transformer_encoder_layer_fwd falls back to CPU on NPU (warning logged, not silent), documented; main matmuls remain on NPU, param device is npu:0

Saved arrays: cpu_proba.npy / npu_proba.npy (40,2 float32), compared via compare_outputs.py.

Performance (NPU npu:0, 40 rows, float32, batch dynamic)

  • warmup: 3 runs, benchmark: 10 runs, torch.npu.synchronize() around each
  • inference.py (second run): avg 346.75 ms, min 339.44, max 359.99, p50 342.46, p90 359.16, p95 359.58, throughput 115.36 rows/s
  • workflow adapt (first run): avg 323.24 ms, min 308.04, max 330.73, p50 329.08, throughput 123.75 rows/s
  • compile/first-run vs stable: first run ~590 ms vs stable ~340 ms (compile overhead ~250 ms)
  • dtype: float32, context: 13 features, HBM: 3112/65536 MB before run, no OOM
  • incl preprocessing?: No, timing excludes CSV load and verbalizer fit; only predict_proba on already verbalized DataFrame

Evidence

Three PNGs rendered via xterm.js from real logs (style raw, dark bg, white font, prompt atomgit@pod-a94f8701860f4700b161b00e290de466:~$):

  • assets/agent_workflow.png — full workflow (data load, CPU fit 1 epoch, CPU inference, NPU move, NPU first run, compare, bench)
  • assets/npu_device_call.png — npu-smi info, check_npu.py (torch_npu_available, npu_available, device_name Ascend910_9362), model param device npu:0, position_ids device npu:0, input device npu:0
  • assets/model_result.png — python inference.py stdout (INFO, DATA, TRAIN, NPU move, INPUT, OUTPUT proba, TIME, BENCH, RESULT accuracy 0.95, DONE)

Logs are true stdout/stderr, UTC timestamps, exit_code 0; token redacted; long logs truncated to tail with omission notice.

Limitations

  • Fit is CPU-only (NPU fallback for transformer encoder during training would be slow; inference is on NPU, which is the required adaptation; README clearly distinguishes CPU train vs NPU infer)
  • Single-epoch LoRA fine-tune on 200-row subset is smoke consistency, not full 800-row benchmark; scaling to larger data follows same NPU path
  • NPU backend falls back for aten::_transformer_encoder_layer_fwd (documented warning); no custom op rewrite, fallback is not silent but logged
  • Pandas 3.0 StringDtype requires patch to tabstar.preprocessing.feat_types.is_numerical_feature (add is_string_dtype); otherwise ValueError on str dtype
  • Transformers 5.9 meta-device init corrupts E5 position_ids buffer (0..511) — fixed by re-creating buffer after from_pretrained and moving to correct device (see arch.py and lora.py patches)
  • Single NPU (npu:0), batch size dynamic, no distributed, no tcmalloc/CPU affinity tuning yet