2024-01-05 20:13:08 +00:00
|
|
|
package llm
|
|
|
|
|
|
|
|
import (
|
2024-01-19 18:23:03 +00:00
|
|
|
"compress/gzip"
|
2024-01-05 20:13:08 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/fs"
|
2024-01-18 18:52:01 +00:00
|
|
|
"log/slog"
|
2024-01-05 20:13:08 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
2024-01-13 00:28:00 +00:00
|
|
|
"golang.org/x/exp/slices"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
|
2024-01-05 20:13:08 +00:00
|
|
|
"github.com/jmorganca/ollama/gpu"
|
|
|
|
)
|
|
|
|
|
2024-01-07 23:48:05 +00:00
|
|
|
// Libraries names may contain an optional variant separated by '_'
|
2024-01-05 20:13:08 +00:00
|
|
|
// For example, "rocm_v6" and "rocm_v5" or "cpu" and "cpu_avx2"
|
2024-01-07 23:48:05 +00:00
|
|
|
// Any library without a variant is the lowest common denominator
|
2024-01-10 04:29:58 +00:00
|
|
|
var availableDynLibs = map[string]string{}
|
2024-01-05 20:13:08 +00:00
|
|
|
|
2024-01-13 00:28:00 +00:00
|
|
|
const pathComponentCount = 7
|
2024-01-05 20:13:08 +00:00
|
|
|
|
2024-01-10 04:29:58 +00:00
|
|
|
// getDynLibs returns an ordered list of LLM libraries to try, starting with the best
|
|
|
|
func getDynLibs(gpuInfo gpu.GpuInfo) []string {
|
2024-01-07 23:48:05 +00:00
|
|
|
// Short circuit if we know we're using the default built-in (darwin only)
|
|
|
|
if gpuInfo.Library == "default" {
|
|
|
|
return []string{"default"}
|
|
|
|
}
|
2024-01-11 22:43:16 +00:00
|
|
|
// TODO - temporary until we have multiple CPU variations for Darwin
|
|
|
|
// Short circuit on darwin with metal only
|
|
|
|
if len(availableDynLibs) == 1 {
|
|
|
|
if _, onlyMetal := availableDynLibs["metal"]; onlyMetal {
|
|
|
|
return []string{availableDynLibs["metal"]}
|
|
|
|
}
|
|
|
|
}
|
2024-01-07 23:48:05 +00:00
|
|
|
|
2024-01-05 20:13:08 +00:00
|
|
|
exactMatch := ""
|
2024-01-10 04:29:58 +00:00
|
|
|
dynLibs := []string{}
|
|
|
|
altDynLibs := []string{}
|
2024-01-05 20:13:08 +00:00
|
|
|
requested := gpuInfo.Library
|
|
|
|
if gpuInfo.Variant != "" {
|
|
|
|
requested += "_" + gpuInfo.Variant
|
|
|
|
}
|
2024-01-07 23:48:05 +00:00
|
|
|
// Try to find an exact match
|
2024-01-10 04:29:58 +00:00
|
|
|
for cmp := range availableDynLibs {
|
2024-01-05 20:13:08 +00:00
|
|
|
if requested == cmp {
|
|
|
|
exactMatch = cmp
|
2024-01-10 04:29:58 +00:00
|
|
|
dynLibs = []string{availableDynLibs[cmp]}
|
2024-01-05 20:13:08 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2024-01-07 23:48:05 +00:00
|
|
|
// Then for GPUs load alternates and sort the list for consistent load ordering
|
2024-01-05 20:13:08 +00:00
|
|
|
if gpuInfo.Library != "cpu" {
|
2024-01-10 04:29:58 +00:00
|
|
|
for cmp := range availableDynLibs {
|
2024-01-07 23:48:05 +00:00
|
|
|
if gpuInfo.Library == strings.Split(cmp, "_")[0] && cmp != exactMatch {
|
2024-01-10 04:29:58 +00:00
|
|
|
altDynLibs = append(altDynLibs, cmp)
|
2024-01-05 20:13:08 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-10 04:29:58 +00:00
|
|
|
slices.Sort(altDynLibs)
|
|
|
|
for _, altDynLib := range altDynLibs {
|
|
|
|
dynLibs = append(dynLibs, availableDynLibs[altDynLib])
|
2024-01-05 20:13:08 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-07 23:48:05 +00:00
|
|
|
|
|
|
|
// Load up the best CPU variant if not primary requested
|
|
|
|
if gpuInfo.Library != "cpu" {
|
|
|
|
variant := gpu.GetCPUVariant()
|
|
|
|
// If no variant, then we fall back to default
|
|
|
|
// If we have a variant, try that if we find an exact match
|
|
|
|
// Attempting to run the wrong CPU instructions will panic the
|
|
|
|
// process
|
|
|
|
if variant != "" {
|
2024-01-10 04:29:58 +00:00
|
|
|
for cmp := range availableDynLibs {
|
2024-01-07 23:48:05 +00:00
|
|
|
if cmp == "cpu_"+variant {
|
2024-01-10 04:29:58 +00:00
|
|
|
dynLibs = append(dynLibs, availableDynLibs[cmp])
|
2024-01-07 23:48:05 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2024-01-10 04:29:58 +00:00
|
|
|
dynLibs = append(dynLibs, availableDynLibs["cpu"])
|
2024-01-07 23:48:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-11 22:25:21 +00:00
|
|
|
// Finally, if we didn't find any matches, LCD CPU FTW
|
2024-01-10 04:29:58 +00:00
|
|
|
if len(dynLibs) == 0 {
|
|
|
|
dynLibs = []string{availableDynLibs["cpu"]}
|
2024-01-07 23:48:05 +00:00
|
|
|
}
|
2024-02-11 22:50:06 +00:00
|
|
|
slog.Debug(fmt.Sprintf("ordered list of LLM libraries to try %v", dynLibs))
|
2024-01-10 04:29:58 +00:00
|
|
|
return dynLibs
|
2024-01-05 20:13:08 +00:00
|
|
|
}
|
|
|
|
|
2024-01-10 04:29:58 +00:00
|
|
|
func rocmDynLibPresent() bool {
|
|
|
|
for dynLibName := range availableDynLibs {
|
|
|
|
if strings.HasPrefix(dynLibName, "rocm") {
|
2024-01-05 20:13:08 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-02-16 01:15:09 +00:00
|
|
|
func nativeInit() error {
|
2024-03-08 17:45:55 +00:00
|
|
|
payloadsDir, err := gpu.PayloadsDir()
|
2024-02-16 01:15:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-08 17:45:55 +00:00
|
|
|
|
2024-03-09 23:55:34 +00:00
|
|
|
slog.Info(fmt.Sprintf("Extracting dynamic libraries to %s ...", payloadsDir))
|
2024-01-05 20:13:08 +00:00
|
|
|
|
2024-03-08 17:45:55 +00:00
|
|
|
libs, err := extractDynamicLibs(payloadsDir, "llama.cpp/build/*/*/*/lib/*")
|
2024-01-05 20:13:08 +00:00
|
|
|
if err != nil {
|
2024-03-15 14:14:12 +00:00
|
|
|
if errors.Is(err, payloadMissing) {
|
2024-01-18 18:52:01 +00:00
|
|
|
slog.Info(fmt.Sprintf("%s", payloadMissing))
|
2024-01-05 20:13:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, lib := range libs {
|
|
|
|
// The last dir component is the variant name
|
|
|
|
variant := filepath.Base(filepath.Dir(lib))
|
2024-01-10 04:29:58 +00:00
|
|
|
availableDynLibs[variant] = lib
|
2024-01-05 20:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := verifyDriverAccess(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report which dynamic libraries we have loaded to assist troubleshooting
|
2024-01-10 04:29:58 +00:00
|
|
|
variants := make([]string, len(availableDynLibs))
|
2024-01-05 20:13:08 +00:00
|
|
|
i := 0
|
2024-01-10 04:29:58 +00:00
|
|
|
for variant := range availableDynLibs {
|
2024-01-05 20:13:08 +00:00
|
|
|
variants[i] = variant
|
|
|
|
i++
|
|
|
|
}
|
2024-01-18 18:52:01 +00:00
|
|
|
slog.Info(fmt.Sprintf("Dynamic LLM libraries %v", variants))
|
|
|
|
slog.Debug("Override detection logic by setting OLLAMA_LLM_LIBRARY")
|
2024-01-05 20:13:08 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-03-08 17:45:55 +00:00
|
|
|
func extractDynamicLibs(payloadsDir, glob string) ([]string, error) {
|
2024-01-05 20:13:08 +00:00
|
|
|
files, err := fs.Glob(libEmbed, glob)
|
|
|
|
if err != nil || len(files) == 0 {
|
|
|
|
return nil, payloadMissing
|
|
|
|
}
|
|
|
|
libs := []string{}
|
|
|
|
|
2024-01-13 00:28:00 +00:00
|
|
|
g := new(errgroup.Group)
|
2024-01-05 20:13:08 +00:00
|
|
|
for _, file := range files {
|
|
|
|
pathComps := strings.Split(file, "/")
|
|
|
|
if len(pathComps) != pathComponentCount {
|
2024-01-18 18:52:01 +00:00
|
|
|
slog.Error(fmt.Sprintf("unexpected payload components: %v", pathComps))
|
2024-01-05 20:13:08 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-01-13 00:28:00 +00:00
|
|
|
file := file
|
|
|
|
g.Go(func() error {
|
|
|
|
// llama.cpp/build/$OS/$GOARCH/$VARIANT/lib/$LIBRARY
|
|
|
|
// Include the variant in the path to avoid conflicts between multiple server libs
|
2024-03-08 17:45:55 +00:00
|
|
|
targetDir := filepath.Join(payloadsDir, pathComps[pathComponentCount-3])
|
2024-01-13 00:28:00 +00:00
|
|
|
srcFile, err := libEmbed.Open(file)
|
2024-01-05 20:13:08 +00:00
|
|
|
if err != nil {
|
2024-01-13 00:28:00 +00:00
|
|
|
return fmt.Errorf("read payload %s: %v", file, err)
|
2024-01-05 20:13:08 +00:00
|
|
|
}
|
2024-01-13 00:28:00 +00:00
|
|
|
defer srcFile.Close()
|
|
|
|
if err := os.MkdirAll(targetDir, 0o755); err != nil {
|
2024-03-08 17:45:55 +00:00
|
|
|
return fmt.Errorf("create payload lib dir %s: %v", payloadsDir, err)
|
2024-01-05 20:13:08 +00:00
|
|
|
}
|
2024-01-13 00:28:00 +00:00
|
|
|
src := io.Reader(srcFile)
|
|
|
|
filename := file
|
2024-01-19 18:23:03 +00:00
|
|
|
if strings.HasSuffix(file, ".gz") {
|
|
|
|
src, err = gzip.NewReader(src)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("decompress payload %s: %v", file, err)
|
|
|
|
}
|
|
|
|
filename = strings.TrimSuffix(filename, ".gz")
|
2024-01-13 00:28:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
destFile := filepath.Join(targetDir, filepath.Base(filename))
|
|
|
|
if strings.Contains(destFile, "server") {
|
|
|
|
libs = append(libs, destFile)
|
|
|
|
}
|
|
|
|
|
2024-02-16 01:15:09 +00:00
|
|
|
destFp, err := os.OpenFile(destFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o755)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("write payload %s: %v", file, err)
|
|
|
|
}
|
|
|
|
defer destFp.Close()
|
|
|
|
if _, err := io.Copy(destFp, src); err != nil {
|
|
|
|
return fmt.Errorf("copy payload %s: %v", file, err)
|
2024-01-13 00:28:00 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2024-01-05 20:13:08 +00:00
|
|
|
}
|
2024-03-13 18:43:45 +00:00
|
|
|
err = g.Wait()
|
|
|
|
if err != nil {
|
|
|
|
// If we fail to extract, the payload dir is unusable, so cleanup whatever we extracted
|
|
|
|
gpu.Cleanup()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return libs, nil
|
2024-01-05 20:13:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func verifyDriverAccess() error {
|
|
|
|
if runtime.GOOS != "linux" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Only check ROCm access if we have the dynamic lib loaded
|
2024-01-10 04:29:58 +00:00
|
|
|
if rocmDynLibPresent() {
|
2024-01-05 20:13:08 +00:00
|
|
|
// Verify we have permissions - either running as root, or we have group access to the driver
|
|
|
|
fd, err := os.OpenFile("/dev/kfd", os.O_RDWR, 0666)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, fs.ErrPermission) {
|
|
|
|
return fmt.Errorf("Radeon card detected, but permissions not set up properly. Either run ollama as root, or add you user account to the render group.")
|
|
|
|
} else if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
// expected behavior without a radeon card
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("failed to check permission on /dev/kfd: %w", err)
|
|
|
|
}
|
|
|
|
fd.Close()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|