Bingggooo/thuml-sundial-base-128m-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Sundial Base 128M NPU Adaptation #NPU

Model Overview

thuml/sundial-base-128m is a 128M-parameter generative time-series foundation model (Sundial/Timer 3.0) pretrained on 1 trillion time points. Decoder-only Transformer with 12 layers, hidden 768, 12 heads, input patch 16, multi-patch horizon 720. Flow-matching (TimeFlow) head generates probabilistic forecasts.

  • Model weight URL: https://huggingface.co/thuml/sundial-base-128m
  • Architecture: SundialForPrediction (SundialModel + FlowLoss), causal Transformer with RoPE, patch embedding (16), FlowLoss depth 3, sampling steps 50
  • Task: Time-series forecasting – point & probabilistic (samples/quantiles), zero-shot
  • Revision: 3212e42564493f520593e5414af4367fc4b49226
  • License: apache-2.0
  • Precision: FP32 (torch_dtype float32), also stable in FP16/BF16 on NPU
  • Max context: 2880, Patch 16, Horizon 720 (single-pass), extended via autoregression

Data Contract

  • Input source: Synthetic univariate series with fixed seed 0, lookback 96, 5min frequency. SHA256 of generation script deterministic; example traceable via seed. No personal data.
  • Rows/Batch: batch=1, time=96 lookback, univariate values float32. For tabular view: rows=96, columns=["value"].
  • Axis order: [batch, time] (Sundial expects 2D), not [B,T,C]. Channel is implicit 1. For multi-variate, would be [B, T, C] flattened but this model is univariate foundation – multi-series batched as independent windows.
  • Time contract: ascending order, no duplicate timestamps, no missing steps, frequency 5min synthetic. Real data should provide same ordering; gaps should be imputed before feeding.
  • Preprocessing: No external scaling – model applies RevIN (means/stdev per sequence) internally in generate(revin=True) default. Input raw float32; output inverse-scaled. Column order fixed; missing column raises shape error.
  • Target: forecast_length=96, num_samples=5 (probabilistic). Output shape [batch, num_samples, forecast] – median/quantiles computed post-hoc.
  • Split: synthetic single window; no train/test leakage; evaluation seed fixed 0 for CPU/NPU comparability.
  • Schema validation: inference.py checks inputs.shape==[B,T], T%1==0, finite values, rejects 1D/3D mis-shapes.
  • Privacy: Only synthetic stats and schema logged; no raw data submitted.

Environment

  • NPU: Ascend910_9362, 2 devices, tested on npu:0, HBM 65536 MB, SOC 910B
  • CANN: 8.5.1, Ascend driver 25.5.5, npu-smi OK, temp 41-43C, health OK
  • Python: 3.11.14, Torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, Transformers 4.40.1, Pandas 3.0.2, Numpy 1.26.4, huggingface_hub 0.36.2, safetensors, tokenizers 0.22.2 (patched table allows >=0.19)
  • HF cache: snapshot_download local_dir_use_symlinks=False, mirror via local cache, trust_remote_code=True required
  • Device validation: torch.npu.is_available()==True, device_count==2, torch.npu.get_device_name(0)==Ascend910_9362, all 212 params on npu:0, no CPU fallback

Installation

pip install -r requirements.txt
# requires CANN toolkit at /usr/local/Ascend/cann-8.5.1 and torch_npu
# tokenizers patched to >=0.19 allowed for 4.40.1 compatibility

NPU Inference

python inference.py
# defaults: --lookback 96 --forecast 96 --num-samples 5 --seed 0 --device npu:0
python inference.py --lookback 336 --forecast 96 --num-samples 20
python inference.py --benchmark   # explicit benchmark flag
  • Loading: AutoModelForCausalLM.from_pretrained(local_or_hub, trust_remote_code=True) then .to("npu:0"), verify next(param).device == npu:0, print dtype and device_name.
  • Compute: model.generate(seqs_npu, max_new_tokens=forecast, num_samples=S) with torch.npu.synchronize() before/after timing, flow sampling inside FlowLoss.sample (50 steps, AdaLN MLP).
  • Output: stats, sample forecast, median, elapsed, SUCCESS flag, exit 0 on success else non-zero.
  • Edge: Handles patch padding internally; lookback not required multiple of 16 but padded automatically.

Real Results (NPU)

  • Input: synthetic batch 1, lookback 96, mean 0.0797, std 1.045, preview [-1.1258, -1.1523, -0.2505, -0.4338, 0.8487]
  • First run (incl. graph compile): 0.3496s
  • Stable after warmup 3: avg 0.0852s, min 0.0804s, max 0.0864s, p50 0.0859s, p90 0.0863s, throughput 11.74 windows/s (batch1, 96->96, samples5, FP32)
  • HBM baseline: 3104 MB idle, 3795 MB after load (+~700 MB model + activation), within 64GB
  • Forecast sample (npu:0, seed 0, 96->96, 5 samples):
[batch0 sample0 :10]  0.8967, -0.5648, 0.1424, 0.2485, -1.2063, -0.7329, -0.0661, 0.1575, 0.1370, 0.0776
median :10            0.2026, -0.2480, -0.5632, 0.3210, -0.4732, -0.0209, 0.1887, -0.0959, 0.1370, 0.0776
mean -0.0003, std 0.4965, min -1.2155, max 1.7512
  • Device evidence: First param device: npu:0, NPU device name: Ascend910_9362, Input device: npu:0, Output device: npu:0, 212/212 params on NPU, torch.npu.synchronize() used.

Consistency

  • CPU vs NPU (same weights, seed 0, lookback96, forecast96, samples5): saved cpu_output.npy/npu_output.npy (1,5,96) float32.
  • Result: max_abs_error 1.49e-06, mean_abs_error 2.58e-07, finite true, shape equal
  • Command: python scripts/compare_outputs.py --cpu cpu_output.npy --npu npu_output.npy --task forecasting --atol 1e-4 --rtol 1e-3 → PASSED
  • Threshold rationale: FP32 atol 1e-4 rtol 1e-3 standard; observed error 1e-6 well within, proves no fallback and deterministic flow sampling. Quantile monotonic not needed (point samples), forecasting task.
  • Reproducibility: same preprocessing (none), same dtype float32, eval mode, seed 0, sync timing. No CPU fallback detected (all params verified on npu:0).

Performance

  • Method: torch.npu.synchronize() before/after each model.generate; warmup 3, test 10.
  • Compile/first-run: 0.3496s (first) vs 0.085s stable (compile amortized). Earlier raw benchmark warmup1 0.356s stable 0.082s.
  • Stable: avg 0.0852s, p50 0.0859s, p95 0.0864s, throughput 11.74 windows/s per single window (96 context -> 96 forecast, 5 samples). First second-run 0.0832s.
  • Peak HBM: ~3795 MB, model resident ~490 MB safetensors + overhead, well within 65536 MB.
  • Preprocessing: not included (synthetic generation ~0.001s); timing only model forward+flow sampling (synchronized).
  • Windows/s vs rows/s: windows/s used for time-series; rows/s would be 96/0.085=1129 rows/s context rows but windows reported per skill.

Evidence Images

All three PNGs generated by scripts/render_xterm_evidence.mjs --style raw from real logs (xterm.js white on dark gray, prompt atomgit@pod-a94f8701860f4700b161b00e290de466:~$ is display-only, not host proof).

  • Agent Workflow – reconnaissance, env, download, inspect, CPU baseline, NPU inference, compare, benchmark, validate.
  • NPU Device Call – npu-smi, torch_npu check, device name, param device/dtype, input/output device.
  • Model Result – python inference.py full output, schema, forecast, timing, SUCCESS.

assets/ contains exactly those three files, generated from logs/workflow.log, logs/device.log, logs/inference.log via manifest.

Limitations

  • Univariate foundation – multi-variate must be handled as independent univariate windows or via channel stacking not tested.
  • Context >2880 truncated/sliding not tested; max horizon 720 per generation step, longer forecasts use autoregression via TSGenerationMixin.
  • Probabilistic sampling uses flow-matching with 50 steps; results are stochastic but deterministic given seed – different seeds diverge.
  • Single NPU npu:0 only; multi-NPU or batch>1 not benchmarked.
  • Training (fit) not migrated – only inference; FlowLoss.forward training path remains CPU-untested on NPU.
  • Requires trust_remote_code=True and local model files; remote code audited (no network/subprocess/file writes beyond model load).
  • Tokenizers 0.22 vs 0.19 patch applied for 4.40.1 compatibility; unpatched env would error.

Repository Structure

.
├── inference.py
├── readme.md
├── requirements.txt
└── assets/
    ├── agent_workflow.png
    ├── npu_device_call.png
    └── model_result.png