z
zhangyuge147/ibm-research-patchtst-fm-r1-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

ibm-research/patchtst-fm-r1 - Ascend NPU Adapted

Base model: ibm-research/patchtst-fm-r1 (also mirrored as ibm-granite/granite-timeseries-patchtst-fm-r1)

This repository is an Ascend NPU #NPU adaptation of PatchTST-FM (260M params, Foundation Model for time-series). Verified on Ascend 910_9362 (npu:0) with CPU-NPU consistency max_abs_error 2.1e-05, mean 1.93e-06 (threshold atol=1e-4, rtol=1e-3) ✅

Model Info

  • Architecture: PatchTSTFMForPrediction (PatchTST FM, 20 layers, d_model 1024, n_head 16, patch 16, 512 patches)
  • Task: time-series probabilistic forecasting (99 quantiles, 64 horizon)
  • Context: 8192 (8192 = input + horizon, short series padded with mean and masked)
  • Dtype: float32
  • Route: quantile forecasting via tsfm_public (granite-tsfm)
  • Weight: 984M safetensors (~257.9M params)
  • Revision: 67dc5f9a1d26bbc782b3ac45bcdb899ce957e681

Data Contract

  • Source: ETTh1.csv (public benchmark, hourly, 17420 rows, 8 cols: date + 7 numeric HUFL/HULL/MUFL/MULL/LUFL/LULL/OT) or synthetic fallback (randn seed 0)
  • Used slice: [1, 336, 7] window from offset 1000 (cols HUFL..OT), hourly frequency, sorted by date, no time_zone, no missing steps in slice (nan handled via mean if present)
  • Input: past_values shape [B, T, C] where T=336 (tested) up to 8192 with pruning, C=7, B=1, dtype float32, device npu:0
  • Output: quantile_outputs shape [B, 99, 64, 7] (99 levels 0.01..0.99), prediction_outputs shape [B, 64, 7] (point), device npu:0
  • Axes: [batch, quantile, time, channel] for quantiles; prediction_length=64 fixed for NPU validation (config default); context/horizon/channel documented
  • Scaling: model internal RevIN per-series (no external scaler saved); outputs are de-normalized internally
  • Evidence seed: synthetic uses seed 0; ETTh1 slice is deterministic offset 1000

Environment

  • Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1, transformers 5.9.0, granite-tsfm (git), einops, pandas 3.0.2, numpy 1.26.4
  • NPU: Ascend910_9362, 2 devices, tested on npu:0, torch.npu.is_available()==True, torch.npu.device_count()==2
  • Toolkit: Ascend CANN 8.5.1

Installation

pip install torch torch_npu transformers einops pandas numpy scikit-learn datasets
# granite-tsfm for PatchTST-FM (provides PatchTSTFMForPrediction)
git clone https://github.com/ibm-granite/granite-tsfm.git /tmp/granite-tsfm
export PYTHONPATH=/tmp/granite-tsfm:$PYTHONPATH

NPU Inference

Device proof logged: model parameters on npu:0, past_values on npu:0, quantile_outputs on npu:0, torch.npu.synchronize() before/after timing.

import sys; sys.path.insert(0, "/tmp/granite-tsfm")
import torch, torch_npu
from tsfm_public.models.patchtst_fm import PatchTSTFMForPrediction
model = PatchTSTFMForPrediction.from_pretrained("/tmp/ms_patchtst_final").to("npu:0").eval()
past = torch.randn(1, 336, 7).to("npu:0")
with torch.no_grad():
    torch.npu.synchronize(); out = model(past_values=past, prediction_length=64); torch.npu.synchronize()
print(out.quantile_outputs.shape)  # [1, 99, 64, 7]

Or run directly:

python inference.py --model-dir /tmp/ms_patchtst_final --device npu:0 --context-length 336 --prediction-length 64

Default python inference.py runs ETTh1 slice if available else synthetic, prints NPU proof and benchmark.

Validation

  • Input: ETTh1.csv slice [1,336,7] offset 1000 (same slice for CPU/NPU, same dtype float32, eval mode, seed)
  • CPU vs NPU: shape [1,99,64,7], finite true, max_abs 2.098e-05, mean_abs 1.93e-06, atol 1e-4 rtol 1e-3 ✅ via scripts/compare_outputs.py --task regression
  • Quantile checks: 99 levels monotonic increasing at each time step ✅, median (q=0.5 idx49) finite
  • Expected outputs (ETTh1 slice, NPU, float32):
    • quantile mean 9.575349, std 9.470970, min -0.114555, max 41.609299
    • q0.01 8.5977, q0.5 11.1107, q0.99 13.3735 (first step ch0)
    • median first 5 steps ch0: [11.1107, 10.9083, 9.9149, 11.3855, 15.5348]

Performance

Measured on Ascend910_9362, single npu:0, synchronize surrounding each run:

  • First run (compile): ~298 ms (includes graph compilation + 1 inference)
  • Stable (avg/min/max/p50/p90/p95): 12.05 / 11.34 / 12.94 / 11.71 / 12.90 / 12.92 ms (10 runs, after 3 warmup)
  • Throughput: ~82.98 windows/s (batch 1, n_vars 7, context 336, horizon 64, dtype float32)
  • Peak NPU memory: 1139.0 MB
  • Preprocessing (CSV read + slice) not included in timing; model forward only

Evidence

Images generated by xterm.js from real logs (workflow, device proof, result):

workflow device call results

Only xterm.js --style raw rendering is used; images are not simulated.

Limitations

  • Shorter context (336) leverages pruning; full 8192 context not benchmarked here but supported (mean padding + mask, pruning reduces compute)
  • Hourly frequency assumed; other frequencies (15min, daily) follow same code path but not validated with long windows here
  • CPU fallback not observed; custom ops fallback would fail validation (Hard Gate 8)
  • Weight 257M params requires ~1GB VRAM; smaller NPU with <2GB may OOM

License

Apache-2.0 (original model). NPU adaptation retains same license.