shim_ext_server.go 6.08 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//go:build !darwin

package llm

/*

#include <stdlib.h>
#include "rocm_shim.h"

*/
import "C"
import (
	"context"
	"embed"
	"errors"
	"fmt"
	"io"
	"io/fs"
	"log"
	"os"
	"path/filepath"
	"runtime"
	"sync"
	"unsafe"

	"github.com/jmorganca/ollama/api"
)

//go:embed llama.cpp/gguf/build/*/lib/*
var libEmbed embed.FS

var RocmShimMissing = fmt.Errorf("ROCm shim library not included in this build of ollama. Radeon GPUs are not supported")
var NoShim = true

type shimExtServer struct {
	s       C.struct_rocm_llama_server
	options api.Options
}

// Note: current implementation does not support concurrent instantiations
var shimMutex sync.Mutex
var llm *shimExtServer

func (llm *shimExtServer) llama_server_init(sparams *C.ext_server_params_t, err *C.ext_server_resp_t) {
	C.rocm_shim_llama_server_init(llm.s, sparams, err)
}
func (llm *shimExtServer) llama_server_start() {
	C.rocm_shim_llama_server_start(llm.s)
}
func (llm *shimExtServer) llama_server_stop() {
	C.rocm_shim_llama_server_stop(llm.s)
}

func (llm *shimExtServer) llama_server_completion(json_req *C.char, resp *C.ext_server_resp_t) {
	C.rocm_shim_llama_server_completion(llm.s, json_req, resp)
}
func (llm *shimExtServer) llama_server_completion_next_result(task_id C.int, resp *C.ext_server_task_result_t) {
	C.rocm_shim_llama_server_completion_next_result(llm.s, task_id, resp)
}
func (llm *shimExtServer) llama_server_completion_cancel(task_id C.int, err *C.ext_server_resp_t) {
	C.rocm_shim_llama_server_completion_cancel(llm.s, task_id, err)
}
func (llm *shimExtServer) llama_server_release_task_result(result *C.ext_server_task_result_t) {
	C.rocm_shim_llama_server_release_task_result(llm.s, result)
}

func (llm *shimExtServer) llama_server_tokenize(json_req *C.char, json_resp **C.char, err *C.ext_server_resp_t) {
	C.rocm_shim_llama_server_tokenize(llm.s, json_req, json_resp, err)
}
func (llm *shimExtServer) llama_server_detokenize(json_req *C.char, json_resp **C.char, err *C.ext_server_resp_t) {
	C.rocm_shim_llama_server_detokenize(llm.s, json_req, json_resp, err)
}
func (llm *shimExtServer) llama_server_embedding(json_req *C.char, json_resp **C.char, err *C.ext_server_resp_t) {
	C.rocm_shim_llama_server_embedding(llm.s, json_req, json_resp, err)
}
func (llm *shimExtServer) llama_server_release_json_resp(json_resp **C.char) {
	C.rocm_shim_llama_server_release_json_resp(llm.s, json_resp)
}

func newRocmShimExtServer(model string, adapters, projectors []string, numLayers int64, opts api.Options) (extServer, error) {
	if NoShim {
		return nil, RocmShimMissing
	}
	log.Printf("Loading ROCM llm server")
	if llm == nil {
		return nil, fmt.Errorf("nativeInit wasnt called or libary load failed")
	}
	llm.options = opts
	return newExtServer(llm, model, adapters, projectors, numLayers, opts)
}

func (llm *shimExtServer) Predict(ctx context.Context, pred PredictOpts, fn func(PredictResult)) error {
	return predict(llm, llm.options, ctx, pred, fn)
}

func (llm *shimExtServer) Encode(ctx context.Context, prompt string) ([]int, error) {
	return encode(llm, ctx, prompt)
}

func (llm *shimExtServer) Decode(ctx context.Context, tokens []int) (string, error) {
	return decode(llm, ctx, tokens)
}

func (llm *shimExtServer) Embedding(ctx context.Context, input string) ([]float64, error) {
	return embedding(llm, ctx, input)
}

func (llm *shimExtServer) Close() {
	close(llm)
}

func nativeInit(workdir string) error {
	err := extractLib(workdir)
	if err != nil {
		if err == RocmShimMissing {
			log.Printf("%s", err)
			return nil
		}
		return err
	}

	// 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) {
			log.Fatalf("Radeon card detected, but permissions not set up properly.  Either run ollama as root, or add you user account to the render group.")
			return err
		} 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()

	shimMutex.Lock()
	defer shimMutex.Unlock()
	if llm != nil {
		return nil
	}
	var libName string
	switch runtime.GOOS {
	case "darwin":
		// shouldn't happen
		return nil
	case "linux":
		libName = "librocm_server.so"
	case "windows":
		libName = "rocm_server.dll"
	default:
		// shouldn't happen
		return nil
	}
	libPath := C.CString(filepath.Join(workdir, libName))
	defer C.free(unsafe.Pointer(libPath))
	resp := newExtServerResp(128)
	defer freeExtServerResp(resp)
	var srv C.struct_rocm_llama_server
	C.rocm_shim_init(libPath, &srv, &resp)
	if resp.id < 0 {
		// TODO - consider softening this failure mode to allow fall-back to the CUDA based built-in llm
		//        and run against CPU
		return fmt.Errorf("Unable to load AMD GPU library: %s", C.GoString(resp.msg))
	}
	llm = &shimExtServer{
		s:       srv,
		options: api.DefaultOptions(),
	}
	return nil
}

func extractLib(workDir string) error {
	files, err := fs.Glob(libEmbed, "llama.cpp/gguf/build/*/lib/*rocm_server*")
	if err != nil || len(files) == 0 {
		// this is expected, ollama may be compiled without shim library packed in
		return RocmShimMissing
	}

	if len(files) != 1 {
		// Shouldn't happen, but just use the first one we find
		log.Printf("WARNING: multiple rocm libraries detected - using %s", files[0])
	}

	srcFile, err := libEmbed.Open(files[0])
	if err != nil {
		return fmt.Errorf("read ROCm shim %s: %v", files[0], err)
	}
	defer srcFile.Close()
	if err := os.MkdirAll(workDir, 0o755); err != nil {
		return fmt.Errorf("create ROCm shim temp dir %s: %v", workDir, err)
	}

	destFile := filepath.Join(workDir, filepath.Base(files[0]))

	_, err = os.Stat(destFile)
	switch {
	case errors.Is(err, os.ErrNotExist):
		destFile, err := os.OpenFile(destFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o755)
		if err != nil {
			return fmt.Errorf("write ROCm shim %s: %v", files[0], err)
		}
		defer destFile.Close()
		if _, err := io.Copy(destFile, srcFile); err != nil {
			return fmt.Errorf("copy ROCm shim %s: %v", files[0], err)
		}
	case err != nil:
		return fmt.Errorf("stat ROCm shim %s: %v", files[0], err)
	}
	NoShim = false
	return nil
}