Browse Source

Initial approach to clear images

pull/664/head
SignalRT 1 year ago
parent
commit
d6890e4ec4
2 changed files with 29 additions and 9 deletions
  1. +4
    -3
      LLama.Examples/Examples/LlavaInteractiveModeExecute.cs
  2. +25
    -6
      LLama/LLamaInteractExecutor.cs

+ 4
- 3
LLama.Examples/Examples/LlavaInteractiveModeExecute.cs View File

@@ -3,6 +3,7 @@ using LLama.Batched;
using LLama.Common; using LLama.Common;
using Spectre.Console; using Spectre.Console;
using LLama.Abstractions; using LLama.Abstractions;
using LLama.Native;


namespace LLama.Examples.Examples namespace LLama.Examples.Examples
{ {
@@ -21,9 +22,6 @@ namespace LLama.Examples.Examples


var parameters = new ModelParams(modelPath) var parameters = new ModelParams(modelPath)
{ {
ContextSize = 4096,
Seed = 1337,
GpuLayerCount = 10
}; };
using var model = LLamaWeights.LoadFromFile(parameters); using var model = LLamaWeights.LoadFromFile(parameters);
using var context = model.CreateContext(parameters); using var context = model.CreateContext(parameters);
@@ -69,6 +67,9 @@ namespace LLama.Examples.Examples
break; break;
} }


// Each prompt with images we clear cache
// When the prompt contains images we clear KV_CACHE to restart conversation
ex.Context.NativeHandle.KvCacheRemove( LLamaSeqId.Zero, -1, -1 );


int index = 0; int index = 0;
foreach (var path in imagePathsWithCurlyBraces) foreach (var path in imagePathsWithCurlyBraces)


+ 25
- 6
LLama/LLamaInteractExecutor.cs View File

@@ -11,7 +11,7 @@ using System.Threading.Tasks;
using LLama.Exceptions; using LLama.Exceptions;
using LLama.Extensions; using LLama.Extensions;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using System.Net.Http;


namespace LLama namespace LLama
{ {
@@ -136,20 +136,29 @@ namespace LLama
text += "\n"; text += "\n";
} }


var line_inp = Context.Tokenize(text, false);
_embed_inps.AddRange(line_inp);
args.RemainedTokens -= line_inp.Length;
if (!this.IsMultiModal)
{
var line_inp = Context.Tokenize(text, false);
_embed_inps.AddRange(line_inp);
args.RemainedTokens -= line_inp.Length;
}
else
{
PreprocessLlava(text, args, false);
}
} }


return Task.CompletedTask; return Task.CompletedTask;
} }


/// <inheritdoc />
private Task PreprocessLlava(string text, InferStateArgs args, bool addBos = true ) private Task PreprocessLlava(string text, InferStateArgs args, bool addBos = true )
{ {
int usedTokens = 0; int usedTokens = 0;
// If the prompt contains the tag <image> extract this. // If the prompt contains the tag <image> extract this.
_imageInPrompt = text.Contains("<image>"); _imageInPrompt = text.Contains("<image>");
if (_imageInPrompt && ClipModel != null)
if (_imageInPrompt && IsMultiModal )
{ {
foreach (var image in Images) foreach (var image in Images)
{ {
@@ -170,7 +179,16 @@ namespace LLama
} }
else else
{ {
_embed_inps = Context.Tokenize(text, true).ToList();
if (addBos)
{
_embed_inps = Context.Tokenize(text, true).ToList();
}
else
{
var line_inp = Context.Tokenize(text, false);
_embed_inps.AddRange(line_inp);
args.RemainedTokens -= line_inp.Length;
}
} }
return Task.CompletedTask; return Task.CompletedTask;
} }
@@ -239,6 +257,7 @@ namespace LLama


_EmbedImagePosition = -1; _EmbedImagePosition = -1;
_imageEmbedHandles.Clear(); _imageEmbedHandles.Clear();
Images.Clear();
} }
else else
{ {


Loading…
Cancel
Save