2023-07-21 20:33:56 +00:00
|
|
|
package llm
|
|
|
|
|
|
|
|
import (
|
2023-08-30 20:35:03 +00:00
|
|
|
"context"
|
2023-07-21 20:33:56 +00:00
|
|
|
"fmt"
|
2023-08-03 22:40:16 +00:00
|
|
|
"log"
|
2023-07-21 20:33:56 +00:00
|
|
|
"os"
|
2023-10-05 16:53:47 +00:00
|
|
|
"runtime"
|
2023-07-21 20:33:56 +00:00
|
|
|
|
2023-08-03 22:47:36 +00:00
|
|
|
"github.com/pbnjay/memory"
|
|
|
|
|
2023-07-21 20:33:56 +00:00
|
|
|
"github.com/jmorganca/ollama/api"
|
2023-10-12 16:34:16 +00:00
|
|
|
"github.com/jmorganca/ollama/format"
|
2023-11-29 19:00:37 +00:00
|
|
|
"github.com/jmorganca/ollama/gpu"
|
2023-07-21 20:33:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type LLM interface {
|
2023-12-05 19:57:33 +00:00
|
|
|
Predict(context.Context, PredictOpts, func(PredictResult)) error
|
2023-08-30 20:35:03 +00:00
|
|
|
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
|
|
|
Close()
|
|
|
|
}
|
|
|
|
|
2023-12-15 22:27:27 +00:00
|
|
|
// Set to false on linux/windows if we are able to load the shim
|
|
|
|
var ShimPresent = false
|
|
|
|
|
2023-11-30 18:30:23 +00:00
|
|
|
func New(workDir, model string, adapters, projectors []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-10-05 16:53:47 +00:00
|
|
|
if runtime.GOOS == "darwin" {
|
|
|
|
switch ggml.FileType() {
|
2023-11-20 21:44:12 +00:00
|
|
|
case "F32", "Q5_0", "Q5_1", "Q8_0":
|
2023-10-05 16:53:47 +00:00
|
|
|
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
|
|
|
|
}
|
2023-08-03 22:40:16 +00:00
|
|
|
}
|
2023-10-12 16:47:17 +00:00
|
|
|
|
2023-10-13 21:41:51 +00:00
|
|
|
var requiredMemory int64
|
|
|
|
var f16Multiplier int64 = 2
|
2023-10-12 17:36:23 +00:00
|
|
|
|
2023-10-13 21:41:51 +00:00
|
|
|
switch ggml.ModelType() {
|
|
|
|
case "3B", "7B":
|
|
|
|
requiredMemory = 8 * format.GigaByte
|
|
|
|
case "13B":
|
|
|
|
requiredMemory = 16 * format.GigaByte
|
|
|
|
case "30B", "34B", "40B":
|
|
|
|
requiredMemory = 32 * format.GigaByte
|
|
|
|
case "65B", "70B":
|
|
|
|
requiredMemory = 64 * format.GigaByte
|
|
|
|
case "180B":
|
|
|
|
requiredMemory = 128 * format.GigaByte
|
|
|
|
f16Multiplier = 4
|
|
|
|
}
|
2023-10-12 17:36:23 +00:00
|
|
|
|
2023-10-13 21:41:51 +00:00
|
|
|
systemMemory := int64(memory.TotalMemory())
|
2023-10-12 17:36:23 +00:00
|
|
|
|
2023-10-13 21:41:51 +00:00
|
|
|
if ggml.FileType() == "F16" && requiredMemory*f16Multiplier > systemMemory {
|
|
|
|
return nil, fmt.Errorf("F16 model requires at least %s of total memory", format.HumanBytes(requiredMemory))
|
|
|
|
} else if requiredMemory > systemMemory {
|
|
|
|
return nil, fmt.Errorf("model requires at least %s of total memory", format.HumanBytes(requiredMemory))
|
|
|
|
}
|
2023-08-03 22:47:36 +00:00
|
|
|
}
|
|
|
|
|
2023-11-24 18:58:09 +00:00
|
|
|
opts.NumGQA = 0
|
|
|
|
opts.RopeFrequencyBase = 0.0
|
|
|
|
opts.RopeFrequencyScale = 0.0
|
2023-11-29 19:00:37 +00:00
|
|
|
gpuInfo := gpu.GetGPUInfo()
|
2023-12-15 22:27:27 +00:00
|
|
|
if gpuInfo.Driver == "ROCM" && ShimPresent {
|
2023-11-29 19:00:37 +00:00
|
|
|
return newRocmShimExtServer(model, adapters, projectors, ggml.NumLayers(), opts)
|
2023-12-15 22:27:27 +00:00
|
|
|
} else {
|
|
|
|
// Rely on the built-in CUDA/Metal based server which will fall back to CPU
|
2023-11-29 19:00:37 +00:00
|
|
|
return newLlamaExtServer(model, adapters, projectors, ggml.NumLayers(), opts)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Give any native cgo implementations an opportunity to initialize
|
|
|
|
func Init(workdir string) error {
|
|
|
|
return nativeInit(workdir)
|
2023-07-21 20:33:56 +00:00
|
|
|
}
|