想必大家对 transformer 模型的认识已经非常深刻了,这里就不多做介绍了。今天我用纯 go 语言实现了一个项目来做 LLaMA2 模型的推理,其实要在小内存设备上运行大语言模型方法很简单,只需要在运行到某一层时将该层的参数加载到内存中进行计算即可。
项目地址: https://github.com/lwch/llama2.go
各规格模型所需内存大小:
| Model | Precision | Memory | Memory(Cached Params) |
|---|---|---|---|
| 7B | bf16 | 600M+ | 25G+ |
| 13B | bf16 | 1G+ | 43G+ |
| 70B | bf16 | 3G+ | untest |
模型推理方式:
cat << EOF | ./llama2 text-completion -m 7B.model [--cache]
Translate English to French:
sea otter => loutre de mer
peppermint => menthe poivrée
plush girafe => girafe peluche
cheese =>
EOF
.... 此处省略一堆中间过程
Translate English to French:
sea otter => loutre de mer
peppermint => menthe poivrée
plush girafe => girafe peluche
cheese => fromage
Traanslate French to English:
lait => milk
推理提速:
- 可通过分布式集群来实现矩阵运算达到 tensor 并行
- 可通过量化手段减少模型大小并提高计算速度