#NPU
本仓库将 cisco-ai/cisco-time-series-model-1.0-preview 适配为可在单卡昇腾 NPU(Ascend910,npu:0)真实运行的自包含推理实现。模型为单变量零样本概率时间序列预测(多分辨率 decoder-only Transformer,基于 TimesFM2.0 并加入分辨率嵌入与特殊 token),输出 128 步 forecast 的均值与 0.1–0.9 分位数。
PatchedTSMultiResolutionDecoder,50 层,hidden 1280,16 heads,head_dim 80inference.py 为自包含单文件:内联了 Google TimesFM 的 pytorch_patched_decoder 基类与 Cisco 多分辨率解码器实现,仅依赖 torch、torch_npu、numpy,不联网下载、不导入未提交模块。
模型期望双分辨率上下文 (x_coarse, x_fine),二者在“右侧”对齐,粗分辨率每个点聚合 60 个细分辨率点(默认 agg_factor=60):
| 字段 | 值 |
|---|---|
| 细分辨率上下文 | 单变量序列,最多 512 个点(取最右 512) |
| 粗分辨率上下文 | 由细序列按 60 为块取均值聚合,最多 512 个点 |
| 输入归一化 | 按每个序列有效(非 pad)部分计算 mean/std,clip 到 [-1000, 1000] |
| 缺失值 | strip 前导 NaN + 线性插值(官方 pipeline) |
| 频率 token | 默认 0 |
| 输出 | horizon=128,mean + quantiles {0.1,…,0.9}(反归一化到原始尺度) |
| 输入张量 | [N, 512] fine / coarse,dtype float32,轴顺序 [series, time] |
默认演示数据为官方 README 示例的两条确定性合成序列(固定 seed 42):
注意:上游提示词标注为“表格数据”,但模型实际为时间序列预测模型;本仓库按时间序列 forecast/quantile 契约验证,不以随机输入冒充业务结果。
pip install -r requirements.txt模型权重(config.json + torch_model.pt)请从上游 HuggingFace 下载到本地目录:
python -c "from huggingface_hub import snapshot_download; snapshot_download('cisco-ai/cisco-time-series-model-1.0-preview', local_dir='model')"或通过 --model-path <dir> 指向已存在的权重目录。
python inference.py --device npu:0 --model-path ./modelinference.py 会:加载同一份权重并把全部参数与参与计算的 Tensor 显式迁移到 npu:0 → 构造多分辨率输入 → 自回归 decode(horizon 128)→ 打印输入/输出摘要、设备、dtype、同步耗时与最终状态。异常返回非零。
device: npu:0 backend: torch 2.9.0+cpu + torch_npu 2.9.0.post1+gitee7ba04
model: PatchedTSMultiResolutionDecoder layers=50 hidden=1280 heads=16 quantiles=[0.1..0.9]
params: 498.83M first param device=npu:0 dtype=torch.float32
sync time: 0.366s (first run incl. kernel compile)
series 0 forecast mean first8=[162.872 164.545 166.257 167.419 168.217 168.943 169.217 169.584]
series 1 forecast mean first8=[79.879 79.9 79.71 79.563 79.556 79.46 79.456 79.386]
final status: OK (no CPU fallback)
peak NPU memory: 2.000 GB同一权重、同一输入、同一预处理、eval mode、float32。阈值按 FP32 与预测输出尺度取 atol=1e-4, rtol=1e-3。
| 输出 | 结果 | max_abs_error | mean_abs_error |
|---|---|---|---|
forecast mean [2,128] | PASS | 6.10e-05 | 9.98e-06 |
quantiles [2,9,128] | PASS | 7.63e-05 | 1.07e-05 |
quantiles 在 CPU 与 NPU 上均单调(0.1<…<0.9),全部数值有限。未发生 CPU fallback。
| 指标 | 值 |
|---|---|
| 首次运行(含 kernel 编译) | 0.416s |
| 稳态 avg/min/max | 101.1 / 99.4 / 101.5 ms(16 序列,batch_size 8) |
| p50 / p90 / p95 | 101.3 / 101.4 / 101.4 ms |
| 吞吐 | ~158 series/s(windows/s) |
| 峰值 NPU 显存 | 2.01 GB / 64 GB |
| 输入 | 16 条 × fine[512] + coarse[512],horizon 128,dtype float32 |
性能计时覆盖模型 decode 全流程(含两批 8 序列的批处理),前后均执行 torch.npu.synchronize();不含数据生成。
以下三图由 xterm.js 根据本次真实日志生成(非手工补写、非绘图库模拟):
assets/agent_workflow.png:侦察、权重检查、CPU 基线、NPU 推理、一致性、benchmark、提交校验assets/npu_device_call.png:npu-smi info、NPU 可用性、模型参数 device/dtype、输入输出 Tensor deviceassets/model_result.png:默认 python inference.py 的真实完整输出与退出码