#NPU
本仓库将 HuggingFace 上的 Datadog/Toto-2.0-1B(Toto 2.0 时间序列基础预测模型,1.04B 参数,patch 32, d_model 1536, 36 层 Transformer,9 分位数概率预测)适配为可在单卡昇腾 Ascend 910B(npu:0)上真实运行的 GitCode AI model 仓库。支持多变量时序的零样本概率预测,输出 9 个分位数(0.1~0.9)与中位数点预测。
model.safetensors 约 3.9 GB(1,041,033,024 参数, float32),config.json patch_size 32, d_model 1536, num_heads 24, num_layers 36, d_ff 4096, num_groups 24, layer_group_size 36toto2.model.Toto2Model, 论文 https://arxiv.org/abs/2605.20119)| 项 | 值 |
|---|---|
| 模型 | Datadog/Toto-2.0-1B(revision 1604e1a, 1.04B params, 3.9 GB safetensors) |
| 架构 | Toto2Model (PatchedCausalStdScaler + PatchProj + 36×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 维度。
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.0dd-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-1B,无需重复下载;缺失时自动从 HuggingFace 拉取(约 3.9 GB, 需 hf_transfer 加速)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 下载(需联网,首次约 3.9 GB, 建议 HF_HUB_ENABLE_HF_TRANSFER=1)
HF_HUB_ENABLE_HF_TRANSFER=1 python inference.py
# 方式三:指定本地权重目录
MODEL_DIR=/path/to/Datadog-Toto-2.0-1B python inference.py设备选择固定为 npu:0(可用时),输入输出均校验 device 均为 npu:0,无 CPU fallback。输入 shape [batch, n_variates, time] 严格校验,不得转置。
以合成时序 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]quantiles shape (9,2,1,96) 均值 0.004896,标准差 0.818395,最值 [-1.418552, 1.395958],horizon0 9 分位数 [-1.292498, -0.832691, -0.505357, -0.232612, 0.030074, 0.293055, 0.566285, 0.887169, 1.332472],单调性 True,median 前5 [0.030074, 0.027982, 0.028167, 0.027691, 0.029076]npu:0 上 quantiles shape (9,2,1,96) 均值 0.0049,标准差 0.8184,最值 [-1.4186, 1.3960],horizon0 9 分位数 [-1.292499, -0.832690, -0.505357, -0.232612, 0.030074, 0.293055, 0.566285, 0.887169, 1.332471],单调性 True,median 前5 [0.030074, 0.027982, 0.028167, 0.027691, 0.029076][STATUS] SUCCESS - NPU inference completed without CPU fallback完整输出见 model_result.png。
使用相同权重(通过 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-3max_abs 5.84e-06,mean_abs 6.45e-07,atol=1e-4 rtol=1e-3 PASS,有限值 True,单调性 CPU True / NPU Trueatol=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-run | 487.80 ms(首次 NPU 编译含图编译) / 164.90 ms(首轮计时 after warmup) |
| avg / min / max | 163.95 / 162.68 / 165.50 ms |
| p50 / p90 / p95 | 163.77 / 164.96 / 165.23 ms |
| throughput | 12.20 windows/s(= batch / avg_time) |
| batch / ctx / horizon / n_variates / quantiles / dtype | 2 / 512 / 96 / 1 / 9 / float32 |
| 权重显存 | 约 3.9 GB(model.safetensors 3.9 GB, 1.04B params) |
| 峰值 NPU 显存 | via npu-smi ~3114 MB 空载 -> 推理期间 ~4-5 GB + 激活,无 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 deviceassets/model_result.png:默认 python inference.py 的真实完整输出,包括 schema/窗口摘要、真实预测、同步耗时和状态README 仅引用图片,不重复渲染工具细节;图片由 xterm.js 根据真实日志生成,非手工模拟。

target 为 float32,文本/分类列需自行编码为数值npu:0),训练仍需 GPU/CPU;未测试多卡/量化/编译缓存持久化apache-2.0,详见 HuggingFace 模型卡inference.py/README),上游 toto 按原仓许可