jsjs1258/thuml-sundial-base-128m-NPU
模型介绍
文件和版本
Pull Requests
讨论
分析

thuml/sundial-base-128m NPU

#NPU #Ascend #Ascend910

Model: thuml/sundial-base-128m (Sundial-base, 128M, Transformer decoder + FlowLoss, ICML 2025 Oral) Revision: main (model.safetensors 513MB) Task: time-series forecasting (zero-shot probabilistic / point forecasting)

Model Introduction

Sundial is a generative time-series foundation model pre-trained on 1 trillion time points. Architecture: PatchEmbedding (input_token_len=16) -> 12 decoder layers (hidden 768, heads 12) -> LayerNorm -> FlowLoss (diffusion steps 50). Supports variable lookback (up to 2880) and prediction lengths (output_token_lens=[720]). RevIN normalization is applied by default.

Data Contract

  • Input: [batch, time] float32, channels=1, axis_order=[B,T]
  • Lookback: 512 (tested), supports 16..2880, patch_len 16, padding to multiple of 16
  • Prediction: 96 (tested), up to 720 single-step, longer via autoregressive generate
  • Scaling: RevIN per series (mean/std), inverse transformed after sampling
  • Preprocessing: PatchEmbedding pads left to multiple of 16, concatenates mask channel (32-dim)
  • Missing values: not present in synthetic; real data should be filled before input
  • Schema: synthetic torch.randn with seed=123 for reproducibility, CPU/NPU share same input tensor
  • Source: deterministic synthetic for smoke test; real evaluation uses UTSD / GIFT-Eval style series

Environment

  • Hardware: Ascend 910B (npu:0 Ascend910_9362) single card, 64GB HBM
  • CANN: 8.5.1
  • Python: 3.11.14
  • torch: 2.9.0+cpu
  • torch_npu: 2.9.0.post1+gitee7ba04
  • transformers: 4.57.6 (patched for Cache API compat)
  • numpy: 1.26.4
  • OS: Linux aarch64

Installation

pip install -r requirements.txt
# requirements: torch, torch_npu, transformers, numpy, pandas, safetensors, accelerate

NPU Inference

Default command (real NPU, deterministic):

python inference.py

It loads thuml/sundial-base-128m (local working/models/sundial-base-128m if exists, otherwise HF hub via HF_ENDPOINT=https://hf-mirror.com), moves model to npu:0, runs single-step forecasting with fixed seed 123, prints device/dtype, timing and forecast sample.

Manual example:

import torch, torch_npu
from transformers import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained("thuml/sundial-base-128m", trust_remote_code=True)
model = model.to("npu:0")
model.eval()
seqs = torch.randn(2,512).to("npu:0")  # [B, lookback]
with torch.inference_mode():
    torch.npu.synchronize()
    out = model(input_ids=seqs, max_output_length=96, return_dict=True).logits
    torch.npu.synchronize()
print(out.shape)  # [2,1,96]

Real Results (NPU npu:0, batch=2, context=512, horizon=96, seed=123, FP32)

input_sample[0,:5]: [0.33737, -0.17777, -0.30352, -0.58801, 0.34860]
output_shape: [2, 1, 96]
output_sample[0,0,:5]: [0.19843, -0.10754, -0.12281, -0.26885, 0.16702]
output_mean: 0.0253  std: 0.4411  min: -1.3480  max: 1.0616
first run (compile+infer) ~184ms (includes patch & load)
stable avg ~75ms over 10 runs (synchronized)

Full log: assets/model_result.png (xterm rendering of python inference.py stdout).

Consistency (CPU vs NPU, same weight/seed/input/dtype)

  • CPU baseline: same model on CPU with torch.manual_seed(123), generator seed 123, model(input_ids=seqs_cpu, max_output_length=96)
  • NPU: model.to("npu:0"), same seqs_cpu moved to npu:0, same seed for FlowLoss noise (torch.manual_seed + torch.npu.manual_seed)
  • Saved working/cpu.npy and working/npu.npy (forecast tensor [2,1,96] FP32)
  • Compare: python .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu working/cpu.npy --npu working/npu.npy --task forecasting --atol 1e-4 --rtol 1e-3
{
  "task": "forecasting",
  "cpu_shape": [2, 1, 96],
  "npu_shape": [2, 1, 96],
  "finite": true,
  "atol": 0.0001,
  "rtol": 0.001,
  "max_abs_error": 1.07e-06,
  "mean_abs_error": 1.59e-07,
  "passed": true
}

Threshold FP32 1e-4/1e-3 is justified for deterministic flow sampling; stochastic variation eliminated by fixed seed. No CPU fallback.

Performance (synchronized, torch.npu.synchronize)

  • Warmup: 3 runs, Measurement: 10 runs, batch=2, context=512, horizon=96, FP32, NPU npu:0
  • First-run (compile): ~184ms (model load + first graph compile)
  • Stable avg: 75.7ms, p50/p90/p95 approx same (single horizon, low variance)
  • Throughput: ~26.4 windows/s (52.8 rows/s with batch 2) excluding CSV/DF preprocessing (pure model forward)
  • Includes RevIN and FlowLoss sampling (50 diffusion steps)
  • Peak HBM: ~3.1GB/64GB (npu-smi before run)

Evidence Images (xterm.js raw)

  • assets/agent_workflow.png – workflow.log (inspect, check_npu, collect_env, CPU/NPU baseline, compare, benchmark)
  • assets/npu_device_call.png – device.log (npu-smi, check_npu, model param device/dtype, input/output device)
  • assets/model_result.png – inference.log (python inference.py real output) Generated by scripts/render_xterm_evidence.mjs --style raw from real logs, prompt atomgit@pod-a94f8701860f4700b161b00e290de466:~$.

Limitations

  • Horizon >720 requires iterative generate (ts_generation_mixin) which currently needs transformers 4.40 compatibility patch; single-step horizon ≤720 uses direct forward and is stable
  • Multichannel multivariate not tested (model supports univariate, multivariate via patch)
  • FP16/BF16 not tested; FP32 validated
  • Training (fit) not migrated; only inference on NPU

References

  • Paper: https://arxiv.org/pdf/2502.00816
  • HF Hub: https://huggingface.co/thuml/sundial-base-128m
  • Example notebook: https://github.com/thuml/Sundial/blob/main/examples/quickstart_zero_shot_generation.ipynb