ubuntu配置n卡驱动使得ollama运行在显存上
·
起因:
在一台服务器上部署了ollama,使用qwen模型进行一些票据的识别。但自从设备异常重启之后,经常出现请求超时的情况,十分影响客户体验。
排查过程:
使用journalctl -u ollama -f命令,经常能看到请求时长在40多秒,而且是长时间不使用模型之后,第一个请求总是40多秒,第二个请求就能保持在7秒左右,用户体验尚可。
Feb 25 11:03:48 luonan ollama[2326649]: [GIN] 2026/02/25 - 11:03:48 | 200 | 7.463517596s | x | POST "/api/generate"
Feb 26 02:20:39 luonan ollama[2326649]: [GIN] 2026/02/26 - 02:20:39 | 200 | 7.485141897s | x | POST "/api/generate"
Feb 27 06:15:23 luonan ollama[2326649]: [GIN] 2026/02/27 - 06:15:23 | 200 | 42.963247128s | x | POST "/api/generate"
看现象应该是模型冷加载了,于是将ollama配置为常驻内存
cat /etc/systemd/system/ollama.service
Environment="OLLAMA_KEEP_ALIVE=-1"
这下测试超过24小时再去请求,发现还是7s,不再出现冷重启的情况啦。但仍然发现有请求500:
Feb 27 05:16:44 luonan ollama[2326649]: time=2026-02-27T05:16:44.337Z level=ERROR source=server.go:807 msg="post predict" error="Post \"http://127.0.0.1:44257/completion\": context canceled"
Feb 27 05:16:44 luonan ollama[2326649]: [GIN] 2026/02/27 - 05:16:44 | 500 | 1m0s | x | POST "/api/generate"
Feb 27 05:17:04 luonan ollama[2326649]: [GIN] 2026/02/27 - 05:17:04 | 200 | 18.873584932s | x | POST "/api/generate"
Feb 27 05:17:11 luonan ollama[2326649]: [GIN] 2026/02/27 - 05:17:11 | 200 | 18.239111824s | x | POST "/api/generate"
Feb 27 06:06:40 luonan ollama[2326649]: time=2026-02-27T06:06:40.358Z level=ERROR source=server.go:807 msg="post predict" error="Post \"http://127.0.0.1:44257/completion\": context canceled"
Feb 27 06:06:40 luonan ollama[2326649]: [GIN] 2026/02/27 - 06:06:40 | 500 | 1m0s | x | POST "/api/generate"
整个崩溃的逻辑链是这样的:
-
某个请求特别复杂(比如图片很大,或者纯 CPU 推理实在太慢),耗时超过了 60 秒。
-
Nginx 等得不耐烦了,到达 60 秒阈值,直接强行切断了和 Ollama 的连接(返回 500 给用户)。
-
Ollama 发现 HTTP 连接断了,触发 context canceled,强行中断底层的推理引擎(llama runner)。
-
关键点来了:这种非正常的强行中断,有时会导致底层的上下文损坏。为了自我保护和释放错乱的内存,Ollama 可能会将被中断的模型从内存中卸载掉。
-
模型被卸载后,下一次请求来临时,就只能再次经历 40 多秒的冷启动拉取了。
设备是有n卡的,直接使用显存来搞
没有驱动?
root@luonan:~# nvidia-smi
NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver. Make sure that the latest NVIDIA driver is installed and running.
安装驱动:(3080性能还行嗷)
root@luonan:~# lspci | grep -i nvidia # 查看n卡硬件
03:00.0 VGA compatible controller: NVIDIA Corporation GA102 [GeForce RTX 3080 Ti] (rev a1)
03:00.1 Audio device: NVIDIA Corporation GA102 High Definition Audio Controller (rev a1)
root@luonan:~# apt update # 更新软件包
root@luonan:~# ubuntu-drivers autoinstall # 自动安装适配的驱动
root@luonan:~# nvidia-smi
Fri Feb 27 06:28:29 2026
+-----------------------------------------------------------------------------------------+
| NVIDIA-SMI 580.95.05 Driver Version: 580.95.05 CUDA Version: 13.0 |
+-----------------------------------------+------------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+========================+======================|
| 0 NVIDIA GeForce RTX 3080 Ti Off | 00000000:03:00.0 Off | N/A |
| 31% 39C P0 97W / 350W | 1MiB / 12288MiB | 0% Default |
| | | N/A |
+-----------------------------------------+------------------------+----------------------+
+-----------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=========================================================================================|
| No running processes found |
+-----------------------------------------------------------------------------------------+
重启ollama:systemctl restart ollama
测试效果如下,直接起飞:1s左右
Feb 27 06:31:03 luonan ollama[2436668]: [GIN] 2026/02/27 - 06:31:03 | 200 | 984.679033ms | x | POST "/api/generate"
Feb 27 06:31:10 luonan ollama[2436668]: [GIN] 2026/02/27 - 06:31:10 | 200 | 990.433119ms | x | POST "/api/generate"
Feb 27 06:49:57 luonan ollama[2436668]: [GIN] 2026/02/27 - 06:49:57 | 200 | 1.027040792s | x | POST "/api/generate"
更多推荐

所有评论(0)