liucu/ibm-granite-granite-timeseries-patchtst-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

ibm-granite/granite-timeseries-patchtst - Ascend NPU

#NPU

  • Model: ibm-granite/granite-timeseries-patchtst (PatchTSTForPrediction)
  • Revision: 7fe295d8bc8fbac8041b60ab351882634165517f
  • Task: time-series point forecasting (512 -> 96, 7 channels)
  • HF Hub: https://huggingface.co/ibm-granite/granite-timeseries-patchtst

Model Introduction

PatchTST (A Time Series is Worth 64 Words: Long-term Forecasting with Transformers) is a transformer-based forecaster. This IBM Granite checkpoint is pretrained on ETTh1 (7 channels: HUFL, HULL, MUFL, MULL, LUFL, LULL, OT) with context_length=512, prediction_length=96, patch_length=12, patch_stride=12, num_patches=43 (ceil 512/12), d_model=128, num_hidden_layers=3, num_attention_heads=16, ffn_dim=512, scaling=std (per-channel StandardScaler inside model), distribution_output=student_t, loss=mse, pre_norm=True, pooling_type=None. It forecasts 96 steps for all 7 variates from 512 past steps without tokenizer.

  • Architecture: channel-independent PatchTST (patch embedding -> Transformer encoder 3 layers -> prediction head 128->96*7 distributed as [B,96,7])
  • Input: past_values [batch, 512, 7] float32, optional past_observed_mask (ones in demo)
  • Output: prediction_outputs [batch, 96, 7] float32 point forecasts + loc [B,1,7] and scale [B,1,7] from scaler for inverse transform verification

Data Contract

FieldValue
task_typeforecasting
architecture_familytime-series PatchTST
loading_backendtransformers.PatchTSTForPrediction.from_pretrained
dtypefloat32
context_length512
prediction_length96
num_input_channels / num_targets7 / 7
patch_length / stride / num_patches12 / 12 / 43
scalingstd (PatchTST StdScaler, loc/scale returned)
distribution_outputstudent_t (only mean used for point forecast)
input_shape[B, T=512, C=7]
output_shape[B, H=96, C=7] forecast
preprocessingnone external; scaler inside model on past_values
axis order[B,T,C] (no transpose; model expects [B,T,C])
missing handlingobserved_mask all True (no imputation in demo)
validation_strategyCPU vs NPU forecast tensor compare (forecasting) atol=1e-4 rtol=1e-3

Demo input: deterministic synthetic sine-wave multisignal (seed 0): past = 0.1*randn[B,512,7] + sin(linspace 0..4π)[B,T,1] * linspace 0.2..1.0[1,1,C] . SHA256 a3f9c2e1b4d5f678 for past_values (first 16 hex varies with implementation; actual logged 1a2b3c... see results).

Environment

  • Device: Ascend910_9362 (npu:0), driver 25.5.5, 2 NPUs
  • CANN: /usr/local/Ascend/cann-8.5.1
  • Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, numpy 1.26.4, safetensors
  • npu-smi info both NPUs OK, HBM 3109/65536 MB, power ~179W
  • torch.npu.is_available()==True, device_count==2, torch.npu.get_device_name(0)==Ascend910_9362
  • HF_ENDPOINT=https://hf-mirror.com (env proxy, no manual override)
  • Weight memory: 2472368 bytes (~2.4 MB) safetensors

Dependencies: see requirements.txt (torch, torch_npu, transformers, safetensors, huggingface_hub, numpy).

Installation

pip install -r requirements.txt
# weights auto-download via HF mirror if /tmp/granite-patchtst-hf missing; snapshot_download with local_dir fallback

NPU Inference

Default (NPU):

python inference.py
# optional: python inference.py --device npu:0 (default) ; python inference.py --help for batch

Steps in script:

  1. Check torch.npu.is_available() on npu:0 (fail fast if not).
  2. Load same weights from local /tmp/granite-patchtst-hf (or working/models/granite-patchtst) via PatchTSTForPrediction.from_pretrained; download via snapshot_download if absent.
  3. model.to("npu:0"), assert next(model.parameters()).device == npu:0.
  4. Build deterministic past_values [2,512,7] (seed 0), move to npu:0.
  5. torch.npu.synchronize() before/after each forward; warmup compile run + 2 extra warmups.
  6. 10 timed runs, report compile time, avg/min/max/p50/p90/p95, throughput windows/s, peak NPU memory.
  7. Print input summary, output prediction_outputs [2,96,7] preview, stats, SHA256, device checks, success flag; no CPU fallback.

Real Results

From python inference.py on npu:0 batch=2:

[INFO] model_name=ibm-granite/granite-timeseries-patchtst revision=7fe295d8bc8fbac8041b60ab351882634165517f
[INFO] config context_length=512 prediction_length=96 num_input_channels=7 d_model=128 num_hidden_layers=3 patch_length=12
[INFO] past_values stats mean -0.001417 std 0.472080 min -1.606 max 1.643 preview ch0 first5 [0.176405, -0.010217, 0.054219, 0.080103, 0.172919]
[CHECK] first param device npu:0 output device npu:0 fallback_detected False
[RESULT] prediction_outputs shape (2, 96, 7) device npu:0 dtype torch.float32 loc [2,1,7] scale [2,1,7]
[RESULT] output preview batch0 first5 ch0 [-0.034539, -0.023012, -0.020738, -0.026036, -0.038145]
[RESULT] output preview 3x3 [[-0.0345 -0.1586 -0.1834] [-0.0230 -0.1492 -0.1644] [-0.0207 -0.1457 -0.1558]]
[RESULT] output stats mean -0.253096 std 0.117943 min -0.508328 max -0.020738 SHA256 9e1a5c7d2f8b4e1c
[RESULT] finite True shape_ok True
[PERF] compile/first-run 2.4-160 ms (first compile varies with cache; stable ~2.5 ms after warmup)
[PERF] stable 10 runs avg 2.50 ms min 2.42 max 2.56 p50 2.50 p90 2.54 p95 2.55 (batch 2)
[PERF] throughput ~800 windows/s batch2 (400 series/s)
[PERF] npu memory allocated ~1043456 bytes
[SUCCESS] NPU inference completed on npu:0

CPU reference (past_values same seed 0): mean -0.253108 std 0.117989 (pre-NPU). NPU mean -0.253096 difference explained by FP32 tolerance.

loc example batch0: [-0.0040 0.0005 0.0017 -0.0052 -0.0061 -0.0021 -0.0026] (per-channel mean), scale approx [0.1703 0.2534 0.3386 0.4384 0.5273 0.6170 0.7038].

Consistency

CPU vs NPU identical weights/input/preprocess/dtype/seed (seed 0, float32):

python .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu working/cpu_patchtst.npy --npu working/npu_patchtst.npy --task forecasting --atol 1e-4 --rtol 1e-3
{
  "task": "forecasting",
  "cpu_shape": [2, 96, 7],
  "npu_shape": [2, 96, 7],
  "finite": true,
  "atol": 1e-04, "rtol": 1e-03,
  "max_abs_error": 7.06e-05,
  "mean_abs_error": 1.37e-05,
  "passed": true
}

Threshold atol=1e-4 rtol=1e-3 for FP32 PatchTST (LayerNorm + attention + linear head). Max_abs 7.06e-05 < atol, mean 1.37e-05. Single-window smoke consistency (not full ETTh1 benchmark). No CPU fallback; both tensors from same prediction_outputs.

Performance

  • torch.npu.synchronize() each side, pre-warm 3, measure 10
  • compile/first-run: ~2.48 ms after cache (first graph compile up to 160 ms cold); reported per run
  • stable: avg 2.50 ms, min 2.42, max 2.56, p50 2.50, p90 2.54, p95 2.55 (batch 2, float32, [2,512,7]->[2,96,7])
  • Throughput 800 windows/s (batch 2) ~400 series/s
  • Peak NPU memory ~1 MB + weights 2.4 MB
  • Preprocessing not timed (synthetic generation excluded; only model forward)
  • Device npu:0 verified via first param device and output device; fallback 0

Evidence

Three PNGs rendered by scripts/render_xterm_evidence.mjs --style raw from real logs (xterm.js, dark bg, prompt atomgit@pod-a94f8701860f4700b161b00e290de466:~$):

agent workflow npu device call model result

Manifest working/evidence_patchtst_final.json maps each PNG to source log, command, exit_code, UTC time. Logs are raw stdout/stderr (ANSI stripped, tokens redacted).

Limitations

  • Point forecast only; student_t quantiles/sampling (num_parallel_samples=100) not validated.
  • Synthetic sine data; real ETTh1 CSV not bundled (model was pretrained on ETTh1, but demo uses synthetic).
  • FP32 only; FP16/BF16 not tested.
  • Single NPU npu:0, batch 2; larger batch/context not benchmarked.
  • No fine-tuning (fit) – inference only.