#NPU
ChengsenWang/ChatTime-1-7B-Chat(revision 20baa88d6ab838b34abbd1d2b1f4cea57c8ff870)LlamaForCausalLM(7B,vocab_size=42001,torch_dtype=bfloat16)ChatTime 把时间序列编码为“数值语言”文本,通过指令微调后的 Llama 模型对序列进行文本式生成,再解码回连续数值,实现零样本预测。本仓将官方 model/model.py 中基于 pipeline(...) + device_map="auto" 的推理路径改写为基于 LlamaForCausalLM.generate() 的显式 npu:0 推理路径(模型权重与 Discretizer/Serializer 逻辑完全一致),保证模型核心 forward 与全部 Tensor 真实运行在昇腾 NPU 上。
dataset/Traffic.csv(真实可追溯流量序列,120 点历史 → 24 点预测)。Hist(历史序列)与 Pred(参考真实未来),均为以 eval 可解析的列表字符串。Discretizer:对当前历史窗口 MinMaxScaler 拟合,映射到 [-0.5,0.5],再离散化为 10002 个 token 中心值。Serializer:每个值序列化为 ###{value:.4f}###,NaN 序列化为 ###Nan###。pred_len 按 max_pred_len=16 分块,逐块生成、解码、反离散化并拼接。Ascend910_9362),单卡 npu:0,HBM 64GBnpu-smi 25.5.5pip install -r requirements.txt默认命令(模型权重需已下载到 models/ChengsenWang-ChatTime-1-7B-Chat/,或在 --model-path 指定本地权重目录):
python inference.py --model-path /path/to/ChatTime-1-7B-Chat --device npu:0 --hist-len 120 --pred-len 24默认使用官方一致的多项采样(do_sample=True、num_samples=8、seed=42),可复现;--no-do-sample 可切换贪心解码。输出包含模型名、任务、backend、device、dtype、首参 device、输入摘要、24 点预测值、同步耗时与最终状态。模型与全部参与计算的 Tensor 均显式位于 npu:0。
在 Traffic 数据集上的 24 点零样本预测(采样,num_samples=8,seed=42):
Forecast[24]: [0.4682, 0.0441, -0.5492, -0.8322, -1.0812, -1.2764, -1.3362, -1.3029,
-1.1201, -0.6676, -0.0766, 0.3649, 0.9131, 0.9029, 0.7958, 0.9983,
1.3051, 1.5526, 1.6793, 1.8488, 1.6869, 1.5635, 1.3422, 1.2166]Pred)末端呈先降后升、末尾约 1.1–2.1 的趋势,预测趋势一致。sync_time(含 2 次 chunk 生成,不含权重加载与编译):2.715 s由于 7B 模型在 CPU 上 fp16 自回归生成极慢(>15s/token),采用两级一致性验证:
logits 级一致性(同一 73-token 提示词、同一权重、fp16、seed=0,单次 forward):
max_abs_error = 0.0703(logits 尺度 ±30,相对误差 ~0.2%)argmax_agreement = 1.0(CPU 与 NPU 预测 token 完全一致)compare_outputs.py --task regression --atol 0.1 --rtol 0.1 → passed预测级一致性(smoke,hist_len=10、pred_len=2、greedy、num_samples=1):
max_abs_error = 0.0 → passed两者均使用完全相同的权重、输入、预处理、dtype(float16)与随机种子。
NPU npu:0,hist_len=120、pred_len=24、num_samples=1、greedy,预热 3 次、正式测试 10 次:
| 指标 | 值 |
|---|---|
| 平均耗时 | 1.981 s |
| min / max | 1.948 / 2.028 s |
| p50 / p90 / p95 | 1.980 / 2.016 / 2.022 s |
| 峰值显存 | 13230 MB |
| 吞吐 | 0.505 series/s |
计时前后均调用 torch.npu.synchronize();编译/首轮耗时 2.62 s 单独报告,不含在上述统计。
三张图由 xterm.js 根据本次真实运行日志生成(非原始桌面截图):
assets/agent_workflow.png:侦察、数据检查、NPU 环境、logits 一致性、预测一致性、benchmark 与提交校验工作流。assets/npu_device_call.png:npu-smi、NPU 可用性、设备名、模型参数 device/dtype。assets/model_result.png:默认 python inference.py 的真实完整输出。--model-path)。max_position_embeddings=4096 限制。