2024-03-30 16:50:05 +00:00
|
|
|
package llm
|
|
|
|
|
|
|
|
import (
|
2024-06-18 18:05:34 +00:00
|
|
|
"fmt"
|
2024-03-30 16:50:05 +00:00
|
|
|
"log/slog"
|
2024-05-18 19:34:31 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2024-03-30 16:50:05 +00:00
|
|
|
|
|
|
|
"github.com/ollama/ollama/api"
|
2024-10-17 00:45:00 +00:00
|
|
|
"github.com/ollama/ollama/discover"
|
2024-09-05 20:46:35 +00:00
|
|
|
"github.com/ollama/ollama/envconfig"
|
2024-03-30 16:50:05 +00:00
|
|
|
"github.com/ollama/ollama/format"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This algorithm looks for a complete fit to determine if we need to unload other models
|
2024-10-17 00:45:00 +00:00
|
|
|
func PredictServerFit(allGpus discover.GpuInfoList, ggml *GGML, adapters, projectors []string, opts api.Options) (bool, uint64) {
|
2024-03-30 16:50:05 +00:00
|
|
|
// Split up the GPUs by type and try them
|
2024-05-10 17:17:12 +00:00
|
|
|
var estimatedVRAM uint64
|
2024-03-30 16:50:05 +00:00
|
|
|
for _, gpus := range allGpus.ByLibrary() {
|
|
|
|
var layerCount int
|
2024-05-18 19:34:31 +00:00
|
|
|
estimate := EstimateGPULayers(gpus, ggml, projectors, opts)
|
|
|
|
layerCount, estimatedVRAM = estimate.Layers, estimate.VRAMSize
|
2024-03-30 16:50:05 +00:00
|
|
|
if opts.NumGPU < 0 {
|
|
|
|
if layerCount > 0 && layerCount >= int(ggml.KV().BlockCount()+1) {
|
|
|
|
return true, estimatedVRAM
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if layerCount > 0 && layerCount >= opts.NumGPU {
|
|
|
|
return true, estimatedVRAM
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false, estimatedVRAM
|
|
|
|
}
|
|
|
|
|
2024-05-18 19:34:31 +00:00
|
|
|
type MemoryEstimate struct {
|
|
|
|
// How many layers we predict we can load
|
|
|
|
Layers int
|
|
|
|
|
|
|
|
// The size of the graph which occupies the main GPU
|
|
|
|
Graph uint64
|
|
|
|
|
|
|
|
// How much VRAM will be allocated given the number of layers we predict
|
|
|
|
VRAMSize uint64
|
|
|
|
|
|
|
|
// The total size of the model if loaded into VRAM. If all layers are loaded, VRAMSize == TotalSize
|
|
|
|
TotalSize uint64
|
|
|
|
|
|
|
|
// For multi-GPU scenarios, this provides the tensor split parameter
|
|
|
|
TensorSplit string
|
|
|
|
|
|
|
|
// For multi-GPU scenarios, this is the size in bytes per GPU
|
|
|
|
GPUSizes []uint64
|
2024-06-18 01:39:48 +00:00
|
|
|
|
|
|
|
// internal fields for logging purposes
|
|
|
|
inferenceLibrary string
|
|
|
|
layersRequested int
|
|
|
|
layersModel int
|
|
|
|
availableList []string
|
|
|
|
kv uint64
|
|
|
|
allocationsList []string
|
|
|
|
memoryWeights uint64
|
|
|
|
memoryLayerOutput uint64
|
|
|
|
graphFullOffload uint64
|
|
|
|
graphPartialOffload uint64
|
2024-05-18 19:34:31 +00:00
|
|
|
}
|
|
|
|
|
2024-05-04 16:15:31 +00:00
|
|
|
// Given a model and one or more GPU targets, predict how many layers and bytes we can load, and the total size
|
2024-03-30 16:50:05 +00:00
|
|
|
// The GPUs provided must all be the same Library
|
2024-10-17 00:45:00 +00:00
|
|
|
func EstimateGPULayers(gpus []discover.GpuInfo, ggml *GGML, projectors []string, opts api.Options) MemoryEstimate {
|
2024-05-18 19:34:31 +00:00
|
|
|
// Graph size for a partial offload, applies to all GPUs
|
|
|
|
var graphPartialOffload uint64
|
|
|
|
|
|
|
|
// Graph size when all layers are offloaded, applies to all GPUs
|
|
|
|
var graphFullOffload uint64
|
|
|
|
|
|
|
|
// Final graph offload once we know full or partial
|
|
|
|
var graphOffload uint64
|
|
|
|
|
|
|
|
// Projectors loaded into GPU0 only
|
|
|
|
var projectorSize uint64
|
|
|
|
|
|
|
|
// Conditional output size on GPU 0
|
|
|
|
var memoryLayerOutput uint64
|
|
|
|
|
2024-06-05 19:07:20 +00:00
|
|
|
// The sizes of a layer
|
|
|
|
var layerSize uint64
|
2024-03-30 16:50:05 +00:00
|
|
|
|
2024-05-18 19:34:31 +00:00
|
|
|
// The sum of all the layer sizes (just for logging)
|
|
|
|
var memoryWeights uint64
|
|
|
|
|
|
|
|
// True if all the layers are loaded
|
|
|
|
var fullyLoaded bool
|
|
|
|
|
|
|
|
// Overflow that didn't fit into the GPU
|
|
|
|
var overflow uint64
|
|
|
|
|
2024-09-05 20:46:35 +00:00
|
|
|
overhead := envconfig.GpuOverhead()
|
2024-05-18 19:34:31 +00:00
|
|
|
availableList := make([]string, len(gpus))
|
|
|
|
for i, gpu := range gpus {
|
|
|
|
availableList[i] = format.HumanBytes2(gpu.FreeMemory)
|
|
|
|
}
|
|
|
|
slog.Debug("evaluating", "library", gpus[0].Library, "gpu_count", len(gpus), "available", availableList)
|
2024-03-30 16:50:05 +00:00
|
|
|
|
|
|
|
for _, projector := range projectors {
|
2024-05-18 19:34:31 +00:00
|
|
|
projectorSize += projectorMemoryRequirements(projector)
|
2024-03-30 16:50:05 +00:00
|
|
|
|
|
|
|
// multimodal models require at least 2048 context
|
|
|
|
opts.NumCtx = max(opts.NumCtx, 2048)
|
|
|
|
}
|
|
|
|
|
2024-05-10 21:40:37 +00:00
|
|
|
layers := ggml.Tensors().Layers()
|
2024-05-13 21:14:10 +00:00
|
|
|
// add one layer worth of memory as a buffer
|
|
|
|
if blk0, ok := layers["blk.0"]; ok {
|
2024-06-05 19:07:20 +00:00
|
|
|
layerSize = blk0.size()
|
|
|
|
} else {
|
|
|
|
slog.Warn("model missing blk.0 layer size")
|
2024-05-13 21:14:10 +00:00
|
|
|
}
|
2024-05-10 21:40:37 +00:00
|
|
|
|
2024-06-20 16:40:17 +00:00
|
|
|
// fp16 k,v = sizeof(float16) * n_ctx * n_layer * (n_embd_head_k + n_embd_head_v) * n_head_kv
|
|
|
|
var kv uint64 = 2 * uint64(opts.NumCtx) * ggml.KV().BlockCount() * (ggml.KV().EmbeddingHeadCountK() + ggml.KV().EmbeddingHeadCountV()) * ggml.KV().HeadCountKV()
|
2024-03-30 16:50:05 +00:00
|
|
|
|
2024-06-05 19:07:20 +00:00
|
|
|
// KV is proportional to the number of layers
|
|
|
|
layerSize += kv / ggml.KV().BlockCount()
|
|
|
|
|
2024-05-18 19:34:31 +00:00
|
|
|
graphPartialOffload, graphFullOffload = ggml.GraphSize(uint64(opts.NumCtx), uint64(min(opts.NumCtx, opts.NumBatch)))
|
2024-03-30 16:50:05 +00:00
|
|
|
if graphPartialOffload == 0 {
|
|
|
|
graphPartialOffload = ggml.KV().GQA() * kv / 6
|
|
|
|
}
|
|
|
|
if graphFullOffload == 0 {
|
|
|
|
graphFullOffload = graphPartialOffload
|
|
|
|
}
|
|
|
|
|
2024-05-01 15:46:03 +00:00
|
|
|
// on metal there's no partial offload overhead
|
|
|
|
if gpus[0].Library == "metal" {
|
|
|
|
graphPartialOffload = graphFullOffload
|
2024-06-05 19:07:20 +00:00
|
|
|
} else if len(gpus) > 1 {
|
|
|
|
// multigpu should always use the partial graph size
|
|
|
|
graphFullOffload = graphPartialOffload
|
2024-05-01 15:46:03 +00:00
|
|
|
}
|
|
|
|
|
2024-04-26 22:00:54 +00:00
|
|
|
if layer, ok := layers["output_norm"]; ok {
|
|
|
|
memoryLayerOutput += layer.size()
|
|
|
|
}
|
|
|
|
if layer, ok := layers["output"]; ok {
|
|
|
|
memoryLayerOutput += layer.size()
|
|
|
|
} else if layer, ok := layers["token_embd"]; ok {
|
|
|
|
memoryLayerOutput += layer.size()
|
2024-04-25 21:41:50 +00:00
|
|
|
}
|
|
|
|
|
2024-06-05 19:07:20 +00:00
|
|
|
// Output layer handled at the end if we have space
|
2024-05-18 19:34:31 +00:00
|
|
|
gpuZeroOverhead := projectorSize
|
|
|
|
|
|
|
|
// Reduce set of GPUs to only those that have sufficient space to fit overhead and at least one layer
|
2024-04-25 21:41:50 +00:00
|
|
|
var layerCount int
|
2024-05-18 19:34:31 +00:00
|
|
|
layerCounts := make([]int, len(gpus))
|
|
|
|
gpuAllocations := make([]uint64, len(gpus))
|
|
|
|
type gs struct {
|
|
|
|
i int
|
2024-10-17 00:45:00 +00:00
|
|
|
g *discover.GpuInfo
|
2024-05-18 19:34:31 +00:00
|
|
|
}
|
|
|
|
gpusWithSpace := []gs{}
|
|
|
|
for i := range gpus {
|
|
|
|
var gzo uint64
|
|
|
|
if len(gpusWithSpace) == 0 {
|
|
|
|
gzo = gpuZeroOverhead
|
|
|
|
}
|
|
|
|
// Only include GPUs that can fit the graph, gpu minimum, the layer buffer and at least more layer
|
2024-09-05 20:46:35 +00:00
|
|
|
if (gpus[i].FreeMemory - overhead) < gzo+max(graphPartialOffload, graphFullOffload)+gpus[i].MinimumMemory+2*layerSize {
|
2024-09-06 15:29:36 +00:00
|
|
|
slog.Debug("gpu has too little memory to allocate any layers",
|
|
|
|
"id", gpus[i].ID,
|
|
|
|
"library", gpus[i].Library,
|
|
|
|
"variant", gpus[i].Variant,
|
|
|
|
"compute", gpus[i].Compute,
|
|
|
|
"driver", fmt.Sprintf("%d.%d", gpus[i].DriverMajor, gpus[i].DriverMinor),
|
|
|
|
"name", gpus[i].Name,
|
|
|
|
"total", format.HumanBytes2(gpus[i].TotalMemory),
|
|
|
|
"available", format.HumanBytes2(gpus[i].FreeMemory),
|
|
|
|
"minimum_memory", gpus[i].MinimumMemory,
|
|
|
|
"layer_size", format.HumanBytes2(layerSize),
|
|
|
|
"gpu_zer_overhead", format.HumanBytes2(gzo),
|
|
|
|
"partial_offload", format.HumanBytes2(graphPartialOffload),
|
|
|
|
"full_offload", format.HumanBytes2(graphFullOffload),
|
|
|
|
)
|
2024-05-18 19:34:31 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
gpusWithSpace = append(gpusWithSpace, gs{i, &gpus[i]})
|
2024-06-05 19:07:20 +00:00
|
|
|
gpuAllocations[i] += gpus[i].MinimumMemory + layerSize // We hold off on graph until we know partial vs. full
|
2024-05-18 19:34:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var gpuZeroID int
|
|
|
|
if len(gpusWithSpace) > 0 {
|
|
|
|
gpuZeroID = gpusWithSpace[0].i
|
|
|
|
gpuAllocations[gpuZeroID] += gpuZeroOverhead
|
|
|
|
}
|
|
|
|
|
2024-06-05 19:07:20 +00:00
|
|
|
// For all the layers, find where they can fit on the GPU(s)
|
2024-05-22 05:21:04 +00:00
|
|
|
for i := range int(ggml.KV().BlockCount()) {
|
2024-06-18 18:05:34 +00:00
|
|
|
// Some models have inconsistent layer sizes
|
|
|
|
if blk, ok := layers[fmt.Sprintf("blk.%d", i)]; ok {
|
|
|
|
layerSize = blk.size()
|
|
|
|
layerSize += kv / ggml.KV().BlockCount()
|
|
|
|
}
|
2024-06-05 19:07:20 +00:00
|
|
|
memoryWeights += layerSize
|
2024-03-30 16:50:05 +00:00
|
|
|
|
2024-05-18 19:34:31 +00:00
|
|
|
if opts.NumGPU >= 0 && layerCount >= opts.NumGPU {
|
|
|
|
// Stop allocating on GPU(s) once we hit the users target NumGPU
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// distribute the layers across the GPU(s) that have space
|
|
|
|
for j := len(gpusWithSpace); j > 0; j-- {
|
|
|
|
g := gpusWithSpace[i%j]
|
|
|
|
used := gpuAllocations[g.i] + max(graphPartialOffload, graphFullOffload)
|
2024-09-05 20:46:35 +00:00
|
|
|
if (g.g.FreeMemory - overhead) > used+layerSize {
|
2024-06-05 19:07:20 +00:00
|
|
|
gpuAllocations[g.i] += layerSize
|
2024-05-18 19:34:31 +00:00
|
|
|
layerCounts[g.i]++
|
2024-05-13 21:14:10 +00:00
|
|
|
layerCount++
|
2024-05-18 19:34:31 +00:00
|
|
|
break
|
|
|
|
} else {
|
|
|
|
gpusWithSpace = append(gpusWithSpace[:i%j], gpusWithSpace[i%j+1:]...)
|
2024-05-13 21:14:10 +00:00
|
|
|
}
|
2024-03-30 16:50:05 +00:00
|
|
|
}
|
2024-05-18 19:34:31 +00:00
|
|
|
}
|
|
|
|
if layerCount >= int(ggml.KV().BlockCount()) {
|
|
|
|
fullyLoaded = true
|
|
|
|
} else {
|
|
|
|
for i := layerCount; i < int(ggml.KV().BlockCount()); i++ {
|
2024-06-05 19:07:20 +00:00
|
|
|
overflow += layerSize
|
2024-05-18 19:34:31 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-05 19:07:20 +00:00
|
|
|
|
|
|
|
// Determine if we need to consider output then find where it fits
|
2024-06-13 16:59:36 +00:00
|
|
|
if memoryLayerOutput > 0 && (opts.NumGPU < 0 || layerCount < opts.NumGPU) {
|
2024-05-18 19:34:31 +00:00
|
|
|
for j := len(gpusWithSpace); j > 0; j-- {
|
|
|
|
g := gpusWithSpace[layerCount%j]
|
|
|
|
used := gpuAllocations[g.i] + max(graphPartialOffload, graphFullOffload)
|
2024-09-05 20:46:35 +00:00
|
|
|
if (g.g.FreeMemory - overhead) > used+memoryLayerOutput {
|
2024-05-18 19:34:31 +00:00
|
|
|
gpuAllocations[g.i] += memoryLayerOutput
|
|
|
|
layerCounts[g.i]++
|
|
|
|
layerCount++
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2024-06-05 19:07:20 +00:00
|
|
|
|
2024-05-18 19:34:31 +00:00
|
|
|
if layerCount < int(ggml.KV().BlockCount())+1 {
|
|
|
|
fullyLoaded = false
|
|
|
|
overflow += memoryLayerOutput
|
|
|
|
}
|
2024-03-30 16:50:05 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 19:34:31 +00:00
|
|
|
// Add the applicable (full or partial) graph allocations
|
|
|
|
for i := range gpus {
|
|
|
|
if layerCounts[i] <= 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if fullyLoaded {
|
|
|
|
gpuAllocations[i] += graphFullOffload
|
|
|
|
} else {
|
|
|
|
gpuAllocations[i] += graphPartialOffload
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if fullyLoaded {
|
|
|
|
graphOffload = graphFullOffload
|
|
|
|
} else {
|
|
|
|
graphOffload = graphPartialOffload
|
2024-03-30 16:50:05 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 19:34:31 +00:00
|
|
|
// Summaries for the log
|
|
|
|
var memoryRequiredPartial, memoryRequiredTotal uint64
|
|
|
|
for i := range gpuAllocations {
|
|
|
|
memoryRequiredPartial += gpuAllocations[i]
|
2024-03-30 16:50:05 +00:00
|
|
|
}
|
2024-05-18 19:34:31 +00:00
|
|
|
memoryRequiredTotal = memoryRequiredPartial + overflow
|
2024-03-30 16:50:05 +00:00
|
|
|
|
2024-05-18 19:34:31 +00:00
|
|
|
tensorSplit := ""
|
|
|
|
if len(gpus) > 1 {
|
|
|
|
splits := make([]string, len(gpus))
|
|
|
|
for i, count := range layerCounts {
|
|
|
|
splits[i] = strconv.Itoa(count)
|
|
|
|
}
|
|
|
|
tensorSplit = strings.Join(splits, ",")
|
|
|
|
}
|
|
|
|
allocationsList := []string{}
|
|
|
|
for _, a := range gpuAllocations {
|
|
|
|
allocationsList = append(allocationsList, format.HumanBytes2(a))
|
|
|
|
}
|
2024-03-30 16:50:05 +00:00
|
|
|
|
2024-06-18 01:39:48 +00:00
|
|
|
estimate := MemoryEstimate{
|
|
|
|
TotalSize: memoryRequiredTotal,
|
|
|
|
Layers: 0,
|
|
|
|
Graph: 0,
|
|
|
|
VRAMSize: 0,
|
|
|
|
GPUSizes: []uint64{},
|
|
|
|
|
|
|
|
inferenceLibrary: gpus[0].Library,
|
|
|
|
layersRequested: opts.NumGPU,
|
|
|
|
layersModel: int(ggml.KV().BlockCount()) + 1,
|
|
|
|
availableList: availableList,
|
|
|
|
kv: kv,
|
|
|
|
allocationsList: allocationsList,
|
|
|
|
memoryWeights: memoryWeights,
|
|
|
|
memoryLayerOutput: memoryLayerOutput,
|
|
|
|
graphFullOffload: graphFullOffload,
|
|
|
|
graphPartialOffload: graphPartialOffload,
|
|
|
|
}
|
|
|
|
|
|
|
|
if gpus[0].Library == "cpu" {
|
|
|
|
return estimate
|
|
|
|
}
|
|
|
|
if layerCount == 0 {
|
|
|
|
slog.Debug("insufficient VRAM to load any model layers")
|
|
|
|
return estimate
|
|
|
|
}
|
|
|
|
estimate.Layers = layerCount
|
|
|
|
estimate.Graph = graphOffload
|
|
|
|
estimate.VRAMSize = memoryRequiredPartial
|
|
|
|
estimate.TotalSize = memoryRequiredTotal
|
|
|
|
estimate.TensorSplit = tensorSplit
|
|
|
|
estimate.GPUSizes = gpuAllocations
|
|
|
|
return estimate
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m MemoryEstimate) log() {
|
2024-09-05 20:46:35 +00:00
|
|
|
overhead := envconfig.GpuOverhead()
|
2024-03-30 16:50:05 +00:00
|
|
|
slog.Info(
|
2024-06-18 01:39:48 +00:00
|
|
|
"offload to "+m.inferenceLibrary,
|
2024-03-30 16:50:05 +00:00
|
|
|
slog.Group(
|
|
|
|
"layers",
|
2024-05-10 21:40:37 +00:00
|
|
|
// requested number of layers to offload
|
2024-06-18 01:39:48 +00:00
|
|
|
"requested", m.layersRequested,
|
2024-05-18 19:34:31 +00:00
|
|
|
// The number of layers the model has (including output)
|
2024-06-18 01:39:48 +00:00
|
|
|
"model", m.layersModel,
|
2024-03-30 16:50:05 +00:00
|
|
|
// estimated number of layers that can be offloaded
|
2024-06-18 01:39:48 +00:00
|
|
|
"offload", m.Layers,
|
|
|
|
// multi-gpu split for tensors
|
|
|
|
"split", m.TensorSplit,
|
2024-03-30 16:50:05 +00:00
|
|
|
),
|
|
|
|
slog.Group(
|
|
|
|
"memory",
|
2024-05-18 19:34:31 +00:00
|
|
|
// memory available by GPU for offloading
|
2024-06-18 01:39:48 +00:00
|
|
|
"available", m.availableList,
|
2024-09-05 20:46:35 +00:00
|
|
|
"gpu_overhead", format.HumanBytes2(overhead),
|
2024-03-30 16:50:05 +00:00
|
|
|
slog.Group(
|
|
|
|
"required",
|
|
|
|
// memory required for full offloading
|
2024-06-18 01:39:48 +00:00
|
|
|
"full", format.HumanBytes2(m.TotalSize),
|
2024-03-30 16:50:05 +00:00
|
|
|
// memory required to offload layers.estimate layers
|
2024-06-18 01:39:48 +00:00
|
|
|
"partial", format.HumanBytes2(m.VRAMSize),
|
2024-03-30 16:50:05 +00:00
|
|
|
// memory of KV cache
|
2024-06-18 01:39:48 +00:00
|
|
|
"kv", format.HumanBytes2(m.kv),
|
2024-05-18 19:34:31 +00:00
|
|
|
// Allocations across the GPUs
|
2024-06-18 01:39:48 +00:00
|
|
|
"allocations", m.allocationsList,
|
2024-03-30 16:50:05 +00:00
|
|
|
),
|
|
|
|
slog.Group(
|
|
|
|
"weights",
|
|
|
|
// memory of the weights
|
2024-06-18 01:39:48 +00:00
|
|
|
"total", format.HumanBytes2(m.memoryWeights),
|
2024-03-30 16:50:05 +00:00
|
|
|
// memory of repeating layers
|
2024-06-18 01:39:48 +00:00
|
|
|
"repeating", format.HumanBytes2(m.memoryWeights-m.memoryLayerOutput),
|
2024-03-30 16:50:05 +00:00
|
|
|
// memory of non-repeating layers
|
2024-06-18 01:39:48 +00:00
|
|
|
"nonrepeating", format.HumanBytes2(m.memoryLayerOutput),
|
2024-03-30 16:50:05 +00:00
|
|
|
),
|
|
|
|
slog.Group(
|
|
|
|
"graph",
|
|
|
|
// memory of graph when fully offloaded
|
2024-06-18 01:39:48 +00:00
|
|
|
"full", format.HumanBytes2(m.graphFullOffload),
|
2024-03-30 16:50:05 +00:00
|
|
|
// memory of graph when not fully offloaded
|
2024-06-18 01:39:48 +00:00
|
|
|
"partial", format.HumanBytes2(m.graphPartialOffload),
|
2024-03-30 16:50:05 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|