Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
5a0e7b8
Mtmd Implementation base
SignalRT Sep 27, 2025
ab0d42c
Update LLama/Native/NativeApi.cs
SignalRT Sep 29, 2025
f591985
Resolve comment: https://github.com/SciSharp/LLamaSharp/pull/1261#dis…
SignalRT Sep 29, 2025
03d4441
Remove duplicate code
SignalRT Oct 5, 2025
ea4ba82
Move common logic to LlamaExecutorBase
SignalRT Oct 5, 2025
385c62a
Rename SafeMtmdWeights
SignalRT Oct 25, 2025
2cdcc5a
Implement SafeHandle
SignalRT Oct 25, 2025
63d8ce4
Add IntPtrExtension
SignalRT Oct 25, 2025
32edd6f
Solve bad DLL naming in Windows with MTMD libraries
Oct 26, 2025
070ff33
Mtmd Implementation base
SignalRT Sep 27, 2025
82c039c
Update LLama/Native/NativeApi.cs
SignalRT Sep 29, 2025
83d31f8
Resolve comment: https://github.com/SciSharp/LLamaSharp/pull/1261#dis…
SignalRT Sep 29, 2025
b65a6cf
Remove duplicate code
SignalRT Oct 5, 2025
3e36bb9
Move common logic to LlamaExecutorBase
SignalRT Oct 5, 2025
e58618c
Rename SafeMtmdWeights
SignalRT Oct 25, 2025
7e92b31
Implement SafeHandle
SignalRT Oct 25, 2025
f13f286
Add IntPtrExtension
SignalRT Oct 25, 2025
09fb90d
Solve bad DLL naming in Windows with MTMD libraries
Oct 26, 2025
8858196
Merge branch 'mtmd_implementation' of https://github.com/SignalRT/LLa…
SignalRT Dec 8, 2025
43c5dc7
Change Macos build only ARM
SignalRT Dec 13, 2025
08bbca2
Merge branch 'SciSharp:master' into mtmd_implementation
SignalRT Dec 25, 2025
1e84dbe
Test passing again in osx
SignalRT Dec 25, 2025
f9a4478
Merge branch 'SciSharp:master' into mtmd_implementation
SignalRT Jan 8, 2026
f068cbb
Temporary working
SignalRT Jan 10, 2026
ffa399c
Update context size assertions to match configured params in unit tests
SignalRT Jan 10, 2026
355efcd
Add MTMD modality checks (vision/audio) and reject video in interact…
SignalRT Jan 10, 2026
9bbb7f3
Clear mode
SignalRT Jan 10, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/compile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ jobs:
matrix:
include:
- build: 'arm64'
defines: '-DCMAKE_OSX_ARCHITECTURES=arm64 -DGGML_METAL_EMBED_LIBRARY=ON'
defines: '-DCMAKE_OSX_ARCHITECTURES=arm64 -DGGML_METAL_EMBED_LIBRARY=ON -DGGML_METAL_USE_BF16=ON -DLLAMA_CURL=OFF -DLLAMA_BUILD_EXAMPLES=OFF -DLLAMA_BUILD_TOOLS=OFF DLLAMA_BUILD_BORINGSSL=ON '
- build: 'x64'
defines: '-DCMAKE_OSX_ARCHITECTURES=x86_64 -DGGML_METAL=OFF -DGGML_AVX=ON -DGGML_AVX2=ON'
- build: 'x64-rosetta2'
Expand Down
4 changes: 2 additions & 2 deletions LLama.Examples/ExampleRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ExampleRunner
{ "Chat Session: Automatic conversation", TalkToYourself.Run },
{ "Chat Session: Chinese characters", ChatChineseGB2312.Run },
{ "Executor: Interactive mode chat", InteractiveModeExecute.Run },
{ "Executor: Llava Interactive mode chat", LlavaInteractiveModeExecute.Run },
{ "Executor: Mtmd Interactive mode chat", MtmdInteractiveModeExecute.Run },
{ "Executor: Instruct mode chat", InstructModeExecute.Run },
{ "Executor: Stateless mode chat", StatelessModeExecute.Run },
{ "Save and Load: chat session", SaveAndLoadSession.Run },
Expand All @@ -33,7 +33,7 @@ public class ExampleRunner
{ "Batched Executor: Save/Load", BatchedExecutorSaveAndLoad.Run },
{ "Batched Executor: Fork", BatchedExecutorFork.Run },
{ "Batched Executor: Rewind", BatchedExecutorRewind.Run },
{ "Batched Executor: LLava", BatchedExecutorLLava.Run },
{ "Batched Executor: Mtmd", BatchedExecutorMtmd.Run },
{ "Batched Executor: BoolQ Benchmark", BatchedExecutorBoolQ.Run },
{ "Batched Executor: Beam Search", BatchedExecutorBeamSearch.Run },
{ "Custom Sampling Pipeline", CustomSampler.Run },
Expand Down
91 changes: 0 additions & 91 deletions LLama.Examples/Examples/BatchedExecutorLLava.cs

This file was deleted.

126 changes: 126 additions & 0 deletions LLama.Examples/Examples/BatchedExecutorMtmd.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using System;
using System.Collections.Generic;
using System.IO;
using LLama.Batched;
using LLama.Common;
using LLama.Exceptions;
using LLama.Native;
using LLama.Sampling;
using Spectre.Console;

namespace LLama.Examples.Examples;

/// <summary>
/// Demonstrates how to evaluate an image with MTMD helpers and continue generation by
/// manually scheduling batches, similar to what the batched executor does internally.
/// </summary>
public class BatchedExecutorMtmd
{
/// <summary>
/// Number of completion tokens to generate after sending the image prompt.
/// </summary>
public const int TokenCount = 10000;

public static async Task Run()
{
// Load the base LLM and its clip/mtmd sidecar weights so the executor has everything it needs.
var parameters = new ModelParams(UserSettings.GetModelPath());
using var model = await LLamaWeights.LoadFromFileAsync(parameters);
var mtmdParams = MtmdContextParams.Default(); // reuse llama.cpp defaults for helper settings
mtmdParams.UseGpu = false;
var marker = mtmdParams.MediaMarker ?? NativeApi.MtmdDefaultMarker() ?? "<media>";

using var mtmd = await MtmdWeights.LoadFromFileAsync(UserSettings.GetMMProjPath(), model, mtmdParams); // multimodal helper weights

using var executor = new BatchedExecutor(model, parameters, mtmd); // drives batched token + chunk evaluation

// Prepend the media marker so the helper knows where to inject the encoded image tokens.
var defaultPrompt = "\nUSER: Provide a full description of the image.\nASSISTANT: ";
var promptSuffix = AnsiConsole.Ask("Prompt (or ENTER for default):", defaultPrompt);
var promptText = string.Concat(marker, promptSuffix);

var imagePath = UserSettings.GetImagePath();
AnsiConsole.Write(new CanvasImage(imagePath));

var vocab = executor.Context.NativeHandle.ModelHandle.Vocab;

// Simple low-temperature sampler keeps the demo deterministic-ish.
var sampler = new DefaultSamplingPipeline
{
Temperature = 0.1f
};

// Stream decoded text to the console as soon as tokens arrive.
var decoder = new StreamingTokenDecoder(executor.Context)
{
DecodeSpecialTokens = false
};

try
{
// Each conversation tracks its own KV cache sequence IDs.
var conversation = executor.Create();
// enqueue the image so MtmdHelper sees it
conversation.QueueMedia(imagePath);
// schedule multimodal prompt
conversation.Prompt(promptText, addBos: true, special: true);

Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Prompt queued with multimodal chunks. Generating response...\n");
Console.ResetColor();

var remaining = TokenCount;

// Run one decode/sampling/prompt cycle – mirrors the batched executor inner loop.
async Task<bool> ProcessNextAsync()
{
var decodeResult = await executor.Infer();
if (decodeResult == DecodeResult.NoKvSlot) // KV cache exhausted – surface to the user
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Insufficient KV cache space for multimodal evaluation.");
Console.ResetColor();
return false;
}

if (decodeResult != DecodeResult.Ok)
throw new RuntimeError($"Failed to evaluate batch: {decodeResult}.");

if (!conversation.RequiresSampling) // another conversation may still be queued
return true;

var token = conversation.Sample(sampler); // pull logits (or -1 for mtmd chunk) and sample
if (token.IsEndOfGeneration(vocab))
return false;

decoder.Add(token);
var delta = decoder.Read();
if (!string.IsNullOrEmpty(delta))
Console.Write(delta);

sampler.Accept(token); // keep sampler state in sync
conversation.Prompt(token); // feed the accepted token back into the batch
remaining--;
return remaining > 0;
}

while (remaining > 0 && await ProcessNextAsync()) // continue until EOS or budget is reached
{
}

Console.WriteLine();
}
catch (IOException ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"Could not load media '{imagePath}': {ex.Message}");
Console.ResetColor();
}
catch (RuntimeError ex)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine($"MTMD processing failed: {ex.Message}");
Console.ResetColor();
}
}
}
136 changes: 0 additions & 136 deletions LLama.Examples/Examples/LlavaInteractiveModeExecute.cs

This file was deleted.

Loading
Loading