llm.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. var requiredMemory int64
  36. var f16Multiplier int64 = 2
  37. switch ggml.ModelType() {
  38. case "3B", "7B":
  39. requiredMemory = 8 * format.GigaByte
  40. case "13B":
  41. requiredMemory = 16 * format.GigaByte
  42. case "30B", "34B", "40B":
  43. requiredMemory = 32 * format.GigaByte
  44. case "47B":
  45. requiredMemory = 48 * format.GigaByte
  46. case "65B", "70B":
  47. requiredMemory = 64 * format.GigaByte
  48. case "180B":
  49. requiredMemory = 128 * format.GigaByte
  50. f16Multiplier = 4
  51. }
  52. systemMemory := int64(memory.TotalMemory())
  53. if ggml.FileType() == "F16" && requiredMemory*f16Multiplier > systemMemory {
  54. return nil, fmt.Errorf("F16 model requires at least %s of memory", format.HumanBytes(requiredMemory))
  55. } else if requiredMemory > systemMemory {
  56. return nil, fmt.Errorf("model requires at least %s of memory", format.HumanBytes(requiredMemory))
  57. }
  58. }
  59. opts.NumGQA = 0
  60. opts.RopeFrequencyBase = 0.0
  61. opts.RopeFrequencyScale = 0.0
  62. gpuInfo := gpu.GetGPUInfo()
  63. return newLlmServer(gpuInfo.Library, model, adapters, projectors, ggml.NumLayers(), opts)
  64. }
  65. // Give any native cgo implementations an opportunity to initialize
  66. func Init(workdir string) error {
  67. return nativeInit(workdir)
  68. }
  69. func newLlmServer(library, model string, adapters, projectors []string, numLayers int64, opts api.Options) (extServer, error) {
  70. if _, libPresent := AvailableShims[library]; libPresent && library != "default" {
  71. srv, err := newDynamicShimExtServer(AvailableShims[library], model, adapters, projectors, numLayers, opts)
  72. if err == nil {
  73. return srv, nil
  74. }
  75. log.Printf("Failed to load dynamic library %s - falling back to CPU mode %s", library, err)
  76. // TODO - update some state to indicate we were unable to load the GPU library for future "info" ux
  77. }
  78. return newDefaultExtServer(model, adapters, projectors, numLayers, opts)
  79. }