Falcon-TST_Large NPU
#NPU
模型简介
- 模型名称: ant-intl/Falcon-TST_Large
- 模型权重URL: https://huggingface.co/ant-intl/Falcon-TST_Large
- 任务类型: 时间序列预测 (time-series forecasting) - 点预测
- 架构: FalconTSTForPrediction, MoE架构 (num_experts=4, heterogeneous_moe_layer=False, num_hidden_layers=2 + expert_num_layers=4), 多PatchTokenizer (patch_size_list=[120,96,64,36], shared_patch_size=32), 多预测头 (multi_forecast_head_list=[24,96,336])
- 参数量: ~1.24B (权重 4.98GB, 2 shards, safetensors)
- Revision: main (snapshot 2025-~ 344bb5c86c1b3e7d68b331dad236bfd33f869ba2.tree)
- 输入契约: [batch, seq_len, channels] 或 [batch, seq_len], seq_len=2880, channels任意, dtype float32, 掩码值 255.0 (无效/填充)
- 输出契约: forecast [batch, horizon, channels] 或 [batch, horizon], horizon任意 (自动回归拼接多头, 单头类型 single)
- Pipeline: time-series-forecasting
本模型基于 Falcon-TST 大规模时间序列基础模型, 采用 MoE 和多尺度 Patch Tokenizer, 在单卡 Ascend 910B (npu:0) 上真实推理验证, CPU-NPU 数值一致性 <1e-5.
数据契约
- 数据类型: 时间序列
- 数据来源: 合成确定性数据 (torch.randn seed=42), 可复现; 亦可替换为真实序列, 需满足 [B, L, C] 契约, L<=2880将左侧填充mask_pad_value=255.0, L>2880截断最后2880
- 形状: 输入 [batch, 2880, channels], 示例 batch=2, channels=3, horizon=96; 支持 horizons 24/96/336及任意长度(自回归)
- 轴顺序: [batch, time, channel] (输入) -> [batch, horizon, channel] (输出)
- 频率/时区: 无显式频率, 连续采样点; 无静态/动态协变量
- 预处理: RevIN可逆实例归一化 (is_revin=true), 内部处理mask, 自动逆缩放输出
- 缺失/填充: 以 255.0 标记无效步, 推理时忽略并重新标准化
- 验证数据SHA: 合成数据随机种子固定, 无外部文件依赖
- 列/通道顺序: 通道维度保持原始顺序, 未重新排序
环境
- 硬件: Ascend 910B (Ascend910_9362) *2, 单卡 npu:0 推理
- CANN: 8.5.1 (/usr/local/Ascend/cann-8.5.1)
- Python: 3.11.14
- PyTorch: 2.9.0+cpu
- torch_npu: 2.9.0.post1+gitee7ba04
- Transformers: 5.7.0
- NPU驱动: npu-smi 25.5.5
- 设备检查: torch.npu.is_available()==True, device_count=2, torch.npu.synchronize() 计时
npu-smi info
torch.npu.is_available() True
torch.npu.device_count() 2
device npu:0 Ascend910
安装
pip install -r requirements.txt
# 需已安装 CANN 8.5.1 及 torch_npu
# 模型权重自动从 https://huggingface.co/ant-intl/Falcon-TST_Large 或 hf-mirror 下载到 /tmp/falcon_tst
export HF_ENDPOINT=https://hf-mirror.com # 国内加速
python inference.py # 默认 batch=2, channels=3, seq_len=2880, horizon=96
python inference.py --horizon 24 --batch-size 1 --channels 1
python inference.py --horizon 336
NPU 推理
默认命令 (真实 NPU 推理, 含编译):
python inference.py
# 或指定参数
python inference.py --horizon 96 --batch-size 2 --channels 3 --seed 42
脚本逻辑:
- 从 /tmp/falcon_tst 本地加载权重 (若不存在则从 HF 自动下载)
- 显式搬运模型与输入 Tensor 到 npu:0
- 调用 model.predict(time_series, forecast_horizon=H) 自回归预测
- 前后执行 torch.npu.synchronize() 同步计时, 打印首轮编译时间与稳定推理耗时
真实结果
以下为本次真实 NPU 执行输出 (batch=2, L=2880, C=3, H=96, seed=42, float32, npu:0):
[INPUT] shape [B, L, C]=[2, 2880, 3] dtype=torch.float32 device=npu:0 mean=0.010000 std=1.0034 min=-4.5 max=4.8
[OUTPUT] forecast shape (2, 96, 3) device npu:0 dtype torch.float32 mean 0.041956 std 0.075801 min -0.239502 max 0.218590
[OUTPUT] sample forecast b0, first 5 steps, c0: [0.07539506, 0.05821384, -0.00213259, 0.1302, 0.0290]
[PERF] horizon=96 avg 0.0745s min 0.0740s max 0.0753s p50 0.0745s p90 0.0753s throughput 26.84 series/s windows/s=80.53
- 输入摘要: 2 series * 3 channels =6 windows, 每窗口2880点
- 输出摘要: 预测 96 步, 数值范围 -0.23~0.21, 均值 0.042
- 已验证多 horizon: 24 (0.0757s), 96 (0.0745s), 336 (0.0847s)
一致性
- 比较方法: 同一权重、同一输入、同一预处理、相同dtype(float32)、相同seed、eval模式, 分别保存 CPU 与 NPU 输出为 .npy, 运行 compare_outputs.py
- 命令:
python scripts/compare_outputs.py --cpu cpu_pred.npy --npu npu_pred.npy --task regression --atol 1e-4 --rtol 1e-3
task regression cpu_shape [2,96,3] npu_shape [2,96,3] finite true
max_abs_error 1.027e-05 mean_abs_error 2.12e-06 passed true (atol 1e-4 rtol 1e-3)
- 阈值说明: FP32 默认 atol=1e-4 rtol=1e-3, 实测误差 1e-05 量级, 远小于阈值, 单样本 smoke consistency 通过
- 是否替换模型: 否; 是否发生 CPU fallback: 否 (已验证首参数 device=npu:0, 输入输出 device=npu:0)
性能
- 计时方式: 每轮前后 torch.npu.synchronize(), 首轮含编译, 稳定段预热3次、测试10次
- 环境: batch=2, channels=3, L=2880, float32, npu:0, Ascend910
- 结果:
| horizon | compile/first-run | avg | min | max | p50 | p90 | throughput series/s | windows/s |
|---|
| 24 | 0.49s | 0.0757s | 0.0745s | 0.0770s | 0.0758s | 0.0770s | 26.42 | 79.26 |
| 96 | 0.49s | 0.0745s | 0.0740s | 0.0753s | 0.0745s | 0.0753s | 26.84 | 80.53 |
| 336 | ~0.50s | 0.0847s | 0.0832s | 0.0853s | 0.0849s | 0.0853s | 23.63 | 70.88 |
- CPU基线同输入: ~8.5s/窗口, NPU稳定推理 ~17x (首轮) / ~118x (稳定) 加速
- 峰值显存: HBM 5614MB/65536MB (npu:0 推理期间)
- 预处理计时: 包含 RevIN 与 patch embedding, 不含 CSV I/O
证据图
三张 PNG 由 xterm.js 根据本次真实日志生成 (render_xterm_evidence.mjs --style raw), 深灰背景、等宽字体、提示符 atomgit@pod-a94f8701860b161b00e290de466:~$:
图片说明: workflow为侦察/下载/加载/NPU推理/一致性/性能全流程; device_call为 npu-smi 与 NPU可用性及参数device校验; model_result为 python inference.py 默认完整输出。图片仅为日志可视化, 不是桌面截图。
限制
- 仅验证 float32, 未测试 FP16/BF16
- 仅验证 L=2880, H=96/24/336, batch≤2, 超长/超大batch需评估显存
- 模型含 MoE 路由 (scatter), NPU 上有 internal format warning 但不影响正确性
- 未在多卡/分布式下测试, 仅单卡 npu:0
- 数据为合成, 真实业务数据需自行验证分布与RevIN效果
- 首轮编译约 0.49s, 批量推理建议复用模型实例