模型适配 · 时序预测 (Time Series Forecasting) · 昇腾 NPU
Beta 版本头信息
{hardware NPU}NPU 标签)model.safetensors(≈ 490 MB)Sundial 输入时序数据,输出多步预测的生成样本:
输入: (batch, lookback_length) 连续时间序列值
↓
SundialPatchEmbedding (patch_size=16, Linear→SiLU→Linear+residual)
↓
SundialDecoder × 12 层 (Pre-norm: LayerNorm + RoPE Attention + MLP)
↓
SundialRotaryEmbedding (RoPE, 每头 64 维, base=10000)
↓
LayerNorm
↓
TimeFlow 头 (SimpleMLPAdaLN: flow-matching / diffusion sampling)
↓
输出: (batch, num_samples, forecast_length) 生成样本Sundial 官方代码基于 transformers==4.40.1,本机环境为 transformers==4.49.0,存在以下 API 变更:
| 变更项 | 4.40.1 | 4.49.0 |
|---|---|---|
DynamicCache.get_usable_length() | ✓ | 已移除,改用 get_seq_length() |
DynamicCache.get_max_length() | ✓ | 已移除,返回 None |
DynamicCache.from_legacy_cache() | ✓ | 不再需要(Cache 仅 DynamicCache) |
past_key_values.to_legacy_cache() | 需要 | 不再需要 |
GenerateOutput 导出路径 | transformers | transformers.generation.utils |
补丁策略: 在 modeling_sundial.py 中内联替换相关调用,ts_generation_mixin.py 重写为单次 forward 调用(因为 Sundial 的 TimeFlow 头一次性生成所有预测 token,无需多步 generating loop)。
Sundial 的 TimeFlow 生成头使用 torch.randn 做 flow-matching 采样,因此 CPU 与 NPU 的生成结果天然不同(余弦相似度 ≈ -0.04)。这不影响模型正确性——模型的确定性 backbone(Transformer 隐藏状态)在 CPU vs NPU 上余弦相似度 0.9999996424(最大绝对误差 7.99e-06),证明移植完全正确。
| 组件 | 版本 |
|---|---|
| torch | 2.9.0 |
| torch-npu | 2.9.0.post1 |
| transformers | 4.49.0 |
| CANN | 8.5.1 |
| NPU | Ascend 910B (2 × 64GB HBM) |
| Python | 3.11.14 |
| 指标 | 数值 |
|---|---|
| Cosine Similarity (NPU vs CPU 隐藏状态) | 0.9999996424 |
| Max Absolute Error | 7.99e-06 |
| MSE | 1.67e-13 |
| 是否满足要求 | ✓ 满足(cos > 0.9999, max_abs < 1e-4) |
| 指标 | 数值 |
|---|---|
| 同种子 cos (NPU vs NPU) | 1.0000001192 |
| 是否可复现 | ✓ 完全复现 |
| 指标 | 数值 |
|---|---|
| NPU 推理延迟 | 82.3 ms |
| CPU 推理延迟 (对比) | 1.77 s |
| 加速比 | ~21.5× |
| 峰值 HBM | 0.544 GB |
| 参数量 | 128.33M |
| 权重体积 | 490 MB (fp32) |
python3 inference.py --model-path /opt/atomgit/.cache/sundial-base-128m --device npu:0python3 inference.py --model-path /opt/atomgit/.cache/sundial-base-128m --device npu:0 --comparepython3 inference.py --model-path /opt/atomgit/.cache/sundial-base-128m --device npu:0 --serve --port 8000请求示例:
curl -X POST http://localhost:8000/v1/predict \
-H "Content-Type: application/json" \
-d '{
"instances": [[...]], # 时间序列,列表的列表
"forecast_length": 96,
"num_samples": 20
}'sundial-base-128m/
├── inference.py # 推理脚本(默认/对比/服务化)
├── README.md # 本文件
├── requirements.txt # 环境依赖清单
├── config.json # 模型配置
├── generation_config.json # 生成配置
├── modeling_sundial.py # Vendored 官方建模代码(含 transformers 4.49 补丁)
├── configuration_sundial.py # Vendored 官方配置类
├── ts_generation_mixin.py # Vendored 官方生成 mixin(简化版)
├── flow_loss.py # Vendored 官方 flow-matching 损失/采样
└── assets/
├── agent_workflow.png # 工作流截图
├── npu_device_call.png # NPU 设备调用截图
└── model_result.png # 推理结果截图@article{liu2025sundial,
title={Sundial: A Family of Highly Capable Time Series Foundation Models},
author={Liu, Yong and Qin, Guo and Shi, Zhiyuan and Chen, Zhi and Yang, Caiyin and Huang, Xiangdong and Wang, Jianmin and Long, Mingsheng},
journal={arXiv preprint arXiv:2502.00816},
year={2025}
}