-
Notifications
You must be signed in to change notification settings - Fork 371
/
Copy pathLLamaInteractExecutor.cs
358 lines (316 loc) · 13.5 KB
/
LLamaInteractExecutor.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
using LLama.Common;
using LLama.Native;
using LLama.Abstractions;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using LLama.Exceptions;
using LLama.Sampling;
using Microsoft.Extensions.Logging;
namespace LLama
{
/// <summary>
/// The LLama executor for interactive mode.
/// </summary>
public class InteractiveExecutor : StatefulExecutorBase
{
private bool _is_prompt_run = true;
// LLava
private int _EmbedImagePosition = -1;
private List<SafeLlavaImageEmbedHandle> _imageEmbedHandles = new List<SafeLlavaImageEmbedHandle>();
private bool _imageInPrompt = false;
private ISamplingPipeline? _pipeline;
/// <summary>
///
/// </summary>
/// <param name="context"></param>
/// <param name="logger"></param>
public InteractiveExecutor(LLamaContext context, ILogger? logger = null)
: base(context, logger)
{
}
public InteractiveExecutor(LLamaContext context, LLavaWeights clipModel, ILogger? logger = null)
: base(context, clipModel, logger)
{
}
/// <inheritdoc />
public override ExecutorBaseState GetStateData()
{
InteractiveExecutorState state = new()
{
ConsumedSessionCount = _n_session_consumed,
EmbedInps = _embed_inps.ToArray(),
IsPromptRun = _is_prompt_run,
ConsumedTokensCount = _consumedTokensCount,
Embeds = _embeds.ToArray(),
LastTokens = _last_n_tokens.ToArray(),
MatchingSessionTokensCount = _n_matching_session_tokens,
PastTokensCount = _pastTokensCount,
SessionFilePath = _pathSession,
SessionTokens = _session_tokens.ToArray(),
LastTokensCapacity = _last_n_tokens.Capacity,
};
return state;
}
/// <inheritdoc />
public override Task LoadState(ExecutorBaseState data)
{
if (data is InteractiveExecutorState state)
{
_n_session_consumed = state.ConsumedSessionCount;
_embed_inps = state.EmbedInps.ToList();
_is_prompt_run = state.IsPromptRun;
_consumedTokensCount = state.ConsumedTokensCount;
_embeds = state.Embeds.ToList();
_last_n_tokens = new FixedSizeQueue<LLamaToken>(state.LastTokensCapacity, state.LastTokens);
_n_matching_session_tokens = state.MatchingSessionTokensCount;
_pastTokensCount = state.PastTokensCount;
_pathSession = state.SessionFilePath;
_session_tokens = state.SessionTokens.ToList();
}
else
throw new ArgumentException("Invalid state data type.");
return Task.CompletedTask;
}
/// <inheritdoc />
public override async Task SaveState(string filename)
{
var state = (InteractiveExecutorState)GetStateData();
using(var fs = new FileStream(filename, FileMode.Create, FileAccess.Write))
{
await JsonSerializer.SerializeAsync(fs, state);
}
}
/// <inheritdoc />
public override async Task LoadState(string filename)
{
using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
var state = await JsonSerializer.DeserializeAsync<InteractiveExecutorState>(fs);
await LoadState(state);
}
}
/// <summary>
/// Define whether to continue the loop to generate responses.
/// </summary>
/// <returns></returns>
protected override Task<bool> GetLoopCondition(InferStateArgs args)
{
return Task.FromResult(args.RemainedTokens != 0 && !args.WaitForInput || _is_prompt_run);
}
/// <inheritdoc />
protected override Task PreprocessInputs(string? text, InferStateArgs args)
{
if (_is_prompt_run)
{
// When running the first input (prompt) in interactive mode, we should specially process it.
if (text == null) throw new ArgumentException("Prompt cannot be null to trigger continuation if a prompt has not been provided previously.");
if (!this.IsMultiModal)
{
_embed_inps = Context.Tokenize(text, true, true).ToList();
}
else
{
PreprocessLlava(text, args, true);
}
}
else
{
// Don't add any tokens if continuation is requested (by providing a null prompt)
if (text != null)
{
if (!text.EndsWith("\n"))
{
text += "\n";
}
if (!this.IsMultiModal)
{
var line_inp = Context.Tokenize(text, false, true);
_embed_inps.AddRange(line_inp);
args.RemainedTokens -= line_inp.Length;
}
else
{
PreprocessLlava(text, args, false);
}
}
}
return Task.CompletedTask;
}
/// <inheritdoc />
private Task PreprocessLlava(string text, InferStateArgs args, bool addBos = true )
{
int usedTokens = 0;
// If the prompt contains the tag <image> extract this.
_imageInPrompt = text.Contains("<image>");
if (_imageInPrompt && IsMultiModal )
{
foreach (var image in Images)
{
_imageEmbedHandles.Add(SafeLlavaImageEmbedHandle.CreateFromMemory(ClipModel.NativeHandle, Context, image));
}
int imageIndex = text.IndexOf("<image>");
// Tokenize segment 1 (before <image> tag)
string preImagePrompt = text.Substring(0, imageIndex);
var segment1 = Context.Tokenize(preImagePrompt, addBos, true);
// Remember the position to add the image embeddings
_EmbedImagePosition = segment1.Length;
string postImagePrompt = text.Substring(imageIndex + 7);
var segment2 = Context.Tokenize(postImagePrompt, false, true);
_embed_inps.AddRange(segment1);
_embed_inps.AddRange(segment2);
usedTokens += (segment1.Length + segment2.Length);
}
else
{
if (addBos)
{
_embed_inps = Context.Tokenize(text, true, true).ToList();
}
else
{
var line_inp = Context.Tokenize(text, false, true);
_embed_inps.AddRange(line_inp);
args.RemainedTokens -= line_inp.Length;
}
}
return Task.CompletedTask;
}
/// <summary>
/// Return whether to break the generation.
/// </summary>
/// <param name="inferenceParams"></param>
/// <param name="args"></param>
/// <returns></returns>
protected override async Task<(bool, IReadOnlyList<string>)> PostProcess(IInferenceParams inferenceParams, InferStateArgs args)
{
if (_embed_inps.Count <= _consumedTokensCount)
{
if (_last_n_tokens.TokensEndsWithAnyString(args.Antiprompts, Context.NativeHandle.ModelHandle, Context.Encoding))
args.WaitForInput = true;
if (_pastTokensCount > 0 && args.WaitForInput)
return (true, Array.Empty<string>());
}
if (_embeds.Count > 0 && _embeds.Last() == Context.Tokens.EOS)
{
return (true, new[] { " [end of text]\n" });
}
if (args.RemainedTokens <= 0 && inferenceParams.MaxTokens != -1)
{
args.RemainedTokens = inferenceParams.MaxTokens;
args.WaitForInput = true;
}
return (false, Array.Empty<string>());
}
/// <inheritdoc />
protected override async Task InferInternal(IInferenceParams inferenceParams, InferStateArgs args)
{
var batch = new LLamaBatch();
if (_embeds.Count > 0)
{
_is_prompt_run = false;
if (_pastTokensCount + _embeds.Count > Context.ContextSize)
{
// number of tokens to keep when resetting context
// Ported from https://github.com/ggerganov/llama.cpp/blob/60325fa56f61c228464c9f065db3aa6a61f2156e/examples/main/main.cpp#L334
var tokensToKeep = inferenceParams.TokensKeep;
if (tokensToKeep < 0 || tokensToKeep > _embed_inps.Count)
{
tokensToKeep = _embed_inps.Count;
}
else
{
tokensToKeep += Convert.ToInt32(Context.ShouldAddBosToken()); // always keep the BOS token
}
HandleRunOutOfContext(tokensToKeep);
}
TryReuseMatchingPrefix();
// Changes to support Multi-Modal LLMs.
//
(DecodeResult, int, int) header, end, result;
if (IsMultiModal && _EmbedImagePosition > 0)
{
// Tokens previous to the images
header = await Context.DecodeAsync(_embeds.GetRange(0, _EmbedImagePosition), LLamaSeqId.Zero, batch, _pastTokensCount);
_pastTokensCount = header.Item3;
if (header.Item1 != DecodeResult.Ok) throw new LLamaDecodeError(header.Item1);
// Images
foreach( var image in _imageEmbedHandles )
ClipModel.EvalImageEmbed(Context, image, ref _pastTokensCount);
// Post-image Tokens
end = await Context.DecodeAsync(_embeds.GetRange(_EmbedImagePosition, _embeds.Count - _EmbedImagePosition), LLamaSeqId.Zero, batch, _pastTokensCount);
_pastTokensCount = end.Item3;
_EmbedImagePosition = -1;
_imageEmbedHandles.Clear();
Images.Clear();
}
else
{
result = await Context.DecodeAsync(_embeds, LLamaSeqId.Zero, batch, _pastTokensCount);
_pastTokensCount = result.Item3;
if (result.Item1 != DecodeResult.Ok) throw new LLamaDecodeError(result.Item1);
}
if (_embeds.Count > 0 && !string.IsNullOrEmpty(_pathSession))
{
_session_tokens.AddRange(_embeds);
_n_session_consumed = _session_tokens.Count;
}
}
_embeds.Clear();
if (_embed_inps.Count <= _consumedTokensCount && !args.WaitForInput)
{
// optionally save the session on first sample (for faster prompt loading next time)
if (!string.IsNullOrEmpty(_pathSession) && args.NeedToSaveSession)
{
args.NeedToSaveSession = false;
SaveSessionFile(_pathSession);
}
// Sample with the pipeline
var id = inferenceParams.SamplingPipeline.Sample(Context.NativeHandle, batch.TokenCount - 1);
_last_n_tokens.Enqueue(id);
if (id == Context.NativeHandle.ModelHandle.Tokens.EOS)
{
id = Context.NativeHandle.ModelHandle.Tokens.Newline!.Value;
if (args.Antiprompts is not null && args.Antiprompts.Count > 0)
{
var first_antiprompt = Context.Tokenize(args.Antiprompts[0], false);
_embed_inps.AddRange(first_antiprompt);
}
}
_embeds.Add(id);
args.RemainedTokens--;
args.ReturnValue = true;
}
else
{
while (_embed_inps.Count > _consumedTokensCount)
{
_embeds.Add(_embed_inps[_consumedTokensCount]);
_last_n_tokens.Enqueue(_embed_inps[_consumedTokensCount]);
_consumedTokensCount++;
if (_embeds.Count >= Context.BatchSize)
{
break;
}
}
}
return;
}
/// <summary>
/// The descriptor of the state of the interactive executor.
/// </summary>
public class InteractiveExecutorState
: ExecutorBaseState
{
/// <summary>
/// Whether the executor is running for the first time (running the prompt).
/// </summary>
[JsonPropertyName("is_prompt_run")]
public bool IsPromptRun { get; set; }
}
}
}