memory.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package llm
  2. import (
  3. "fmt"
  4. "log/slog"
  5. "os"
  6. "strconv"
  7. "github.com/ollama/ollama/api"
  8. "github.com/ollama/ollama/format"
  9. "github.com/ollama/ollama/gpu"
  10. )
  11. // This algorithm looks for a complete fit to determine if we need to unload other models
  12. func PredictServerFit(allGpus gpu.GpuInfoList, ggml *GGML, adapters, projectors []string, opts api.Options) (bool, uint64) {
  13. var estimatedVRAM uint64
  14. if opts.NumCtx > int(ggml.KV().ContextLength()) {
  15. slog.Warn("requested context length is greater than model max context length", "requested", opts.NumCtx, "model", ggml.KV().ContextLength())
  16. opts.NumCtx = int(ggml.KV().ContextLength())
  17. }
  18. if opts.NumCtx < 4 {
  19. opts.NumCtx = 4
  20. }
  21. // Split up the GPUs by type and try them
  22. for _, gpus := range allGpus.ByLibrary() {
  23. var layerCount int
  24. layerCount, estimatedVRAM = EstimateGPULayers(gpus, ggml, projectors, opts)
  25. if opts.NumGPU < 0 {
  26. if layerCount > 0 && layerCount >= int(ggml.KV().BlockCount()+1) {
  27. return true, estimatedVRAM
  28. }
  29. } else {
  30. if layerCount > 0 && layerCount >= opts.NumGPU {
  31. return true, estimatedVRAM
  32. }
  33. }
  34. }
  35. return false, estimatedVRAM
  36. }
  37. // Given a model and one or more GPU targets, predict how many layers and bytes we can load
  38. // The GPUs provided must all be the same Library
  39. func EstimateGPULayers(gpus []gpu.GpuInfo, ggml *GGML, projectors []string, opts api.Options) (int, uint64) {
  40. if gpus[0].Library == "cpu" {
  41. return 0, 0
  42. }
  43. var memoryAvailable uint64
  44. for _, info := range gpus {
  45. memoryAvailable += info.FreeMemory
  46. }
  47. userLimit := os.Getenv("OLLAMA_MAX_VRAM")
  48. if userLimit != "" {
  49. avail, err := strconv.ParseUint(userLimit, 10, 64)
  50. if err != nil {
  51. slog.Error("invalid setting, ignoring", "OLLAMA_MAX_VRAM", userLimit, "error", err)
  52. } else {
  53. slog.Info("user override memory limit", "OLLAMA_MAX_VRAM", avail, "actual", memoryAvailable)
  54. memoryAvailable = avail
  55. }
  56. }
  57. slog.Debug("evaluating", "library", gpus[0].Library, "gpu_count", len(gpus), "available", format.HumanBytes2(memoryAvailable))
  58. // TODO - this is probably wrong, first GPU vs secondaries will have different overheads
  59. memoryMinimum := gpus[0].MinimumMemory
  60. for _, projector := range projectors {
  61. memoryMinimum += projectorMemoryRequirements(projector)
  62. // multimodal models require at least 2048 context
  63. opts.NumCtx = max(opts.NumCtx, 2048)
  64. }
  65. // fp16 k,v = (1 (k) + 1 (v)) * sizeof(float16) * n_ctx * n_layer * n_embd / n_head * n_head_kv
  66. var kv uint64 = 2 * 2 * uint64(opts.NumCtx) * ggml.KV().BlockCount() * ggml.KV().EmbeddingLength() / ggml.KV().HeadCount() * ggml.KV().HeadCountKV()
  67. graphPartialOffload, graphFullOffload := ggml.GraphSize(uint64(opts.NumCtx), uint64(min(opts.NumCtx, opts.NumBatch)))
  68. if graphPartialOffload == 0 {
  69. graphPartialOffload = ggml.KV().GQA() * kv / 6
  70. }
  71. if graphFullOffload == 0 {
  72. graphFullOffload = graphPartialOffload
  73. }
  74. graphFullOffload *= uint64(len(gpus))
  75. graphPartialOffload *= uint64(len(gpus))
  76. // memoryRequiredTotal represents the memory required for full GPU offloading (all layers)
  77. memoryRequiredTotal := memoryMinimum + graphFullOffload
  78. // memoryRequiredPartial represents the memory required for partial GPU offloading (n > 0, n < layers)
  79. memoryRequiredPartial := memoryMinimum + graphPartialOffload
  80. if memoryRequiredPartial > memoryAvailable {
  81. slog.Debug("insufficient VRAM to load any model layers")
  82. return 0, 0
  83. }
  84. layers := ggml.Tensors().Layers()
  85. var memoryLayerOutput uint64
  86. if layer, ok := layers["output_norm"]; ok {
  87. memoryLayerOutput += layer.size()
  88. }
  89. if layer, ok := layers["output"]; ok {
  90. memoryLayerOutput += layer.size()
  91. } else if layer, ok := layers["token_embd"]; ok {
  92. memoryLayerOutput += layer.size()
  93. }
  94. if gpus[0].Library == "metal" && opts.UseMMap {
  95. // memory is preallocated for output tensors
  96. memoryRequiredTotal += memoryLayerOutput
  97. memoryRequiredPartial += memoryLayerOutput
  98. }
  99. var layerCount int
  100. for i := 0; i < int(ggml.KV().BlockCount()); i++ {
  101. memoryLayer := layers[fmt.Sprintf("blk.%d", i)].size()
  102. // KV is proportional to the number of layers
  103. memoryLayer += kv / ggml.KV().BlockCount()
  104. memoryRequiredTotal += memoryLayer
  105. if memoryAvailable > memoryRequiredPartial+memoryLayer {
  106. memoryRequiredPartial += memoryLayer
  107. layerCount++
  108. }
  109. }
  110. if gpus[0].Library != "metal" || !opts.UseMMap {
  111. // memory was not preallocated for output tensors
  112. memoryRequiredTotal += memoryLayerOutput
  113. }
  114. if memoryAvailable > memoryRequiredTotal {
  115. layerCount = int(ggml.KV().BlockCount()) + 1
  116. memoryRequiredPartial = memoryRequiredTotal
  117. }
  118. memoryWeights := memoryRequiredTotal - memoryMinimum - graphFullOffload - kv
  119. slog.Info(
  120. "offload to gpu",
  121. slog.Group(
  122. "layers",
  123. // actual number of layers offloaded
  124. "real", opts.NumGPU,
  125. // estimated number of layers that can be offloaded
  126. "estimate", layerCount,
  127. ),
  128. slog.Group(
  129. "memory",
  130. // memory available for offloading
  131. "available", format.HumanBytes2(memoryAvailable),
  132. slog.Group(
  133. "required",
  134. // memory required for full offloading
  135. "full", format.HumanBytes2(memoryRequiredTotal),
  136. // memory required to offload layers.estimate layers
  137. "partial", format.HumanBytes2(memoryRequiredPartial),
  138. // memory of KV cache
  139. "kv", format.HumanBytes2(kv),
  140. ),
  141. slog.Group(
  142. "weights",
  143. // memory of the weights
  144. "total", format.HumanBytes2(memoryWeights),
  145. // memory of repeating layers
  146. "repeating", format.HumanBytes2(memoryWeights-memoryLayerOutput),
  147. // memory of non-repeating layers
  148. "nonrepeating", format.HumanBytes2(memoryLayerOutput),
  149. ),
  150. slog.Group(
  151. "graph",
  152. // memory of graph when fully offloaded
  153. "full", format.HumanBytes2(graphFullOffload),
  154. // memory of graph when not fully offloaded
  155. "partial", format.HumanBytes2(graphPartialOffload),
  156. ),
  157. ),
  158. )
  159. return layerCount, uint64(memoryRequiredPartial)
  160. }