-
Notifications
You must be signed in to change notification settings - Fork 0
/
structs.go
360 lines (304 loc) · 9.67 KB
/
structs.go
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
package common
// Note: These intentionally are taken from go-llama.cpp initially!
// This should generally be a _superset_ of all allowed configuration options.
// A backend can safely _ignore_ something!
// As I march though the go-* projects, I will add anything that is currently missing
type InitializationOptions struct {
ContextSize int
Seed int
NBatch int
F16Memory bool
MLock bool
MMap bool
NUMA bool
Embeddings bool
NGPULayers int
MainGPU string
TensorSplit string
LowVRAM bool
VocabOnly bool
}
type PredictTextOptions struct {
Seed, Threads, Tokens, TopK, Repeat, Batch, NKeep int
TopP, Temperature, Penalty float64
F16KV bool
DebugMode bool
StopPrompts []string
IgnoreEOS bool
TailFreeSamplingZ float64
TypicalP float64
FrequencyPenalty float64
PresencePenalty float64
Mirostat int
MirostatETA float64
MirostatTAU float64
PenalizeNL bool
LogitBias string
TokenCallback func(string) bool
PathPromptCache string
MLock, MMap, PromptCacheAll bool
PromptCacheRO bool
MainGPU string
TensorSplit string
}
// ==== Setters ====
type PredictTextOptionSetter func(p *PredictTextOptions)
type InitializationOptionSetter func(p *InitializationOptions)
// SetContext sets the context size.
func SetContext(c int) InitializationOptionSetter {
return func(p *InitializationOptions) {
p.ContextSize = c
}
}
func SetModelSeed(c int) InitializationOptionSetter {
return func(p *InitializationOptions) {
p.Seed = c
}
}
// SetContext sets the context size.
func SetMMap(b bool) InitializationOptionSetter {
return func(p *InitializationOptions) {
p.MMap = b
}
}
// SetNBatch sets the n_Batch
func SetNBatch(n_batch int) InitializationOptionSetter {
return func(p *InitializationOptions) {
p.NBatch = n_batch
}
}
// Set sets the tensor split for the GPU
func SetTensorSplit(maingpu string) InitializationOptionSetter {
return func(p *InitializationOptions) {
p.TensorSplit = maingpu
}
}
// SetMainGPU sets the main_gpu
func SetMainGPU(maingpu string) InitializationOptionSetter {
return func(p *InitializationOptions) {
p.MainGPU = maingpu
}
}
// SetPredictionTensorSplit sets the tensor split for the GPU
func SetPredictionTensorSplit(maingpu string) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.TensorSplit = maingpu
}
}
// SetPredictionMainGPU sets the main_gpu
func SetPredictionMainGPU(maingpu string) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.MainGPU = maingpu
}
}
var EnableEmbeddings InitializationOptionSetter = func(p *InitializationOptions) {
p.Embeddings = true
}
var EnableF16Memory InitializationOptionSetter = func(p *InitializationOptions) {
p.F16Memory = true
}
var EnableF16KV PredictTextOptionSetter = func(p *PredictTextOptions) {
p.F16KV = true
}
var Debug PredictTextOptionSetter = func(p *PredictTextOptions) {
p.DebugMode = true
}
var EnablePromptCacheAll PredictTextOptionSetter = func(p *PredictTextOptions) {
p.PromptCacheAll = true
}
var EnablePromptCacheRO PredictTextOptionSetter = func(p *PredictTextOptions) {
p.PromptCacheRO = true
}
var EnableMLock InitializationOptionSetter = func(p *InitializationOptions) {
p.MLock = true
}
var EnableLowVRAM InitializationOptionSetter = func(p *InitializationOptions) {
p.LowVRAM = true
}
var EnableNUMA InitializationOptionSetter = func(p *InitializationOptions) {
p.NUMA = true
}
var VocabOnly InitializationOptionSetter = func(p *InitializationOptions) {
p.VocabOnly = true
}
var IgnoreEOS PredictTextOptionSetter = func(p *PredictTextOptions) {
p.IgnoreEOS = true
}
// SetMlock sets the memory lock.
func SetMlock(b bool) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.MLock = b
}
}
// SetMemoryMap sets memory mapping.
func SetMemoryMap(b bool) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.MMap = b
}
}
// SetGPULayers sets the number of GPU layers to use to offload computation
func SetGPULayers(n int) InitializationOptionSetter {
return func(p *InitializationOptions) {
p.NGPULayers = n
}
}
// SetTokenCallback sets the prompts that will stop predictions.
func SetTokenCallback(fn func(string) bool) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.TokenCallback = fn
}
}
// SetStopWords sets the prompts that will stop predictions.
func SetStopWords(stop ...string) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.StopPrompts = stop
}
}
// SetSeed sets the random seed for sampling text generation.
func SetSeed(seed int) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.Seed = seed
}
}
// SetThreads sets the number of threads to use for text generation.
func SetThreads(threads int) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.Threads = threads
}
}
// SetTokens sets the number of tokens to generate.
func SetTokens(tokens int) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.Tokens = tokens
}
}
// SetTopK sets the value for top-K sampling.
func SetTopK(topk int) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.TopK = topk
}
}
// SetTopP sets the value for nucleus sampling.
func SetTopP(topp float64) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.TopP = topp
}
}
// SetTemperature sets the temperature value for text generation.
func SetTemperature(temp float64) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.Temperature = temp
}
}
// SetPathPromptCache sets the session file to store the prompt cache.
func SetPathPromptCache(f string) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.PathPromptCache = f
}
}
// SetPenalty sets the repetition penalty for text generation.
func SetPenalty(penalty float64) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.Penalty = penalty
}
}
// SetRepeat sets the number of times to repeat text generation.
func SetRepeat(repeat int) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.Repeat = repeat
}
}
// SetBatch sets the batch size.
func SetBatch(size int) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.Batch = size
}
}
// SetKeep sets the number of tokens from initial prompt to keep.
func SetNKeep(n int) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.NKeep = n
}
}
// SetTailFreeSamplingZ sets the tail free sampling, parameter z.
func SetTailFreeSamplingZ(tfz float64) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.TailFreeSamplingZ = tfz
}
}
// SetTypicalP sets the typicality parameter, p_typical.
func SetTypicalP(tp float64) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.TypicalP = tp
}
}
// SetFrequencyPenalty sets the frequency penalty parameter, freq_penalty.
func SetFrequencyPenalty(fp float64) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.FrequencyPenalty = fp
}
}
// SetPresencePenalty sets the presence penalty parameter, presence_penalty.
func SetPresencePenalty(pp float64) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.PresencePenalty = pp
}
}
// SetMirostat sets the mirostat parameter.
func SetMirostat(m int) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.Mirostat = m
}
}
// SetMirostatETA sets the mirostat ETA parameter.
func SetMirostatETA(me float64) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.MirostatETA = me
}
}
// SetMirostatTAU sets the mirostat TAU parameter.
func SetMirostatTAU(mt float64) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.MirostatTAU = mt
}
}
// SetPenalizeNL sets whether to penalize newlines or not.
func SetPenalizeNL(pnl bool) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.PenalizeNL = pnl
}
}
// SetLogitBias sets the logit bias parameter.
func SetLogitBias(lb string) PredictTextOptionSetter {
return func(p *PredictTextOptions) {
p.LogitBias = lb
}
}
// ==== Mergers ====
// Create a new PredictTextOptions object with the given options.
func MergePredictTextOptions(p *PredictTextOptions, opts ...PredictTextOptionSetter) *PredictTextOptions {
for _, opt := range opts {
opt(p)
}
return p
}
func GetMergePredictTextOptionsFnFromDefault(defaultOptions PredictTextOptions) func(opts ...PredictTextOptionSetter) *PredictTextOptions {
return func(opts ...PredictTextOptionSetter) *PredictTextOptions {
optionCopy := defaultOptions
return MergePredictTextOptions(&optionCopy, opts...)
}
}
// Create a new InitializationOptions object with the given options.
func MergeInitializationOptions(p *InitializationOptions, opts ...InitializationOptionSetter) *InitializationOptions {
for _, opt := range opts {
opt(p)
}
return p
}
// Explicitly pass by value to copy the default rather than modify it.
func GetMergeInitializationOptionsFnFromDefault(defaultOptions InitializationOptions) func(opts ...InitializationOptionSetter) *InitializationOptions {
return func(opts ...InitializationOptionSetter) *InitializationOptions {
optionCopy := defaultOptions
return MergeInitializationOptions(&optionCopy, opts...)
}
}