ollama/llm/llm.go

95 lines
2.9 KiB
Go
Raw Normal View History

2023-07-21 20:33:56 +00:00
package llm
import (
"context"
2023-07-21 20:33:56 +00:00
"fmt"
"log"
2023-07-21 20:33:56 +00:00
"os"
"github.com/pbnjay/memory"
2023-07-21 20:33:56 +00:00
"github.com/jmorganca/ollama/api"
)
type LLM interface {
Predict(context.Context, []int, string, func(api.GenerateResponse)) error
Embedding(context.Context, string) ([]float64, error)
Encode(context.Context, string) ([]int, error)
Decode(context.Context, []int) (string, error)
2023-07-21 20:33:56 +00:00
SetOptions(api.Options)
Close()
Ping(context.Context) error
2023-07-21 20:33:56 +00:00
}
func New(model string, adapters []string, opts api.Options) (LLM, error) {
2023-07-21 20:33:56 +00:00
if _, err := os.Stat(model); err != nil {
return nil, err
}
f, err := os.Open(model)
if err != nil {
return nil, err
}
2023-08-14 23:08:02 +00:00
defer f.Close()
2023-07-21 20:33:56 +00:00
2023-09-07 17:55:37 +00:00
ggml, err := DecodeGGML(f)
2023-07-21 20:33:56 +00:00
if err != nil {
return nil, err
}
2023-08-17 18:37:27 +00:00
switch ggml.FileType().String() {
2023-09-07 17:55:37 +00:00
case "Q8_0":
if ggml.Name() != "gguf" && opts.NumGPU != 0 {
// GGML Q8_0 do not support Metal API and will
// cause the runner to segmentation fault so disable GPU
log.Printf("WARNING: GPU disabled for F32, Q5_0, Q5_1, and Q8_0")
opts.NumGPU = 0
}
case "F32", "Q5_0", "Q5_1":
if opts.NumGPU != 0 {
2023-09-07 17:55:37 +00:00
// F32, Q5_0, Q5_1, and Q8_0 do not support Metal API and will
// cause the runner to segmentation fault so disable GPU
log.Printf("WARNING: GPU disabled for F32, Q5_0, Q5_1, and Q8_0")
opts.NumGPU = 0
}
}
totalResidentMemory := memory.TotalMemory()
2023-08-17 18:37:27 +00:00
switch ggml.ModelType() {
case ModelType3B, ModelType7B:
if ggml.FileType().String() == "F16" && totalResidentMemory < 16*1024*1024 {
return nil, fmt.Errorf("F16 model requires at least 16GB of memory")
} else if totalResidentMemory < 8*1024*1024 {
return nil, fmt.Errorf("model requires at least 8GB of memory")
}
case ModelType13B:
if ggml.FileType().String() == "F16" && totalResidentMemory < 32*1024*1024 {
return nil, fmt.Errorf("F16 model requires at least 32GB of memory")
} else if totalResidentMemory < 16*1024*1024 {
return nil, fmt.Errorf("model requires at least 16GB of memory")
}
2023-08-26 15:29:21 +00:00
case ModelType30B, ModelType34B:
if ggml.FileType().String() == "F16" && totalResidentMemory < 64*1024*1024 {
return nil, fmt.Errorf("F16 model requires at least 64GB of memory")
} else if totalResidentMemory < 32*1024*1024 {
return nil, fmt.Errorf("model requires at least 32GB of memory")
}
case ModelType65B:
if ggml.FileType().String() == "F16" && totalResidentMemory < 128*1024*1024 {
return nil, fmt.Errorf("F16 model requires at least 128GB of memory")
} else if totalResidentMemory < 64*1024*1024 {
return nil, fmt.Errorf("model requires at least 64GB of memory")
}
}
2023-09-07 17:55:37 +00:00
switch ggml.Name() {
case "gguf":
opts.NumGQA = 0 // TODO: remove this when llama.cpp runners differ enough to need separate newLlama functions
return newLlama(model, adapters, ggufRunner(), opts)
case "ggml", "ggmf", "ggjt", "ggla":
return newLlama(model, adapters, ggmlRunner(), opts)
2023-07-21 20:33:56 +00:00
default:
2023-08-17 18:37:27 +00:00
return nil, fmt.Errorf("unknown ggml type: %s", ggml.ModelFamily())
2023-07-21 20:33:56 +00:00
}
}