llm.go 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 - falling back to CPU mode %s", err)
  76. }
  77. return newDefaultExtServer(model, adapters, projectors, numLayers, opts)
  78. }