shim_ext_server.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. //go:build !darwin
  2. package llm
  3. /*
  4. #include <stdlib.h>
  5. #include "dynamic_shim.h"
  6. */
  7. import "C"
  8. import (
  9. "context"
  10. "embed"
  11. "errors"
  12. "fmt"
  13. "io/fs"
  14. "log"
  15. "os"
  16. "path/filepath"
  17. "strings"
  18. "sync"
  19. "unsafe"
  20. "github.com/jmorganca/ollama/api"
  21. )
  22. //go:embed llama.cpp/gguf/build/lib/*
  23. var libEmbed embed.FS
  24. var RocmShimMissing = fmt.Errorf("ROCm shim library not included in this build of ollama. Radeon GPUs are not supported")
  25. type shimExtServer struct {
  26. s C.struct_dynamic_llama_server
  27. options api.Options
  28. }
  29. // Note: current implementation does not support concurrent instantiations
  30. var shimMutex sync.Mutex
  31. var llm *shimExtServer
  32. func (llm *shimExtServer) llama_server_init(sparams *C.ext_server_params_t, err *C.ext_server_resp_t) {
  33. C.dynamic_shim_llama_server_init(llm.s, sparams, err)
  34. }
  35. func (llm *shimExtServer) llama_server_start() {
  36. C.dynamic_shim_llama_server_start(llm.s)
  37. }
  38. func (llm *shimExtServer) llama_server_stop() {
  39. C.dynamic_shim_llama_server_stop(llm.s)
  40. }
  41. func (llm *shimExtServer) llama_server_completion(json_req *C.char, resp *C.ext_server_resp_t) {
  42. C.dynamic_shim_llama_server_completion(llm.s, json_req, resp)
  43. }
  44. func (llm *shimExtServer) llama_server_completion_next_result(task_id C.int, resp *C.ext_server_task_result_t) {
  45. C.dynamic_shim_llama_server_completion_next_result(llm.s, task_id, resp)
  46. }
  47. func (llm *shimExtServer) llama_server_completion_cancel(task_id C.int, err *C.ext_server_resp_t) {
  48. C.dynamic_shim_llama_server_completion_cancel(llm.s, task_id, err)
  49. }
  50. func (llm *shimExtServer) llama_server_release_task_result(result *C.ext_server_task_result_t) {
  51. C.dynamic_shim_llama_server_release_task_result(llm.s, result)
  52. }
  53. func (llm *shimExtServer) llama_server_tokenize(json_req *C.char, json_resp **C.char, err *C.ext_server_resp_t) {
  54. C.dynamic_shim_llama_server_tokenize(llm.s, json_req, json_resp, err)
  55. }
  56. func (llm *shimExtServer) llama_server_detokenize(json_req *C.char, json_resp **C.char, err *C.ext_server_resp_t) {
  57. C.dynamic_shim_llama_server_detokenize(llm.s, json_req, json_resp, err)
  58. }
  59. func (llm *shimExtServer) llama_server_embedding(json_req *C.char, json_resp **C.char, err *C.ext_server_resp_t) {
  60. C.dynamic_shim_llama_server_embedding(llm.s, json_req, json_resp, err)
  61. }
  62. func (llm *shimExtServer) llama_server_release_json_resp(json_resp **C.char) {
  63. C.dynamic_shim_llama_server_release_json_resp(llm.s, json_resp)
  64. }
  65. func newDynamicShimExtServer(library, model string, adapters, projectors []string, numLayers int64, opts api.Options) (extServer, error) {
  66. shimMutex.Lock()
  67. defer shimMutex.Unlock()
  68. libPath := C.CString(library)
  69. defer C.free(unsafe.Pointer(libPath))
  70. resp := newExtServerResp(128)
  71. defer freeExtServerResp(resp)
  72. var srv C.struct_dynamic_llama_server
  73. C.dynamic_shim_init(libPath, &srv, &resp)
  74. if resp.id < 0 {
  75. return nil, fmt.Errorf("Unable to load dynamic library: %s", C.GoString(resp.msg))
  76. }
  77. llm = &shimExtServer{
  78. s: srv,
  79. options: opts,
  80. }
  81. log.Printf("Loading Dynamic Shim llm server: %s", library)
  82. return newExtServer(llm, model, adapters, projectors, numLayers, opts)
  83. }
  84. func (llm *shimExtServer) Predict(ctx context.Context, pred PredictOpts, fn func(PredictResult)) error {
  85. return predict(llm, llm.options, ctx, pred, fn)
  86. }
  87. func (llm *shimExtServer) Encode(ctx context.Context, prompt string) ([]int, error) {
  88. return encode(llm, ctx, prompt)
  89. }
  90. func (llm *shimExtServer) Decode(ctx context.Context, tokens []int) (string, error) {
  91. return decode(llm, ctx, tokens)
  92. }
  93. func (llm *shimExtServer) Embedding(ctx context.Context, input string) ([]float64, error) {
  94. return embedding(llm, ctx, input)
  95. }
  96. func (llm *shimExtServer) Close() {
  97. close(llm)
  98. }
  99. func nativeInit(workdir string) error {
  100. libs, err := extractDynamicLibs(workdir, "llama.cpp/gguf/build/lib/*server*")
  101. if err != nil {
  102. if err == payloadMissing {
  103. log.Printf("%s", payloadMissing)
  104. return nil
  105. }
  106. return err
  107. }
  108. for _, lib := range libs {
  109. libName := strings.Split(strings.TrimPrefix(filepath.Base(lib), "lib"), ".")[0]
  110. AvailableShims[libName] = lib
  111. }
  112. // Only check ROCm access if we have the dynamic lib loaded
  113. if _, rocmPresent := AvailableShims["rocm_server"]; rocmPresent {
  114. // Verify we have permissions - either running as root, or we have group access to the driver
  115. fd, err := os.OpenFile("/dev/kfd", os.O_RDWR, 0666)
  116. if err != nil {
  117. if errors.Is(err, fs.ErrPermission) {
  118. 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.")
  119. return err
  120. } else if errors.Is(err, fs.ErrNotExist) {
  121. // expected behavior without a radeon card
  122. return nil
  123. }
  124. return fmt.Errorf("failed to check permission on /dev/kfd: %w", err)
  125. }
  126. fd.Close()
  127. }
  128. return nil
  129. }