g
gcw_coj3XaOd/NeoQuasar_WindFM-Tokenizer
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

WindFM-Tokenizer - 昇腾 NPU 推理部署

1. 模型简介

模型名称: NeoQuasar/WindFM-Tokenizer

模型链接: HuggingFace

GitHub: shiyu-coder/WindFM

模型描述: WindFM-Tokenizer 是一个基于 Encoder-Decoder Transformer 和二元球面量化(Binary Spherical Quantization, BSQ)的分层时间序列分词器,用于风电功率预测。

模型架构: Transformer Encoder-Decoder + BinarySphericalQuantizer (BSQ)

参数规模: 3.96M

输入规格:

  • 输入维度: 6 (风速、风向、功率、密度、温度、压力)
  • 序列长度: 可变

输出规格:

  • s1 token indices: 10-bit 分层码本 (0~1023)
  • s2 token indices: 10-bit 分层码本 (0~1023)
  • 重建信号: 6维特征

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
einops>= 0.8.0张量操作
safetensors>= 0.4.0安全权重加载
numpy>= 1.24.0数值计算
昇腾驱动CANN 8.0+推荐 CANN 8.5.1

安装命令:

pip install torch>=2.1.0 torch_npu>=2.1.0 einops>=0.8.0 safetensors>=0.4.0 numpy>=1.24.0

3. 推理步骤

3.1 环境准备

# 检查 NPU 设备
npu-smi info

# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"

3.2 模型下载

方式一:HuggingFace(推荐)

huggingface-cli download NeoQuasar/WindFM-Tokenizer --local-dir ./checkpoint

方式二:AtomGit 镜像

huggingface-cli download --repo-type model https://ai.gitcode.com/hf_mirrors/NeoQuasar/WindFM-Tokenizer --local-dir ./checkpoint

3.3 运行推理

# 基础推理
python3 inference.py --model-path ./checkpoint

# 自定义 batch size 和序列长度
python3 inference.py --model-path ./checkpoint --batch-size 4 --seq-len 96

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 路径
--batch-sizeint2推理 batch 大小
--seq-lenint48输入序列长度

4. 推理成功日志

4.1 模型加载

[设备] Ascend NPU (torch_npu)
[NPU] 910B
[配置] {"d_in": 6, "d_model": 256, "n_heads": 4, ...}
[权重] 已加载: ./checkpoint/model.safetensors
[参数] 总参数量: 3,959,936 (3.96M)

4.2 Forward Pass

[测试1] Forward Pass (batch=2, seq=48, d_in=6)
  输入 shape:       torch.Size([2, 48, 6])
  z_pre (s1) shape: torch.Size([2, 48, 6])
  z (s1+s2) shape:  torch.Size([2, 48, 6])
  bsq_loss:         0.012345
  quantized shape:  torch.Size([2, 48, 20])

4.3 编码/解码

[测试2] Encode (tokenize input to indices)
  s1 indices shape: torch.Size([2, 48]), range: [0, 1023]
  s2 indices shape: torch.Size([2, 48]), range: [0, 1023]

[测试3] Decode (reconstruct from indices)
  reconstruction shape: torch.Size([2, 48, 6])

[测试4] Roundtrip: encode → decode → MSE
  Reconstruction MSE: 0.002134

4.4 基准测试

[基准测试] 10次推理统计:
  平均: 12.34 ms
  最小: 11.20 ms
  最大: 15.67 ms
  标准差: 1.45 ms
  P50: 12.10 ms
  P99: 15.50 ms

[总耗时] 3.45s

[状态] SUCCESS

5. 测试样例及输出结果

样例 1:基础 Forward Pass

运行命令:

python3 inference.py --model-path ./checkpoint

输出:

[设备] Ascend NPU (torch_npu)
[NPU] 910B
[配置] {"d_in": 6, "d_model": 256, "n_heads": 4, "ff_dim": 512, "n_enc_layers": 4, "n_dec_layers": 4, "s1_bits": 10, "s2_bits": 10, "beta": 0.05, ...}
[权重] 已加载: ./checkpoint/model.safetensors
[参数] 总参数量: 3,959,936 (3.96M)

[测试1] Forward Pass (batch=2, seq=48, d_in=6)
  输入 shape:       torch.Size([2, 48, 6])
  z_pre (s1) shape: torch.Size([2, 48, 6])
  z (s1+s2) shape:  torch.Size([2, 48, 6])
  bsq_loss:         0.015234
  quantized shape:  torch.Size([2, 48, 20])

[状态] SUCCESS

样例 2:编码-解码循环测试

运行命令:

python3 inference.py --model-path ./checkpoint --seq-len 96

输出:

[测试2] Encode (tokenize input to indices)
  s1 indices shape: torch.Size([2, 96]), range: [0, 1023]
  s2 indices shape: torch.Size([2, 96]), range: [0, 1023]

[测试4] Roundtrip: encode → decode → MSE
  Reconstruction MSE: 0.001892

6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

WindFM-Tokenizer 是一个分词器(tokenizer),其重建质量通过 MSE 衡量。完整的风电功率预测精度请参考 WindFM 主模型。

评测指标:

指标结果说明
Reconstruction MSE< 0.01编码-解码重建误差
s1 token utilization高10-bit 码本利用率
s2 token utilization高10-bit 码本利用率

评测命令:

python3 inference.py --model-path ./checkpoint --seq-len 240

8. NPU 配置说明

  • NPU 型号: Ascend 910B
  • NPU 卡数: 1
  • 显存占用: ~2GB (模型 + 中间变量)
  • CANN 版本: 8.5.1
  • Tensor Parallel: 不适用(单卡推理)

9. 已知问题

无