llm.go 2.5 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. // Set to false on linux/windows if we are able to load the shim
  21. var ShimPresent = false
  22. func New(workDir, model string, adapters, projectors []string, opts api.Options) (LLM, error) {
  23. if _, err := os.Stat(model); err != nil {
  24. return nil, err
  25. }
  26. f, err := os.Open(model)
  27. if err != nil {
  28. return nil, err
  29. }
  30. defer f.Close()
  31. ggml, err := DecodeGGML(f)
  32. if err != nil {
  33. return nil, err
  34. }
  35. if runtime.GOOS == "darwin" {
  36. switch ggml.FileType() {
  37. case "F32", "Q5_0", "Q5_1", "Q8_0":
  38. if ggml.Name() != "gguf" && opts.NumGPU != 0 {
  39. // GGML Q8_0 do not support Metal API and will
  40. // cause the runner to segmentation fault so disable GPU
  41. log.Printf("WARNING: GPU disabled for F32, Q5_0, Q5_1, and Q8_0")
  42. opts.NumGPU = 0
  43. }
  44. }
  45. var requiredMemory int64
  46. var f16Multiplier int64 = 2
  47. switch ggml.ModelType() {
  48. case "3B", "7B":
  49. requiredMemory = 8 * format.GigaByte
  50. case "13B":
  51. requiredMemory = 16 * format.GigaByte
  52. case "30B", "34B", "40B":
  53. requiredMemory = 32 * format.GigaByte
  54. case "65B", "70B":
  55. requiredMemory = 64 * format.GigaByte
  56. case "180B":
  57. requiredMemory = 128 * format.GigaByte
  58. f16Multiplier = 4
  59. }
  60. systemMemory := int64(memory.TotalMemory())
  61. if ggml.FileType() == "F16" && requiredMemory*f16Multiplier > systemMemory {
  62. return nil, fmt.Errorf("F16 model requires at least %s of total memory", format.HumanBytes(requiredMemory))
  63. } else if requiredMemory > systemMemory {
  64. return nil, fmt.Errorf("model requires at least %s of total memory", format.HumanBytes(requiredMemory))
  65. }
  66. }
  67. opts.NumGQA = 0
  68. opts.RopeFrequencyBase = 0.0
  69. opts.RopeFrequencyScale = 0.0
  70. gpuInfo := gpu.GetGPUInfo()
  71. if gpuInfo.Driver == "ROCM" && ShimPresent {
  72. return newRocmShimExtServer(model, adapters, projectors, ggml.NumLayers(), opts)
  73. } else {
  74. // Rely on the built-in CUDA/Metal based server which will fall back to CPU
  75. return newLlamaExtServer(model, adapters, projectors, ggml.NumLayers(), opts)
  76. }
  77. }
  78. // Give any native cgo implementations an opportunity to initialize
  79. func Init(workdir string) error {
  80. return nativeInit(workdir)
  81. }