w
gcw_uQ09W7jl/bytedance-research-Timer-S1-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Timer-S1 NPU #NPU

bytedance-research/Timer-S1 在华为昇腾 Ascend 910 (910B) 单卡 npu:0 上的真实适配与推理验证。基于 torch_npu 将原始 PyTorch MoE 时间序列基础模型迁移至 NPU,保持 BF16 精度与概率分位输出一致性。

  • 模型: bytedance-research/Timer-S1 (revision: 8911430cc7f32add5c8913afe12e3b05742f5bb2, 8.3B total / 0.75B active, MoE 32 experts, 24 layers, hidden 1024)
  • 任务: 时间序列概率预测 (probabilistic quantile forecasting, 9 quantiles [0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9])
  • 权重: https://huggingface.co/bytedance-research/Timer-S1
  • 推理入口: python inference.py (默认 npu:0, bfloat16, revin=True)
  • 标签: NPU Ascend Ascend910 time-series forecasting MoE Timer-S1

数据契约

  • 输入格式: [batch, context_length] (B T), float/bfloat16, 例 [1,512]
  • 轴顺序: B T (batch, time), 时间维连续
  • context_length: 512 (示例, 兼容 16 的倍数, 最大 11520), input_token_len=16
  • prediction_length (horizon): 32 (max_new_tokens), 输出 [B,9,H] 例 [1,9,32]
  • 量化输出: 固定 9 分位, 已按 [0.1..0.9] 排序, 单调递增需满足 q0<q1<...<q8
  • 缩放: revin=True (模型内可逆实例归一化, 含逆变换)
  • 预处理: 无外部 CSV, 使用确定性合成数据 torch.randn(42) 生成, 哈希校验 745e4d5ed328cd10, 统计 mean=0.0388 std=1.0624 min=-2.89 max=2.89
  • 后处理: 无, 直接返回分位预测 float32
  • 批次: 1, 通道隐式 1 (单变量), 支持批量多序列

不得擅自改变轴顺序或用随机输入冒充业务数据, 本示例窗口与数据来源已固定并记录哈希。

环境

  • 硬件: Ascend910_9362, npu:0 (HBM 64GB), 2 devices
  • 驱动: npu-smi 25.5.5, CANN 8.5.1 /usr/local/Ascend/cann-8.5.1
  • 软件: Python 3.11.14, torch 2.9.0+cpu, torch_npu 2.9.0.post1+gitee7ba04, transformers 4.57.6, numpy 1.26.4
  • 镜像: HF_ENDPOINT=https://hf-mirror.com
  • 校验: torch.npu.is_available()==True, device_count=2, torch.npu.get_device_name(0)=Ascend910_9362
+------------------------------------------------------------------------------------------------+
| npu-smi 25.5.5  Ascend910  HBM 3105/65536 MB  Power 168.9W  Temp 45C |

安装

pip install -r requirements.txt
# requirements: torch==2.9.0 torch-npu==2.9.0.post1 transformers==4.57.6 numpy==1.26.4 safetensors accelerate huggingface_hub

模型下载 (必须设镜像, 权重 16.6GB, 4 分片):

HF_ENDPOINT=https://hf-mirror.com python3 -c "from huggingface_hub import snapshot_download; snapshot_download(repo_id='bytedance-research/Timer-S1', local_dir='/tmp/Timer-S1', max_workers=1)"
# 或 huggingface-cli: HF_ENDPOINT=https://hf-mirror.com huggingface-cli download bytedance-research/Timer-S1 --local-dir /tmp/Timer-S1

本地加载无需联网: AutoModelForCausalLM.from_pretrained("/tmp/Timer-S1", trust_remote_code=True, dtype=torch.bfloat16)

NPU 推理

python inference.py
# 预期: 加载 4 分片 -> CPU 基线 -> NPU npu:0 推理 -> 一致性 PASS -> 性能 3.5 windows/s

核心逻辑 (inference.py:resolve_model_dir, get_model, model.to("npu:0"), torch.npu.synchronize()):

model = AutoModelForCausalLM.from_pretrained(model_dir, trust_remote_code=True, dtype=torch.bfloat16, device_map="cpu")
model.to("npu:0")
seqs = torch.randn(1, 512, dtype=torch.bfloat16).to("npu:0")
torch.npu.synchronize(); t0=time.time()
out = model.generate(seqs, max_new_tokens=32, revin=True)
torch.npu.synchronize(); print((time.time()-t0)*1000)
# out shape [1,9,32]

设备显式检查: first_param device: npu:0, input device: npu:0, output device: npu:0 (无 CPU fallback)

真实结果 (本次运行)

  • CPU: shape [1,9,32] dtype float32 device cpu time 1857ms median [0,4,:5]=[0.1109,0.1571,0.1565,0.0953,0.1026] quantiles first step [-1.256, -0.807, -0.469, -0.169, 0.110, 0.387, 0.661, 0.976, 1.366] mean 0.0656 std 0.8253
  • NPU first run (compile) 594 ms, second run 277 ms (torch.npu.synchronize() 计时)
  • NPU: shape [1,9,32] device npu:0 median [0,4,:5]=[0.1088,0.1560,0.1545,0.0958,0.1034] quantiles first step [-1.264, -0.812, -0.469, -0.172, 0.108, 0.385, 0.661, 0.976, 1.383] mean 0.0652 std 0.8265 finite True
  • 输出已保存 /tmp/timer_cpu.npy /tmp/timer_npu.npy

一致性

  • 比较对象: 原始 forecast quantiles (非逆变换后标签), float32 视图
  • 指标: max_abs 0.0166, mean_abs 0.00209, allclose(atol=1e-2, rtol=1e-2)=True
  • 阈值解释: BF16 模型在 Ascend 上的算子融合与舍入导致约 1e-2 量级误差, 单调性保持 (q0<...<q8 CPU/NPU 均为 True), finite True, shape [1,9,32] 一致
  • 命令: python scripts/compare_outputs.py --cpu /tmp/timer_cpu.npy --npu /tmp/timer_npu.npy --task regression --atol 1e-2 --rtol 1e-2 -> passed true
  • 声明: 同权重、同输入、同预处理、同 dtype (bfloat16)、同 seed (42)、eval 模式, 无 CPU fallback, 单窗口 smoke consistency (非全量 benchmark)

性能

  • 计时: torch.npu.synchronize() 前后, 首轮编译与稳定阶段分离
  • 预热 3 次, 测试 10 次
  • NPU npu:0 bfloat16 batch=1 context=512 horizon=32 quantiles=9
  • compile/first_run 594.00 ms | avg 279.71 ms min 270.29 max 287.80 p50 278.87 p90 286.18 p95 286.99
  • 吞吐: 3.58 windows/s (3.58 series/s) (batch 1)
  • 显存: allocated 16000 MB reserved 19362 MB peak 16032 MB (8.3B BF16 权重 ~16GB)
  • 预处理不计入, revin 含在模型内
  • 数据: 合成张量直接生成, 无 CSV 读取

证据图

三图由 xterm.js 根据本次真实日志渲染 (--style raw), 深灰背景白字, 提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$, 仅展示非敏感日志, 详细生成见 scripts/render_xterm_evidence.mjs。

agent workflow npu device call model result

限制

  • 单变量 B T 输入, 多变量需通道独立外循环
  • 最大上下文 11520, 超长需分段或 use_cache=False
  • 概率输出仅 9 固定分位, 非采样
  • 8.3B 模型需 >=16GB HBM, 推荐 40GB+ (NPU 64GB 可行, CPU 需 32GB 内存)
  • 未做训练适配, 仅推理 (fit 无)
  • 超长序列/大 batch 可能 OOM, 可减 context 或 batch 至 1

许可证

Apache-2.0 (同上游). 使用 trust_remote_code=True 需审查 configuration_TimerS1.py modeling_TimerS1.py ts_generation_mixin.py 无网络/子进程/文件写入风险.