llama.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. // MIT License
  2. // Copyright (c) 2023 go-skynet authors
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. // The above copyright notice and this permission notice shall be included in all
  10. // copies or substantial portions of the Software.
  11. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  17. // SOFTWARE.
  18. package llama
  19. // #cgo LDFLAGS: -Lbuild -lbinding -lllama -lm -lggml_static -lstdc++
  20. // #cgo CXXFLAGS: -std=c++11
  21. // #cgo darwin LDFLAGS: -framework Accelerate -framework Foundation -framework Metal -framework MetalKit -framework MetalPerformanceShaders
  22. // #include "binding/binding.h"
  23. // #include <stdlib.h>
  24. import "C"
  25. import (
  26. "fmt"
  27. "strings"
  28. "sync"
  29. "unsafe"
  30. )
  31. type LLama struct {
  32. ctx unsafe.Pointer
  33. embeddings bool
  34. contextSize int
  35. }
  36. func New(model string, opts ...ModelOption) (*LLama, error) {
  37. mo := NewModelOptions(opts...)
  38. modelPath := C.CString(model)
  39. defer C.free(unsafe.Pointer(modelPath))
  40. ctx := C.load_model(modelPath, C.int(mo.ContextSize), C.int(mo.Seed), C.bool(mo.F16Memory), C.bool(mo.MLock), C.bool(mo.Embeddings), C.bool(mo.MMap), C.bool(mo.LowVRAM), C.bool(mo.VocabOnly), C.int(mo.NGPULayers), C.int(mo.NBatch), C.CString(mo.MainGPU), C.CString(mo.TensorSplit), C.bool(mo.NUMA))
  41. if ctx == nil {
  42. return nil, fmt.Errorf("failed loading model")
  43. }
  44. ll := &LLama{ctx: ctx, contextSize: mo.ContextSize, embeddings: mo.Embeddings}
  45. return ll, nil
  46. }
  47. func (l *LLama) Free() {
  48. C.llama_binding_free_model(l.ctx)
  49. }
  50. func (l *LLama) Eval(text string, opts ...PredictOption) error {
  51. po := NewPredictOptions(opts...)
  52. input := C.CString(text)
  53. if po.Tokens == 0 {
  54. po.Tokens = 99999999
  55. }
  56. defer C.free(unsafe.Pointer(input))
  57. reverseCount := len(po.StopPrompts)
  58. reversePrompt := make([]*C.char, reverseCount)
  59. var pass **C.char
  60. for i, s := range po.StopPrompts {
  61. cs := C.CString(s)
  62. reversePrompt[i] = cs
  63. pass = &reversePrompt[0]
  64. defer C.free(unsafe.Pointer(cs))
  65. }
  66. cLogitBias := C.CString(po.LogitBias)
  67. defer C.free(unsafe.Pointer(cLogitBias))
  68. cMainGPU := C.CString(po.MainGPU)
  69. defer C.free(unsafe.Pointer(cMainGPU))
  70. cTensorSplit := C.CString(po.TensorSplit)
  71. defer C.free(unsafe.Pointer(cTensorSplit))
  72. params := C.llama_allocate_params(input, C.int(po.Seed), C.int(po.Threads), C.int(po.Tokens), C.int(po.TopK),
  73. C.float(po.TopP), C.float(po.Temperature), C.float(po.Penalty), C.int(po.Repeat),
  74. C.bool(po.IgnoreEOS), C.bool(po.F16KV),
  75. C.int(po.Batch), C.int(po.NKeep), pass, C.int(reverseCount),
  76. C.float(po.TailFreeSamplingZ), C.float(po.TypicalP), C.float(po.FrequencyPenalty), C.float(po.PresencePenalty),
  77. C.int(po.Mirostat), C.float(po.MirostatETA), C.float(po.MirostatTAU), C.bool(po.PenalizeNL), cLogitBias,
  78. C.bool(po.MLock), C.bool(po.MMap), cMainGPU, cTensorSplit,
  79. )
  80. defer C.llama_free_params(params)
  81. ret := C.eval(params, l.ctx, input)
  82. if ret != 0 {
  83. return fmt.Errorf("inference failed")
  84. }
  85. return nil
  86. }
  87. func (l *LLama) Predict(text string, opts ...PredictOption) (string, error) {
  88. po := NewPredictOptions(opts...)
  89. if po.TokenCallback != nil {
  90. setCallback(l.ctx, po.TokenCallback)
  91. }
  92. input := C.CString(text)
  93. if po.Tokens == 0 {
  94. po.Tokens = 99999999
  95. }
  96. defer C.free(unsafe.Pointer(input))
  97. out := make([]byte, po.Tokens)
  98. reverseCount := len(po.StopPrompts)
  99. reversePrompt := make([]*C.char, reverseCount)
  100. var pass **C.char
  101. for i, s := range po.StopPrompts {
  102. cs := C.CString(s)
  103. reversePrompt[i] = cs
  104. pass = &reversePrompt[0]
  105. defer C.free(unsafe.Pointer(cs))
  106. }
  107. cLogitBias := C.CString(po.LogitBias)
  108. defer C.free(unsafe.Pointer(cLogitBias))
  109. cMainGPU := C.CString(po.MainGPU)
  110. defer C.free(unsafe.Pointer(cMainGPU))
  111. cTensorSplit := C.CString(po.TensorSplit)
  112. defer C.free(unsafe.Pointer(cTensorSplit))
  113. params := C.llama_allocate_params(input, C.int(po.Seed), C.int(po.Threads), C.int(po.Tokens), C.int(po.TopK),
  114. C.float(po.TopP), C.float(po.Temperature), C.float(po.Penalty), C.int(po.Repeat),
  115. C.bool(po.IgnoreEOS), C.bool(po.F16KV),
  116. C.int(po.Batch), C.int(po.NKeep), pass, C.int(reverseCount),
  117. C.float(po.TailFreeSamplingZ), C.float(po.TypicalP), C.float(po.FrequencyPenalty), C.float(po.PresencePenalty),
  118. C.int(po.Mirostat), C.float(po.MirostatETA), C.float(po.MirostatTAU), C.bool(po.PenalizeNL), cLogitBias,
  119. C.bool(po.MLock), C.bool(po.MMap), cMainGPU, cTensorSplit,
  120. )
  121. defer C.llama_free_params(params)
  122. ret := C.llama_predict(params, l.ctx, (*C.char)(unsafe.Pointer(&out[0])), C.bool(po.DebugMode))
  123. if ret != 0 {
  124. return "", fmt.Errorf("inference failed")
  125. }
  126. res := C.GoString((*C.char)(unsafe.Pointer(&out[0])))
  127. res = strings.TrimPrefix(res, " ")
  128. res = strings.TrimPrefix(res, text)
  129. res = strings.TrimPrefix(res, "\n")
  130. for _, s := range po.StopPrompts {
  131. res = strings.TrimRight(res, s)
  132. }
  133. if po.TokenCallback != nil {
  134. setCallback(l.ctx, nil)
  135. }
  136. return res, nil
  137. }
  138. // CGo only allows us to use static calls from C to Go, we can't just dynamically pass in func's.
  139. // This is the next best thing, we register the callbacks in this map and call tokenCallback from
  140. // the C code. We also attach a finalizer to LLama, so it will unregister the callback when the
  141. // garbage collection frees it.
  142. // SetTokenCallback registers a callback for the individual tokens created when running Predict. It
  143. // will be called once for each token. The callback shall return true as long as the model should
  144. // continue predicting the next token. When the callback returns false the predictor will return.
  145. // The tokens are just converted into Go strings, they are not trimmed or otherwise changed. Also
  146. // the tokens may not be valid UTF-8.
  147. // Pass in nil to remove a callback.
  148. //
  149. // It is save to call this method while a prediction is running.
  150. func (l *LLama) SetTokenCallback(callback func(token string) bool) {
  151. setCallback(l.ctx, callback)
  152. }
  153. var (
  154. m sync.Mutex
  155. callbacks = map[uintptr]func(string) bool{}
  156. )
  157. //export tokenCallback
  158. func tokenCallback(statePtr unsafe.Pointer, token *C.char) bool {
  159. m.Lock()
  160. defer m.Unlock()
  161. if callback, ok := callbacks[uintptr(statePtr)]; ok {
  162. return callback(C.GoString(token))
  163. }
  164. return true
  165. }
  166. // setCallback can be used to register a token callback for LLama. Pass in a nil callback to
  167. // remove the callback.
  168. func setCallback(statePtr unsafe.Pointer, callback func(string) bool) {
  169. m.Lock()
  170. defer m.Unlock()
  171. if callback == nil {
  172. delete(callbacks, uintptr(statePtr))
  173. } else {
  174. callbacks[uintptr(statePtr)] = callback
  175. }
  176. }