|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.IO; |
| 4 | +using LLama.Batched; |
| 5 | +using LLama.Common; |
| 6 | +using LLama.Exceptions; |
| 7 | +using LLama.Native; |
| 8 | +using LLama.Sampling; |
| 9 | +using Spectre.Console; |
| 10 | + |
| 11 | +namespace LLama.Examples.Examples; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// Demonstrates how to evaluate an image with MTMD helpers and continue generation by |
| 15 | +/// manually scheduling batches, similar to what the batched executor does internally. |
| 16 | +/// </summary> |
| 17 | +public class BatchedExecutorMtmd |
| 18 | +{ |
| 19 | + /// <summary> |
| 20 | + /// Number of completion tokens to generate after sending the image prompt. |
| 21 | + /// </summary> |
| 22 | + public const int TokenCount = 64; |
| 23 | + |
| 24 | + public static async Task Run() |
| 25 | + { |
| 26 | + var parameters = new ModelParams(UserSettings.GetModelPath()); |
| 27 | + using var model = await LLamaWeights.LoadFromFileAsync(parameters); |
| 28 | + var mtmdParams = MtmdContextParams.Default(); |
| 29 | + mtmdParams.UseGpu = false; |
| 30 | + var marker = mtmdParams.MediaMarker ?? NativeApi.MtmdDefaultMarker() ?? "<media>"; |
| 31 | + |
| 32 | + using var mtmd = await SafeMtmdWeights.LoadFromFileAsync(UserSettings.GetMMProjPath(), model, mtmdParams); |
| 33 | + |
| 34 | + using var executor = new BatchedExecutor(model, parameters, mtmd); |
| 35 | + |
| 36 | + var defaultPrompt = "\nUSER: Provide a full description of the image.\nASSISTANT: "; |
| 37 | + var promptSuffix = AnsiConsole.Ask("Prompt (or ENTER for default):", defaultPrompt); |
| 38 | + var promptText = string.Concat(marker, promptSuffix); |
| 39 | + |
| 40 | + var imagePath = UserSettings.GetImagePath(); |
| 41 | + AnsiConsole.Write(new CanvasImage(imagePath)); |
| 42 | + |
| 43 | + var vocab = executor.Context.NativeHandle.ModelHandle.Vocab; |
| 44 | + |
| 45 | + var sampler = new DefaultSamplingPipeline |
| 46 | + { |
| 47 | + Temperature = 0.1f |
| 48 | + }; |
| 49 | + |
| 50 | + var decoder = new StreamingTokenDecoder(executor.Context) |
| 51 | + { |
| 52 | + DecodeSpecialTokens = false |
| 53 | + }; |
| 54 | + |
| 55 | + try |
| 56 | + { |
| 57 | + var conversation = executor.Create(); |
| 58 | + conversation.QueueMedia(imagePath); |
| 59 | + conversation.Prompt(promptText, addBos: true, special: true); |
| 60 | + |
| 61 | + Console.ForegroundColor = ConsoleColor.Yellow; |
| 62 | + Console.WriteLine("Prompt queued with multimodal chunks. Generating response...\n"); |
| 63 | + Console.ResetColor(); |
| 64 | + |
| 65 | + var remaining = TokenCount; |
| 66 | + while (remaining > 0) |
| 67 | + { |
| 68 | + var decodeResult = await executor.Infer(); |
| 69 | + if (decodeResult == DecodeResult.NoKvSlot) |
| 70 | + { |
| 71 | + Console.ForegroundColor = ConsoleColor.Red; |
| 72 | + Console.WriteLine("Insufficient KV cache space for multimodal evaluation."); |
| 73 | + Console.ResetColor(); |
| 74 | + break; |
| 75 | + } |
| 76 | + |
| 77 | + if (decodeResult != DecodeResult.Ok) |
| 78 | + throw new RuntimeError($"Failed to evaluate batch: {decodeResult}."); |
| 79 | + |
| 80 | + if (!conversation.RequiresSampling) |
| 81 | + continue; |
| 82 | + |
| 83 | + var token = conversation.Sample(sampler); |
| 84 | + if (token.IsEndOfGeneration(vocab)) |
| 85 | + break; |
| 86 | + |
| 87 | + decoder.Add(token); |
| 88 | + var delta = decoder.Read(); |
| 89 | + if (!string.IsNullOrEmpty(delta)) |
| 90 | + Console.Write(delta); |
| 91 | + |
| 92 | + sampler.Accept(token); |
| 93 | + conversation.Prompt(token); |
| 94 | + remaining--; |
| 95 | + } |
| 96 | + |
| 97 | + Console.WriteLine(); |
| 98 | + } |
| 99 | + catch (IOException ex) |
| 100 | + { |
| 101 | + Console.ForegroundColor = ConsoleColor.Red; |
| 102 | + Console.WriteLine($"Could not load media '{imagePath}': {ex.Message}"); |
| 103 | + Console.ResetColor(); |
| 104 | + } |
| 105 | + catch (RuntimeError ex) |
| 106 | + { |
| 107 | + Console.ForegroundColor = ConsoleColor.Red; |
| 108 | + Console.WriteLine($"MTMD processing failed: {ex.Message}"); |
| 109 | + Console.ResetColor(); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments