本项目将 thuml/timer-base-84m(Timer 2.0,Llama-like 生成式时间序列基础模型,~84M 参数)
适配到 昇腾 Ascend NPU 上运行。代码不含任何 CUDA 调用,模型与输入 Tensor 全部运行在 npu:0。
thuml/timer-base-84m(HF,Timer 2.0,零样本点预测)torch_npu),固定使用 npu:0[1.0, 2.1, 3.2, 4.0]horizon 步的预测序列(Timer 2.0 为确定性点预测,单样本输出)torch 2.9.0+cpu + torch_npu 2.9.0、transformers 4.57.6requirements.txt:pip install -r requirements.txt启动时优先加载本地缓存 ./model_weights/thuml__timer-base-84m/;
若未命中(首次部署),自动通过 HF 镜像 https://hf-mirror.com 下载到该目录,并同样应用兼容补丁。
模型文件含自定义代码 modeling_timer.py / configuration_timer.py / ts_generation_mixin.py,
通过 trust_remote_code=True + local_files_only=True 加载。权重目录已加入 .gitignore,不会提交。
python3 inference.py --text "[1.0, 2.1, 3.2, 4.0]"
# 可选参数:
python3 inference.py --text "[...]" --horizon 96 --samples 1输出为 JSON:model / device / lookback / horizon / samples / shape / forecast。
注意:Timer 2.0 的 patch embedding 要求输入长度至少为 96 且是 96 的整数倍; 短输入会按周期重复确定性填充到 96,长输入截取末尾整数倍窗口。
uvicorn inference:app --host 0.0.0.0 --port 8001POST /predict:
curl -X POST http://127.0.0.1:8001/predict \
-H "Content-Type: application/json" \
-d '{"text": [20.0, 20.8295, 21.636, 22.3971, 23.0918, 23.7009, 20.3, 21.5, 22.1, 23.8, 24.2, 25.1], "horizon": 96, "samples": 1}'返回预测 JSON(forecast 为未来 96 步预测序列)。
.cuda() / cuda:;模型用 model.to("npu:0"),输入 Tensor 用 device="npu:0"。init_npu() 以 try-except 包裹 torch_npu 导入与 npu:0 可用性检查,
失败时打印清晰错误并优雅退出/返回 500。inference.py 内 _apply_file_patches()):
DynamicCache.get_usable_length、
seen_tokens、get_max_length → 统一替换为 get_seq_length()。GREEDY_SEARCH 解码并入 _sample,导致模型自带的按 patch 粒度解码
_greedy_search(含 attention_mask 分 token 截断)不再被调用 →
_force_greedy_dispatch() 将 GENERATION_MODES_MAPPING[GREEDY_SEARCH] 强制映射回
_greedy_search,保证预测语义正确。_extract_past_from_model_output → ts_generation_mixin.py 中改用
ALL_CACHE_NAMES 扫描提取 KV Cache。./model_weights/thuml__timer-base-84m/,未命中才下载。python3 inference.py --text "[1.0, 2.1, 3.2, 4.0]" --horizon 96 --samples 1 在 npu:0 成功推理,输出 shape (1, 1, 96)。POST /predict 返回预测 JSON。assets/。