Ollama显存优化
前言
ollama这个大模型执行框架可以让大模型跑在CPU,或者CPU+GPU的混合模式下。本文主要以llama3:8b为例,通过设置决定模型加载进gpu的层数来优化推理效率。
原文博客地址:Ollama显存优化 | 旅程blog (cxy.red)
1. 法一:交互模式指定
ollama run llama3:8b
>>> /set parameter num_gpu 5
Set parameter 'num_gpu' to '5'
查看日志server.log文件可以看到加载模型GPU的层数从31层变为了5层。
2. 法二:修改模型配置文件
2.1 配置模型文件
用以下命令查看llama3:8b模型配置文件
ollama show llama3:8b --modelfile
得到如下配置文件信息:
# Modelfile generated by "ollama show"
# To build a new Modelfile based on this, replace FROM with:
# FROM llama3:8b
FROM E:\ollama\blobs\sha256-00e1317cbf74d901080d7100f57580ba8dd8de57203072dc6f668324ba545f29
TEMPLATE "{{ if .System }}<|start_header_id|>system<|end_header_id|>
{{ .System }}<|eot_id|>{{ end }}{{ if .Prompt }}<|start_header_id|>user<|end_header_id|>
{{ .Prompt }}<|eot_id|>{{ end }}<|start_header_id|>assistant<|end_header_id|>
{{ .Response }}<|eot_id|>"
PARAMETER num_keep 24
PARAMETER stop <|start_header_id|>
PARAMETER stop <|end_header_id|>
PARAMETER stop <|eot_id|>
2.2 修改配置文件
创建模型配置文件mymodel.modefile,将获取的配置文件加入参数PARAMETER num_gpu 5
,该参数表名指定加载进gpu的模型层数
# Modelfile generated by "ollama show"
# To build a new Modelfile based on this, replace FROM with:
FROM llama3:8b
# 添加参数
PARAMETER num_gpu 5
TEMPLATE "{{ if .System }}<|start_header_id|>system<|end_header_id|>
{{ .System }}<|eot_id|>{{ end }}{{ if .Prompt }}<|start_header_id|>user<|end_header_id|>
{{ .Prompt }}<|eot_id|>{{ end }}<|start_header_id|>assistant<|end_header_id|>
{{ .Response }}<|eot_id|>"
PARAMETER num_keep 24
PARAMETER stop <|start_header_id|>
PARAMETER stop <|end_header_id|>
PARAMETER stop <|eot_id|>
1.3 创建模型
利用新建的模型配置文件创建模型命令为:
ollama create mymodel -f mymodel.modefile
创建成功后可利用ollama list
查看创建的模型
1.4 查看修改结果
ollama run mymodel
运行模型,然后查看日志server.log文件可以看到加载模型GPU的层数变为了5层。
参考文献