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

ChatTime-1-7B-Chat — 昇腾 Ascend NPU 适配

ChengsenWang/ChatTime-1-7B-Chat 是专用于时间序列分析的多模态 Chat LLM(7B 参数量,LlamaForCausalLM,Apache-2.0)。本项目将其适配到昇腾 Ascend 910 NPU(torch_npu),提供 CLI 与 FastAPI 两种推理方式,权重自动下载并本地缓存。

  • 模型架构:LlamaForCausalLM(标准 Llama,无自定义 modeling 文件,无需 trust_remote_code)
  • 权重精度:torch.bfloat16(7B 约 14GB,NPU 单卡 64GB 可容纳)
  • 推理设备:固定 npu:0
  • 本仓库不含任何 GPU 设备调用,模型与输入 Tensor 全部运行在 npu:0

1. NPU 环境要求

  • 昇腾 Ascend 910 设备(本机实测 Ascend910_9362,单卡 64GB,双卡可用)
  • CANN + torch_npu(本机实测 torch_npu 2.9.0.post1 + torch 2.9.0+cpu)
  • transformers(本机实测 4.57.6)
pip install -r requirements.txt

验证 NPU 可用:

python3 -c "import torch, torch_npu; print(torch.npu.device_count(), torch.npu.is_available())"

2. 权重下载与缓存

huggingface.co 直连不可达时使用 hf-mirror.com 镜像:

export HF_ENDPOINT=https://hf-mirror.com

启动推理时若本地缓存未命中,会自动执行 huggingface_hub.snapshot_download 下载到固定缓存路径:

model_weights/ChengsenWang__ChatTime-1-7B-Chat/

(模型全名中 / 换成 __。共 3 个 safetensors 分片,约 14~15GB,下载耗时较长请耐心等待。)

3. CLI 推理

python3 inference.py --text "<时间序列>"

--text 接受 JSON 数值数组字符串,或逗号分隔数值,例如:

python3 inference.py \
  --text "[0.0,0.34,0.64,0.87,0.99,0.98,0.85,0.62,0.33,0.02,-0.29,-0.57,-0.78,-0.92,-0.97,-0.92,-0.78,-0.57,-0.29,0.02,0.33,0.62,0.85,0.98]" \
  --pred_len 8

常用参数:

参数默认说明
--text必填历史时间序列(JSON 数组或逗号分隔)
--hist_len全序列仅使用输入序列最后 N 个值
--pred_len24预测长度
--max_pred_len16单次生成最大步数(官方默认)
--num_samples8采样数,取中位数作为点预测
--seed无随机种子

输出为 JSON,含 forecast 预测数组、device、hist_len、pred_len 等字段。

4. FastAPI 服务

uvicorn inference:app --host 0.0.0.0 --port 8004

调用:

curl -s -X POST http://127.0.0.1:8004/predict \
  -H "Content-Type: application/json" \
  -d '{"text": "[0.0,0.34,0.64,0.87,0.99,0.98,0.85,0.62,0.33,0.02,-0.29,-0.57,-0.78,-0.92,-0.97,-0.92,-0.78,-0.57,-0.29,0.02,0.33,0.62,0.85,0.98]", "pred_len": 8}'

返回推理 JSON(forecast 为预测数值数组)。接口文档:http://127.0.0.1:8004/docs。

5. 实现要点

  • ChatTime 提示词模板:<system> ### Instruction: ... ### Input: ... ### Response:,忠实复刻官方 utils/prompt.py。
  • 离散化 / 序列化:Discretizer(MinMax 缩放 + 10002 个 bin 中心)+ Serializer(每个值格式化为 ###<4 位小数>### 的 token,官方 utils/tools.py),生成结果再反离散化回数值。
  • NPU 适配:模型以 torch.bfloat16 加载,.to("npu:0");init_npu() 用 try-except 捕获 torch_npu 导入失败 / 设备不可用并优雅退出。
  • 生成:官方 model.generate(do_sample=True, top_k=100, top_p=1.0, temperature=1.0, num_return_sequences=num_samples),多轮迭代(每次最多 max_pred_len 步),最终取各采样中位数作为点预测。

6. 目录结构

ChatTime-1-7B-Chat-npu/
├── inference.py          # CLI + FastAPI 双模式推理
├── requirements.txt
├── README.md
├── assets/               # 验证截图
└── model_weights/        # 权重缓存(.gitignore 忽略,不上传)

7. 引用

@inproceedings{
  author    = {Chengsen Wang and Qi Qi and Jingyu Wang and Haifeng Sun and Zirui Zhuang and Jinming Wu and Lei Zhang and Jianxin Liao},
  title     = {ChatTime: A Unified Multimodal Time Series Foundation Model Bridging Numerical and Textual Data},
  booktitle = {AAAI Conference on Artificial Intelligence},
  year      = {2025},
}