toto2.Toto2Model.from_pretrained (PyTorchModelHubMixin, standalone inference-only, safetensors)target [B, n_var, T] float32, target_mask [B, n_var, T] bool, series_ids [B, n_var] long, 轴顺序 [B, C, T] 固定, context_length=512 (16 patches), prediction_length=96 (3 patches), patch_size=32, 需 target_mask 指示缺失值, series_ids 分组, 缩放通过 PatchedCausalStdScaler + asinh 内部完成, 输入保持原始尺度quantiles [9, B, n_var, horizon] float32, 分位点 [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9], 逆变换至真实尺度, 已排序保证分位数单调性, loc/scale 内部 causal scaler 统计量time-series-forecastingnpu:0torch.npu.is_available()=True, device_count=2/tmp/toto-4m (HuggingFace snapshot, model.safetensors 16582848 bytes)/tmp/toto-main (DataDog/toto @ main, toto2/toto2/model.py 1509 lines, Apache-2.0)pip install -r requirements.txt
# 额外需克隆源码以支持 Python 3.11 (官方要求 >=3.12):
git clone --depth 1 https://github.com/DataDog/toto /tmp/toto-main
export PYTHONPATH=/tmp/toto-main/toto2:/tmp/toto-main/dd_unit_scaling:$PYTHONPATHrequirements.txt:
torch==2.9.0
torch-npu==2.9.0.post1
transformers>=4.52.0
numpy==1.26.4
einops>=0.7.0
safetensors>=0.4.0
huggingface_hub>=0.20.0
jaxtyping>=0.2.25
beartype>=0.18.0
gluonts[torch]>=0.16.0
lightning>=2.3.0
dd-unit-scaling>=0.1.0
rotary-embedding-torch>=0.8.0
pandas>=2.0.0# 默认执行 (CPU 基准 + NPU 推理 + 一致性验证 + 性能测试)
PYTHONPATH=/tmp/toto-main/toto2:/tmp/toto-main/dd_unit_scaling python inference.py
# 指定设备
PYTHONPATH=/tmp/toto-main/toto2:/tmp/toto-main/dd_unit_scaling python inference.py --device npu:0
PYTHONPATH=/tmp/toto-main/toto2:/tmp/toto-main/dd_unit_scaling python inference.py --device cpu
# 仅性能测试
PYTHONPATH=/tmp/toto-main/toto2:/tmp/toto-main/dd_unit_scaling python inference.py --mode benchmark核心逻辑 (inference.py:30):
Toto2Model.from_pretrained(model_dir) → model.to("npu:0") → model.eval()target [1,2,512] + target_mask [1,2,512] 全 True + series_ids [1,2] 全 0, seed=42 确定性PatchedCausalStdScaler (cumulative mean/var per patch) → asinh → InputResidualMLP(64→1024→256) → VariateTimeTransformerDecoder (4 layers, alternating time/variate SDPA) → QuantileKnotsOutputHead(9 quantiles) → sinh inverse + sorttorch.npu.synchronize() 前后, 验证首参数 next(model.parameters()).device == npu:0quantiles [9,1,2,96], 检查 finite 与分位数单调性执行命令: PYTHONPATH=/tmp/toto-main/toto2:/tmp/toto-main/dd_unit_scaling python inference.py (在 npu:0 真实执行, 权重来自本地 /tmp/toto-4m/model.safetensors)
[INFO] Model: Datadog/Toto-2.0-4m
[INFO] Weight URL: https://huggingface.co/Datadog/Toto-2.0-4m
[INFO] Loading Datadog/Toto-2.0-4m from /tmp/toto-4m -> device npu:0
[INFO] First param device: npu:0, dtype: torch.float32, shape: [1024, 64]
[INFO] Input target shape [1, 2, 512] device npu:0 dtype torch.float32 mean 1.2788 std 1.7703
[INFO] Input target_mask shape [1, 2, 512] device npu:0 sum 1024
[INFO] horizon 96 patch_size 32 n_quantiles 9 levels [0.1 ... 0.9]
[INFO] Output quantiles shape [9, 1, 2, 96] device npu:0 dtype torch.float32
[INFO] Output mean 3.554461 std 2.745282 min -0.044925 max 7.332680
[INFO] Quantiles finite: True, monotonic: True
[INFO] Median (q=0.5) first variate first 8 steps: [0.0223, 0.0462, 0.0677, 0.0891, 0.1091, 0.1314, 0.1588, 0.1773]
[INFO] All quantiles at step 0, variate 0: [-0.0449, -0.0203, -0.0033, 0.0093, 0.0223, 0.0362, 0.0503, 0.0671, 0.0910]
[INFO] Latency 243.12 ms (first run includes compilation) / 24.52 ms stable
[CPU] latency ~150 ms (reference)
[NPU] stable latency 24.5 ms
[CONSISTENCY] max_abs 8.58e-06 mean_abs 1.43e-06 finite True monotonic True passed True
[BENCHMARK] avg 24.11 ms min 23.91 max 24.27 p50 24.14 p90 24.20 p95 24.23
[RESULT] PASS - NPU inference completed, CPU-NPU consistency within threshold, no CPU fallbackPatchedCausalStdScaler+asinh 预处理、同一 dtype float32、同一 seed=42, 分别在 cpu 与 npu:0 执行, 保存原始输出 .npy 后比较PYTHONPATH=/tmp/toto-main/toto2:/tmp/toto-main/dd_unit_scaling python -c "import numpy as np; ..."
python .opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu /tmp/toto_cpu_quantiles.npy --npu /tmp/toto_npu_quantiles.npy --task regression --atol 1e-4 --rtol 1e-3F.scaled_dot_product_attention with MuP scale 1/qk_dim)npu:0, warmup 3, iters 10, context 512, horizon 96, batch 1, variates 2, dtype float32, 计时含 torch.npu.synchronize() 前后同步, 不含数据生成npu:0三张 PNG 由 xterm.js 根据本次真实日志生成 (非手工绘图, 展示用固定提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$):
assets/agent_workflow.png - 侦察、下载、加载、NPU 推理、一致性、benchmark 与提交校验的完整工作流assets/npu_device_call.png - npu-smi info、NPU 可用性、设备名、模型参数 device/dtype、输入输出 Tensor device 证明 npu:0 真实调用assets/model_result.png - python inference.py 默认执行的完整输出, 含输入摘要、真实分位数预测、同步耗时与 PASS 状态

图片由 xterm.js 根据本次真实日志渲染, 深灰背景、白色等宽字体, 非原始桌面截图。
npu:0 推理, 未覆盖多卡分布式trust_remote_code=False, 无自定义 Python 风险代码has_missing_values=False, decode_block_size=768, scaler_fallback_min_obs=0#NPU Ascend Ascend910