模型名称: 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)
输入规格:
输出规格:
| 依赖项 | 版本要求 | 说明 |
|---|---|---|
| Python | >= 3.10 | 推荐 3.11 |
| torch | >= 2.1.0 | PyTorch 框架 |
| torch_npu | >= 2.1.0 | 昇腾 NPU 后端 |
| vllm | 0.18.0 | vLLM 推理引擎 |
| vllm-ascend | 配套版本 | 昇腾适配插件 |
| transformers | 4.57.6 | HuggingFace Transformers |
| sentencepiece | >= 0.1.99 | Tokenizer 依赖 |
| 昇腾驱动 | 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# 检查 NPU 设备
npu-smi info
# 验证 torch_npu
python3 -c "import torch_npu; print(torch.npu.device_count(), torch.npu.get_device_name(0))"方式一:HuggingFace(推荐)
huggingface-cli download hmellor/tiny-random-LlamaForCausalLM --local-dir ./model方式二:AtomGit 镜像
git clone https://gitcode.com/<username>/hmellor_tiny-random-LlamaForCausalLM.git方式一:使用 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}'| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| --model-path | str | 必填 | 模型 checkpoint 路径 |
| --tensor-parallel-size | int | 1 | NPU 卡数 |
| --max-model-len | int | 64 | 最大序列长度 |
| --max-num-seqs | int | 1 | 最大并发序列数 |
| --temperature | float | 0.7 | 采样温度 |
| --top-p | float | 0.9 | Top-p 采样 |
| --max-tokens | int | 32 | 最大生成 token 数 |
[模型] 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[基准测试] 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运行命令:
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运行命令:
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运行命令:
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注意: 由于模型使用随机权重初始化,输出文本为随机字符,不具备语义。这是预期行为。



由于模型权重为随机初始化,不具备实际语言理解能力。以下评测仅用于验证模型前向推理和数值计算正确性。
测试数据: 4 条英文短句(人工构造)
评测指标:Perplexity (PPL)
| 指标 | 结果 | 说明 |
|---|---|---|
| Perplexity (avg) | 53344.14 | 随机初始化模型,PPL 远高于正常训练模型 |
| Average Loss | 10.88 | Cross-entropy loss |
| 参数量 | 27.0M | vocab_size=50260,与 GPT-2 tokenizer 对齐 |
单条 PPL 明细:
| 输入文本 | tokens | Loss | PPL |
|---|---|---|---|
| The capital of France is Paris. | 8 | 10.95 | 57160.37 |
| Python is a programming language. | 7 | 10.97 | 58331.63 |
| Once upon a time, there was a king. | 11 | 10.78 | 48106.32 |
| Machine learning is a subset of artificial intelligence. | 10 | 10.83 | 50483.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}')
"无。模型使用标准 LLaMA 架构,完全兼容 vLLM-Ascend 在昇腾 NPU 上的推理。