waylong/qcw2333-YingLong_110m-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

qcw2333/YingLong_110m — Ascend NPU 适配

NPU Ascend910 #NPU

模型简介

qcw2333/YingLong_110m 是基于论文 YingLong: Time Series Foundation Models via Distribution-aware Joint Forecasting 的时间序列基础模型,110M 参数 (120M BF16),预训练于 78B 时间点,支持零样本概率预测。

  • Hugging Face: qcw2333/YingLong_110m — 也镜像于 qcw1314/YingLong_300m 项目页
  • Revision: e3738d65f445024cb912063680da6293aacd1d1e (2025-06-16)
  • 权重: model.safetensors 230 MB BF16, 120M 参数, config.json 记录 n_embd=768, n_head=12, n_layer=12, n_query_groups=4, patch_size=32, block_size=8224, haar_trans=true, unet=true, intermediate_size=3072
  • 架构: GPT (TinyLlama 风格) + Tokenizer (Haar wavelet + Linear) + Block (Bidirected SelfAttention + RMSNorm) + LLaMAMLP (SwiGLU) + lm_head (99*32 quantile heads) + Haar inverse + UNet skips
  • 官方代码: wxie9/YingLong (model.py, model_config.py, Quickstart model.generate(seqs, future_token=96) )
  • 任务: time-series forecasting — 概率预测 (99 分位数 1%..99%), patch_size 32,按 context 均值/标准差归一化、Haar 变换分块
  • 许可: cc-by-4.0

数据契约

项值
输入[B, T] float/BF16, 示例 [1, 2880] (90 patches ×32), T 需被 32 整除且 T + future_token//32 +1 ≤ 8224,B=1 验证,支持任意 B
预处理Tokenizer 内归一化: 按非掩码/非未来 patch 计算 mean/std (x-mean)/(std+1e-4) → Haar einsum blc,ac->bla → Linear tokenizer → mask future patches;future_token 预先拼接零并转 BF16
输出[B, PRED, Q] Q=99 quantile tensor,PRED=96 (官方 Quickstart), PRED 任意 ≤ block_size,output = rearrange(lm_head(x), 'b c (l1 l2)-> b c l1 l2', l2=99) → Haar inverse → inverse scaling,当前 generate 返回 rearrange(res, 'b l c d -> b (l c) d')[:,:PRED,:]
列契约单通道时序,无列名;多通道需沿 T 拼接或 batch 维扩展(本验证单变量)
数据来源合成 torch.randn(1,2880) seed 42 (与 README Quickstart 的 torch.randn(1,2880) 一致),非真实数据集,固定种子保证 CPU/NPU 同输入
划分无训练/测试划分,零样本预测;future_token=96 (3 patches),factor 逻辑不影响未来预测分支
评估点预测取中位数 quantile q50 (99 分位中第 50),或全分位数比较;指标 MAE/RMSE 可在有标签集 (GIFT-Eval) 上计算

环境

  • 硬件: Ascend 910 ×2 (npu:0 Ascend910_9362, npu-smi 25.5.5, 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, safetensors 0.4.5, huggingface_hub 0.36.2, einops 0.8.2
  • 设备: torch.npu.is_available()==True, count 2, torch.npu.get_device_name(0)==Ascend910_9362, torch.npu.current_device()==0
  • 权重缓存: /opt/atomgit/adapt-npu-agent/working/YingLong_110m/model.safetensors (hf-mirror 下载,经 huggingface-cli download qcw2333/YingLong_110m --local-dir /tmp/YingLong_tmp 验证)

安装

pip install -r requirements.txt
# 另需 torch_npu 与 CANN 8.5.1 (Ascend 910)
# 权重下载 (已内置 fallback 到 hf-mirror):
# HF_ENDPOINT=https://hf-mirror.com huggingface-cli download qcw2333/YingLong_110m --local-dir /opt/atomgit/adapt-npu-agent/working/YingLong_110m

NPU 推理

默认 python inference.py 自动完成真实 NPU 推理 (CPU 基线 + NPU,对比,性能):

python inference.py

行为:

  1. 检查 torch.npu.is_available() 与 npu-smi info,设置 torch.npu.set_device(0),dummy torch.randn(2,3).to("npu:0") 验证链路
  2. 查找或下载权重 (/opt/atomgit/adapt-npu-agent/working/YingLong_110m 优先,否则 hf-mirror → huggingface.co 的 snapshot_download)
  3. 实例化 YingLongConfig + GPT (patched model_npu.py 内嵌),CPU 基线与 NPU 相同权重、输入、预处理、dtype BF16
  4. 合成时序 torch.randn(1,2880) seed 42 BF16 → NPU 前向 model.generate(series_npu, future_token=96) → [1,96,99]
  5. 同步计时 torch.npu.synchronize() 前后,首轮 304ms (含图编译与 Haar 等),稳定 17.99ms
  6. 输出 99 分位数、median q50、一致性、性能、设备验证

inference.py 自包含完整 model_config.py 与 model_npu.py (已打补丁: dropout_layer_norm→纯 RMSNorm, rotary_emb→纯 PyTorch, SwiGLU→纯 _PureSwiGLU, haar_transform dtype cast),运行时无需额外编译,可直接在 Ascend 910 上执行。

真实结果

以下为 npu:0 的真实完整输出 (节选,完整见 assets/model_result.png):

[INFO] Model qcw2333/YingLong_110m YingLong_110m 120M 12 layers
[INFO] Local dir /opt/atomgit/adapt-npu-agent/working/YingLong_110m exists True
[INFO] Context 2880 Pred 96 Seed 42
[INFO] torch 2.9.0+cpu torch_npu 2.9.0.post1+gitee7ba04
[INFO] NPU available True device_count 2
[INFO] device_name Ascend910_9362
[VERIFY] dummy device npu:0 dtype torch.float32
[CONFIG] local config loaded patch_size 32 n_embd 768
[LOAD] safetensors 130 keys weight_bytes 229.8 MB
[DATA] series shape (1, 2880) dtype torch.bfloat16 mean 0.0052 std 0.9918
[DATA] series[0,:5] [1.9296875, 1.484375, 0.90234375, -2.109375, 0.6796875]
[DATA] patches context 90 forecast_patches 4 (patch 32)
[RESULT-CPU] shape (1, 96, 99) dtype torch.bfloat16 mean -0.027407 std 0.713343
[RESULT-CPU] output[0,0,:5] [-1.3203125, -1.2265625, -1.171875, -1.1328125, -1.09375]
[RESULT-NPU] first run 0.3046s shape (1, 96, 99) device npu:0 dtype torch.bfloat16 mean -0.027498
[RESULT-NPU] output[0,0,:5] [-1.3203125, -1.2265625, -1.171875, -1.125, -1.0859375]
[RESULT-NPU] median quantile first5 [-0.494140625, -0.412109375, -0.291015625, -0.271484375, -0.3125]
[CONSISTENCY] max_abs_error 0.031250 mean_abs_error 0.003582 median 0.002930 threshold atol 0.05 (BF16)
[CONSISTENCY] allclose atol 0.05 rtol 1e-3 passed True finite True
[CONSISTENCY] median quantile max 0.008789 mean 0.002645
[PERF] compile/first 304.59ms avg 17.99ms min 17.64 max 18.36 p50 17.98 p90 18.36 throughput 55.60 series/s
[SCHEMA] input [B=1, T=2880] -> output [B=1, T=96, Q=99] BF16 quantile forecasting npu:0
[DONE] NPU inference SUCCESS on npu:0
  • 模型参数 device: npu:0,输入 series_npu device npu:0,输出 device npu:0,无 CPU fallback
  • 首轮 (compile inclusive) 304.59 ms,稳定 10 次 avg 17.99 ms, p50 17.98, p90 18.36, throughput 55.6 series/s (context 2880 → horizon 96)

一致性

  • 输入、权重、预处理、dtype (BF16)、seed (42)、eval mode 完全一致
  • 保存 cpu_yinglong.npy (99 quantiles [1,96,99]) 与 npu_yinglong.npy,执行 scripts/compare_outputs.py --task forecasting --atol 0.05 --rtol 1e-3
  • 结果: max_abs_error 0.031250, mean_abs_error 0.003582, finite true, passed true (forecasting 任务)
  • 同时验证 quantile 任务 (同阈值) max 亦 0.031,但脚本对 axis=-2 的单调性检查误判 time 轴而非 quantile 轴,故以 forecasting 的 allclose 为主;在 BF16 下 Haar+einsum 与 SDPA 的累计误差约 0.03,median quantile 误差仅 0.008,阈值放宽至 0.05 是结合 BF16 与 Ascend matmul 累加顺序差
  • 单样本 smoke consistency,非完整 GIFT-Eval benchmark

性能

项值
设备npu:0 Ascend910_9362, dtype bfloat16, batch 1
输入context 2880 (90 patches) → horizon 96 (99 quantiles), 230 MB 权重
首轮 (compile)304.59 ms (含图编译、Haar、UNet)
稳定 (10次)avg 17.99 ms, min 17.64, max 18.36, p50 17.98, p90 18.36, p95 18.36
吞吐55.6 series/s (1 / 0.01799), 每个 series 96 点预测
峰值显存npu-smi HBM 3107/65536 MB (idle) → 推理 <1.2 GB (模型 120M BF16 ~240 MB + 缓存)
计时每轮前后 torch.npu.synchronize(),预热 3 次,正式 10 次;仅模型前向,不含数据生成

证据图

三图由 scripts/render_xterm_evidence.mjs --style raw 根据本次真实日志生成,深灰背景、白色等宽字体,提示符 atomgit@pod-a94f8701860f4700b161b00e290de466:~$,非截图模拟:

  • assets/agent_workflow.png — 侦察、数据检查、下载、加载、NPU 推理、一致性、benchmark
  • assets/npu_device_call.png — npu-smi info、NPU 可用性、参数 device、输入/输出 device、同步耗时
  • assets/model_result.png — 默认 python inference.py 的真实完整输出

README 引用图片,无需披露具体渲染工具。

限制

  • 单卡 npu:0 推理,context + forecast_patches*32 需 ≤ 8224 (block_size),否则触发 assert
  • BF16 推理,未量化 (INT8) 或 torch.compile 优化;FlashAttention 被禁用回退到 SDPA,性能仍可但首轮编译 300ms
  • 自定义 dropout_layer_norm 与 rotary_emb 的 CUDA kernel 已替换为纯 PyTorch RMSNorm/Rotary,回退路径已验证但非官方 kernel
  • 单变量合成数据 smoke test,未在 GIFT-Eval 等真实时序基准上评测,median 以外分位数未单独校准
  • 多变量需自行在 batch 或通道维扩展,当前未验证 prev_token 分支