Toto-2.0-22m 是 Datadog 发布的 Toto 2.0 系列时间序列基础模型(HF),全称 Time Series Optimized Transformer for Observability,面向可观测性/多变量时间序列的概率预测。Toto 2.0 采用 u-μP 缩放技术,4m→2.5B 五个规格共享同一训练配方,本权重为 22m 规格(约 2200 万参数,fp32 权重约 88MB),适合低资源部署;在 BOOM 基准上 CRPS=0.363、MASE=0.601,家族整体在 BOOM、GIFT-Eval、TIME 三大基准上达到基础模型 SOTA。模型为 decoder-only patch 化 Transformer(6 层、d_model=512、8 头、patch_size=32),注意力层在时间轴(因果,含 RoPE-xPos)与变量轴(全量)间分组交替,采用连续 patch 掩码(CPM)单遍并行解码、分位数输出头(pinball loss)、arcsinh 鲁棒输入缩放,零样本即可输出多分位数概率预测。
| 项 | 值 |
|---|---|
| 参数量 | ~22M |
| 架构 | VariateTimeTransformerDecoder(时间轴因果注意力 + 变量轴注意力交替,6 层、d_model=512、8 头、patch_size=32) |
| 任务 | 时间序列零样本概率预测(9 分位数:0.1~0.9) |
| 输入 | (batch, n_variates, context) float32 + 观测掩码 + series_ids |
| 输出 | 分位数张量 (9, batch, n_variates, horizon) |
| 任务类型 | 表格数据 / 时间序列预测 |
| 依赖 | 版本 | 说明 |
|---|---|---|
| Python | 3.10+ | 推荐 3.11 |
| torch | >=2.4 | CPU/NPU 均可 |
| torch_npu | 与 CANN 配套 | 昇腾NPU支持 |
| gluonts | >=0.16 | 模型实现依赖(含 pytorch-lightning) |
| einops / jaxtyping | - | 张量重排与类型标注 |
| unit-scaling | >=0.2.0 | u-μP 缩放算子 |
| huggingface-hub / safetensors | - | 权重加载 |
| numpy / pandas / matplotlib | - | 数据处理与绘图 |
依赖安装到独立目录(不污染共享环境):
pip install --target /mnt/workspace/Toto-2.0-22m/libs \
-i https://pypi.tuna.tsinghua.edu.cn/simple \
einops gluonts huggingface-hub safetensors jaxtyping unit-scaling \
pytorch-lightning lightning torchmetrics模型定义 toto2 与 dd_unit_scaling 已 vendored 在仓库内(Apache-2.0,源自 DataDog/toto),无需额外安装。
模型权重:/home/developer/models/Toto-2.0-22m(含 config.json + model.safetensors)
确认权重与依赖就位后,脚本会自动将 /mnt/workspace/Toto-2.0-22m/libs 与仓库目录加入 PYTHONPATH,无需手动设置。可用 npu-smi info 检查 NPU 设备状态。
python3 inference.py --model_path /home/developer/models/Toto-2.0-22m脚本内置合成单变量时间序列示例输入(趋势+周期+噪声,512 步上下文),只传 --model_path 即可跑通;设备自动选择(优先 NPU,不可用回退 CUDA/CPU),执行零样本概率预测并打印耗时(耗时/elapsed 字段),结果图保存至 assets/model_result.png。
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
--model_path | str | /home/developer/models/Toto-2.0-22m | 模型权重目录 |
--context_length | int | 512 | 输入历史长度 |
--horizon | int | 96 | 预测步数 |
--decode_block_size | int | 768 | CPM 并行解码块大小 |
--device | str | 自动 (npu > cuda > cpu) | 指定推理设备 |
--output | str | assets/model_result.png | 结果图保存路径 |
输入为内置合成的单变量时间序列(趋势+周期+噪声,512 步),zero-shot 预测未来 96 步的 9 分位数。实测输出示例:
[INFO] device = npu
[INFO] model_path = /home/developer/models/Toto-2.0-22m
[INFO] 模型加载完成 load_time=x.xxs params=22M (layers=6, d_model=512, heads=8, patch_size=32)
[INFO] 示例数据: 合成时间序列(趋势+周期+噪声), context=512, n_variates=1, horizon=96
[INFO] 推理完成 耗时/elapsed=x.xx s (context=512, horizon=96, device=npu)
[INFO] 输出分位数形状: (9, 1, 1, 96), 分位水平 [0.1..0.9]
[RESULT] status=ok device=npu 耗时/elapsed=x.xx s结果截图见 assets/model_result.png。
见 assets/agent_workflow.png。
见 assets/npu_device_call.png。
见 assets/model_result.png。
F.scaled_dot_product_attention)实现,勿启用 flash_attention_2;toto2/ 与 dd_unit_scaling/(Apache-2.0,改写自 DataDog/toto 源码),官方 toto-models 包未发布到 PyPI,故直接内嵌源码;dd_unit_scaling 依赖 PyPI 上的 unit-scaling 包;forecast() 返回形状 (9, batch, n_variates, horizon) 的分位数张量,分位水平依次为 0.1~0.9,中位数取 index=4;.gitignore 已排除 *.safetensors 等);依赖通过 pip install --target 隔离安装到 /mnt/workspace/Toto-2.0-22m/libs,勿直接装入共享环境;libs 中不要安装 torch/numpy/pandas(使用系统版本,避免与 torch_npu 冲突)。