Skip to content

Get up and running with Kimi, GLM, MiniMax, DeepSeek, gpt-oss, Qwen, Gemma and other models.

llm: keep gemma3n projector off the CPU (#18376)

Daniel Hiltgen53fed2611281
diff --git a/llm/llama_server.go b/llm/llama_server.go
index 0f4019799..c3e917265 100644
--- a/llm/llama_server.go
+++ b/llm/llama_server.go
@@ -661,12 +661,22 @@ func appendMMProjArgs(params []string, launch llamaServerLaunchConfig) []string
}
func (launch llamaServerLaunchConfig) mmprojOffloadDisabled() (bool, string) {
+ if launch.requiresMMProjGPUOffload() {
+ return false, ""
+ }
if launch.forceNoMMProjOffload {
return true, "startup-oom-retry"
}
return shouldDisableMMProjOffload(launch.opts, launch.gpus, launch.modelLayers, launch.mmprojMemory)
}
+func (launch llamaServerLaunchConfig) requiresMMProjGPUOffload() bool {
+ // Gemma3n's MobileNetV5 projector silently produces corrupted image
+ // embeddings when it runs on the CPU backend, so keep it on the GPU
+ // whenever one is in play.
+ return launch.modelArch == "gemma3n" && launch.opts.NumGPU != 0 && len(launch.gpus) > 0
+}
+
func shouldDisableMMProjOffload(opts api.Options, gpus []ml.DeviceInfo, modelLayers, mmprojMemory uint64) (bool, string) {
if opts.NumGPU == 0 {
return true, "cpu-only"
@@ -1107,6 +1117,9 @@ func (s *llamaServerRunner) shouldRetryMMProjCPUOffload(err error) bool {
if err == nil || s.mmprojOffloadOOMRetried || !IsOutOfMemory(err) || len(s.launch.projectors) == 0 {
return false
}
+ if s.launch.requiresMMProjGPUOffload() {
+ return false
+ }
// llama-server --fit can select a text-layer placement that fits before
// mtmd/CLIP allocates the multimodal projector. Retry once with the
// projector on CPU so the scheduler can keep the text model placement.
diff --git a/llm/llama_server_test.go b/llm/llama_server_test.go
index 4d4300b5e..256b57dc3 100644
--- a/llm/llama_server_test.go
+++ b/llm/llama_server_test.go
@@ -2195,6 +2195,7 @@ func TestAppendMMProjArgs(t *testing.T) {
tests := []struct {
name string
+ modelArch string
projectors []string
opts api.Options
gpus []ml.DeviceInfo
@@ -2299,12 +2300,23 @@ func TestAppendMMProjArgs(t *testing.T) {
retry: true,
want: []string{"base", "--mmproj", "model.gguf", "--no-mmproj-offload"},
},
+ {
+ name: "gemma3n keeps projector offload under partial text offload",
+ modelArch: "gemma3n",
+ projectors: []string{"model.gguf"},
+ opts: partialOpts,
+ gpus: []ml.DeviceInfo{{DeviceID: ml.DeviceID{Library: "CUDA"}, FreeMemory: 24 << 30}},
+ mmprojMemory: 933 << 20,
+ modelLayers: 81,
+ want: []string{"base", "--mmproj", "model.gguf"},
+ },
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := appendMMProjArgs([]string{"base"}, llamaServerLaunchConfig{
modelPath: "model.gguf",
+ modelArch: tt.modelArch,
projectors: tt.projectors,
mmprojMemory: tt.mmprojMemory,
opts: tt.opts,
@@ -2319,6 +2331,45 @@ func TestAppendMMProjArgs(t *testing.T) {
}
}
+func TestShouldRetryMMProjCPUOffload(t *testing.T) {
+ launch := func(modelArch string) llamaServerLaunchConfig {
+ return llamaServerLaunchConfig{
+ modelArch: modelArch,
+ projectors: []string{"mmproj.gguf"},
+ opts: api.DefaultOptions(),
+ gpus: []ml.DeviceInfo{{DeviceID: ml.DeviceID{Library: "CUDA"}, FreeMemory: 24 << 30}},
+ mmprojMemory: 933 << 20,
+ modelLayers: 81,
+ }
+ }
+
+ tests := []struct {
+ name string
+ launch llamaServerLaunchConfig
+ want bool
+ }{
+ {
+ name: "oom retries with projector on cpu",
+ launch: launch("qwen3vl"),
+ want: true,
+ },
+ {
+ name: "gemma3n oom does not retry with projector on cpu",
+ launch: launch("gemma3n"),
+ want: false,
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ s := &llamaServerRunner{launch: tt.launch}
+ if got := s.shouldRetryMMProjCPUOffload(errors.New("out of memory")); got != tt.want {
+ t.Fatalf("shouldRetryMMProjCPUOffload = %v, want %v", got, tt.want)
+ }
+ })
+ }
+}
+
func TestMMProjFitTargetExtraEnvs(t *testing.T) {
t.Setenv(llamaArgFitTargetEnv, "")
_ = os.Unsetenv(llamaArgFitTargetEnv)