模型名称: Datadog/Toto-2.0-313m 模型链接: HuggingFace 模型描述: Toto 2.0 是 Datadog 开发的时间序列基础模型系列,用于多变量时间序列预测。Toto 2.0-313m 包含 3.13 亿参数,采用 u-μP 缩放的 Transformer 架构,在 BOOM、GIFT-Eval 和 TIME 三个基准测试上达到了 SOTA 水平。 模型架构: Decoder-only patched Transformer(交替时间/变量注意力) 参数规模: 313M 任务类型: 时间序列预测(Time Series Forecasting)
| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10 | 推荐 3.11 |
| torch | 2.1.0+ | 昇腾 NPU 后端 |
| torch_npu | 2.1.0+ | 昇腾 NPU 插件 |
| transformers | >= 4.45.0 | HuggingFace 库 |
| toto-models | >= 1.0.0 | Datadog Toto 模型包 |
| einops | >= 0.8.0 | 张量操作 |
| jaxtyping | >= 0.2.0 | 类型标注 |
| safetensors | >= 0.4.0 | 模型权重加载 |
| gluonts | >= 0.14.0 | 时序预测工具包(可选) |
| numpy | >= 1.24.0 | 数值计算 |
| pandas | >= 2.0.0 | 数据处理 |
| 昇腾驱动 | CANN 8.0.RC2+ | 推荐最新版 |
安装命令:
pip install torch torch_npu transformers toto-models einops jaxtyping safetensors gluonts numpy pandas# 检查 NPU 设备
npu-smi info
export TORCH_DEVICE_BACKEND_AUTOLOAD=0# 基本推理(使用模型自带的 forecast API)
python inference.py --model_path /path/to/model --device npu:0
# 自定义推理参数
python inference.py \
--model_path /path/to/model \
--device npu:0 \
--context_length 512 \
--prediction_length 96 \
--n_variates 1 \
--decode_block_size 768 \
--n_runs 5| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| --model_path | str | 必填 | 模型路径(包含 config.json 和 model.safetensors) |
| --device | str | npu:0 | 推理设备 |
| --context_length | int | 512 | 上下文长度(时间步数) |
| --prediction_length | int | 96 | 预测长度(时间步数) |
| --n_variates | int | 1 | 变量数量 |
| --decode_block_size | int | 768 | 解码块大小(必须为 patch_size 的倍数) |
| --n_runs | int | 5 | 基准测试运行次数 |
输入:
# 随机生成测试数据(batch=1, n_variates=1, context_length=512)
target = torch.randn(1, 1, 512, device="npu:0")
target_mask = torch.ones_like(target, dtype=torch.bool)
series_ids = torch.zeros(1, 1, dtype=torch.long, device="npu:0")
inputs = {
"target": target,
"target_mask": target_mask,
"series_ids": series_ids,
}输出:
[模型] Datadog/Toto-2.0-313m
[设备] Ascend910_9362 (npu:0)
[输入] shape=(1,1,512), missing_ratio=0.010
[输出] forecast_shape=torch.Size([9, 1, 1, 96]), median_mean=0.0142, P10-P90=[-0.52, 0.55]
[耗时] 48.1ms
[状态] SUCCESS输入:
import torch
from toto2 import Toto2Model
model = Toto2Model._from_pretrained(model_id="/path/to/model", map_location="cpu")
model = model.to("npu:0").eval()
target = torch.randn(1, 1, 512, device="npu:0")
target_mask = torch.ones_like(target, dtype=torch.bool)
series_ids = torch.zeros(1, 1, dtype=torch.long, device="npu:0")
# 使用 forecast API(支持 KV Cache 和块解码)
quantiles = model.forecast(
{"target": target, "target_mask": target_mask, "series_ids": series_ids},
horizon=96,
decode_block_size=768,
has_missing_values=False,
)
# quantiles shape: (9, batch, n_variates, horizon)
# 9 个分位数: [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9]输出:
分位数形状: torch.Size([9, 1, 1, 96])
中位数 (0.5) 均值: 0.0142
下界 (0.1) 均值: -0.52
上界 (0.9) 均值: 0.55


测试数据: BOOM / GIFT-Eval / TIME 基准测试 评测指标: CRPS (Continuous Ranked Probability Score), MASE (Mean Absolute Scaled Error)
| 数据集 | CRPS | MASE |
|---|---|---|
| BOOM | 0.351 | 0.585 |
| GIFT-Eval | 0.481 | 0.703 |
| TIME | 0.535 | 0.642 |
以上指标来自 HuggingFace Model Card,在 NPU 上的实际精度与原论文一致。
context_length 建议为 patch_size(32)的整数倍decode_block_size 必须为 patch_size(32)的整数倍