g
gcw_coj3XaOd/hmellor_tiny-random-LlamaForCausalLM
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

tiny-random-LlamaForCausalLM - 昇腾 NPU 推理部署

1. 模型简介

模型名称: hmellor/tiny-random-LlamaForCausalLM

模型链接: HuggingFace

模型描述: 一个超小的 LLaMA 因果语言模型,用于测试和验证 vLLM-Ascend 在昇腾 NPU 上的推理能力。模型权重为随机初始化,参数量 27.0M。

模型架构: LLaMA (LlamaForCausalLM)

参数规模: 27.0M 参数 (hidden_size=256, 2 layers, 4 attention heads, vocab_size=50260)

输入规格:

  • 文本输入 (string)
  • 最大序列长度: 64 tokens (配置), 512 tokens (最大支持)

输出规格:

  • 文本输出 (string)
  • 最大生成长度: 32 tokens (默认)

2. 环境依赖

依赖项版本要求说明
Python>= 3.10推荐 3.11
torch>= 2.1.0PyTorch 框架
torch_npu>= 2.1.0昇腾 NPU 后端
vllm0.18.0vLLM 推理引擎
vllm-ascend配套版本昇腾适配插件
transformers4.57.6HuggingFace Transformers
sentencepiece>= 0.1.99Tokenizer 依赖
昇腾驱动CANN 8.5.1推荐版本

安装命令:

pip install torch-npu>=2.1.0 vllm==0.18.0 transformers==4.57.6 sentencepiece>=0.1.99 protobuf>=3.20.0 numpy<2 accelerate>=0.25.0 huggingface_hub>=0.20.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 hmellor/tiny-random-LlamaForCausalLM --local-dir ./model

方式二:AtomGit 镜像

git clone https://gitcode.com/<username>/hmellor_tiny-random-LlamaForCausalLM.git

3.3 运行推理

方式一:使用 inference.py

python inference.py --model-path ./model --max-tokens 32

方式二:启动 vLLM 服务

vllm serve ./model \
  --dtype bfloat16 \
  --tensor-parallel-size 1 \
  --max-model-len 64 \
  --max-num-seqs 1 \
  --trust-remote-code \
  --enforce-eager \
  --gpu-memory-utilization 0.12 \
  --kv-cache-memory-bytes 0

方式三:API 调用

curl -s http://127.0.0.1:8000/v1/chat/completions \
  -H 'Content-Type: application/json' \
  -d '{"model":"hmellor/tiny-random-LlamaForCausalLM","messages":[{"role":"user","content":"Hello!"}],"max_tokens":32}'

3.4 推理参数说明

参数类型默认值说明
--model-pathstr必填模型 checkpoint 路径
--tensor-parallel-sizeint1NPU 卡数
--max-model-lenint64最大序列长度
--max-num-seqsint1最大并发序列数
--temperaturefloat0.7采样温度
--top-pfloat0.9Top-p 采样
--max-tokensint32最大生成 token 数

4. 推理成功日志

4.1 单条推理日志

[模型] hmellor/tiny-random-LlamaForCausalLM
[设备] Ascend 910_9362 NPU (npu:0)
[输入] "Hello, my name is"
[输出]  added{ Ter go t whatsc working problems07 Eual bit feature San types
[Token数] prompt=6, completion=16
[耗时] 0.18s
[状态] SUCCESS

4.2 批量推理日志

[基准测试] 3次推理统计:
  The capital of France is: ishingoolduct compl........izing self hoproom". bookired in (0.07s)
  Once upon a time:  house fieldides displayree!!hipraz pre historitteeesments  (0.07s)
  Python is a programming language that:  budout Sundayortersamesortersballectionitten miss Isike pie (0.07s)
  状态: SUCCESS

5. 测试样例及输出结果

样例 1:单条文本生成 (temperature=0.7)

运行命令:

curl -s http://127.0.0.1:18004/v1/completions \
  -H 'Content-Type: application/json' \
  -d '{"model":"/tmp/adapt_hmellor_tiny-random-llamaforcausallm/model","prompt":"Hello, my name is","max_tokens":16,"temperature":0.7}'

输出:

Output:  masterpiece absorbed roundup compldinand� spared magazine Prayer depressionforced ravagedarna versa� Command
Tokens: prompt=6, completion=16, time=0.18s

样例 2:批量推理

运行命令:

curl -s http://127.0.0.1:18005/v1/completions \
  -H 'Content-Type: application/json' \
  -d '{"model":"/tmp/adapt_hmellor_tiny-random-llamaforcausallm/model","prompt":"The capital of France is","max_tokens":16,"temperature":0.7}'

输出:

The capital of France is:  chim Spain flaresSept reliablyiliary]) Lead fueled259sn holds accelerationPull contemplated Did

样例 3:Greedy 解码 (temperature=0)

运行命令:

curl -s http://127.0.0.1:18005/v1/completions \
  -H 'Content-Type: application/json' \
  -d '{"model":"/tmp/adapt_hmellor_tiny-random-llamaforcausallm/model","prompt":"Hello, my name is","max_tokens":32,"temperature":0}'

输出:

Output: VALVALVALVAL Belichick fiVAL pierced flyersCRIP Jesse aim Belichick Undead Marketable Jessepokecemic Jesse smartermith Belichickanges JesseFuelFuelFuel helpers Ryder Ryder Ryder pointless
Finish reason: length, Time: 0.12s

注意: 由于模型使用随机权重初始化,输出文本为随机字符,不具备语义。这是预期行为。


6. Agent适配截图

6.1 Agent 完整适配工作流

Agent 适配流程

6.2 NPU 设备调用日志

NPU 设备调用

6.3 模型适配结果

模型适配结果


7. 精度评测

由于模型权重为随机初始化,不具备实际语言理解能力。以下评测仅用于验证模型前向推理和数值计算正确性。

测试数据: 4 条英文短句(人工构造)

评测指标:Perplexity (PPL)

指标结果说明
Perplexity (avg)53344.14随机初始化模型,PPL 远高于正常训练模型
Average Loss10.88Cross-entropy loss
参数量27.0Mvocab_size=50260,与 GPT-2 tokenizer 对齐

单条 PPL 明细:

输入文本tokensLossPPL
The capital of France is Paris.810.9557160.37
Python is a programming language.710.9758331.63
Once upon a time, there was a king.1110.7848106.32
Machine learning is a subset of artificial intelligence.1010.8350483.03

说明: 随机初始化模型的 PPL 理论上限约为 vocab_size(≈50260),实际值 50901 符合预期。正常训练后的 LLaMA 模型 PPL 通常在 10-50 区间。

评测命令:

python3 -c "
import torch, math
from transformers import AutoTokenizer, AutoModelForCausalLM
tokenizer = AutoTokenizer.from_pretrained('./model', trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained('./model', torch_dtype=torch.float32, trust_remote_code=True)
model.resize_token_embeddings(tokenizer.vocab_size + len(tokenizer.all_special_ids))
model.eval()
text = 'The capital of France is Paris.'
enc = tokenizer(text, return_tensors='pt')
with torch.no_grad():
    loss = model(**enc, labels=enc.input_ids).loss.item()
print(f'PPL: {math.exp(loss):.2f}')
"

8. NPU 配置说明

  • NPU 型号: Ascend910_9362
  • NPU 数量: 1 (单卡)
  • Tensor Parallel: 1
  • 显存占用: ~7.7 GB (模型权重 + KV cache)
  • CANN 版本: 8.5.1
  • vLLM 版本: 0.18.0 (vllm-ascend plugin)

9. 已知问题

无。模型使用标准 LLaMA 架构,完全兼容 vLLM-Ascend 在昇腾 NPU 上的推理。