llm.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 "65B", "70B":
  45. requiredMemory = 64 * format.GigaByte
  46. case "180B":
  47. requiredMemory = 128 * format.GigaByte
  48. f16Multiplier = 4
  49. }
  50. systemMemory := int64(memory.TotalMemory())
  51. if ggml.FileType() == "F16" && requiredMemory*f16Multiplier > systemMemory {
  52. return nil, fmt.Errorf("F16 model requires at least %s of total memory", format.HumanBytes(requiredMemory))
  53. } else if requiredMemory > systemMemory {
  54. return nil, fmt.Errorf("model requires at least %s of total memory", format.HumanBytes(requiredMemory))
  55. }
  56. }
  57. opts.NumGQA = 0
  58. opts.RopeFrequencyBase = 0.0
  59. opts.RopeFrequencyScale = 0.0
  60. gpuInfo := gpu.GetGPUInfo()
  61. return newLlmServer(gpuInfo.Library, model, adapters, projectors, ggml.NumLayers(), opts)
  62. }
  63. // Give any native cgo implementations an opportunity to initialize
  64. func Init(workdir string) error {
  65. return nativeInit(workdir)
  66. }
  67. func newLlmServer(library, model string, adapters, projectors []string, numLayers int64, opts api.Options) (extServer, error) {
  68. if _, libPresent := AvailableShims[library]; libPresent && library != "default" {
  69. srv, err := newDynamicShimExtServer(AvailableShims[library], model, adapters, projectors, numLayers, opts)
  70. if err == nil {
  71. return srv, nil
  72. }
  73. log.Printf("Failed to load dynamic library - falling back to CPU mode %s", err)
  74. }
  75. return newDefaultExtServer(model, adapters, projectors, numLayers, opts)
  76. }