FlowState 是 IBM Research 提出的尺度不变时间序列基础模型,编码器为 S5 状态空间模型,解码器为函数基解码器,实现跨采样率的连续预测。本仓库将 FlowState 迁移至单卡 Ascend 910B (npu:0) 并提供可复现 NPU 推理入口,使用 tsfm_public + torch_npu,无 CPU fallback。
past_values [batch, time, channel] float32, batch_first=True
prediction_outputs [batch, horizon, channel] float32, 形状 [1,96,1]quantile_outputs [batch, quantiles, horizon, channel] float32, 形状 [1,9,96,1], 分位数 [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9],单调递增pip install -r requirements.txt
# 克隆 FlowState 代码 (推理必需,已在适配中完成)
git clone --depth 1 https://github.com/ibm-granite/granite-tsfm /tmp/granite-tsfm
# 模型权重预下载到本地 (正式推理不联网下载,推理默认读 /tmp/flowstate_test)
huggingface-cli download ibm-research/flowstate --local-dir /tmp/flowstate_test --local-dir-use-symlinks False
# 或从 ModelScope /本地路径
python -c "from huggingface_hub import snapshot_download; snapshot_download('ibm-research/flowstate', local_dir='/tmp/flowstate_test')"python inference.py
# 自定义本地目录/设备/窗口
python inference.py --model-dir /tmp/flowstate_test --device npu:0 --context 2048 --horizon 96 --scale-factor 1.0默认自动探测 torch_npu.is_available(),显式 .to(npu:0),全部 76 参数在 NPU,无 CPU fallback (检测到 fallback 直接非零退出)。计时前后 torch.npu.synchronize(),支持 batch_first 切换与多 batch 推理。
在 Ascend910 npu:0 上的真实运行输出 (evidence_logs/inference.log, 由 python inference.py 直接生成):
[STEP] FlowState NPU inference
[INFO] model=ibm-research/flowstate task=probabilistic forecasting context=2048 horizon=96 quantiles=[0.1-0.9]
[INFO] backend=tsfm_public+torch_npu device=npu:0 dtype=float32
[INFO] torch=2.9.0+cpu torch_npu=2.9.0.post1+gitee7ba04 npu_available=True device_name=Ascend910_9362
[INFO] model_dir=/tmp/flowstate_test
[DATA] Forecasting task: context=2048 horizon=96 batch=1 channel=1 batch_first=True scale_factor=1.0
[DATA] Input shape [batch,time,channel]=[1,2048,1] dtype torch.float32
[DATA] SHA256 prefix f23a3814ca5b seed=42 mean=0.008625 std=0.423453
[DATA] Input preview [0:3,0]: [0.0496, 0.1352, 0.3539]
[DATA] Future gt preview [0:3]: [0.6600, 0.6185, 0.5705]
[INFO] Loading FlowStateForPrediction from /tmp/flowstate_test
[INFO] Model moved to npu:0
[INFO] First param device: npu:0, dtype: torch.float32
[INFO] Params on NPU: 76/76
[INFO] Input moved to npu:0 shape (1, 2048, 1) dtype torch.float32
[NPU] First run (compile+inference) 1.0418s
[NPU] Output pred shape (1, 96, 1) device npu:0 dtype torch.float32
[NPU] Output quantile shape (1, 9, 96, 1) device npu:0 dtype torch.float32
[NPU] Forecast (mean) preview [0:3,0]: [0.679599404335022, 0.6383888721466064, 0.5091256499290466]
[NPU] Quantile 0.5 preview [0:3,0]: [0.6817355751991272, 0.6400461792945862, 0.5100233554840088]
[NPU] Quantile monotonic check axis=1: True
[NPU] Stats mean 0.042523 std 0.370365 min -0.599778 max 0.679599
[NPU] Quantile stats mean 0.045241 q10 min -0.786741 q90 max 0.895786
[BENCH] avg 0.7613s min 0.7581s max 0.7632s p50 0.7618s p90 0.7626s p95 0.7629s throughput 1.31 windows/s
[BENCH] peak memory 219.9 MB batch=1 context=2048 horizon=96 dtype=float32
[SUCCESS] FlowState NPU inference completed on npu:0 without CPU fallbackCPU 同输入对照 (同权重、同输入、同 dtype、同 seed): pred mean 0.042525, preview [0.679602, 0.638390, 0.509126], 与 NPU 误差 max_abs 2.10e-05。
同一权重、输入、预处理、seed=42、float32、eval 模式对比 (scripts/compare_outputs.py):
python .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu working_flowstate_final/cpu_pred.npy --npu working_flowstate_final/npu_pred.npy --task forecasting --atol 1e-4 --rtol 1e-3
python .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu working_flowstate_final/cpu_q.npy --npu working_flowstate_final/npu_q.npy --task quantile --atol 1e-4 --rtol 1e-3单窗口 smoke consistency,非完整数据集精度评测。已保存 cpu_pred.npy / npu_pred.npy / cpu_q.npy / npu_q.npy 可复现。无 CPU fallback。
NPU 同步计时 (torch.npu.synchronize()):
动态 shape 首次编译后至少复跑一次,10 次稳定测试。
三张 PNG 由 xterm.js 根据本次真实日志生成 (scripts/render_xterm_evidence.mjs --style raw):
assets/agent_workflow.png - 侦察、环境、下载、加载、NPU 推理、一致性、benchmark、校验全流程assets/npu_device_call.png - npu-smi 与模型参数 device/dtype/input device 证明assets/model_result.png - 默认 python inference.py 完整输出与同步耗时提示符固定为 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 仅为展示标签。

遵循原模型许可 Apache-2.0,权重未包含在本仓库。