Repository: ollama/ollama
File: server/sched.go (Go)

Code before:
		if !envconfig.SchedSpread() {
			for _, p := range numParallelToTry {
				req.opts.NumCtx = req.origNumCtx * p
				// Try to pack into as few GPUs as possible, starting from 1 GPU
				for numGPUs := 1; numGPUs <= len(sgl); numGPUs++ {
					gpuSubset := sgl[:numGPUs]
					ok, _ := llm.PredictServerFit(gpuSubset, f, req.model.AdapterPaths, req.model.ProjectorPaths, req.opts, p)

					if ok {
						var totalFreeMemory uint64
						for _, g := range gpuSubset {
							totalFreeMemory += g.FreeMemory
						}
						*numParallel = p
						return gpuSubset
					}
				}
			}
		}

		// TODO future refinements
		// - if multiple Libraries, see if any single GPU in any Library will fit
		// - try subsets of GPUs instead of just falling back to 1 or all in a family

		// Now try all the GPUS (OLLAMA_SCHED_SPREAD is set)
		for _, p := range numParallelToTry {
			req.opts.NumCtx = req.origNumCtx * p
			if envconfig.SchedSpread() {
				if ok, _ := llm.PredictServerFit(sgl, f, req.model.AdapterPaths, req.model.ProjectorPaths, req.opts, p); ok {
					*numParallel = p
					return sgl
				}
			}
		}
	}
	return nil
}

// If multiple Libraries are detected, pick the Library which loads the most layers for the model
func pickBestPartialFitByLibrary(req *LlmRequest, f *ggml.GGML, gpus discover.GpuInfoList, numParallel *int) discover.GpuInfoList {
	if *numParallel <= 0 {
		*numParallel = 1
		req.opts.NumCtx = req.origNumCtx
	}
	byLibrary := gpus.ByLibrary()
	if len(byLibrary) <= 1 {
		return gpus
	}
	var bestEstimate uint64
	var bestFit int
	for i, gl := range byLibrary {

Review comment (suggestion):
This whole block can be an else on `if !envconfig.SchedSpread() {`. No point in going through a loop only to check a constant condition in the middle.

I would also keep the existing log message here.

Code after:
		if !envconfig.SchedSpread() {
			for _, p := range numParallelToTry {
				req.opts.NumCtx = req.origNumCtx * p
				// Try to pack into as few GPUs as possible, starting from 1 GPU
				for numGPUs := 1; numGPUs <= len(sgl); numGPUs++ {
					gpuSubset := sgl[:numGPUs]
					ok, estimatedVRAM := llm.PredictServerFit(gpuSubset, f, req.model.AdapterPaths, req.model.ProjectorPaths, req.opts, p)

					if ok {
						slog.Info("new model will fit in available VRAM across minimum required GPUs, loading",
							"model", req.model.ModelPath,
							"library", sgl[0].Library,
							"parallel", p,
							"required", format.HumanBytes2(estimatedVRAM),
							"gpus", numGPUs)
						*numParallel = p
						return gpuSubset
					}
				}
			}
		} else {
			// TODO future refinements
			// - if multiple Libraries, see if any single GPU in any Library will fit
			// - try subsets of GPUs instead of just falling back to 1 or all in a family

			// Now try all the GPUS (OLLAMA_SCHED_SPREAD is set)
			for _, p := range numParallelToTry {
				req.opts.NumCtx = req.origNumCtx * p
				if ok, estimatedVRAM := llm.PredictServerFit(sgl, f, req.model.AdapterPaths, req.model.ProjectorPaths, req.opts, p); ok {
					slog.Info("new model will fit in available VRAM, loading",
						"model", req.model.ModelPath,
						"library", sgl[0].Library,
						"parallel", p,
						"required", format.HumanBytes2(estimatedVRAM),
						"gpus", len(sgl))
					*numParallel = p
					return sgl
				}
			}
		}
	}
	return nil
}

// If multiple Libraries are detected, pick the Library which loads the most layers for the model
func pickBestPartialFitByLibrary(req *LlmRequest, f *ggml.GGML, gpus discover.GpuInfoList, numParallel *int) discover.GpuInfoList {
	if *numParallel <= 0 {
		*numParallel = 1
		req.opts.NumCtx = req.origNumCtx
	}
	byLibrary := gpus.ByLibrary()