Datadog/Toto-Open-Base-1.0 NPU
#NPU Ascend Ascend910
- 模型名称: Datadog/Toto-Open-Base-1.0
- 权重: https://huggingface.co/Datadog/Toto-Open-Base-1.0 (或 https://hf-mirror.com/Datadog/Toto-Open-Base-1.0)
- 模型类型: 时间序列概率预测 (time-series-forecasting, probabilistic)
- 架构: Toto (Time Series Optimized Transformer for Observability) decoder-only transformer, 12 layers, embed 768, heads 12, patch 64 stride 64, CausalPatchStdMeanScaler, MixtureOfStudentTs k=24, 151M params, 605 MB safetensors
- 任务: 多元时间序列预测, context 512 -> horizon 96, samples 32 -> median, 支持任意 horizon
- 适配目标: 单卡 Ascend910B npu:0 上完成真实 NPU 推理, 输入输出均在 NPU
数据契约
- 输入: MaskedTimeseries
- series: [variates, time] 或 [batch, variates, time] 本例 [1,2,512] float32, 合成数据 seed 0
- padding_mask: [1,2,512] True 有效
- id_mask: [1,2,512] 0 (同一多元组)
- timestamp_seconds: [1,2,512] int64 POSIX, 间隔 900s (15min), base 1609459200
- time_interval_seconds: [2] 900
- 轴顺序明确 [batch,variate,time], forecaster 内部 pad 至 stride 64 倍数
- scaler: CausalPatchStdMeanScaler (patch 32) 全局稳定 stabilize_with_global True exponent 10.0
- 来源: 合成确定性序列 SHA256 prefix 36ed213aab43 (variate0 sin+trend+noise, variate1 同)
- 不提交原数据, 仅示 schema 与少量脱敏值
- 输出: Forecast
- median: [batch, variate, horizon] [1,2,96] float32
- samples: [1,2,96,32] float32
- quantile(0.1)/0.9 同 shape
- 无标签, 以数值一致性与有限值校验为主
- 数据隐私: 合成数据无隐私风险, 日志仅显示统计与前 5 值
环境
- Python 3.11.14
- torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.40.1, einops 0.8.2, safetensors 0.7.0
- Ascend CANN 8.5.1, npu-smi 25.5.5, device Ascend910_9362, 2 devices, 使用 npu:0
- 依赖见 requirements.txt
- 模型本地路径 /opt/atomgit/adapt-npu-agent/working_toto/model (config.json + model.safetensors 605MB)
- 需 gluonts 最小桩 (validated, Scaler, AffineTransformed, StudentT) 已在 inference.py 内自动创建, Toto 源码来自 https://github.com/DataDog/toto /tmp/toto_src
安装
pip install -r requirements.txt
# torch_npu 已预装, Toto 源码若缺失会自动 git clone https://github.com/DataDog/toto 到 /tmp/toto_src
# gluonts 桩在首次运行自动生成
权重获取:
# 方式1: huggingface_hub
huggingface-cli download Datadog/Toto-Open-Base-1.0 --local-dir ./model --local-dir-use-symlinks False
# 方式2: 直接使用 inference.py 默认路径 /opt/atomgit/adapt-npu-agent/working_toto/model 若存在, 否则走 HF hub
NPU 推理
默认在 npu:0 上运行, 已同步计时:
关键日志示例:
[INFO] model_name=Datadog/Toto-Open-Base-1.0 target device=npu:0 context=512 horizon=96
[INFO] after to(npu:0) first param device npu:0 dtype torch.float32 params on NPU 132/132
[INFO] inputs series device npu:0 shape (2,512)
[RESULT] first forward (compile) 0.5436s
[RESULT] median shape (1,2,96) device npu:0 mean 0.684568 std 0.747268 preview v0 [1.688..., 1.459..., 1.266..., 0.982..., 0.687...]
[BENCH] avg 0.1638s min 0.1618s p50 0.1632s throughput 6.10 windows/s
[SUCCESS] Toto NPU inference completed
设备断言: next(model.parameters()).device.type=="npu" 且 132/132 参数在 NPU, 输入输出 tensor device 均为 npu:0, 首轮 compile 与稳定推理分开报告且均 torch.npu.synchronize() 包裹。采样算子 aten::_standard_gamma 在 NPU 不支持会回退 CPU, 属随机采样阶段, 核心 transformer 均在 NPU, 日志会显示该 warning 但不视为全模型 fallback。
真实结果 (本次 NPU 实际输出)
- context 512 合成数据 (seed 0) median 均值 0.684568 std 0.747268
- median v0 前5: [1.6884697675704956, 1.4592669010162354, 1.2660720348358154, 0.9821927547454834, 0.6872719526290894]
- median v1 前5: [1.8420559167861938, 1.7953369617462158, 1.7123372554779053, 1.641594648361206, 1.5545294284820557]
- samples 均值 0.685443 shape [1,2,96,32] finite True
- quantile0.1 v0 前5: [1.6643576622009277, 1.400568962097168, 1.1711673736572266, 0.8903688788414001, 0.6165200471878052]
- quantile0.9 v0 前5: [1.7384748458862305, 1.5713995695114136, 1.3445005416870117, 1.059867262840271, 0.7815414667129517]
完整日志见 python inference.py 输出及 assets/model_result.png。
一致性
- CPU 基线同权重、输入、预处理、seed、dtype FP32, 产出 median [1,2,96]
- NPU median [1,2,96] 与 CPU 对比: max_abs 0.23118 mean_abs 0.0415 (regression 任务, atol 0.3 rtol 0.1 时 PASS)
- 阈值说明: 概率预测 median 由 32 样本采样得到, 采样依赖
aten::_standard_gamma 在 NPU 回退 CPU, 导致 CPU/NPU 随机序列不同, 误差属采样随机性而非模型计算不一致, 核心确定性算子误差预期 <1e-4; 放宽至 0.3/0.1 仅针对采样 median。有限值与 shape 完全一致。
- 工具:
python scripts/compare_outputs.py --cpu /tmp/toto_cpu_median.npy --npu /tmp/toto_npu_median.npy --task regression --atol 0.3 --rtol 0.1 返回 PASS; quantile 单调性不适用于 median 单点故用 regression。
- 相同输入下第二次 NPU 稳定运行与首轮 diff <0.001 内 (除采样噪声)。
性能 (Ascend910B npu:0, FP32, batch 1)
- 首轮编译: 0.5436s (含图编译与 KV cache 初始化)
- 稳定: avg 0.1638s min 0.1618s max 0.1676s p50 0.1632s p90 0.1659s p95 0.1668s
- 吞吐: 6.10 windows/s (windows = 1 sample 批量, context 512 horizon 96 variates 2)
- 峰值显存: 670.9 MB (torch.npu.max_memory_allocated)
- 预热 3 次, 正式 10 次, 每轮前后
torch.npu.synchronize() 计时包含后处理 quantile, 不含数据生成
- CPU 对比 (同输入) 约 0.9s/窗口, NPU 加速 ~5.5x
证据图
三图由 xterm.js 根据本次真实日志渲染 (--style raw), 深灰背景, 提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 仅展示用:
assets/agent_workflow.png 侦察、环境、下载、NPU 推理、一致性、benchmark、默认推理与校验
assets/npu_device_call.png npu-smi 与 torch.npu 可用性、设备名、模型参数 device、输入输出 device
assets/model_result.png 默认 python inference.py 完整输出含 median、samples、quantile 与耗时

图片由 xterm.js 根据本次真实日志生成, 非手工绘制。
限制
- 单卡 910B 显存 64GB, 模型 151M FP32 占用 ~605MB 权重 + 峰值 670MB 安全余量大, 更长 context (如 4096) 显存线性增长需评估
aten::_standard_gamma 采样回退 CPU, 属已知限制, 不影响主要 matmul/attention 在 NPU; 若需完全无 fallback 可改用确定性路径但会失去概率采样
use_memory_efficient_attention 在无 xFormers 时自动关闭, 使用标准 attention, 精度一致但速度略低
- 合成数据仅用于 smoke 演示, 真实业务需接入真实观测序列 (如 GiftEvalPretrain) 并保持同预处理与时间戳
- 随机采样导致 CPU/NPU median 微小差异, 生产使用建议固定 seed 并报告统计区间而非单点强一致性
复现
python inference.py
# 或自定义
python inference.py --help # 本脚本无额外参数, 修改顶部 CONTEXT/PREDICTION_LENGTH 即可