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

Datadog/Toto-2.0-4m NPU 适配

#NPU

本仓库将 HuggingFace 上的 Datadog/Toto-2.0-4m(Toto 2.0 时间序列基础预测模型,4M 参数,patch 32, d_model 256, 4 层 Transformer,9 分位数概率预测)适配为可在单卡昇腾 Ascend 910B(npu:0)上真实运行的 GitCode AI model 仓库。支持多变量时序的零样本概率预测,输出 9 个分位数(0.1~0.9)与中位数点预测。

  • 模型原始仓库:https://huggingface.co/Datadog/Toto-2.0-4m
  • 权重:model.safetensors 约 16 MB(4,144,456 参数, float32),config.json patch_size 32, d_model 256, num_heads 4, num_layers 4
  • 源实现:https://github.com/DataDog/toto(toto2.model.Toto2Model, 论文 https://arxiv.org/abs/2605.20119)
  • Revision:8306a9801cf98c0f5ffe4b2dcc8f496e616d84d9(pipeline_tag time-series-forecasting)

任务与路由

项值
模型Datadog/Toto-2.0-4m(revision 8306a9801, 4.1M params)
架构Toto2Model (PatchedCausalStdScaler + PatchProj + 4×TransformerBlock 含交替 time/variate attention + Quantile Head)
任务时间序列概率预测 probabilistic forecasting(9 分位数, pinball loss 训练)
输入target shape [batch, n_variates, time](本仓 batch 2, n_variates 1, time 512, float32),target_mask bool, series_ids long, horizon=96
输出quantiles shape [9, batch, n_variates, horizon](本例 [9,2,1,96])分位数预测,median 为 q=0.5
数据契约轴顺序明确为 [batch, n_variates, time] 输入 -> [quantile, batch, n_variates, horizon] 输出;context 512(patch 32, 16 patches),horizon 96(可变,最大 768 decode_block_size),频率隐式(无时间特征),缩放经 PatchedCausalStdScaler(因果 patch-aware 标准化),缺失值以 mask 标记(本例全 observed),dtype float32
路由time-series 概率预测(参考 time-series-models.md),验收 quantile monotonic 与数值一致性

表格数据类型关联:本模型为表格型时序概率预测基础模型,适用于表格数据(CSV/Parquet)中多列数值时序的零样本预测,任务映射为 forecasting,验收指标为 quantile 单调性、forecast 数值一致性与性能,而非表格分类 logits。表格时序可按列映射为 variate 维度。

环境

  • 操作系统:Linux aarch64,昇腾 910B(Ascend910_9362),CANN 8.5.1,npu-smi 25.5.5
  • Python 3.11,torch 2.9.0+cpu + torch_npu 2.9.0.post1,transformers 4.57.6,einops 0.8.2,safetensors, huggingface_hub, jaxtyping, unit-scaling 0.2.0, dd-unit-scaling 0.1.0
  • 依赖见 requirements.txt:torch、torch_npu、transformers、huggingface_hub、safetensors、einops、jaxtyping、numpy、pandas、unit-scaling
  • 额外依赖 dd-unit-scaling 与 toto2 通过源码安装(https://github.com/DataDog/toto,--ignore-requires-python 适配 Python 3.11),gluonts 仅需 stub(Toto2Model 核心推理不依赖 GluonTS 完整功能)

安装

pip install -r requirements.txt
# 昇腾 NPU 需确保 torch 与 torch_npu 版本匹配,并已加载 Ascend 驱动/固件
# 需安装 toto2 与 dd-unit-scaling(Python 3.11 需 --ignore-requires-python):
pip install /tmp/toto_git/dd_unit_scaling --no-build-isolation --ignore-requires-python
pip install /tmp/toto_git/toto2 --no-build-isolation --ignore-requires-python --no-deps
# 或直接从 GitHub 安装:
pip install "git+https://github.com/DataDog/toto.git#subdirectory=toto2" --no-build-isolation --ignore-requires-python --no-deps
pip install "git+https://github.com/DataDog/toto.git#subdirectory=dd_unit_scaling" --no-build-isolation --ignore-requires-python
# 权重已缓存在 working/models/Datadog-Toto-2.0-4m,无需重复下载;缺失时自动从 HuggingFace 拉取(约 16 MB)

NPU 推理

inference.py 为自包含脚本,默认使用合成可复现时序(seed 0)在 npu:0 上完成 CPU 基线、NPU 推理、一致性与性能基准。核心 forward 的所有参数与参与计算的 Tensor 均显式位于 npu:0(见 npu_device_call.png 与 model_result.png)。计时前后均调用 torch.npu.synchronize()。通过 copy.deepcopy 保持 CPU/NPU 权重一致。

# 方式一:权重已缓存在本地(离线,推荐)
python inference.py

# 方式二:自动从 HuggingFace 下载(需联网,首次约 16 MB)
python inference.py

# 方式三:指定本地权重目录
MODEL_DIR=/path/to/Datadog-Toto-2.0-4m python inference.py

设备选择固定为 npu:0(可用时),输入输出均校验 device 均为 npu:0,无 CPU fallback。输入 shape [batch, n_variates, time] 严格校验,不得转置。

真实结果(Ascend 910B 单卡,npu:0)

以合成时序 batch=2, n_variates=1, ctx=512, horizon=96, 9 quantiles 为例(seed 0,torch.randn):

  • 输入:target shape (2,1,512),均值 0.0322,标准差 1.0280,范围 [-3.1537, 4.1015];target_mask 全 True;series_ids [0]
  • CPU 基线:quantiles shape (9,2,1,96) 均值 0.006625,标准差 0.837177,最值 [-1.484988, 1.497315],horizon0 9 分位数 [-1.284568, -0.831958, -0.508684, -0.237849, 0.020862, 0.284858, 0.570074, 0.916165, 1.413192],单调性 True,median 前5 [0.020862, 0.015852, 0.016639, 0.017608, 0.022102]
  • NPU 输出:相同输入在 npu:0 上 quantiles shape (9,2,1,96) 均值 0.0066,标准差 0.8372,最值 [-1.4850, 1.4973],horizon0 9 分位数 [-1.284568, -0.831959, -0.508685, -0.237849, 0.020862, 0.284857, 0.570074, 0.916166, 1.413189],单调性 True,median 前5 [0.020862, 0.015852, 0.016639, 0.017608, 0.022102]
  • 推理状态:[STATUS] SUCCESS - NPU inference completed without CPU fallback

完整输出见 model_result.png。

CPU-NPU 一致性

使用相同权重(通过 deepcopy)、相同数据切片(seed 0)、相同预处理、相同随机种子与 float32 精度,分别在 CPU 与 npu:0 上运行并保存原始输出,用 scripts/compare_outputs.py 比较:

python scripts/compare_outputs.py --cpu /tmp/toto_cpu.npy --npu /tmp/toto_npu.npy --task forecasting --atol 1e-4 --rtol 1e-3
  • 分位数(quantiles):shape (9,2,1,96) 一致,max_abs 2.74e-06,mean_abs 3.89e-07,atol=1e-4 rtol=1e-3 PASS,有限值 True,单调性 CPU True / NPU True
  • 阈值:atol=1e-4 rtol=1e-3(FP32 跨硬件正常波动,Transformer 含 LayerNorm/GELU/PatchedCausalStdScaler,误差远低于阈值)

结果:PASS。数值差异在 fp32 跨硬件正常波动范围内。

性能基准

在 npu:0、float32 下,预热 3 次后正式计时 10 次(batch=2, ctx=512, horizon=96, n_variates=1, 9 quantiles):

指标值
compile/first-run353.87 ms(首次 NPU 编译含图编译) / 22.96 ms(首轮计时 after warmup)
avg / min / max22.79 / 22.42 / 23.05 ms
p50 / p90 / p9522.82 / 23.04 / 23.04 ms
throughput87.74 windows/s(= batch / avg_time)
batch / ctx / horizon / n_variates / quantiles / dtype2 / 512 / 96 / 1 / 9 / float32
权重显存约 16 MB(model.safetensors 16 MB, 4.1M params)
峰值 NPU 显存via npu-smi ~3107 MB(空载) -> 推理期间见 device log,无 OOM

计时均含 torch.npu.synchronize() 前后同步,不含 CSV 读取/预处理(合成数据生成 <1ms)。首轮编译时间单列,稳定推理时间取 warmup 后均值。

证据图

三图由 scripts/render_xterm_evidence.mjs --style raw 根据本次真实日志生成,深灰背景、等宽字体,提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$:

  • assets/agent_workflow.png:侦察、数据检查、下载、加载、NPU 推理、一致性、benchmark 和提交校验
  • assets/npu_device_call.png:npu-smi、NPU 可用性、设备名、模型参数 device/dtype、输入与输出 Tensor device
  • assets/model_result.png:默认 python inference.py 的真实完整输出,包括 schema/窗口摘要、真实预测、同步耗时和状态

README 仅引用图片,不重复渲染工具细节;图片由 xterm.js 根据真实日志生成,非手工模拟。

限制

  • 当前为零样本推理,未在 BOOM/GIFT-Eval 全量评测;仅提供单窗口 smoke consistency,完整数据集精度需额外评测
  • 输入仅支持数值型时序,target 为 float32,文本/分类列需自行编码为数值
  • 模型最大 context 受 patch_size 32 限制,当前固定 512+96 安全;超长序列需分段或裁剪
  • 概率预测为 9 分位数(0.1~0.9),需自行筛选所需 quantiles;点预测取 median(0.5)
  • 仅适配推理(npu:0),训练仍需 GPU/CPU;未测试多卡/量化/编译缓存持久化
  • NPU 需 CANN 8.5.1 + torch_npu 2.9.0.post1,低版本可能缺算子;float64 自动回退为 float32(有限精度损失已在阈值内)
  • 合成数据仅用于 NPU 可用性验证,业务数据需替换为真实时序并重跑一致性验证

许可

  • 权重许可:apache-2.0,详见 HuggingFace 模型卡
  • 代码许可:Apache-2.0(本仓 inference.py/README),上游 toto 按原仓许可