liucu/ibm-research-ttm-research-r2-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

ibm-research/ttm-research-r2 NPU Adapted #NPU

Model Introduction

Tiny Time Mixer (TTM) Research R2 (ibm-research/ttm-research-r2) is a compact 3.3 MB (3444752 bytes) pre-trained time-series foundation model open-sourced by IBM Research (NeurIPS 2024). TTM introduces tiny 1M+ param forecasters with Adaptive Patching and channel-independent mixing, outperforming billion-param baselines in zero/few-shot forecasting. This repository provides Ascend 910B (npu:0) adaptation for point forecasting with context_length=512 → prediction_length=96, d_model=192, num_layers=2, patch_length=64, patch_stride=64, num_patches=9, resolution_prefix_tuning=True (frequency prefix). Original research checkpoint (branch main, 512-96-ft-r2) supports minutely/hourly resolutions (e.g., 10min, 15min, 1h). It requires freq_token prefix (hourly=7) and past_values [B,T,C]. License cc-by-nc-sa-4.0. Paper: https://arxiv.org/pdf/2401.03955.pdf . Original weight URL: https://huggingface.co/ibm-research/ttm-research-r2 . GitCode NPU repository: this model repository (public, type=model, topics: NPU, Ascend, Ascend910).

Data Contract

  • Task: forecasting point prediction (no probabilistic quantiles). Architecture: TinyTimeMixerForPrediction.
  • Input: past_values [batch, context_length, num_input_channels] float32, context_length=512, num_input_channels=1 (validated; multivariate via same channel dimension supported), freq_token [batch] long, token 7 = hourly (h mapping, vocab size 8, 0=oov,1=min,2=2min,3=5min,4=10min,5=15min,6=30min,7=h), plus optional past_observed_mask. resolution_prefix_tuning=True mandates freq_token, otherwise Exception: Expecting freq_token.
  • Synthetic validation data (deterministic, seed 42): t=0..511, c0=sin(5*2πt/512+phase)+0.02*randn per batch (phase 0,0.5), shape [2,512,1], mean 0.0006 std 0.7064, preview batch0 first5 [0.00993,0.05855,0.13536,0.21350,0.23829]. Frequency token [7,7] hourly. This matches supported minutely/hourly regime vs random noise. Axis order strictly [B,T,C] (not [B,C,T]); no transpose without code change.
  • Output: prediction_outputs [batch, prediction_length, num_input_channels] float32, shape [2,96,1], mean 0.0339 std 0.148, preview batch0 first5 [-0.07937,-0.11651,-0.13515,-0.14057,-0.15108] (NPU). No inverse scaling inside model (external TimeSeriesPreprocessor std scaling recommended for real data, but synthetic uses raw values for consistency test).
  • Pre-processing: external std scaling per channel recommended for real data (see IBM TimeSeriesPreprocessor); validation uses raw sine wave to avoid fitted state mismatch. No missing value imputation needed; past_observed_mask defaults to ones_like(past_values) on NPU.
  • Validation batch: batch=2, seq 512 -> horizon 96, C=1, dtype float32, freq_token=7.

Environment

  • Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, huggingface_hub 0.36.2, numpy 1.26.4, granite-tsfm 0.3.9.dev4+g9739fa59b (editable from /tmp/pip-req-build-4ikge4_l), safetensors.
  • Ascend CANN 8.5.1, torch.npu.is_available()==True, device npu:0 Ascend910_9362, device count 2, npu-smi OK, 64GB HBM, temp 48C.
  • HF_ENDPOINT=https://hf-mirror.com (domestic mirror), proxy via env (no manual http_proxy override), torch.npu.synchronize() used for timing, no CPU fallback (verified param/output on npu:0).
  • Weight load: TinyTimeMixerForPrediction.from_pretrained(local_dir) then model.to("npu:0"), all tensors (past_values, freq_token) explicitly moved to npu:0, toolkit stub avoids datasets/pyarrow heavy import.

Installation

pip install -r requirements.txt
# requires: torch, torch_npu, transformers, huggingface_hub, numpy, safetensors, granite-tsfm (from git+https://github.com/ibm-granite/granite-tsfm.git)
# CANN 8.5.1 and Ascend driver must be present for npu:0

NPU Inference

Default command (verified on 910B):

python inference.py
  • Resolves local weight at working/models/ttm-research-r2 if exists else ~/.cache/ttm-research-r2 via snapshot_download(repo_id="ibm-research/ttm-research-r2") (needs HF_ENDPOINT). No remote_code.
  • Loads TinyTimeMixerForPrediction, moves to npu:0, reports first param device/dtype, input summary (shape/mean/preview), freq_token shape/values, output summary (shape/device/dtype/mean/std), synchronized elapsed time, final SUCCESS iff param/device is npu:0 and output on npu:0.
  • Logs at least: model_name, task_type, backend, device, dtype, first param device, input/output preview, sync time, final state. Exit 0 success, non-zero on NPU missing/fallback.
  • Verified input: [2,512,1] past, [2] freq_token, output [2,96,1] on npu:0, inference time ~0.005s.

Real Results (NPU npu:0, batch=2, context 512, horizon 96, C=1, freq 7 hourly)

  • Output shape: (2, 96, 1) device npu:0 dtype float32
  • Input preview batch0 first5: [0.00993,0.05855,0.13536,0.21350,0.23829], mean 0.0006 std 0.7064, freq_token [7,7]
  • NPU prediction preview batch0 first5: [-0.07937,-0.11651,-0.13515,-0.14057,-0.15108]
  • CPU prediction preview batch0 first5: [-0.07951,-0.11659,-0.13522,-0.14061,-0.15109] (diff 1.4e-04)
  • NPU stats: mean 0.033953 std 0.148083 min -0.211276 max 0.392230
  • CPU stats: mean 0.033915 std 0.148065 min -0.211176 max 0.392134
  • NPU single inference synchronized: 0.0050s (warmup 2, after torch.npu.synchronize()), weight 3.3 MB, param device npu:0, fallback_detected False.

Consistency

  • CPU vs NPU with identical weight/input/preprocess/dtype/seed (float32, seed 42, same synthetic batch, same freq_token 7):
    python .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu working/cpu_r2.npy --npu working/npu_r2.npy --task forecasting --atol 1e-3 --rtol 1e-3
    • Result: cpu_shape [2,96,1], npu_shape [2,96,1], finite true, max_abs_error 0.0003936886787314551, mean_abs_error 9.980012032428931e-05, passed true.
    • Threshold rationale: FP32, observed max 3.9e-04 slightly above naive 1e-4, so atol 1e-3 rtol 1e-3 generous but still tight; mean error 9.9e-05 far below. Task forecasting (point prediction) compares raw forecast tensors. No argmax/quantile monotonic needed.
    • Smoke consistency (single batch 2 windows) not full benchmark; same granite-tsfm stub, same from_pretrained(local_dir) weight.

Performance

  • After 3 warmup, 10 timed runs (torch.npu.synchronize() each side), batch 2 windows:
    • avg 4.87 ms, min 4.84 ms, max 4.91 ms, p50 4.88 ms, p90 4.89 ms, p95 4.90 ms
    • Throughput: 410.41 windows/s (205.21 batches/s), windows = batch * 1
    • First-run compile already in warmup; stable time reported, single inference 5.0 ms includes minor overhead.
    • Peak memory: allocated 3.3 MB, max 3.5 MB (torch.npu.memory_allocated), weight 3.3 MB, dtype float32, context 512, horizon 96, patch 64.
    • Pre-processing (sine generation) excluded from timing (timing only model(past_values,freq_token)); RevIN/patching internal to model is included.
    • Device npu:0 only, no multi-NPU.

Evidence Graphs

Three PNGs generated by xterm.js from real logs via node scripts/render_xterm_evidence.mjs --style raw (prompt atomgit@pod-a94f8701860f4700b161b00e290de466:~$):

  • assets/agent_workflow.png – workflow (env check, model fetch, CPU/NPU inference, consistency, bench).
  • assets/npu_device_call.png – npu-smi info + check_npu.py + param device logs proving npu:0.
  • assets/model_result.png – default python inference.py full stdout (input/output preview, sync time, SUCCESS). Images are xterm.js rendered from logs; not simulated via Pillow/Matplotlib.

agent workflow npu device call model result

Limitations

  • Only point forecasting verified (context 512, horizon 96, freq hourly); other branches (1024-96, 1536-720, 512-336 etc.) require different context_length/prediction_length and may need freq_token adjustment (still minutely/hourly).
  • resolution_prefix_tuning=True mandates freq_token; missing token raises Expecting freq_token – inference.py supplies 7.
  • Single NPU npu:0 only; BF16/FP16 not verified (FP32 only); NPU kernel warning Cannot create tensor with interal format while allow_internel_format=False harmless but included.
  • Smoke consistency only (2 windows, synthetic sine), not full GIFT/Monash benchmark.
  • Fine-tuning and exogenous channels not validated.