options.go 9.3 KB

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