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

ChatTime-1-7B-Chat 昇腾NPU部署文档

1. 模型简介

模型名称: ChatTime-1-7B-Chat(ChengsenWang/ChatTime-1-7B-Chat) 模型链接: https://ai.gitcode.com/hf_mirrors/ChengsenWang/ChatTime-1-7B-Chat 模型描述: ChatTime 将时间序列建模为一门"外语",把序列数值量化为词表中的 ###value### 时间序列 token,与自然语言统一在同一框架内处理。本模型为 Chat 版本,支持零样本时间序列预测、上下文引导预测与时间序列问答(bimodal 输入/输出)。 模型架构: LlamaForCausalLM(Llama-2 底座微调),32 层、hidden_size=4096;词表在原 Llama 词表基础上扩展 10001 个时间序列 token(###-10~10###,步长 0.0002),vocab_size=42001。 参数规模: 约 6.9B(model.safetensors 约 13GB)

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch / torch_npu2.1.0+NPU 上需安装 torch_npu
transformers>= 4.41LlamaForCausalLM 加载(随 libs 目录分发)
numpy最新版序列离散化/反离散化
sentencepiece最新版Llama tokenizer 分词器
safetensors / huggingface_hub最新版权重加载
昇腾驱动CANN 8.0.RC2+推荐最新版

安装命令:

pip install --target /mnt/workspace/ChatTime-1-7B-Chat/libs -i https://pypi.tuna.tsinghua.edu.cn/simple "transformers>=4.41,<5" numpy sentencepiece

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info
# 权重已就位
ls /home/developer/models/ChatTime-1-7B-Chat/   # config.json model-*.safetensors tokenizer.model 等
# 专属依赖已就位
ls /mnt/workspace/ChatTime-1-7B-Chat/libs       # transformers 等依赖包

3.2 运行推理

PYTHONPATH=/mnt/workspace/ChatTime-1-7B-Chat/libs python3 inference.py --model_path /home/developer/models/ChatTime-1-7B-Chat

3.3 推理参数说明

参数类型默认值说明
--model_pathstr/home/developer/models/ChatTime-1-7B-Chat本地权重目录
--devicestr自动(npu > cuda > cpu)推理设备
--hist_lenint120输入历史序列长度
--pred_lenint16预测长度(<=16 时单轮生成完成)
--num_samplesint4采样条数,多条取中位数
--outputstrassets/prediction.npy预测结果保存路径

4. 测试样例及输出结果

样例 1:内置正弦+趋势合成序列零样本预测(默认参数直接运行)

输入:

PYTHONPATH=/mnt/workspace/ChatTime-1-7B-Chat/libs python3 inference.py --model_path /home/developer/models/ChatTime-1-7B-Chat

输出:

[INFO] device = npu
[INFO] 模型加载完成 load_time=xx.xxs (params=69xxM)
[INFO] 推理完成 耗时/elapsed: x.xxx s (pred_shape=(16,), num_samples=4, device=npu)
[INFO] 预测统计 mean=x.xxxx std=x.xxxx min=x.xxxx max=x.xxxx
[RESULT] status=ok device=npu 耗时/elapsed: x.xxx s

样例 2:自定义预测长度与采样数

输入:

python3 inference.py --model_path /home/developer/models/ChatTime-1-7B-Chat --pred_len 8 --num_samples 8

输出:

[INFO] 预测值前5步: [...],预测结果保存至 assets/prediction.npy

5. Agent适配截图

5.1 Agent适配全过程截图

Agent 适配流程

5.2 NPU设备调用截图

NPU 设备调用

5.3 模型适配结果截图

模型适配结果

6. 注意事项

  • 推理脚本通过 torch_npu 调用 NPU,设备自动选择 npu > cuda > cpu;
  • 全程离线推理:权重仅从本地 --model_path 加载(local_files_only=True),不访问网络;
  • 时间序列处理逻辑(Discretizer 归一化离散化 + Serializer ###value### 序列化 + Alpaca 指令模板)与官方 ForestsKing/ChatTime 仓库实现一致,已内置到 inference.py,无需额外下载官方代码;
  • 注意力使用 eager 实现(NPU 上不使用 flash_attention_2),权重以 float16 加载;数值后处理(MinMax 归一化等)在 CPU 侧用 numpy 完成,不涉及 float64 NPU 算子;
  • 官方默认 num_samples=8,脚本默认降为 4 以缩短推理耗时,可通过 --num_samples 调回;
  • pred_len 大于 16 时需多轮滚动生成(官方逻辑),当前脚本针对单轮生成做了简化,建议 pred_len <= 16;
  • 如有精度差异请与官方 ChatTime pipeline 输出对比确认。