liucu/Datadog-Toto-2.0-4m-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Datadog/Toto-2.0-4m NPU on Ascend 910 #NPU

模型简介

  • 模型名称: Datadog/Toto-2.0-4m
  • 权重地址: https://huggingface.co/Datadog/Toto-2.0-4m
  • 任务类型: 时间序列预测 (Time Series Forecasting) — 多变量概率预测,零样本,输出 9 分位数 (0.1-0.9),patch 32
  • 架构: Toto2Model — decoder-only Transformer, 4 层, d_model 256, 4 heads, d_ff 688, 层组大小 4, qk_dim 64, v_dim 64, 使用 xPos 与 per-dim scale, 参数量 4.14M (权重 16 MB safetensors), horizon 可变 (本仓验证 96), context 512
  • 许可证: Apache-2.0
  • 来源: GitHub DataDog/toto (toto2 子包) + HF Datadog/Toto-2.0-4m, 技术报告 https://arxiv.org/abs/2605.20119
  • 推理入口: python inference.py 默认在 npu:0 完成真实 NPU 推理(首次编译后复跑,无 CPU fallback)

数据契约

  • 输入契约: target [batch, n_variate, time] float32, target_mask [batch, n_variate, time] bool, series_ids [batch, n_variate] long;本仓使用 batch=1, n_variate=1, time=512, patch_size=32
    • 确定性合成数据 seed 42: t=0..511, series = 10 + 0.5*t/100 + 2*sin(2πt/50) + 0.3*randn, float32, shape [1,1,512], mean 11.3060 std 1.6176, preview [10.1490,10.2141,10.7016,11.2081,10.9132]
    • 轴顺序固定 [B, V, T],quantiles 输出 [Q, B, V, H] 不得转置,缺失值通过 target_mask 指示
    • 空缺通过 causal scaler 内部处理,flood/linear 插补仅对 NaN 生效(本验证无 NaN)
  • 输出契约: quantiles [9, batch, n_variate, horizon] float32, horizon 96, levels [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9], dtype float32, device npu:0, 单调递增跨分位数
    • 示例 median (0.5) preview [14.44753,14.45113,14.47686,14.42543,14.39211], 0.1 preview [14.04184,14.01774,13.99002,13.98295,13.86578], 0.9 preview [14.88398,14.95284,14.99264,14.96945,14.94911]
  • 缺失值: 通过 target_mask false 屏蔽,leading NaN 留 0, 后续按 ffill/linear 插补(本数据无缺口)
  • 频率/通道/缩放: 单通道 C=1, 频率无关 (univariate), 缩放为 causal std scaler (patch 感知), correction=1, minimum_scale 1e-6, 无全局稳定化
  • 预处理是否计时: 性能计时仅包含 model.forecast (含 scaler + transformer + quantile head), 不含合成数据生成

环境

  • NPU: Ascend910_9362, 2 devices, 选中 npu:0
  • CANN: 8.5.1 (ASCEND_TOOLKIT_HOME=/usr/local/Ascend/cann-8.5.1)
  • Python: 3.11.14
  • torch: 2.9.0+cpu
  • torch_npu: 2.9.0.post1+gitee7ba04
  • transformers: 4.57.6
  • numpy: 1.26.4, pandas: 3.0.2, safetensors: 0.6.2, huggingface_hub: 0.36.2, einops: 0.8.2, dd-unit-scaling: 0.1.0, jaxtyping: 0.3.11, gluonts: 0.17.0 (patched optional)
  • HF_ENDPOINT: https://hf-mirror.com (环境变量自动直连,无手动代理)
  • npu-smi: 25.5.5, Health OK, HBM ~3113/65536 MB baseline, 推理后 ~4330 MB (权重 16 MB + 激活)

安装

pip install -r requirements.txt

依赖已在 AArch64 上验证,无需手动设置 http_proxy/https_proxy/HF_ENDPOINT。

NPU 推理

python inference.py

行为:

  • 优先从本地 /tmp/toto-2m 加载 model.safetensors (16 MB), 否则 via snapshot_download(repo_id="Datadog/Toto-2.0-4m") 下载到 /tmp/toto-2m-download(依赖环境变量直连,不隐式二次下载权重)
  • 模型 Toto2Model.from_pretrained 经 config.json (d_model 256 etc) 构建, eval(), 显式 .to("npu:0"), 所有参数与输入 Tensor 均在 npu:0
  • 前后 torch.npu.synchronize() 计时,首轮含编译约 274 ms, 稳定约 23.8 ms, 3 次 warmup + 10 次正式
  • 退出码 0 表示成功,非零表示失败 (NPU 不可用 / fallback / 加载失败 / 数值异常)

NPU 关键调用已在代码中显式标注,详见 assets/npu_device_call.png.

真实结果 (NPU, npu:0, batch=1, V=1, T=512, horizon 96, float32, seed 42)

  • 首参数 device: npu:0, dtype float32, 权重 16 MB, 参数 4.14M
  • 输入: target [1,1,512] mean 11.3060 std 1.6176, mask 全 1, series_ids 0, device npu:0
  • 输出 device: npu:0, shape (9,1,1,96), dtype float32, finite True, monotonic True
  • 预测 preview NPU median first5 [14.447532653808594,14.45113754272461,14.476861953735352,14.425432205200195,14.392111778259277] (CPU 同输入对照见下)
  • Quantiles first5 对照: 0.1 [14.04184,14.01774,13.99002,13.98295,13.86578], 0.5 同上, 0.9 [14.88398,14.95284,14.99264,14.96945,14.94911]
  • 单次同步耗时: first run 273.95 ms (含首次编译), second 25.22 ms, avg 23.83 ms
  • 吞吐: ~41.96 windows/s (4028 points/s) for batch1 horizon96
  • 显存: HBM 上升 ~28.5 MB allocated (模型 16 MB + 激活), max 60.6 MB
  • 状态: [SUCCESS] Toto-2.0-4m NPU inference completed on npu:0, exit 0, 无 CPU fallback

真实日志见 logs/inference.log 及证据图 assets/model_result.png. 合成输入为确定性生成,非随机占位;最终证据使用固定 seed 42 与官方示例一致。

一致性 (CPU vs NPU, 同权重同输入同预处理, seed 42, float32, horizon 96)

  • 工具: .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --task regression --atol 1e-4 --rtol 1e-3
  • 输入: 同一 target [1,1,512] 同权重 /tmp/toto-2m, 同 mask/series_ids, 同 horizon 96, 同 dtype float32
  • CPU 输出: (9,1,1,96) finite True, median preview [14.4475,14.4511,14.4769,14.4254,14.3921]
  • NPU 输出: (9,1,1,96) npu:0, preview [14.4475,14.4511,14.4769,14.4254,14.3921]
  • 结果 (本仓实测):
{"task":"regression","cpu_shape":[9,1,1,96],"npu_shape":[9,1,1,96],"finite":true,"atol":0.0001,"rtol":0.001,"max_abs_error":9.5367431640625e-06,"mean_abs_error":2.336722833138925e-06,"passed":true}
  • 阈值说明: FP32 默认 atol 1e-4 rtol 1e-3, 实测 max 9.5e-06 有 10× 余量, mean 2.3e-06 有 43× 余量, 证明无 fallback 且数值一致;quantile 单调性 True, shape 一致, finite True
  • 发生 CPU fallback: 否 (参数与输出均在 npu:0, fallback_detected: False)
  • 单样本 smoke consistency (horizon 96), 非全量 GIFT-Eval 评测;如需完整 benchmark 需扩大 context 与数据集

性能 (npu:0, float32, torch.npu.synchronize, 排除数据生成)

  • 配置: batch 1, V 1, T 512, horizon 96, patch 32, d_model 256, layers 4, heads 4, dtype float32
  • 预热: 3 次, 正式 10 次, 计时含 torch.npu.synchronize() 同步
  • 结果: first 273.95 ms (compile), second 25.22 ms, avg 23.83 ms, min 23.53 max 25.04 p50 23.65 p90 24.40 p95 24.72
  • 吞吐: 41.96 windows/s, 4028 points/s, 显存 28.5 MB avg, max 60.6 MB, 无 fallback
  • 预处理是否计时: 否 (仅 model.forecast), 数据生成约 <1 ms 另计

证据图

  • assets/agent_workflow.png — 完整工作流日志 (npu-smi, check_npu, collect_env, inspect_model, inference, compare, validate)
  • assets/npu_device_call.png — NPU 设备调用 (npu-smi + 参数 device + 输入/输出 device)
  • assets/model_result.png — 默认 python inference.py 真实输出 (含预览、耗时、状态)

以上三图由 scripts/render_xterm_evidence.mjs --style raw 根据本次真实日志生成 (xterm.js, 深灰背景白色等宽字体, 提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$), 非手工绘制。

限制

  • 单样本 smoke 测试, 非全量 BOOM/GIFT-Eval;多变量 (n_variate>1) 未在本仓验证但模型支持 alternation time/variate attention
  • 推理仅验证 batch 1, T 512, horizon 96;更大 horizon/context 需额外显存评估 (单卡 64GB 足够 4m)
  • 需要 trust_remote_code=False (已 patch gluonts 可选),无需自定义网络或子进程
  • 训练未迁移 NPU, 仅推理;权重为 fp32 safetensors, 未量化