w
gcw_uQ09W7jl/Datadog-Toto-2.0-313m-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Datadog/Toto-2.0-313m on Ascend NPU

1. 模型简介

  • 模型名称: Datadog/Toto-2.0-313m (Datadog Toto 2.0 Foundation Model, 313M parameters, 24 layers)
  • 任务类型: 多元时间序列概率预测 (probabilistic multivariate time-series forecasting, quantile prediction)
  • 架构: Toto2Model - Decoder-only patched transformer,时间/变量交替注意力 (alternating time/variate attention),patch_size=32,d_model=1024,24层 (16 groups, layer_group_size=24, 1 variate layer per group),16 heads (qkv 64),d_ff=2736,xPos相对位置编码,PatchedCausalStdScaler (causal patch-aware scaling + asinh)
  • 权重: model.safetensors 1.25 GB (FP32, 313M params),config.json 598 B
  • 输入: [batch, n_variates, time] (B V T),例如 [1,2,512],支持 target, target_mask, series_ids,频率无关 (frequency-agnostic)
  • 输出: quantiles [9, batch, n_variates, horizon],分位点 [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9] (9 levels),含 median q0.5
  • 论文/卡片: https://huggingface.co/Datadog/Toto-2.0-313m
  • 许可证: Apache-2.0

2. 验证环境

  • NPU: Ascend910 (910B, HBM 64GB) x2,测试设备 npu:0 (Ascend910_9362)
  • CANN: 8.5.1
  • 驱动: npu-smi 25.5.5
  • Python: 3.11.14
  • PyTorch: 2.9.0+cpu
  • torch_npu: 2.9.0.post1+gitee7ba04
  • toto2: 2.0.0 (from /tmp/toto/toto2, with lightweight lightning mock for NPU)
  • 依赖: einops 0.8.2, gluonts 0.17.0, jaxtyping 0.3.11, dd-unit-scaling 0.1.0, rotary-embedding-torch 0.8.6, beartype 0.18.5, unit_scaling 0.3.5, toolz 1.1.0
  • 系统: Linux aarch64, CPU 240GB RAM
  • 模型目录: /opt/atomgit/adapt-npu-agent/model_cache/Datadog_Toto-2.0-313m (本地加载,无网络回退需 HF_ENDPOINT=https://hf-mirror.com)
  • 镜像: HF_ENDPOINT=https://hf-mirror.com

3. 安装依赖

pip install -r requirements.txt
# toto2 与 dd_unit_scaling 需从源码安装(已处理 Python 3.11 兼容 mock):
# git clone https://github.com/DataDog/toto.git /tmp/toto
# pip install --ignore-requires-python --no-deps /tmp/toto/dd_unit_scaling /tmp/toto/toto2
# pip install unit_scaling gluonts toolz einops jaxtyping beartype rotary-embedding-torch

requirements.txt 已固定 torch==2.9.0 torch_npu==2.9.0.post1 等版本。

4. NPU 推理

默认命令(CPU 基准 + NPU 推理 + 一致性 + 性能):

python inference.py

脚本自动从本地候选路径加载权重,优先级:

  1. /opt/atomgit/adapt-npu-agent/model_cache/Datadog_Toto-2.0-313m
  2. /opt/atomgit/adapt-npu-agent/working/models/Toto-2.0-313m
  3. ./model_cache/Datadog_Toto-2.0-313m
  4. 回退 HuggingFace Hub Datadog/Toto-2.0-313m (需 HF_ENDPOINT)

5. 真实推理结果

使用合成多元序列(sinusoid + trend + noise,seed=42,B=1, N=2, T=512,as float32):

  • 输入统计: mean=0.2661 std=0.7261 min=-1.1109 max=1.6596,sample [0,0,:5]=[0.1927,0.2751,0.3408,0.1606,0.5536]
  • 配置: context_length=512, horizon=96, patch_size=32, quantiles=9
  • 输出契约: [Q,B,N,H] = [9,1,2,96]

CPU 基准 (float32, ~1.50s):

quantiles median (q0.5) [0,0,:5]=[1.5145249,1.5148589,1.5063951,1.4884542,1.4420119]
quantiles q0.1   [0,0,:5]=[1.3872376,1.3874069,1.3834891,1.3587645,1.3178761]
quantiles q0.9   [0,0,:5]=[1.6283168,1.6363148,1.6221295,1.6013562,1.5577158]
finite True monotonic True (quantiles sorted increasing axis 0)

NPU (npu:0, float32, first run ~430ms compile, second run ~118ms):

quantiles median [0,0,:5]=[1.5145241,1.5148575,1.5063917,1.4884557,1.4420117]
quantiles q0.1  [0,0,:5]=[1.3872375,1.3874068,1.3834881,1.3587653,1.3178756]
quantiles q0.9  [0,0,:5]=[1.6283156,1.6363174,1.6221290,1.6013540,1.5577132]
finite True monotonic True

扩展测试 (horizon=192, decode_block_size=768, KV cache 多块):

block decode 192 horizon shape (9,1,2,192) median[0,0,:5]=[1.5145237,1.5148571,1.5063912,1.4884554,1.4420113]

终态: SUCCESS - NPU inference validated (exit 0)

6. CPU-NPU 一致性验证

  • 方法: 同一权重、同一输入、同一预处理、同一 dtype (float32)、同一 seed、eval 模式,分别保存 .npy 对比
  • 比较文件: /tmp/cpu_toto313.npy vs /tmp/npu_toto313.npy,shape [9,1,2,96]
  • 指标:
    • max_abs = 4.89e-06
    • mean_abs = 7.77e-07
    • allclose(atol=1e-4, rtol=1e-3) = True
    • finite True (两端)
    • monotonic True (quantiles 单调递增 axis 0, inference.py 校验)
    • shape_ok True ([9,1,2,96])
    • compare_outputs.py --task regression --atol 1e-4 --rtol 1e-3 => passed True (quantile monotonic 在 inference 内单独校验,因脚本 axis 假设不同)

此为 smoke consistency(固定单窗口,B=1 N=2 T=512 H=96),非完整数据集评测。已通过阈值,未发生 CPU fallback(首参设备验证: cpu vs npu:0)。

运行: python .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu /tmp/cpu_toto313.npy --npu /tmp/npu_toto313.npy --task regression --atol 1e-4 --rtol 1e-3

7. 性能测试

所有计时前后调用 torch.npu.synchronize(),预热 3 次,正式 10 次。

  • 编译/首轮: 429.84 ms (含图编译与缓存)
  • 稳定推理: avg 114.42 ms / min 111.28 ms / max 115.88 ms / p50 114.84 ms / p90 115.69 ms / p95 115.78 ms
  • 配置: batch=1, n_var=2, context=512, horizon=96, dtype=float32, device=npu:0, decode_block_size=768
  • 吞吐: 17.48 windows/s (1 series *2 variates => 34.96 variate-windows/s)
  • 显存: allocated 1330.7 MB reserved 1434.0 MB peak ~1.3 GB (权 1.25 GB + 激活)
  • 预处理计时: 排除 (合成张量),逆缩放包含在模型内 (PatchedCausalStdScaler)
  • 额外: horizon 192 仍为单次 forward (block decode),未出现 OOM

对比 CPU: 1505 ms (单次,无 synchronize),NPU 加速 ~13x (稳定阶段)。

8. 自验证截图

三张证据图由 scripts/render_xterm_evidence.mjs --style raw 从本次真实日志渲染,深灰背景,等宽字体,提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$,每张显示 source SHA256、exit code、UTC 时间。图片由 xterm.js 根据本次真实日志生成,非手绘或摆拍。

  • assets/agent_workflow.png - 工作流:inspect_model → collect_env → npu-smi → check_npu → inference.py (完整 CPU+NPU+一致性+性能)
  • assets/npu_device_call.png - 设备调用:npu-smi info、torch_npu 可用性、device_name、模型首参 device、输入输出 Tensor device
  • assets/model_result.png - 默认 python inference.py 完整输出:数据契约、真实预测、耗时、一致性、PASS

agent workflow npu device call model result

9. 已知限制

  • 仅验证合成数据单窗口 (B=1,N=2,T=512,H=96),未在真实数据集 (如 BOOM, GIFT-Eval, ETTh, Electricity) 上做完整 benchmark
  • FP32 精度,显存 ~1.4GB;更大 batch/horizon/多变量可能需 BF16/FP16 并重测阈值
  • has_missing_values=False 路径,不含缺失值掩码与缺失插补分支
  • decode_block_size=768 单块可覆盖 96 horizon;更大 horizon 会触发多块自回归,性能与数值需另测
  • 分位数单调性在 1e-6 容差内检查,极端输入下可能需更宽容差
  • 模型为概率预测,未做点预测 (mean/median) 的业务指标 (MAE/CRPS) 评测,仅做 CPU-NPU 数值一致性
  • lightning 依赖通过轻量 mock 绕过(forecast 路径不使用 Trainer),若需 Gluonts Trainer 训练则需完整 lightning 安装

10. 标签

#NPU