llm.go 2.47 KB
Newer Older
1
2
3
package llm

import (
4
	"context"
5
	"fmt"
6
	"log"
7
8
	"os"

9
10
	"github.com/pbnjay/memory"

11
12
13
14
	"github.com/jmorganca/ollama/api"
)

type LLM interface {
15
16
17
18
	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)
19
20
	SetOptions(api.Options)
	Close()
21
	Ping(context.Context) error
22
23
}

24
func New(model string, adapters []string, opts api.Options) (LLM, error) {
25
26
27
28
29
30
31
32
	if _, err := os.Stat(model); err != nil {
		return nil, err
	}

	f, err := os.Open(model)
	if err != nil {
		return nil, err
	}
Michael Yang's avatar
Michael Yang committed
33
	defer f.Close()
34
35
36
37
38
39

	ggml, err := DecodeGGML(f, ModelFamilyLlama)
	if err != nil {
		return nil, err
	}

Michael Yang's avatar
Michael Yang committed
40
	switch ggml.FileType().String() {
Michael Yang's avatar
Michael Yang committed
41
	case "F32", "Q5_0", "Q5_1", "Q8_0":
42
		if opts.NumGPU != 0 {
Michael Yang's avatar
Michael Yang committed
43
			// F32, F16, Q5_0, Q5_1, and Q8_0 do not support Metal API and will
44
			// cause the runner to segmentation fault so disable GPU
Michael Yang's avatar
Michael Yang committed
45
			log.Printf("WARNING: GPU disabled for F32, Q5_0, Q5_1, and Q8_0")
46
47
48
49
			opts.NumGPU = 0
		}
	}

50
	totalResidentMemory := memory.TotalMemory()
Michael Yang's avatar
Michael Yang committed
51
	switch ggml.ModelType() {
52
	case ModelType3B, ModelType7B:
Michael Yang's avatar
Michael Yang committed
53
54
55
		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 {
56
57
58
			return nil, fmt.Errorf("model requires at least 8GB of memory")
		}
	case ModelType13B:
Michael Yang's avatar
Michael Yang committed
59
60
61
		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 {
62
63
			return nil, fmt.Errorf("model requires at least 16GB of memory")
		}
Michael Yang's avatar
Michael Yang committed
64
	case ModelType30B, ModelType34B:
Michael Yang's avatar
Michael Yang committed
65
66
67
		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 {
68
69
70
			return nil, fmt.Errorf("model requires at least 32GB of memory")
		}
	case ModelType65B:
Michael Yang's avatar
Michael Yang committed
71
72
73
		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 {
74
75
76
77
			return nil, fmt.Errorf("model requires at least 64GB of memory")
		}
	}

Michael Yang's avatar
Michael Yang committed
78
	switch ggml.ModelFamily() {
79
	case ModelFamilyLlama:
80
		return newLlama(model, adapters, ggmlRunner(), opts)
81
	default:
Michael Yang's avatar
Michael Yang committed
82
		return nil, fmt.Errorf("unknown ggml type: %s", ggml.ModelFamily())
83
84
	}
}