llm.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package llm
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "os"
  7. "runtime"
  8. "github.com/pbnjay/memory"
  9. "github.com/jmorganca/ollama/api"
  10. "github.com/jmorganca/ollama/format"
  11. "github.com/jmorganca/ollama/gpu"
  12. )
  13. type LLM interface {
  14. Predict(context.Context, PredictOpts, func(PredictResult)) error
  15. Embedding(context.Context, string) ([]float64, error)
  16. Encode(context.Context, string) ([]int, error)
  17. Decode(context.Context, []int) (string, error)
  18. Close()
  19. }
  20. var AvailableShims = map[string]string{}
  21. func New(workDir, model string, adapters, projectors []string, opts api.Options) (LLM, error) {
  22. if _, err := os.Stat(model); err != nil {
  23. return nil, err
  24. }
  25. f, err := os.Open(model)
  26. if err != nil {
  27. return nil, err
  28. }
  29. defer f.Close()
  30. ggml, err := DecodeGGML(f)
  31. if err != nil {
  32. return nil, err
  33. }
  34. if runtime.GOOS == "darwin" {
  35. switch ggml.FileType() {
  36. case "F32", "Q5_0", "Q5_1", "Q8_0":
  37. if ggml.Name() != "gguf" && opts.NumGPU != 0 {
  38. // GGML Q8_0 do not support Metal API and will
  39. // cause the runner to segmentation fault so disable GPU
  40. log.Printf("WARNING: GPU disabled for F32, Q5_0, Q5_1, and Q8_0")
  41. opts.NumGPU = 0
  42. }
  43. }
  44. var requiredMemory int64
  45. var f16Multiplier int64 = 2
  46. switch ggml.ModelType() {
  47. case "3B", "7B":
  48. requiredMemory = 8 * format.GigaByte
  49. case "13B":
  50. requiredMemory = 16 * format.GigaByte
  51. case "30B", "34B", "40B":
  52. requiredMemory = 32 * format.GigaByte
  53. case "65B", "70B":
  54. requiredMemory = 64 * format.GigaByte
  55. case "180B":
  56. requiredMemory = 128 * format.GigaByte
  57. f16Multiplier = 4
  58. }
  59. systemMemory := int64(memory.TotalMemory())
  60. if ggml.FileType() == "F16" && requiredMemory*f16Multiplier > systemMemory {
  61. return nil, fmt.Errorf("F16 model requires at least %s of total memory", format.HumanBytes(requiredMemory))
  62. } else if requiredMemory > systemMemory {
  63. return nil, fmt.Errorf("model requires at least %s of total memory", format.HumanBytes(requiredMemory))
  64. }
  65. }
  66. opts.NumGQA = 0
  67. opts.RopeFrequencyBase = 0.0
  68. opts.RopeFrequencyScale = 0.0
  69. gpuInfo := gpu.GetGPUInfo()
  70. return newLlmServer(gpuInfo.Library, model, adapters, projectors, ggml.NumLayers(), opts)
  71. }
  72. // Give any native cgo implementations an opportunity to initialize
  73. func Init(workdir string) error {
  74. return nativeInit(workdir)
  75. }
  76. func newLlmServer(library, model string, adapters, projectors []string, numLayers int64, opts api.Options) (extServer, error) {
  77. if _, libPresent := AvailableShims[library]; libPresent && library != "default" {
  78. srv, err := newDynamicShimExtServer(AvailableShims[library], model, adapters, projectors, numLayers, opts)
  79. if err == nil {
  80. return srv, nil
  81. }
  82. log.Printf("Failed to load dynamic library - falling back to CPU mode %s", err)
  83. }
  84. return newDefaultExtServer(model, adapters, projectors, numLayers, opts)
  85. }