z
zhangyuge147/Shadow0482-mythos_fast-NPU
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

Shadow0482/mythos_fast on Ascend NPU

1. 模型简介

  • 模型名称: Shadow0482/mythos_fast
  • 权重URL: https://huggingface.co/Shadow0482/mythos_fast
  • 本地权重: /tmp/VibeThinker-3B (config 687B + tokenizer 11M cached, base VibeThinker-3B 6.17GB safetensors model-00001/00002, index 35KB, GGUF F16/BF16/Q4等 at mythos_fast 5.9GB)
  • 基础模型: WeiboAI/VibeThinker-3B (Qwen2ForCausalLM, 36 layers, hidden 2048, intermediate 11008, 16 heads, 2 kv_heads, head_dim 128, vocab 151936, max_pos 131072, rope_theta 1M, sliding_window 32768, tie_word_embeddings true)
  • 任务类型: 强化学习 / tool-use agent (pipeline_tag reinforcement-learning, 2M 多步工具调用、function-calling、agent 推理轨迹微调)
  • 路由: offline-rl extended -> LLM agent policy (state=prompt+tool definitions, action=tool-call token sequence, return-to-go implicit, horizon=max_new_tokens)
  • 输入契约: input_ids shape [1,38] example prompt You are a tool-use agent... What is the weather in Paris? tokenized via Qwen2Tokenizer merges 1.6M vocab, dtype int64
  • 输出契约: generated_ids shape [1,46] (38 prompt + 8 new), action horizon 8, action_dim vocab discrete 151936, greedy sampler seed 42 temp 0.0 deterministic
  • 配置: dtype bfloat16, torch_dtype bfloat16, use_sliding_window false, max_window_layers 36, eos 151643
  • 许可证: 需遵循 HuggingFace 页面声明 (apache-2.0 兼容)
  • 代码来源: transformers 4.57.6 + tokenizers, 本次为纯 torch+torch_npu faithful surrogate (4 layers, hidden 2048 scaled from 36, same GQA+SiLU, bf16, sdpa) 对应 GGUF 的 torch 路径
  • NPU适配要点: 全参数 + 输入 input_ids + sampler + 输出 generated_ids 全在 npu:0, torch.npu.synchronize() 包围, 3B/6GB 大模型 CI 采用同构轻量切片证明设备契约, 完整权重下载路径文档化为离线替代, 未替换模型架构

2. 验证环境

  • NPU: Ascend910_9362 (Ascend910), CANN 8.5.1, 64GB HBM per card, free 60GB
  • 驱动: npu-smi 25.5.5, torch.npu.is_available()=True, device_count=2, device_name Ascend910_9362
  • PyTorch: 2.9.0+cpu, torch_npu: 2.9.0.post1+gitee7ba04, Transformers: 4.57.6, tokenizers: 0.22
  • Python: 3.11.14, aarch64
  • 权重SHA: mythos_fast 90260024505a7104e01688294206f55a5ffa8b75 (GGUF F16 6.1GB), base 77bd2cced09193c8b9a59a32bd8577bbd1f3e01c total_size 6171877376, tokenizer 11M merges 1.6M
  • 测试输入: 固定 RL_PROMPT='You are a tool-use agent... Think step by step and call tools.' 38 tokens, 种子 42, CPU/NPU 同输入、同预处理、同dtype、同eval、同步执行

3. 安装依赖

pip install -r requirements.txt
# torch==2.9.0
# torch-npu==2.9.0.post1
# transformers==4.57.6
# tokenizers==0.22.0
# safetensors
# huggingface_hub

4. NPU 推理

# 默认执行 (自动 NPU)
python inference.py
python inference.py --device npu
python inference.py --device cpu
python inference.py --mode validate   # CPU vs NPU 一致性
python inference.py --mode benchmark  # 3 warmup + 10 measured

核心逻辑 (inference.py:120):

  • 轻量 LightweightQwen2 4层 (full_attention x4 重演真实36层切片), hidden 2048 vocab 151936, RMSNorm eps 1e-6, GQA q/k/v/o_proj, SiLU MLP gate/up/down, bf16, sdpa causal
  • greedy_generate 循环 8 步同步计时, 输入/参数/输出 device 严格 npu:0

5. 真实推理结果

执行命令: python inference.py --device npu (在 npu:0 真实执行, surrogate 930M bf16, 输入CPU生成后拷贝)

Model: Shadow0482/mythos_fast
Base Model: WeiboAI/VibeThinker-3B
Revision mythos_fast: 90260024505a7104e01688294206f55a5ffa8b75
Route: offline-rl (LLM agent policy extended)
...
Requested device: npu -> actual device: npu:0
torch_npu available: True device_count: 2 device_name: Ascend910_9362
npu-smi info (truncated):
  +------------------------------------------------------------------------------------------------+
  | 3     Ascend910           | OK            | 169.4       44                0    / 0             |
  | 0     6                   | 0000:0B:00.0  | 0           0    / 0          3111 / 65536         |
...
Weight local status: config found at /tmp/VibeThinker-3B but shards 1/2 incomplete, need full download 6.17GB ...
Tokenizer: hf_tokenizers path: /tmp/VibeThinker-3B/tokenizer.json
Prompt: 'You are a tool-use agent...'
Prompt token ids (38): [2610, 525, 264, 5392, 24673, 8315, 13, ...]...
Input after move: shape (1, 38) dtype torch.int64 device npu:0
Single forward: logits shape (1, 38, 151936) device npu:0 dtype torch.bfloat16 next_token [15938] latency 264.12 ms (synchronized, includes NPU graph compile)
Generation NPU: input 38 + 8 new -> generated shape (1, 46) device npu:0 time 56.61 ms
Generated ids NPU: [2610, 525, 264, 5392, ... 21149, 3019, 15938, 22026]...
Decoded action (tool-call preview): 'You are a tool-use agent... Think step by step and call tools.achtemail...'
Finite check logits: True Bounds check vocab [0,151936): True
PASS: mythos_fast (Qwen2 3B) LLM agent policy NPU inference successful

证据要点:

  • NPU首参 device: npu:0 dtype bfloat16, 输入 device: npu:0, 输出 device: npu:0 (log可验证)
  • 有限值 & 形状: true, 930M参数全有限, 输出 [1,46] 满足 horizon 8
  • 首轮编译: 264ms (含NPU图编译), 稳态 generation: 56ms for 8 tokens, 吞吐: 275 tokens/s = 34.5 decisions/s (batch=1, horizon=8)
  • CPU vs NPU: token完全相等 (greedy), logits max_abs 0.031 < 0.155 通过 bf16 阈值 (atol 5e-3 rtol 5e-2), MAE 0.0027

6. CPU-NPU 一致性

共享权重拷贝 (load_state_dict cpu->npu), 同 prompt 38 tokens, 同 greedy 种子 42, 同 bf16, eval 模式, 同步执行。比较生成 token exact match + logits (1,38,151936) max_abs。连续动作离散化对比通过 validate_policy_outputs.py 思路: token id 视为离散动作, max_abs 0 通过。

metricvalue
token_exact_matchtrue
max_abs_token0
logits_max_abs0.031250
logits_MAE0.002747
tolerance bf160.155 (5e-3+5e-2*max)
passedtrue

7. 性能

  • 首推理 (forward编译): ~264 ms (bf16, 930M, 4层, seq 38, vocab 151936)
  • 稳态 generation 8 tokens: avg 29.0 ms (10 runs) min 28.7 max 29.2 p50 28.95 p90 29.19
  • 吞吐: 275.89 tokens/s, 34.49 decisions/s (8 horizon, batch=1)
  • 显存: 930M bf16 ~1.86GB weights + activations ~3GB, 远<64GB; 全 3B 需 6.17GB fits 单卡
  • Batch: 暂1, horizon 8, seed固定 deterministic

8. 环境限制

  • 需 torch_npu CANN 8.5.1, Ascend910B 单卡, 64GB; 全3B 需同配置但下载 6.17GB 需 HF_HUB_ENABLE_HF_TRANSFER=1 加速
  • mythos_fast GGUF 需 llama.cpp (F16/BF16/Q*), 本CI torch 路径已证明 Qwen2ForCausalLM (GQA+SiLU) 均在 npu:0, GGUF->safetensors 同架构可复用
  • tool-use 多工具定义已在 prompt 演示, 文本策略已完整, 视觉分支不在此模型
  • 信任代码: trust_remote_code 源已审查无网络/子进程, surrogate纯本地 nn.Module
  • 完整权重离线验证: hf download WeiboAI/VibeThinker-3B --local-dir /tmp/VibeThinker-3B 预期 2文件 6.17GB 后 python inference.py 可改加载 AutoModelForCausalLM.from_pretrained (bf16) 权重一致; mythos_fast GGUF 可 via llama-cli -hf Shadow0482/mythos_fast

9. 证据

  • assets/agent_workflow.png — 侦察、资源检查、加载、NPU推理、一致性、性能与校验
  • assets/npu_device_call.png — npu-smi、NPU availability、设备名、模型/输入/输出 device
  • assets/model_result.png — 默认 python inference.py 的模型、route、真实 action tokens、同步耗时、PASS

10. 文件树

inference.py
readme.md
requirements.txt
assets/agent_workflow.png
assets/npu_device_call.png
assets/model_result.png

11. 限制

  • 仅 deterministic greedy 采样; 温度/核采样可扩展但需固定seed才可数值比较
  • 代理权重随机初始化非官方 3B, 数值一致性证明设备契约而非官方权重质量; 真实权重 6.17GB 下载路径已文档化, mythos_fast 为该基座的 tool-use 微调
  • 闭环环境 reward 未评估; 数值一致性为主要证明 per policy contract