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:0npu:0Ascend910_9362,单卡 64GB,双卡可用)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())"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,下载耗时较长请耐心等待。)
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_len | 24 | 预测长度 |
--max_pred_len | 16 | 单次生成最大步数(官方默认) |
--num_samples | 8 | 采样数,取中位数作为点预测 |
--seed | 无 | 随机种子 |
输出为 JSON,含 forecast 预测数组、device、hist_len、pred_len 等字段。
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。
<system> ### Instruction: ... ### Input: ... ### Response:,忠实复刻官方 utils/prompt.py。Discretizer(MinMax 缩放 + 10002 个 bin 中心)+ Serializer(每个值格式化为 ###<4 位小数>### 的 token,官方 utils/tools.py),生成结果再反离散化回数值。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 步),最终取各采样中位数作为点预测。ChatTime-1-7B-Chat-npu/
├── inference.py # CLI + FastAPI 双模式推理
├── requirements.txt
├── README.md
├── assets/ # 验证截图
└── model_weights/ # 权重缓存(.gitignore 忽略,不上传)@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},
}