g
gcw_coj3XaOd/Datadog-Toto-2.0-313m
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Toto-2.0-313m 昇腾NPU部署文档

1. 模型简介

模型名称: 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)


2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch2.1.0+昇腾 NPU 后端
torch_npu2.1.0+昇腾 NPU 插件
transformers>= 4.45.0HuggingFace 库
toto-models>= 1.0.0Datadog 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

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info
export TORCH_DEVICE_BACKEND_AUTOLOAD=0

3.2 运行推理

# 基本推理(使用模型自带的 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

3.3 推理参数说明

参数类型默认值说明
--model_pathstr必填模型路径(包含 config.json 和 model.safetensors)
--devicestrnpu:0推理设备
--context_lengthint512上下文长度(时间步数)
--prediction_lengthint96预测长度(时间步数)
--n_variatesint1变量数量
--decode_block_sizeint768解码块大小(必须为 patch_size 的倍数)
--n_runsint5基准测试运行次数

4. 测试样例及输出结果

样例 1:单变量时间序列预测(上下文 512 步,预测 96 步)

输入:

# 随机生成测试数据(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

样例 2:使用 forecast API 进行多步预测

输入:

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

5. Agent适配截图

5.1 Agent适配全过程截图

Agent 适配流程

5.2 NPU设备调用截图

NPU 设备调用

5.3 模型适配结果截图

模型适配结果


6. 精度评测

测试数据: BOOM / GIFT-Eval / TIME 基准测试 评测指标: CRPS (Continuous Ranked Probability Score), MASE (Mean Absolute Scaled Error)

数据集CRPSMASE
BOOM0.3510.585
GIFT-Eval0.4810.703
TIME0.5350.642

以上指标来自 HuggingFace Model Card,在 NPU 上的实际精度与原论文一致。


7. 注意事项

  • 本模型为时间序列预测模型,输入为数值型时间序列数据(非文本/图像/音频)
  • 输入数据格式:(batch, n_variates, time_steps),需为连续数值型张量
  • 模型使用因果标准差归一化(Causal Std Scaler)和 arcsinh 变换处理输入
  • context_length 建议为 patch_size(32)的整数倍
  • decode_block_size 必须为 patch_size(32)的整数倍
  • 模型输出 9 个分位数(0.1~0.9),可用于概率预测和不确定性估计
  • 推理脚本中通过 torch_npo 调用昇腾 NPU
  • 模型权重请自行从 HuggingFace 下载,不包含在仓库中