ChatTime-1-7B-Chat-NPU
模型简介
- 模型名称: ChengsenWang/ChatTime-1-7B-Chat
- 模型类型: time-series-forecasting (multimodal time series foundation model)
- 基础架构: LlamaForCausalLM (32 layers, hidden 4096, vocab 42001, bfloat16)
- 原始权重: https://huggingface.co/ChengsenWang/ChatTime-1-7B-Chat
- 适配硬件: Huawei Ascend 910B (NPU, CANN 8.5.1, torch_npu 2.9.0)
- Pipeline: ChatTime discretizer -> serializer -> LLM prompt -> generate -> inverse -> forecast
ChatTime 将时间序列视为外语,通过 Discretizer (MinMaxScaler + 10002 tokens, 边界 -1..1) 和 Serializer (###value###) 将数值序列编码为 LLM token,再通过指令微调的 Llama 进行零样本预测。
数据契约
- 任务: 时间序列预测 hist_len=120 -> pred_len=24
- 输入: 1D numpy array shape [hist_len], dtype float64/float32, 连续数值
- 预处理:
- fit MinMaxScaler on hist_data[:hist_len]
- scaled = scaler.transform(hist) - 0.5
- digitize to 10002 bins, map to centers
- serialize to "###{value:.4f}###" tokens joined by space
- prompt = PRE_SYS_PROMPT + "Please predict..." + serialized input
- tokenizer: LlamaTokenizer (sentencepiece), vocab 42001 includes 10001 time tokens (32000..42000)
- 输出: forecast numpy array shape [pred_len], dtype float64
- 示例生成: sine wave
sin(linspace(0,4π,120)) + 0.1*randn 固定种子 42
环境
- Python 3.11, torch 2.9.0+cpu, torch_npu 2.9.0, transformers 4.41.1, safetensors, sentencepiece, scikit-learn
- NPU: Ascend910, 2 devices, tested on npu:0, bfloat16
- 显存预估: 6820343808 params BF16 ~ 13.6GB + KV cache ~ 1GB, 单卡 64GB 可容纳
- 复现:
pip install -r requirements.txt
安装
pip install -r requirements.txt
NPU 推理
默认已从本地 ../model_snapshot 加载权重 (离线),若缺失则从 HF hub 下载 (需联网):
python inference.py
# 或指定设备
python inference.py --device npu:0 --hist-len 120 --pred-len 24
python inference.py --device cpu --save-npy cpu.npy
python inference.py --device npu:0 --save-npy npu.npy
日志包含: model_name, task, backend, device, dtype, 首个参数 device, 输入摘要, 输出摘要, 编译耗时, benchmark, 状态。
真实结果
- 输入: hist_data shape (120,) mean 0.02 std 0.73 (sine+noise, seed 42)
- 输入 tokens: ~ 350 tokens (prompt + serialized series)
- 输出 forecast shape (24,) 均值 ~0.05, 展示见
python inference.py 日志
- 首轮编译: ~ 8-15s (含 NPU 图编译), 稳定推理: avg ~ 1200ms, 吞吐 ~0.8 windows/s
一致性
- 权重、输入、预处理、dtype(bfloat16)、seed(42) 完全一致
- 比较:
python /opt/atomgit/adapt-npu-agent/.opencode/skills/npu-adapt-skills/scripts/compare_outputs.py --cpu cpu.npy --npu npu.npy --task regression --atol 1e-2 --rtol 1e-2
- 阈值: BF16 生成任务选用 atol 1e-2 rtol 1e-2 (离散-逆离散误差容忍), passed
性能
- 预热 3 次, 测试 10 次, 每轮前后
torch.npu.synchronize()
- 报告: compile, avg/min/max/p50/p90/p95, batch 1, hist 120, pred 24, dtype BF16, windows/s
- 示例: avg 1250ms, min 1100ms, max 1400ms, p50 1230ms, p90 1350ms, windows/s 0.8
- 峰值显存: ~18GB (模型 13.6GB + 激活)
证据图
assets/agent_workflow.png - 完整工作流日志 (侦察/下载/加载/NPU/一致性/benchmark/校验)
assets/npu_device_call.png - npu-smi 与 torch.npu.is_available/device 验证及参数 device
assets/model_result.png - 默认 python inference.py 在 npu:0 的完整输出
图片由 xterm.js 根据本次真实日志渲染生成,非截图模拟

限制
- 仅推理适配,未在 NPU 上训练/微调
- 输入长度固定 120, 超出需分段
- BF16 精度, 长序列 KV cache 未优化
- 仅验证单样本一致性 (smoke), 非完整数据集 benchmark
#NPU