2
gcw_BOxvkDDf/ug-cppo-finai-2025
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

UG-CPPO-FINAI-2025 · 昇腾 NPU 适配

HuggingFace 模型 graceesthi/ug-cppo-finai-2025 (Stable-Baselines3 金融交易 RL 策略,非 LLM)在华为昇腾 NPU(torch_npu)上的推理适配。

  • 模型类型:SB3 PPO / CPPO / UG-CPPO 策略(30 个 {mode}_seed{seed}.zip)
  • 任务:10 只美股(AAPL/MSFT/AMZN/NVDA/META/GOOGL/TSLA/NFLX/AMD/COST)投资组合权重
  • 观察:81 维市场状态向量(Box(-inf, inf))
  • 动作:10 维连续权重(Box(-1, 1))
  • 默认权重:ug_cppo_seed42.zip(旗舰模型),可用 --model <zip> 切换

环境

  • Ascend910,ASCEND_RT_VISIBLE_DEVICES=0
  • torch 2.9.0 + torch_npu 2.9.0.post1,python 3.11
  • stable-baselines3 2.9.0 + gymnasium 1.3.0

用法

# 权重缓存:model_weights/graceesthi/ug-cppo-finai-2025/(ug_cppo_seed42.zip 存在即跳过下载)

# CLI 推理(缺省合成市场观察,或传 81 维 obs JSON)
python inference.py --input default
python inference.py --input "[0.1, -0.2, ... 81 个浮点数]"
python inference.py --model cppo_seed47.zip --input default

# FastAPI 服务(端口 8016)
python inference.py --serve --port 8016
# GET /   -> 上传/输入页面
# POST /predict -> {"obs": [81 浮点]} 或空体(用缺省观察)-> JSON

实现要点

  • policy.to("npu:0").eval(),模型/输入 tensor 全部 npu:0;get_device() 带 try-except
  • 手动前向:policy.get_distribution(obs_tensor) → dist.get_actions()(随机采样)+ dist.mode()(确定性),不使用 model.predict()
  • 输出 JSON:{model, device, obs_dim, action_dim, action, deterministic_action, action_sum, action_softmax, ...}
  • numpy 2.x pickle 兼容补丁(zip 由 numpy 2.x 保存):numpy._core.* 子模块别名 + BitGenerator 反序列化
  • 工程 .py 无任何 CUDA 语句

结果

  • CLI:python inference.py --input default → exit 0,npu:0 输出合法 JSON
  • FastAPI:POST /predict → 合法 JSON(缺省/自定义 obs 均可用)