options.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. type ModelOptions struct {
  20. ContextSize int
  21. Seed int
  22. NBatch int
  23. F16Memory bool
  24. MLock bool
  25. MMap bool
  26. VocabOnly bool
  27. LowVRAM bool
  28. Embeddings bool
  29. NUMA bool
  30. NGPULayers int
  31. MainGPU string
  32. TensorSplit string
  33. }
  34. type PredictOptions struct {
  35. Seed, Threads, Tokens, TopK, Repeat, Batch, NKeep int
  36. TopP, Temperature, Penalty float64
  37. F16KV bool
  38. DebugMode bool
  39. StopPrompts []string
  40. IgnoreEOS bool
  41. TailFreeSamplingZ float64
  42. TypicalP float64
  43. FrequencyPenalty float64
  44. PresencePenalty float64
  45. Mirostat int
  46. MirostatETA float64
  47. MirostatTAU float64
  48. PenalizeNL bool
  49. LogitBias string
  50. TokenCallback func(string) bool
  51. MLock, MMap bool
  52. MainGPU string
  53. TensorSplit string
  54. }
  55. type PredictOption func(p *PredictOptions)
  56. type ModelOption func(p *ModelOptions)
  57. var DefaultModelOptions ModelOptions = ModelOptions{
  58. ContextSize: 512,
  59. Seed: 0,
  60. F16Memory: false,
  61. MLock: false,
  62. Embeddings: false,
  63. MMap: true,
  64. LowVRAM: false,
  65. }
  66. var DefaultOptions PredictOptions = PredictOptions{
  67. Seed: -1,
  68. Threads: 4,
  69. Tokens: 128,
  70. Penalty: 1.1,
  71. Repeat: 64,
  72. Batch: 512,
  73. NKeep: 64,
  74. TopK: 40,
  75. TopP: 0.95,
  76. TailFreeSamplingZ: 1.0,
  77. TypicalP: 1.0,
  78. Temperature: 0.8,
  79. FrequencyPenalty: 0.0,
  80. PresencePenalty: 0.0,
  81. Mirostat: 0,
  82. MirostatTAU: 5.0,
  83. MirostatETA: 0.1,
  84. MMap: true,
  85. }
  86. // SetContext sets the context size.
  87. func SetContext(c int) ModelOption {
  88. return func(p *ModelOptions) {
  89. p.ContextSize = c
  90. }
  91. }
  92. func SetModelSeed(c int) ModelOption {
  93. return func(p *ModelOptions) {
  94. p.Seed = c
  95. }
  96. }
  97. // SetContext sets the context size.
  98. func SetMMap(b bool) ModelOption {
  99. return func(p *ModelOptions) {
  100. p.MMap = b
  101. }
  102. }
  103. // SetNBatch sets the n_Batch
  104. func SetNBatch(n_batch int) ModelOption {
  105. return func(p *ModelOptions) {
  106. p.NBatch = n_batch
  107. }
  108. }
  109. // Set sets the tensor split for the GPU
  110. func SetTensorSplit(maingpu string) ModelOption {
  111. return func(p *ModelOptions) {
  112. p.TensorSplit = maingpu
  113. }
  114. }
  115. // SetMainGPU sets the main_gpu
  116. func SetMainGPU(maingpu string) ModelOption {
  117. return func(p *ModelOptions) {
  118. p.MainGPU = maingpu
  119. }
  120. }
  121. // SetPredictionTensorSplit sets the tensor split for the GPU
  122. func SetPredictionTensorSplit(maingpu string) PredictOption {
  123. return func(p *PredictOptions) {
  124. p.TensorSplit = maingpu
  125. }
  126. }
  127. // SetPredictionMainGPU sets the main_gpu
  128. func SetPredictionMainGPU(maingpu string) PredictOption {
  129. return func(p *PredictOptions) {
  130. p.MainGPU = maingpu
  131. }
  132. }
  133. var VocabOnly ModelOption = func(p *ModelOptions) {
  134. p.VocabOnly = true
  135. }
  136. var EnabelLowVRAM ModelOption = func(p *ModelOptions) {
  137. p.LowVRAM = true
  138. }
  139. var EnableNUMA ModelOption = func(p *ModelOptions) {
  140. p.NUMA = true
  141. }
  142. var EnableEmbeddings ModelOption = func(p *ModelOptions) {
  143. p.Embeddings = true
  144. }
  145. var EnableF16Memory ModelOption = func(p *ModelOptions) {
  146. p.F16Memory = true
  147. }
  148. var EnableF16KV PredictOption = func(p *PredictOptions) {
  149. p.F16KV = true
  150. }
  151. var Debug PredictOption = func(p *PredictOptions) {
  152. p.DebugMode = true
  153. }
  154. var EnableMLock ModelOption = func(p *ModelOptions) {
  155. p.MLock = true
  156. }
  157. // Create a new PredictOptions object with the given options.
  158. func NewModelOptions(opts ...ModelOption) ModelOptions {
  159. p := DefaultModelOptions
  160. for _, opt := range opts {
  161. opt(&p)
  162. }
  163. return p
  164. }
  165. var IgnoreEOS PredictOption = func(p *PredictOptions) {
  166. p.IgnoreEOS = true
  167. }
  168. // SetMlock sets the memory lock.
  169. func SetMlock(b bool) PredictOption {
  170. return func(p *PredictOptions) {
  171. p.MLock = b
  172. }
  173. }
  174. // SetMemoryMap sets memory mapping.
  175. func SetMemoryMap(b bool) PredictOption {
  176. return func(p *PredictOptions) {
  177. p.MMap = b
  178. }
  179. }
  180. // SetGPULayers sets the number of GPU layers to use to offload computation
  181. func SetGPULayers(n int) ModelOption {
  182. return func(p *ModelOptions) {
  183. p.NGPULayers = n
  184. }
  185. }
  186. // SetTokenCallback sets the prompts that will stop predictions.
  187. func SetTokenCallback(fn func(string) bool) PredictOption {
  188. return func(p *PredictOptions) {
  189. p.TokenCallback = fn
  190. }
  191. }
  192. // SetStopWords sets the prompts that will stop predictions.
  193. func SetStopWords(stop ...string) PredictOption {
  194. return func(p *PredictOptions) {
  195. p.StopPrompts = stop
  196. }
  197. }
  198. // SetSeed sets the random seed for sampling text generation.
  199. func SetSeed(seed int) PredictOption {
  200. return func(p *PredictOptions) {
  201. p.Seed = seed
  202. }
  203. }
  204. // SetThreads sets the number of threads to use for text generation.
  205. func SetThreads(threads int) PredictOption {
  206. return func(p *PredictOptions) {
  207. p.Threads = threads
  208. }
  209. }
  210. // SetTokens sets the number of tokens to generate.
  211. func SetTokens(tokens int) PredictOption {
  212. return func(p *PredictOptions) {
  213. p.Tokens = tokens
  214. }
  215. }
  216. // SetTopK sets the value for top-K sampling.
  217. func SetTopK(topk int) PredictOption {
  218. return func(p *PredictOptions) {
  219. p.TopK = topk
  220. }
  221. }
  222. // SetTopP sets the value for nucleus sampling.
  223. func SetTopP(topp float64) PredictOption {
  224. return func(p *PredictOptions) {
  225. p.TopP = topp
  226. }
  227. }
  228. // SetTemperature sets the temperature value for text generation.
  229. func SetTemperature(temp float64) PredictOption {
  230. return func(p *PredictOptions) {
  231. p.Temperature = temp
  232. }
  233. }
  234. // SetPenalty sets the repetition penalty for text generation.
  235. func SetPenalty(penalty float64) PredictOption {
  236. return func(p *PredictOptions) {
  237. p.Penalty = penalty
  238. }
  239. }
  240. // SetRepeat sets the number of times to repeat text generation.
  241. func SetRepeat(repeat int) PredictOption {
  242. return func(p *PredictOptions) {
  243. p.Repeat = repeat
  244. }
  245. }
  246. // SetBatch sets the batch size.
  247. func SetBatch(size int) PredictOption {
  248. return func(p *PredictOptions) {
  249. p.Batch = size
  250. }
  251. }
  252. // SetKeep sets the number of tokens from initial prompt to keep.
  253. func SetNKeep(n int) PredictOption {
  254. return func(p *PredictOptions) {
  255. p.NKeep = n
  256. }
  257. }
  258. // Create a new PredictOptions object with the given options.
  259. func NewPredictOptions(opts ...PredictOption) PredictOptions {
  260. p := DefaultOptions
  261. for _, opt := range opts {
  262. opt(&p)
  263. }
  264. return p
  265. }
  266. // SetTailFreeSamplingZ sets the tail free sampling, parameter z.
  267. func SetTailFreeSamplingZ(tfz float64) PredictOption {
  268. return func(p *PredictOptions) {
  269. p.TailFreeSamplingZ = tfz
  270. }
  271. }
  272. // SetTypicalP sets the typicality parameter, p_typical.
  273. func SetTypicalP(tp float64) PredictOption {
  274. return func(p *PredictOptions) {
  275. p.TypicalP = tp
  276. }
  277. }
  278. // SetFrequencyPenalty sets the frequency penalty parameter, freq_penalty.
  279. func SetFrequencyPenalty(fp float64) PredictOption {
  280. return func(p *PredictOptions) {
  281. p.FrequencyPenalty = fp
  282. }
  283. }
  284. // SetPresencePenalty sets the presence penalty parameter, presence_penalty.
  285. func SetPresencePenalty(pp float64) PredictOption {
  286. return func(p *PredictOptions) {
  287. p.PresencePenalty = pp
  288. }
  289. }
  290. // SetMirostat sets the mirostat parameter.
  291. func SetMirostat(m int) PredictOption {
  292. return func(p *PredictOptions) {
  293. p.Mirostat = m
  294. }
  295. }
  296. // SetMirostatETA sets the mirostat ETA parameter.
  297. func SetMirostatETA(me float64) PredictOption {
  298. return func(p *PredictOptions) {
  299. p.MirostatETA = me
  300. }
  301. }
  302. // SetMirostatTAU sets the mirostat TAU parameter.
  303. func SetMirostatTAU(mt float64) PredictOption {
  304. return func(p *PredictOptions) {
  305. p.MirostatTAU = mt
  306. }
  307. }
  308. // SetPenalizeNL sets whether to penalize newlines or not.
  309. func SetPenalizeNL(pnl bool) PredictOption {
  310. return func(p *PredictOptions) {
  311. p.PenalizeNL = pnl
  312. }
  313. }
  314. // SetLogitBias sets the logit bias parameter.
  315. func SetLogitBias(lb string) PredictOption {
  316. return func(p *PredictOptions) {
  317. p.LogitBias = lb
  318. }
  319. }