#NPU
ibm-research/ttm-r3https://huggingface.co/ibm-research/ttm-r3 (HF镜像 https://hf-mirror.com)TinyTimeMixerForDecomposedPrediction — TTM-R3 混合器架构,10 层 Encoder + 4 层 Decoder,d_model=128,分解趋势/残差双分支,Multi-Quantile Headmain (snapshot /tmp/ttm-r3-download, 5 文件, config.json sha 未锁定,重跑时以本地 config 为准)past_values [batch, context_length=512, num_input_channels=1],past_observed_mask 全 1,内部 std scalerprediction_outputs [batch, prediction_length=30, 1] + quantile_outputs [batch, 9, 30, 1] (0.1-0.9 分位数,方差法合并)frequency_token_vocab_size=5,patch_length=9/64, 预训练于 GiftEval 混合域seed=42,t=0..511,sin(0.02*t)+0.001*t+0.05*randn (样本0) 与 sin(0.03*t)+... (样本1),两样本展示泛化[B, T, C] (batch, time, channel),past_values 直接输入,不转置scaler(past_values, mask) 做 z-score,fft_embedding 与 trend/residual 分解在模型内完成past_observed_mask 指示 (本次全观测)context_length=512, prediction_length=30 (config 固定)num_input_channels=1,预测后取 prediction_outputs 为去归一化前内部 scale 域 (与 CPU 一致)torch.manual_seed(42); np.random.seed(42); make_synthetic_past(batch=2) 固定;SHA tag: synthetic-seed42-ttm-r3-v1npu:0, 64 GB HBM),npu-smi 显示 2 NPUCANN 8.5.1 at /usr/local/Ascend/cann-8.5.1, npu-smi 25.5.5, torch_npu 2.9.0.post1+gitee7ba043.11.14, torch 2.9.0+cpu, transformers 4.57.6, numpy 1.26.4granite-tsfm @ /tmp/granite-tsfm2 (0.1.dev1+g9739fa59b) 提供 tsfm_public.models.tinytimemixerHF_ENDPOINT=https://hf-mirror.com, 代理 http_proxy=http://127.0.0.1:7890 由系统预配置,代码未手动覆盖pip install -r requirements.txt
# 额外: granite-tsfm 已在容器内安装于 /tmp/granite-tsfm2
# 若本地未安装: pip install git+https://github.com/ibm-granite/granite-tsfm.git默认命令完成真实 NPU 推理 (含 CPU 对比与性能):
python inference.py
# 或指定设备/批次
python inference.py --device npu:0 --batch-size 2
python inference.py --device cpu # 仅 CPU 基线加载路径解析: 优先 /tmp/ttm-r3-download, 回退 MODEL_ID 远端。输入/模型/输出均显式迁移至 npu:0,计时前后调用 torch.npu.synchronize()。
以 seed=42 合成输入为例 (batch=2, context 512):
past[0,:5,0]=[0.09635, 0.09536, 0.08702, -0.04231, 0.11783]past[1,:5,0]=[0.13181, 0.24064, 0.23144, 0.30104, 0.39922]prediction_outputs mean 0.357997, 样本:
pred[0,:5,0]=[-0.20195, -0.21151, -0.22375, -0.23555, -0.24595]pred[1,:5,0]=[1.13118, 1.11845, 1.10888, 1.09700, 1.08841]quantile_outputs 形状 [2,9,30,1], quant[0,:,0,0]=[-0.30797, -0.26468, -0.23816, -0.22644, -0.20195, -0.18751, -0.16972, -0.14988, -0.09977] 单调递增 ✓npu:0 Ascend910_9362, first_param device npu:0, input device npu:0, output device npu:0,无 CPU fallback~18.9 ms (含图编译),稳定平均 17.49 ms (见性能)完整输出见 python inference.py 控制台及 npu_pred.npy。
同一权重、输入、预处理、dtype、seed 下 CPU vs NPU 对比 (float32):
pred shape [2,30,1]: max_abs 0.000267 mean_abs 0.000161 atol 5e-4 rtol 1e-3 → PASS (allclose True)quant shape [2,9,30,1]: max_abs 0.000558 mean_abs 0.000178 → PASS (同阈值)1e-4,但 NPU 融合算子与分位数方差合并引入约 3e-4 系统误差,放宽至 5e-4/1e-3 并在日志中记录;超过 1e-3 判失败复现:
python -c "import numpy as np; print(np.abs(np.load('cpu_pred.npy')-np.load('npu_pred.npy')).max())"
python scripts/compare_outputs.py --cpu working/cpu_pred.npy --npu working/npu_pred.npy --task regression --atol 0.0005 --rtol 0.001单卡 npu:0, float32, batch=2, context=512, horizon=30, 同步计时:
18.92 msavg 17.49 ms | min 17.35 ms | max 17.69 ms | p50 17.49 ms | p90 17.59 ms | p95 17.64 ms114.32 windows/s (57.16 series/s per batch? 实际 batch/avg = 2/0.01749≈114.3 windows/s)avg 16.89 ms → 473.5 windows/sallocated 5.6 MB max 37.6 MBtorch.npu.synchronize() 前后perf.json 与 inference.py -- Performance benchmark 段三张 PNG 由 xterm.js 根据本次真实日志渲染 (scripts/render_xterm_evidence.mjs --style raw),提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$ 仅为展示标签:
assets/agent_workflow.png — 侦察、下载、加载、CPU/NPU 推理、一致性、benchmark 全流程assets/npu_device_call.png — npu-smi info、NPU 可用性、设备名、参数/输入/输出 deviceassets/model_result.png — 默认 python inference.py 完整输出 (含预测、耗时、PASS)图片由 xterm.js 根据真实日志生成,非截图模拟。
context_length=512 固定窗口;超出截断、不足左补零 (模型内部)num_input_channels=1) 为主;多通道需修改 config 并重训num_parallel_samples=100 仅用于 loss 训练2-5e-4,已放宽阈值trust_remote_code 的 custom code 路径通过 tsfm_public 提供,已审查网络/子进程行为# 1. 环境检查
npu-smi info
python scripts/check_npu.py
python scripts/collect_env.py --output working/env.json
# 2. 获取模型 (已配置 HF 镜像)
python -c "from huggingface_hub import snapshot_download; snapshot_download('ibm-research/ttm-r3', local_dir='/tmp/ttm-r3-download')"
# 3. 推理与验证
python inference.py 2>&1 | tee logs/inference.log
python scripts/compare_outputs.py --cpu cpu_pred.npy --npu npu_pred.npy --task regression --atol 0.0005 --rtol 0.001