atlasleong/t0-alpha-npu
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

t0-alpha Ascend NPU 推理包

对固定版本模型 theforecastingcompany/t0-alpha(revision 31fae62d01db93a48d6922e015bca56f666e84d2,官方门控权重 model.safetensors,406601492 字节,SHA256 16c030d3fd70f06dc4238e9a8356e9b5a631d07f80f1bc76ba539991aed5897f) 在 Ascend NPU(torch_npu,npu:3,Ascend910B4-1) 上的离线推理适配包。

  • 任务:time-series-forecasting(概率多步时间序列预测,分位数输出)
  • 引擎:torch_npu 2.9.0 + torch 2.9.0+cpu + CANN 8.5.1 + npu-smi 25.5.1
  • 设备:npu:3(本包不允许 CPU 静默回退;CPU 仅限文件 IO 与必需的预处理)
  • 架构包:tfc-t0==0.3.0(Apache-2.0,The Forecasting Company 官方包;wheel SHA256 0f2c8350562ef11cd8caf1587a8cfdfa6f56f4f65d09900ccc610dae389c7e06 已固定,记录于 requirements.txt / SHA256SUMS / logs/npu_result.json)
  • 模型权重:不随本包分发,保持在外部固定模型目录 /work/pipeline/models/tabular16/t0-alpha,运行时通过 --model-dir 指定
  • 可复用隔离环境:不随本包分发,位于 /work/pipeline/runtime/model-venvs/t0-alpha(继承容器匹配的 torch/torch_npu)
  • 许可:模型与代码均为 Apache-2.0(见模型卡 LICENSE / NOTICE)

注意:本交付为离线公开可用包,未向任何远端发布;不含任何凭据。


模型简介

t0-alpha 是 The Forecasting Company 发布的开放权重时间序列预测基础模型 (t0 家族的第一个公开迭代)。t0 是基于 decoder-style patch transformer 的 模型,原生支持概率多步预测(分位数输出)与多协变量条件。

架构要点(来自模型卡与 t0 源码):

  • patch size = 32;embed_dim = 512;24 层(每 3 层一组: 2 个 time-attention + 1 个 group-attention);8 头;mlp_hidden 2048; dropout 0.1;SwiGLU FFN;pre-norm RMSNorm;quantile head 用 cumsum-of-softplus 强制分位数单调;
  • 因果 time-axis self-attention + 时间感知 RoPE(TimeAwareRotaryEmbedding, xPos);variate-axis group self-attention;
  • 推理时 target/history 用因果 running statistics(Welford)归一化, 未来协变量用全局统计;scaler_use_arcsinh=True;
  • 原生分位点 0.1,0.25,0.5,0.75,0.9;推理请求的分位点(如 0.1,0.5,0.9)由插值得到;horizon ≤ 1024 一次前向解码;
  • 参数量约 102M(实测 101,641,536)。

本包不改写、不替换上游 t0 架构:直接安装官方 tfc-t0==0.3.0(Apache-2.0),逐张量严格加载官方权重,在 npu:3 上运行 原生 T0Forecaster.predict() 完整路径。

验证摘要(真实运行)

  • 权重校验:大小 406601492 字节,SHA256 匹配官方门控产物;
  • 架构包:tfc-t0==0.3.0,wheel SHA256 0f2c8350562ef11cd8caf1587a8cfdfa6f56f4f65d09900ccc610dae389c7e06;
  • 严格加载:303 个 checkpoint 张量,missing=[],unexpected=[],complete=true;
  • 设备:params/input/output 全部位于 npu:3;384 个子模块前向钩子验证, intermediate_off_device=0;
  • 输出:quantiles [4,64,3](batch, horizon, 3 分位),median [4,64], float32,位于 npu:3;
  • 检查:finite_ok=true,monotonic_quantiles=true(q0.1 ≤ q0.5 ≤ q0.9 全通过, 单调比例 1.0);
  • 统计:mean=0.00909,std=0.47250,min=-0.95622,max=0.98464;
  • 时延:前向 0.5826 s(含首次算子编译),wall 6.158 s(不含 hold);
  • 确定性:固定 seed 0 的确定性上下文,多次运行输出一致;
  • PID 关联:容器 PID 312467 ↔ host npu-smi PID 1289195(NPU 3,666 MB)。

环境依赖清单

运行环境(本包实测):

组件版本
Python3.11.14
torch2.9.0+cpu(容器自带,勿替换)
torch_npu2.9.0(容器自带,勿替换)
CANN8.5.1(/usr/local/Ascend/cann-8.5.1)
npu-smi25.5.1
硬件Ascend 910B4-1(npu:3)

非 torch 依赖(经 Aliyun pip 镜像安装,见 requirements.txt):

包版本说明
tfc-t00.3.0官方 T0 架构包(Apache-2.0),wheel SHA256 已固定
jaxtyping0.3.11tfc-t0 运行期依赖
rotary-embedding-torch0.9.1tfc-t0 运行期依赖
wadler-lindig0.1.7tfc-t0 运行期依赖
einops / huggingface-hub / numpy / safetensors系统自带继承自容器

关键约束:必须使用容器内已匹配的 torch + torch_npu 组合, 绝不通过普通 PyPI wheel 安装或替换 torch/torch_npu。本包通过 python -m venv --system-site-packages 继承系统 torch/torch_npu。


目录结构

t0-alpha-npu/
├── inference.py                 # NPU 推理入口(torch_npu,npu:3,--model-dir)
├── requirements.txt             # 非 torch 依赖(Aliyun 镜像,tfc-t0 已固定)
├── SHA256SUMS                   # 交付物逐文件 SHA256 清单
├── README.md / readme.md        # 本说明(内容完全一致)
├── scripts/
│   ├── run_inference_npu.sh     # 一键运行 + npu-smi PID 捕获
│   └── render_assets.py         # 从真实日志渲染 4 张截图
├── logs/
│   ├── npu_result.json          # 结构化结果
│   ├── npu_inference.log        # 最终真实调用的推理日志(sanitized)
│   ├── npu-smi-info.txt         # npu-smi 全量(hold 期间捕获)
│   ├── npu-smi-proc-mem-i3.txt  # npu-smi proc-mem(NPU 3)
│   ├── npu_device_call.log      # 设备调用记录
│   └── agent_workflow_runner.log# 运行器工作流日志
└── assets/
    ├── agent_workflow.png
    ├── modelagent_live_session.png
    ├── npu_device_call.png
    └── model_result.png

模型权重与可复用环境均不在本交付目录内:权重在 /work/pipeline/models/tabular16/t0-alpha,venv 在 /work/pipeline/runtime/model-venvs/t0-alpha。


逐步运行

1. 创建可复用隔离环境(继承系统 torch/torch_npu)

/usr/local/python3.11.14/bin/python3 -m venv --system-site-packages \
    /work/pipeline/runtime/model-venvs/t0-alpha

2. 安装非 torch 依赖(Aliyun 镜像)

export PIP_INDEX_URL=https://mirrors.aliyun.com/pypi/simple/
export PIP_TRUSTED_HOST=mirrors.aliyun.com
/work/pipeline/runtime/model-venvs/t0-alpha/bin/pip install -r requirements.txt

3. 验证环境(可选)

/work/pipeline/runtime/model-venvs/t0-alpha/bin/python -c \
  "import torch, torch_npu; print(torch.__version__, torch_npu.__version__, torch.npu.device_count())"

4. 语法检查

/work/pipeline/runtime/model-venvs/t0-alpha/bin/python -m py_compile inference.py

5. 一键运行(真实 NPU 推理 + npu-smi PID 捕获)

./scripts/run_inference_npu.sh --model-dir /work/pipeline/models/tabular16/t0-alpha \
    --device npu:3 --hold 20

等价于直接调用:

/work/pipeline/runtime/model-venvs/t0-alpha/bin/python inference.py \
  --model-dir /work/pipeline/models/tabular16/t0-alpha \
  --device npu:3 \
  --context-len 512 --horizon 64 --batch 4 --quantiles 0.1,0.5,0.9 --seed 0 \
  --hold 20 --monitor \
  --output-json logs/npu_result.json

脚本在前向结束后保留 NPU 上下文 --hold 秒,期间并行执行 npu-smi info 与 npu-smi info -t proc-mem -i 3,从而把真实 Python PID 关联到 npu:3。

6. 查看结果

cat logs/npu_result.json        # 结构化结果
cat logs/npu_device_call.log    # 设备调用与 PID 关联记录

完整测试与真实输出

命令(exit code = 0):

/work/pipeline/runtime/model-venvs/t0-alpha/bin/python \
  /work/pipeline/jobs/tabular16/t0-alpha/t0-alpha-npu/inference.py \
  --model-dir /work/pipeline/models/tabular16/t0-alpha \
  --device npu:3 --context-len 512 --horizon 64 --batch 4 \
  --quantiles 0.1,0.5,0.9 --seed 0 --hold 20 --monitor \
  --output-json /work/pipeline/jobs/tabular16/t0-alpha/t0-alpha-npu/logs/npu_result.json

真实输出(来自 logs/npu_result.json):

engine            : torch_npu   exit_code: 0
npu_available     : true   device: npu:3   npu_device_name: Ascend910B4-1
model_device      : npu:3   input_device: npu:3   output_device: npu:3
strict_load       : 303 keys, missing=[], unexpected=[], complete=true
param_count       : 101641536 (torch.float32)
output shape      : quantiles=[4, 64, 3]   median=[4, 64]
quantile levels   : [0.1, 0.5, 0.9]
monotonic_quantiles: true   finite: true   intermediate modules off device: 0
latency_s         : 0.582643   wall_time_s: 6.158   pid: 312467
forecast stats    : mean=0.00909  std=0.47250  min=-0.95622  max=0.98464

sample quantiles 前 8 步(row 0,[q0.1, q0.5, q0.9]):

[0.0555, 0.1145, 0.1764]
[0.0474, 0.1423, 0.2396]
[0.0014, 0.1202, 0.2394]
[-0.0088, 0.1109, 0.2365]
[0.0183, 0.1344, 0.2650]
[0.0558, 0.1811, 0.3125]
[0.1336, 0.2691, 0.3978]
[0.2250, 0.3681, 0.5069]

sample median 前 8 步(row 0):

[0.1145, 0.1423, 0.1202, 0.1109, 0.1344, 0.1811, 0.2691, 0.3681]

NPU PID 关联(npu-smi,hold 期间 NPU 3 唯一进程):

NPU ID: 3   Process id: 1289195   Process memory(MB): 666

许可与署名

  • 模型权重与模型卡:Apache-2.0(theforecastingcompany/t0-alpha, revision 31fae62d01db93a48d6922e015bca56f666e84d2)。
  • 架构包 tfc-t0==0.3.0:Apache-2.0(The Forecasting Company;含 Datadog Toto 与 Amazon Chronos-2 的 Apache-2.0 代码级署名,见包内 NOTICE)。
  • 本适配包(inference.py、requirements.txt、scripts/):Apache-2.0。
  • 测试输入为确定性合成时间序列(CC0,趋势 + 双季节性 + 固定噪声),可自由再分发。

参考

  • 模型卡:https://huggingface.co/theforecastingcompany/t0-alpha
  • 源码:https://github.com/theforecastingcompany/tfc-t0
  • PyPI:https://pypi.org/project/tfc-t0/