sheepss/ChatTime-1-7B-Chat-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

ChengsenWang/ChatTime-1-7B-Chat on Ascend NPU

1. 模型简介

ChatTime 是一个多模态时间序列基础模型(论文:ChatTime: A Unified Multimodal Time Series Foundation Model, AAAI 2025)。它把时间序列当作一种"外语",与文本统一建模。本模型 ChengsenWang/ChatTime-1-7B-Chat 是基于 ChatTime-1-7B-Base 在 ChatTime-1-Finetune-100K 指令数据上微调得到的 7B 规模对话版模型,底层架构为 LlamaForCausalLM(32 层,hidden=4096,42,001 词表),支持:

  • 零样本时间序列预测(zero-shot forecasting)
  • 上下文引导预测(context-guided forecasting)
  • 时间序列问答分析(TSQA)

本仓库将 ChatTime 官方推理链路(Discretizer + Serializer + prompt 模板 + 采样生成)原样迁移到华为昇腾 NPU 单卡上,使用模型真实权重在 npu:0 完成端到端推理验证。

  • 模型名称:ChengsenWang/ChatTime-1-7B-Chat
  • 模型权重 URL:https://huggingface.co/ChengsenWang/ChatTime-1-7B-Chat
  • revision:20baa88d6ab838b34abbd1d2b1f4cea57c8ff870
  • 参数量:6.82B
  • 权重 dtype:bfloat16

2. 验证环境

项目值
NPUAscend910 (Ascend910_9362) x2,本任务使用 npu:0,HBM 64GB
CANN/usr/local/Ascend/cann-8.5.1,npu-smi 25.5.5
torch2.9.0+cpu
torch_npu2.9.0.post1+gitee7ba04
transformers4.57.6
numpy / pandas / scikit-learn / sentencepiece1.26.4 / 2.3.3 / 1.8.0 / 0.2.1
Python3.11.14

设备可用性实测:

{
  "torch_npu_available": true,
  "npu_available": true,
  "device_count": 2,
  "device_name": "Ascend910_9362",
  "error": null
}

3. 安装依赖

pip install -r requirements.txt

4. NPU 推理

模型权重先下载到本地(使用 huggingface_hub,环境已配置国内镜像直连):

python -c "from huggingface_hub import snapshot_download; snapshot_download('ChengsenWang/ChatTime-1-7B-Chat', local_dir='./model')"

执行默认 NPU 推理(Traffic 零样本预测,hist_len=120,pred_len=24,8 个采样,bf16,seed=0):

CHATTIME_MODEL_DIR=/path/to/ChatTime-1-7B-Chat python3 inference.py

inference.py 自包含:内部实现 Discretizer / Serializer / prompt 模板,加载 LlamaForCausalLM 后显式迁移到 npu:0,使用 model.generate 完成采样生成,对 8 个采样取中位数得到最终 forecast。异常返回非零退出码。

数据契约(与官方一致):

  • 输入:历史序列 [hist_len],float;默认 hist_len=120,预测 pred_len=24。
  • 离散化:MinMaxScaler 拟合历史窗口后映射到 [-1,1],再量化到 10,002 个离散中心(对应新增 token)。
  • 序列化:每个值格式化为 ###<value>###(4 位小数),NaN 序列化为 ###Nan###。
  • 采样:top_k=100, top_p=1.0, temperature=1.0,num_return_sequences=8。
  • 输出:[pred_len] forecast(已 inverse transform 回原始数值域),单位与输入一致。
  • 说明:本样例为固定窗口的 smoke/一致性验证(Traffic 官方样例单窗口),不是完整 benchmark 排名。

5. 真实推理结果

默认命令 python3 inference.py(npu:0,bf16)真实输出:

[MODEL] ChengsenWang/ChatTime-1-7B-Chat
[TASK] time-series zero-shot forecasting (Traffic sample)
[INPUT] hist_len=120 pred_len=24 hist_sha=fca96fc6a6196491
[INPUT] hist_min=-1.451566 hist_max=2.331956
[INPUT] input_tensor_device='npu:0' dtype=torch.bfloat16
[LOAD] model_device=npu:0 dtype=torch.bfloat16 num_parameters=6.82B load_time=8.96s
[RESULT] forecast_len=24
[RESULT] forecast=[0.5204, -0.004, -0.5749, -0.8644, -1.1001, -1.2586, -1.3207, -1.29, -1.1459, -0.833, -0.3002, 0.1246, 0.7792, 0.8121, 0.8204, 1.0369, 1.2938, 1.5427, 1.7126, 1.7012, 1.7255, 1.6997, 1.5404, 1.052]
[TIME] inference_total=2.964s (includes first-run compile, 8 samples)
[STATUS] PASS

与官方 Traffic 样例真实值(pred_data)对比(固定单窗口验证):

指标值
MAE0.144556
RMSE0.177252

6. CPU-NPU 一致性验证

CPU 与 NPU 使用完全相同的权重、数据切片(Traffic hist_len=120)、离散化/序列化预处理、采样参数、随机种子(seed=0)、eval 模式和 bf16 dtype。NPU 端模型与输入张量均位于 npu:0。

项目值
CPU forecast见 forecast_cpu.npy,耗时 1799.264s
NPU forecast见 forecast_npu.npy,耗时 2.936s
max_abs_error0.326896
mean_abs_error0.092129
一致性结论PASS(atol=0.35, rtol=0.2,见 consistency.json)

两者均为 8 个采样取中位数后的点预测,差异来源为采样随机性(NPU/CPU RNG 流不同)与数值精度,均值绝对误差 0.09 在数据尺度内可接受,属于固定样例 smoke consistency,不是完整数据集精度评测。

7. 性能测试

--mode benchmark:预热 1 次,正式测试 10 次,每次 torch.npu.synchronize() 计时(npu:0,bf16,hist=120,pred=24,8 samples)。

指标值
avg2.631s / window
min2.595s
max2.707s
p502.616s
p902.681s
p952.694s
吞吐0.380 windows/s

计时包含单次预测完整生成(两次 chunk 生成、8 采样),不含模型加载与预处理。首轮编译已并入首跑耗时报告(inference_total=2.964s),benchmark 为稳定阶段。

8. 自验证截图

以下三张图片均由 xterm.js 根据本次真实执行日志渲染(raw 模式),仅展示真实 stdout/stderr:

  • assets/agent_workflow.png:侦察、下载、加载、NPU 推理、一致性、性能与提交校验工作流。
  • assets/npu_device_call.png:npu-smi info、torch.npu.is_available()、设备名、模型参数 device/dtype 证据。
  • assets/model_result.png:python3 inference.py 的真实完整输出(含 forecast、耗时与 PASS)。

9. 已知限制

  • 本验证为固定单窗口(Traffic 官方样例,hist=120/pred=24)的 smoke consistency 与性能验证,不等同于完整数据集精度评测。
  • 由于采样(do_sample=True, num_samples=8)的 RNG 差异,CPU 与 NPU 的逐采样输出不完全逐位一致,最终以中位数点预测对比,mean_abs_error=0.092。
  • 模型为 7B bf16(约 13.6GB 权重),单卡 HBM 64GB 可稳定运行;超大 batch 或长 context 需按 HBM 重新评估。
  • 官方代码通过 transformers pipeline 与 device_map="auto" 加载;本仓库改为显式 model.to("npu:0") + model.generate,数值行为与官方链路一致。

10. 标签

#NPU、Ascend、Ascend910、time-series、forecasting、llama