-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLlamaModel.cs
372 lines (315 loc) · 14.8 KB
/
LlamaModel.cs
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
361
362
363
364
365
366
367
368
369
370
371
372
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using UnityEngine;
namespace Abuksigun.LlamaCpp
{
public sealed class LlamaModel : IDisposable
{
public class LlamaException : Exception
{
public LlamaException(string message) : base(message) { }
}
IntPtr modelPointer;
IntPtr contextPointer;
readonly CancellationTokenSource disposeCancellationTokenSource = new();
public IntPtr NativeModelPointer => modelPointer;
public IntPtr NativeContextPointer => contextPointer;
public int EosToken => LlamaLibrary.llama_token_eos(modelPointer);
public int ContextSize => LlamaLibrary.llama_n_ctx(contextPointer);
public int VocabLength => LlamaLibrary.llama_n_vocab(modelPointer);
public static async Task<LlamaModel> LoadModel(string modelPath, IProgress<float> progress, uint contextSize = 2048, int gpuLayers = 0)
{
int threadsN = SystemInfo.processorCount;
(IntPtr newModelPointer, IntPtr newContextPointer) = await Task.Run<(IntPtr, IntPtr)>(() =>
{
LlamaLibrary.llama_backend_init(numa: false);
var modelParams = new LlamaLibrary.LlamaModelParams((float progressFloat, IntPtr _) => progress.Report(progressFloat), IntPtr.Zero, gpuLayers);
try
{
IntPtr model = LlamaLibrary.llama_load_model_from_file(modelPath, modelParams);
if (model == IntPtr.Zero)
throw new LlamaException("Failed to load the Llama model");
try
{
var ctxParams = new LlamaLibrary.LlamaContextParams(1234, (uint)threadsN, contextSize: contextSize);
IntPtr ctx = LlamaLibrary.llama_new_context_with_model(model, ctxParams);
if (ctx == IntPtr.Zero)
throw new LlamaException("Failed to create the Llama context");
return (model, ctx);
}
catch
{
LlamaLibrary.llama_free_model(model);
throw;
}
}
catch
{
LlamaLibrary.llama_backend_free();
throw;
}
});
return new LlamaModel(newModelPointer, newContextPointer);
}
LlamaModel(IntPtr modelPointer, IntPtr contextPointer)
{
this.modelPointer = modelPointer;
this.contextPointer = contextPointer;
}
~LlamaModel()
{
Dispose();
}
public void Dispose()
{
if (modelPointer == IntPtr.Zero && contextPointer == IntPtr.Zero)
return;
disposeCancellationTokenSource.Cancel();
if (contextPointer != IntPtr.Zero)
{
LlamaLibrary.llama_free(contextPointer);
contextPointer = IntPtr.Zero;
}
if (modelPointer != IntPtr.Zero)
{
LlamaLibrary.llama_free_model(modelPointer);
LlamaLibrary.llama_backend_free();
modelPointer = IntPtr.Zero;
}
}
public Task<string> RunAsync(string prompt, int outputLength = 32, SamplingParams samplingParams = null, IProgress<string> progress = null, CancellationToken? ct = null)
{
return Task.Run(() => {
var tokenSource = ct != null ? CancellationTokenSource.CreateLinkedTokenSource(disposeCancellationTokenSource.Token, ct.Value) : disposeCancellationTokenSource;
return Run(prompt, contextPointer, outputLength, samplingParams ?? new(), progress, tokenSource.Token);
});
}
string Run(string prompt, IntPtr context, int outputLength, SamplingParams samplingParams, IProgress<string> progress = null, CancellationToken? cancellationToken = null)
{
StringBuilder outputStringBuilder = new StringBuilder();
int eosToken = EosToken;
int[] tokens = TokenizePrompt(prompt, true);
var samplingContext = new LlamaSamplingContext(samplingParams, tokens);
int totalTokens = tokens.Length + outputLength;
if (totalTokens > ContextSize)
throw new LlamaException($"Error: Model context size {ContextSize} tokens can't fit total of {totalTokens} tokens expected");
LlamaLibrary.LlamaBatch batch = CreateBatch(tokens, totalTokens);
int decodeResult = LlamaLibrary.llama_decode(context, batch);
if (decodeResult != 0)
throw new LlamaException($"llama_decode() failed Code: {decodeResult}");
for (int i = batch.n_tokens; i < totalTokens; i++)
{
int newTokenId = SampleToken(samplingContext, batch.n_tokens - 1);
samplingContext.AddToken(newTokenId);
if (newTokenId == eosToken)
break;
// Output the generated text
string tokenText = LlamaTokenToPiece(newTokenId);
outputStringBuilder.Append(tokenText);
progress?.Report(outputStringBuilder.ToString());
batch.n_tokens = 0;
// push this new token for next evaluation
LlamaBatchAdd(ref batch, newTokenId, i, true, 0);
if (cancellationToken?.IsCancellationRequested ?? false)
break;
if (LlamaLibrary.llama_decode(context, batch) != 0)
throw new LlamaException("llama_decode() failed");
}
return outputStringBuilder.ToString();
}
unsafe int SampleTokenGreedy(IntPtr ctx, int idx)
{
LlamaLibrary.LlamaTokenData[] candidates = FindCandidates(ctx, idx);
fixed (LlamaLibrary.LlamaTokenData* pCandidates = candidates)
{
var candidatesArray = new LlamaLibrary.LlamaTokenDataArray
{
data = pCandidates,
size = candidates.Length,
sorted = false
};
// Sample the most likely token
int newTokenId = LlamaLibrary.llama_sample_token_greedy(ctx, ref candidatesArray);
return newTokenId;
}
}
public unsafe LlamaLibrary.LlamaTokenData[] FindCandidates(IntPtr ctx, int idx)
{
IntPtr logitsPtr = LlamaLibrary.llama_get_logits_ith(ctx, idx);
int vocabLength = VocabLength;
LlamaLibrary.LlamaTokenData[] candidates = new LlamaLibrary.LlamaTokenData[vocabLength];
float* logits = (float*)logitsPtr.ToPointer();
for (int j = 0; j < vocabLength; j++)
candidates[j] = new LlamaLibrary.LlamaTokenData { id = j, logit = logits[j], p = 0.0f };
return candidates;
}
public static LlamaLibrary.LlamaBatch CreateBatch(int[] tokens, int size)
{
LlamaLibrary.LlamaBatch batch = LlamaLibrary.llama_batch_init(size, 0, 1);
for (int i = 0; i < tokens.Length; i++)
LlamaBatchAdd(ref batch, tokens[i], i, false, 0);
unsafe
{
// Ensure logits are output for the last token of the prompt
batch.logits[batch.n_tokens - 1] = 1;
}
return batch;
}
public unsafe static void LlamaBatchAdd(ref LlamaLibrary.LlamaBatch batch, int id, int pos, bool logits, params int[] seqIds)
{
batch.token[batch.n_tokens] = id;
batch.pos[batch.n_tokens] = pos;
batch.n_seq_id[batch.n_tokens] = seqIds.Length;
for (int i = 0; i < seqIds.Length; ++i)
{
batch.seq_id[batch.n_tokens][i] = seqIds[i];
}
batch.logits[batch.n_tokens] = logits ? (byte)1 : (byte)0;
batch.n_tokens++;
}
public int[] TokenizePrompt(string prompt, bool addBos)
{
int[] tokens = new int[prompt.Length + (addBos ? 1 : 0)];
int nTokens = LlamaLibrary.llama_tokenize(modelPointer, prompt, prompt.Length, tokens, tokens.Length, addBos, false);
Array.Resize(ref tokens, nTokens);
return tokens;
}
public string LlamaTokenToPiece(int token)
{
const int initialSize = 16;
byte[] buffer = new byte[initialSize];
int nTokens = LlamaLibrary.llama_token_to_piece(modelPointer, token, buffer, buffer.Length);
if (nTokens < 0)
{
Array.Resize(ref buffer, -nTokens);
int check = LlamaLibrary.llama_token_to_piece(modelPointer, token, buffer, buffer.Length);
if (check == -nTokens)
return null;
}
else
{
Array.Resize(ref buffer, nTokens);
}
string result = Encoding.UTF8.GetString(buffer);
return result;
}
public unsafe class LlamaSamplingContext
{
public SamplingParams Params { get; }
public int[] Prev { get; }
public List<LlamaLibrary.LlamaTokenData> Cur { get; } = new();
public LlamaLibrary.LlamaGrammar* Grammar { get; }
public LlamaSamplingContext(SamplingParams parameters, int[] promptTokens)
{
Params = parameters;
int fillLength = Mathf.Max(parameters.NPrev - promptTokens.Length, 0);
int skipLength = Mathf.Max(promptTokens.Length - parameters.NPrev, 0);
Prev = Enumerable.Repeat(0, fillLength).Concat(promptTokens.Skip(skipLength)).ToArray();
}
public void AddToken(int id)
{
for (int i = 0; i < Prev.Length - 1; i++)
Prev[i] = Prev[i + 1];
Prev[Prev.Length - 1] = id;
}
}
public class SamplingParams
{
public float Temp { get; set; } = 0.80f;
public int TopK { get; set; } = 40;
public float TopP { get; set; } = 0.95f;
public float MinP { get; set; } = 0.05f;
public float TfsZ { get; set; } = 1.00f;
public float TypicalP { get; set; } = 1.00f;
public int PenaltyLastN { get; set; } = 64;
public float PenaltyRepeat { get; set; } = 1.10f;
public float PenaltyFreq { get; set; } = 0.00f;
public float PenaltyPresent { get; set; } = 0.00f;
public bool PenalizeNl { get; set; } = true;
public Dictionary<int, float> LogitBias { get; set; } = new Dictionary<int, float>();
public int NPrev { get; set; } = 64;
public int NProbs { get; set; } = 0;
}
public unsafe int SampleToken(LlamaSamplingContext samplingContext, int idx)
{
SamplingParams parameters = samplingContext.Params;
int vocabLength = VocabLength;
float temp = parameters.Temp;
int topK = parameters.TopK <= 0 ? vocabLength : parameters.TopK;
float topP = parameters.TopP;
float minP = parameters.MinP;
float tfsZ = parameters.TfsZ;
float typicalP = parameters.TypicalP;
int penaltyLastN = parameters.PenaltyLastN < 0 ? parameters.NPrev : parameters.PenaltyLastN;
float penaltyRepeat = parameters.PenaltyRepeat;
float penaltyFreq = parameters.PenaltyFreq;
float penaltyPresent = parameters.PenaltyPresent;
bool penalizeNl = parameters.PenalizeNl;
var prev = samplingContext.Prev;
var cur = samplingContext.Cur;
IntPtr logitsPtr = LlamaLibrary.llama_get_logits_ith(contextPointer, idx);
float[] logits = new float[vocabLength];
Marshal.Copy(logitsPtr, logits, 0, vocabLength);
foreach (var bias in parameters.LogitBias)
logits[bias.Key] += bias.Value;
cur.Clear();
for (int tokenID = 0; tokenID < vocabLength; tokenID++)
cur.Add(new LlamaLibrary.LlamaTokenData { id = tokenID, logit = logits[tokenID], p = 0 });
var curArray = cur.ToArray();
fixed (LlamaLibrary.LlamaTokenData* pCurArray = curArray)
{
LlamaLibrary.LlamaTokenDataArray curP = new LlamaLibrary.LlamaTokenDataArray
{
data = pCurArray,
size = cur.Count,
sorted = false
};
if (prev.Length > 0)
{
int nlTokenId = LlamaLibrary.llama_token_nl(modelPointer);
float nlLogit = logits[nlTokenId];
LlamaLibrary.llama_sample_repetition_penalties(contextPointer, &curP, prev, prev.Length, penaltyRepeat, penaltyFreq, penaltyPresent);
// If not penalizing new lines, reset the logit for the newline token
if (!penalizeNl)
{
for (int i = 0; i < curP.size; i++)
{
if (curP.data[i].id == nlTokenId)
{
curP.data[i].logit = nlLogit;
break;
}
}
}
}
int id = 0;
if (temp < 0.0f)
{
LlamaLibrary.llama_sample_softmax(contextPointer, &curP);
id = curP.data[0].id;
}
else if (temp == 0.0f)
{
id = LlamaLibrary.llama_sample_token_greedy(contextPointer, ref curP);
}
else
{
int minKeep = Math.Max(1, parameters.NProbs);
LlamaLibrary.llama_sample_top_k(contextPointer, &curP, topK, minKeep);
LlamaLibrary.llama_sample_tail_free(contextPointer, &curP, tfsZ, minKeep);
LlamaLibrary.llama_sample_typical(contextPointer, &curP, typicalP, minKeep);
LlamaLibrary.llama_sample_top_p(contextPointer, &curP, topP, minKeep);
LlamaLibrary.llama_sample_min_p(contextPointer, &curP, minP, minKeep);
LlamaLibrary.llama_sample_temp(contextPointer, &curP, temp);
id = LlamaLibrary.llama_sample_token(contextPointer, &curP);
}
return id;
}
}
}
}