From 138f89cbb30f9341853c651470a10af3afba8370 Mon Sep 17 00:00:00 2001 From: Lyrcaxis Date: Wed, 26 Feb 2025 15:07:47 +0200 Subject: [PATCH 1/5] Cleaned as many warnings as possible --- .../Examples/LlavaInteractiveModeExecute.cs | 2 +- .../Examples/SemanticKernelHomeAutomation.cs | 2 +- .../Examples/SemanticKernelMemory.cs | 1 - LLama.Examples/LLama.Examples.csproj | 1 + LLama.Web/Pages/Error.cshtml.cs | 2 +- LLama/ChatSession.cs | 2 +- LLama/Common/PolymorphicJSONConverter.cs | 2 +- LLama/LLamaExecutorBase.cs | 2 ++ LLama/LLamaInstructExecutor.cs | 22 +++++++-------- LLama/LLamaInteractExecutor.cs | 28 +++++++++++-------- LLama/LLamaTransforms.cs | 2 ++ LLama/Native/LLamaAttentionType.cs | 2 ++ LLama/Native/LLamaPoolingType.cs | 3 ++ LLama/Native/LLamaRopeType.cs | 1 + LLama/Native/LLamaTokenAttr.cs | 1 + LLama/Native/LLavaImageEmbed.cs | 7 +++++ LLama/Native/Load/NativeLibraryConfig.cs | 4 +++ LLama/Native/Load/NativeLibraryMetadata.cs | 1 + LLama/Native/NativeApi.LLava.cs | 2 ++ LLama/Native/NativeApi.cs | 11 ++++---- 20 files changed, 63 insertions(+), 35 deletions(-) diff --git a/LLama.Examples/Examples/LlavaInteractiveModeExecute.cs b/LLama.Examples/Examples/LlavaInteractiveModeExecute.cs index c763d0a7c..dc2dee06e 100644 --- a/LLama.Examples/Examples/LlavaInteractiveModeExecute.cs +++ b/LLama.Examples/Examples/LlavaInteractiveModeExecute.cs @@ -93,7 +93,7 @@ public static async Task Run() Console.WriteLine($"Here are the images, that are sent to the chat model in addition to your message."); Console.WriteLine(); - foreach (var consoleImage in imageBytes?.Select(bytes => new CanvasImage(bytes))) + foreach (var consoleImage in imageBytes?.Select(bytes => new CanvasImage(bytes)) ?? Array.Empty()) { consoleImage.MaxWidth = 50; AnsiConsole.Write(consoleImage); diff --git a/LLama.Examples/Examples/SemanticKernelHomeAutomation.cs b/LLama.Examples/Examples/SemanticKernelHomeAutomation.cs index 4a1ef63db..0805fbcd5 100644 --- a/LLama.Examples/Examples/SemanticKernelHomeAutomation.cs +++ b/LLama.Examples/Examples/SemanticKernelHomeAutomation.cs @@ -123,7 +123,7 @@ [WHICH LIGHT IS ON] ChatMessageContent chatResult = await chatCompletionService.GetChatMessageContentAsync(chatHistory, llamaSharpPromptExecutionSettings, _kernel, stoppingToken); FunctionResult? fres = null; - if (chatResult.Content.Contains("[TURN ON THE LIGHT]")) + if (chatResult.Content!.Contains("[TURN ON THE LIGHT]")) { fres = await _kernel.InvokeAsync("OfficeLight", "TurnOn"); } diff --git a/LLama.Examples/Examples/SemanticKernelMemory.cs b/LLama.Examples/Examples/SemanticKernelMemory.cs index 9da8d6c99..d2f817983 100644 --- a/LLama.Examples/Examples/SemanticKernelMemory.cs +++ b/LLama.Examples/Examples/SemanticKernelMemory.cs @@ -15,7 +15,6 @@ public static async Task Run() Console.WriteLine("This example is from: \n" + "https://github.com/microsoft/semantic-kernel/blob/main/dotnet/samples/KernelSyntaxExamples/Example14_SemanticMemory.cs"); - var seed = 1337u; // Load weights into memory var parameters = new ModelParams(modelPath) { diff --git a/LLama.Examples/LLama.Examples.csproj b/LLama.Examples/LLama.Examples.csproj index f56313749..ea24c9ca6 100644 --- a/LLama.Examples/LLama.Examples.csproj +++ b/LLama.Examples/LLama.Examples.csproj @@ -19,6 +19,7 @@ + diff --git a/LLama.Web/Pages/Error.cshtml.cs b/LLama.Web/Pages/Error.cshtml.cs index 33f9562b4..655633588 100644 --- a/LLama.Web/Pages/Error.cshtml.cs +++ b/LLama.Web/Pages/Error.cshtml.cs @@ -8,7 +8,7 @@ namespace LLama.Web.Pages [IgnoreAntiforgeryToken] public class ErrorModel : PageModel { - public string? RequestId { get; set; } + public string RequestId { get; set; } public bool ShowRequestId => !string.IsNullOrEmpty(RequestId); diff --git a/LLama/ChatSession.cs b/LLama/ChatSession.cs index 5aca6c435..03127932b 100644 --- a/LLama/ChatSession.cs +++ b/LLama/ChatSession.cs @@ -779,7 +779,7 @@ public static SessionState Load(string path) return new SessionState( contextState, - executorState, + executorState!, history, inputTransforms.ToList(), outputTransform, diff --git a/LLama/Common/PolymorphicJSONConverter.cs b/LLama/Common/PolymorphicJSONConverter.cs index c5f4ee358..03891883e 100644 --- a/LLama/Common/PolymorphicJSONConverter.cs +++ b/LLama/Common/PolymorphicJSONConverter.cs @@ -45,7 +45,7 @@ internal class PolymorphicJSONConverter : JsonConverter public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) { writer.WriteStartObject(); - writer.WriteString("Name", value.GetType().Name); + writer.WriteString("Name", value!.GetType().Name); writer.WritePropertyName("Data"); JsonSerializer.Serialize(writer, value, value.GetType(), options); writer.WriteEndObject(); diff --git a/LLama/LLamaExecutorBase.cs b/LLama/LLamaExecutorBase.cs index bc8812cb5..d184f505d 100644 --- a/LLama/LLamaExecutorBase.cs +++ b/LLama/LLamaExecutorBase.cs @@ -395,6 +395,7 @@ protected class InferStateArgs public bool NeedToSaveSession { get; set; } } +#pragma warning disable // Missing XML and nullable warnings that [JsonConverter(typeof(PolymorphicJSONConverter))] public class ExecutorBaseState { @@ -431,5 +432,6 @@ public class ExecutorBaseState [JsonPropertyName("mirostat_mu")] public float? MirostatMu { get; set; } } +#pragma warning restore } } diff --git a/LLama/LLamaInstructExecutor.cs b/LLama/LLamaInstructExecutor.cs index a009844c4..331591fba 100644 --- a/LLama/LLamaInstructExecutor.cs +++ b/LLama/LLamaInstructExecutor.cs @@ -25,8 +25,6 @@ public class InstructExecutor private LLamaToken[] _inp_pfx; private LLamaToken[] _inp_sfx; - private ISamplingPipeline? _pipeline; - /// /// /// @@ -72,17 +70,17 @@ public override Task LoadState(ExecutorBaseState data) if(data is InstructExecutorState state) { _n_session_consumed = state.ConsumedSessionCount; - _embed_inps = state.EmbedInps.ToList(); + _embed_inps = state.EmbedInps!.ToList(); _is_prompt_run = state.IsPromptRun; _consumedTokensCount = state.ConsumedTokensCount; - _embeds = state.Embeds.ToList(); - _last_n_tokens = new FixedSizeQueue(state.LastTokensCapacity, state.LastTokens); - _inp_pfx = state.InputPrefixTokens; - _inp_sfx = state.InputSuffixTokens; + _embeds = state.Embeds!.ToList(); + _last_n_tokens = new FixedSizeQueue(state.LastTokensCapacity, state.LastTokens!); + _inp_pfx = state.InputPrefixTokens!; + _inp_sfx = state.InputSuffixTokens!; _n_matching_session_tokens = state.MatchingSessionTokensCount; _pastTokensCount = state.PastTokensCount; _pathSession = state.SessionFilePath; - _session_tokens = state.SessionTokens.ToList(); + _session_tokens = state.SessionTokens!.ToList(); } else { @@ -107,7 +105,7 @@ public override async Task LoadState(string filename) using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) { var state = await JsonSerializer.DeserializeAsync(fs); - await LoadState(state); + await LoadState(state!); } } @@ -224,7 +222,7 @@ protected override async Task InferInternal(IInferenceParams inferenceParams, In if (!string.IsNullOrEmpty(_pathSession) && args.NeedToSaveSession) { args.NeedToSaveSession = false; - SaveSessionFile(_pathSession); + SaveSessionFile(_pathSession!); } // Sample with the pipeline @@ -266,12 +264,12 @@ public class InstructExecutorState : ExecutorBaseState /// Instruction prefix tokens. /// [JsonPropertyName("inp_pfx")] - public LLamaToken[] InputPrefixTokens { get; set; } + public LLamaToken[]? InputPrefixTokens { get; set; } /// /// Instruction suffix tokens. /// [JsonPropertyName("inp_sfx")] - public LLamaToken[] InputSuffixTokens { get; set; } + public LLamaToken[]? InputSuffixTokens { get; set; } } } } diff --git a/LLama/LLamaInteractExecutor.cs b/LLama/LLamaInteractExecutor.cs index d1f6833a1..c19ad3bc8 100644 --- a/LLama/LLamaInteractExecutor.cs +++ b/LLama/LLamaInteractExecutor.cs @@ -27,8 +27,6 @@ public class InteractiveExecutor : StatefulExecutorBase private List _imageEmbedHandles = new List(); private bool _imageInPrompt = false; - private ISamplingPipeline? _pipeline; - /// /// /// @@ -39,6 +37,12 @@ public InteractiveExecutor(LLamaContext context, ILogger? logger = null) { } + /// + /// + /// + /// + /// + /// public InteractiveExecutor(LLamaContext context, LLavaWeights clipModel, ILogger? logger = null) : base(context, clipModel, logger) { @@ -69,15 +73,15 @@ public override Task LoadState(ExecutorBaseState data) if (data is InteractiveExecutorState state) { _n_session_consumed = state.ConsumedSessionCount; - _embed_inps = state.EmbedInps.ToList(); + _embed_inps = state.EmbedInps!.ToList(); _is_prompt_run = state.IsPromptRun; _consumedTokensCount = state.ConsumedTokensCount; - _embeds = state.Embeds.ToList(); - _last_n_tokens = new FixedSizeQueue(state.LastTokensCapacity, state.LastTokens); + _embeds = state.Embeds!.ToList(); + _last_n_tokens = new FixedSizeQueue(state.LastTokensCapacity, state.LastTokens!); _n_matching_session_tokens = state.MatchingSessionTokensCount; _pastTokensCount = state.PastTokensCount; _pathSession = state.SessionFilePath; - _session_tokens = state.SessionTokens.ToList(); + _session_tokens = state.SessionTokens!.ToList(); } else throw new ArgumentException("Invalid state data type."); @@ -99,7 +103,7 @@ public override async Task LoadState(string filename) using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read)) { var state = await JsonSerializer.DeserializeAsync(fs); - await LoadState(state); + await LoadState(state!); } } @@ -161,11 +165,11 @@ private Task PreprocessLlava(string text, InferStateArgs args, bool addBos = tru // If the prompt contains the tag extract this. _imageInPrompt = text.Contains(""); - if (_imageInPrompt && IsMultiModal ) + if (_imageInPrompt && IsMultiModal) { foreach (var image in Images) { - _imageEmbedHandles.Add(SafeLlavaImageEmbedHandle.CreateFromMemory(ClipModel.NativeHandle, Context, image)); + _imageEmbedHandles.Add(SafeLlavaImageEmbedHandle.CreateFromMemory(ClipModel!.NativeHandle, Context, image)); } int imageIndex = text.IndexOf(""); @@ -195,7 +199,7 @@ private Task PreprocessLlava(string text, InferStateArgs args, bool addBos = tru } return Task.CompletedTask; } - + /// /// Return whether to break the generation. /// @@ -267,7 +271,7 @@ protected override async Task InferInternal(IInferenceParams inferenceParams, In // Images foreach( var image in _imageEmbedHandles ) - ClipModel.EvalImageEmbed(Context, image, ref _pastTokensCount); + ClipModel!.EvalImageEmbed(Context, image, ref _pastTokensCount); // Post-image Tokens end = await Context.DecodeAsync(_embeds.GetRange(_EmbedImagePosition, _embeds.Count - _EmbedImagePosition), LLamaSeqId.Zero, batch, _pastTokensCount); @@ -301,7 +305,7 @@ protected override async Task InferInternal(IInferenceParams inferenceParams, In if (!string.IsNullOrEmpty(_pathSession) && args.NeedToSaveSession) { args.NeedToSaveSession = false; - SaveSessionFile(_pathSession); + SaveSessionFile(_pathSession!); } diff --git a/LLama/LLamaTransforms.cs b/LLama/LLamaTransforms.cs index 5dfeea6b3..9a216608e 100644 --- a/LLama/LLamaTransforms.cs +++ b/LLama/LLamaTransforms.cs @@ -30,11 +30,13 @@ public class DefaultHistoryTransform : IHistoryTransform private readonly string _unknownName; private readonly bool _isInstructMode; +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member public string UserName => _userName; public string AssistantName => _assistantName; public string SystemName => _systemName; public string UnknownName => _unknownName; public bool IsInstructMode => _isInstructMode; +#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member /// /// diff --git a/LLama/Native/LLamaAttentionType.cs b/LLama/Native/LLamaAttentionType.cs index f26c73278..fda8e52ec 100644 --- a/LLama/Native/LLamaAttentionType.cs +++ b/LLama/Native/LLamaAttentionType.cs @@ -6,7 +6,9 @@ namespace LLama.Native; /// llama_attention_type public enum LLamaAttentionType { +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member Unspecified = -1, Causal = 0, NonCausal = 1, +#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member } \ No newline at end of file diff --git a/LLama/Native/LLamaPoolingType.cs b/LLama/Native/LLamaPoolingType.cs index 3ee767b51..dcdcbac82 100644 --- a/LLama/Native/LLamaPoolingType.cs +++ b/LLama/Native/LLamaPoolingType.cs @@ -28,6 +28,9 @@ public enum LLamaPoolingType /// CLS = 2, + /// + /// Return the embeddings of the last token + /// Last = 3, /// diff --git a/LLama/Native/LLamaRopeType.cs b/LLama/Native/LLamaRopeType.cs index 8bdeeb298..987d26d0a 100644 --- a/LLama/Native/LLamaRopeType.cs +++ b/LLama/Native/LLamaRopeType.cs @@ -1,4 +1,5 @@ namespace LLama.Native; +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member /// /// diff --git a/LLama/Native/LLamaTokenAttr.cs b/LLama/Native/LLamaTokenAttr.cs index ae5e28019..5b66cbd87 100644 --- a/LLama/Native/LLamaTokenAttr.cs +++ b/LLama/Native/LLamaTokenAttr.cs @@ -1,6 +1,7 @@ using System; namespace LLama.Native; +#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member /// /// Token attributes diff --git a/LLama/Native/LLavaImageEmbed.cs b/LLama/Native/LLavaImageEmbed.cs index 559a245ec..e9b2e2c59 100644 --- a/LLama/Native/LLavaImageEmbed.cs +++ b/LLama/Native/LLavaImageEmbed.cs @@ -7,6 +7,13 @@ namespace LLama.Native; [StructLayout(LayoutKind.Sequential)] public unsafe struct LLavaImageEmbed { + /// + /// The embeddings of the embedded image. Should be a float[] + /// public float* embed; + + /// + /// + /// public int n_image_pos; } \ No newline at end of file diff --git a/LLama/Native/Load/NativeLibraryConfig.cs b/LLama/Native/Load/NativeLibraryConfig.cs index 2bfa0554b..8362a8ac2 100644 --- a/LLama/Native/Load/NativeLibraryConfig.cs +++ b/LLama/Native/Load/NativeLibraryConfig.cs @@ -245,6 +245,7 @@ private static bool CheckAVX512() /// /// /// + /// public record Description(string? Path, NativeLibraryName Library, bool UseCuda, bool UseVulkan, AvxLevel AvxLevel, bool AllowFallback, bool SkipCheck, string[] SearchDirectories) { @@ -276,6 +277,9 @@ public override string ToString() } #endif + /// + /// Global configuration handle for the Native (cpp) library. + /// public sealed partial class NativeLibraryConfig { /// diff --git a/LLama/Native/Load/NativeLibraryMetadata.cs b/LLama/Native/Load/NativeLibraryMetadata.cs index d458610b6..e07d40986 100644 --- a/LLama/Native/Load/NativeLibraryMetadata.cs +++ b/LLama/Native/Load/NativeLibraryMetadata.cs @@ -10,6 +10,7 @@ namespace LLama.Native /// Which AvxLevel it's compiled with. public record class NativeLibraryMetadata(NativeLibraryName NativeLibraryName, bool UseCuda, bool UseVulkan, AvxLevel AvxLevel) { + /// public override string ToString() { return $"(NativeLibraryName: {NativeLibraryName}, UseCuda: {UseCuda}, UseVulkan: {UseVulkan}, AvxLevel: {AvxLevel})"; diff --git a/LLama/Native/NativeApi.LLava.cs b/LLama/Native/NativeApi.LLava.cs index bda796d4c..692e3f0ad 100644 --- a/LLama/Native/NativeApi.LLava.cs +++ b/LLama/Native/NativeApi.LLava.cs @@ -53,6 +53,8 @@ SafeLlavaImageEmbedHandle llava_image_embed_make_with_filename(SafeLlavaModelHan /// /// Llama Context /// Embedding handle + /// + /// /// True on success [DllImport(llavaLibraryName, EntryPoint = "llava_eval_image_embed", CallingConvention = CallingConvention.Cdecl)] [return: MarshalAs(UnmanagedType.U1)] diff --git a/LLama/Native/NativeApi.cs b/LLama/Native/NativeApi.cs index 06e3baee4..279f2deb4 100644 --- a/LLama/Native/NativeApi.cs +++ b/LLama/Native/NativeApi.cs @@ -18,11 +18,6 @@ public static void llama_empty_call() llama_max_devices(); } - /// - /// Call once at the end of the program - currently only used for MPI - /// - public static extern void llama_backend_free(); - /// /// Get the maximum number of devices supported by llama.cpp /// @@ -104,9 +99,15 @@ public static void llama_empty_call() [return: MarshalAs(UnmanagedType.U1)] public static extern bool llama_state_save_file(SafeLLamaContextHandle ctx, string path_session, LLamaToken[] tokens, ulong n_token_count); + /// + /// Saves the specified sequence as a file on specified filepath. Can later be loaded via + /// [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)] public static extern unsafe nuint llama_state_seq_save_file(SafeLLamaContextHandle ctx, string filepath, LLamaSeqId seq_id, LLamaToken* tokens, nuint n_token_count); + /// + /// Loads a sequence saved as a file via into the specified sequence + /// [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)] public static extern unsafe nuint llama_state_seq_load_file(SafeLLamaContextHandle ctx, string filepath, LLamaSeqId dest_seq_id, LLamaToken* tokens_out, nuint n_token_capacity, out nuint n_token_count_out); From 5c9c073956b0af20135901f1c95cb4edd4ac51b2 Mon Sep 17 00:00:00 2001 From: Lyrcaxis Date: Wed, 26 Feb 2025 15:23:07 +0200 Subject: [PATCH 2/5] Cleaned some "Messages" from the error list window. --- .../Examples/SemanticKernelHomeAutomation.cs | 12 ++++++------ .../SemanticKernel/LLamaSharpChatCompletionTests.cs | 8 ++++---- LLama/Common/ChatHistory.cs | 8 ++++---- LLama/LLamaExecutorBase.cs | 2 +- LLama/LLamaInteractExecutor.cs | 9 +++------ LLama/Native/LLamaAttentionType.cs | 13 +++++++++++-- LLama/Native/LLavaImageEmbed.cs | 2 +- LLama/Native/Load/NativeLibraryConfig.cs | 2 +- 8 files changed, 31 insertions(+), 25 deletions(-) diff --git a/LLama.Examples/Examples/SemanticKernelHomeAutomation.cs b/LLama.Examples/Examples/SemanticKernelHomeAutomation.cs index 0805fbcd5..07e1a5591 100644 --- a/LLama.Examples/Examples/SemanticKernelHomeAutomation.cs +++ b/LLama.Examples/Examples/SemanticKernelHomeAutomation.cs @@ -68,7 +68,7 @@ public static async Task Run() } } - class Worker( + internal class Worker( IHostApplicationLifetime hostApplicationLifetime, [FromKeyedServices("HomeAutomationKernel")] Kernel kernel) : BackgroundService { @@ -92,7 +92,7 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken) TopP = 0.1f }; - string? input = null; + string? input; while ((input = Console.ReadLine()) != null) { @@ -125,17 +125,17 @@ [WHICH LIGHT IS ON] FunctionResult? fres = null; if (chatResult.Content!.Contains("[TURN ON THE LIGHT]")) { - fres = await _kernel.InvokeAsync("OfficeLight", "TurnOn"); + fres = await _kernel.InvokeAsync("OfficeLight", "TurnOn", cancellationToken: stoppingToken); } else if (chatResult.Content.Contains("[TURN OFF THE LIGHT]")) { - fres = await _kernel.InvokeAsync("OfficeLight", "TurnOff"); + fres = await _kernel.InvokeAsync("OfficeLight", "TurnOff", cancellationToken: stoppingToken); } Console.ForegroundColor = ConsoleColor.Green; if (fres != null || chatResult.Content.Contains("[WHICH LIGHT IS ON]")) { - fres = await _kernel.InvokeAsync("OfficeLight", "IsTurnedOn"); + fres = await _kernel.InvokeAsync("OfficeLight", "IsTurnedOn", cancellationToken: stoppingToken); Console.Write($">>> Result:\n {(fres.GetValue()==true?"The light is ON.": "The light is OFF.")}\n\n> "); } else @@ -154,7 +154,7 @@ [WHICH LIGHT IS ON] /// Class that represents a controllable light. /// [Description("Represents a light")] - class MyLightPlugin(bool turnedOn = false) + internal class MyLightPlugin(bool turnedOn = false) { private bool _turnedOn = turnedOn; diff --git a/LLama.Unittest/SemanticKernel/LLamaSharpChatCompletionTests.cs b/LLama.Unittest/SemanticKernel/LLamaSharpChatCompletionTests.cs index 0873a713b..0ac1beca0 100644 --- a/LLama.Unittest/SemanticKernel/LLamaSharpChatCompletionTests.cs +++ b/LLama.Unittest/SemanticKernel/LLamaSharpChatCompletionTests.cs @@ -12,13 +12,13 @@ public class LLamaSharpChatCompletionTests public LLamaSharpChatCompletionTests() { - this.mockStatelessExecutor = new Mock(); + mockStatelessExecutor = new Mock(); } private LLamaSharpChatCompletion CreateLLamaSharpChatCompletion() { return new LLamaSharpChatCompletion( - this.mockStatelessExecutor.Object, + mockStatelessExecutor.Object, null, null, null); @@ -28,7 +28,7 @@ private LLamaSharpChatCompletion CreateLLamaSharpChatCompletion() public async Task GetChatMessageContentsAsync_StateUnderTest_ExpectedBehavior() { // Arrange - var unitUnderTest = this.CreateLLamaSharpChatCompletion(); + var unitUnderTest = CreateLLamaSharpChatCompletion(); ChatHistory chatHistory = new ChatHistory(); PromptExecutionSettings? executionSettings = null; Kernel? kernel = null; @@ -51,7 +51,7 @@ public async Task GetChatMessageContentsAsync_StateUnderTest_ExpectedBehavior() public async Task GetStreamingChatMessageContentsAsync_StateUnderTest_ExpectedBehavior() { // Arrange - var unitUnderTest = this.CreateLLamaSharpChatCompletion(); + var unitUnderTest = CreateLLamaSharpChatCompletion(); ChatHistory chatHistory = new ChatHistory(); PromptExecutionSettings? executionSettings = null; Kernel? kernel = null; diff --git a/LLama/Common/ChatHistory.cs b/LLama/Common/ChatHistory.cs index c22cc7c06..094cf0815 100644 --- a/LLama/Common/ChatHistory.cs +++ b/LLama/Common/ChatHistory.cs @@ -64,8 +64,8 @@ public class Message /// Message content public Message(AuthorRole authorRole, string content) { - this.AuthorRole = authorRole; - this.Content = content; + AuthorRole = authorRole; + Content = content; } } @@ -87,7 +87,7 @@ public ChatHistory() { } /// public ChatHistory(Message[] messageHistory) { - this.Messages = messageHistory.ToList(); + Messages = messageHistory.ToList(); } /// @@ -97,7 +97,7 @@ public ChatHistory(Message[] messageHistory) /// Message content public void AddMessage(AuthorRole authorRole, string content) { - this.Messages.Add(new Message(authorRole, content)); + Messages.Add(new Message(authorRole, content)); } /// diff --git a/LLama/LLamaExecutorBase.cs b/LLama/LLamaExecutorBase.cs index d184f505d..daad161a8 100644 --- a/LLama/LLamaExecutorBase.cs +++ b/LLama/LLamaExecutorBase.cs @@ -395,7 +395,7 @@ protected class InferStateArgs public bool NeedToSaveSession { get; set; } } -#pragma warning disable // Missing XML and nullable warnings that +#pragma warning disable // Missing XML and irrelevant nullable warnings [JsonConverter(typeof(PolymorphicJSONConverter))] public class ExecutorBaseState { diff --git a/LLama/LLamaInteractExecutor.cs b/LLama/LLamaInteractExecutor.cs index c19ad3bc8..43ee236d1 100644 --- a/LLama/LLamaInteractExecutor.cs +++ b/LLama/LLamaInteractExecutor.cs @@ -123,7 +123,7 @@ protected override Task PreprocessInputs(string? text, InferStateArgs args) { // 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) + if (!IsMultiModal) { _embed_inps = Context.Tokenize(text, true, true).ToList(); } @@ -142,7 +142,7 @@ protected override Task PreprocessInputs(string? text, InferStateArgs args) text += "\n"; } - if (!this.IsMultiModal) + if (!IsMultiModal) { var line_inp = Context.Tokenize(text, false, true); _embed_inps.AddRange(line_inp); @@ -160,9 +160,7 @@ protected override Task PreprocessInputs(string? text, InferStateArgs args) /// private Task PreprocessLlava(string text, InferStateArgs args, bool addBos = true ) - { - int usedTokens = 0; - + { // If the prompt contains the tag extract this. _imageInPrompt = text.Contains(""); if (_imageInPrompt && IsMultiModal) @@ -182,7 +180,6 @@ private Task PreprocessLlava(string text, InferStateArgs args, bool addBos = tru var segment2 = Context.Tokenize(postImagePrompt, false, true); _embed_inps.AddRange(segment1); _embed_inps.AddRange(segment2); - usedTokens += (segment1.Length + segment2.Length); } else { diff --git a/LLama/Native/LLamaAttentionType.cs b/LLama/Native/LLamaAttentionType.cs index fda8e52ec..bcc42e6d7 100644 --- a/LLama/Native/LLamaAttentionType.cs +++ b/LLama/Native/LLamaAttentionType.cs @@ -6,9 +6,18 @@ namespace LLama.Native; /// llama_attention_type public enum LLamaAttentionType { -#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member + /// + /// Unspecified attention type. The library will attempt to find the best fit + /// Unspecified = -1, + + /// + /// The causal mask will be applied, causing tokens to only see previous tokens in the same sequence, and not future ones + /// Causal = 0, + + /// + /// The causal mask will not be applied, and tokens of the same sequence will be able to see each other + /// NonCausal = 1, -#pragma warning restore CS1591 // Missing XML comment for publicly visible type or member } \ No newline at end of file diff --git a/LLama/Native/LLavaImageEmbed.cs b/LLama/Native/LLavaImageEmbed.cs index e9b2e2c59..27c1e9068 100644 --- a/LLama/Native/LLavaImageEmbed.cs +++ b/LLama/Native/LLavaImageEmbed.cs @@ -13,7 +13,7 @@ public unsafe struct LLavaImageEmbed public float* embed; /// - /// + /// The position of the image's tokens. /// public int n_image_pos; } \ No newline at end of file diff --git a/LLama/Native/Load/NativeLibraryConfig.cs b/LLama/Native/Load/NativeLibraryConfig.cs index 8362a8ac2..652b1da48 100644 --- a/LLama/Native/Load/NativeLibraryConfig.cs +++ b/LLama/Native/Load/NativeLibraryConfig.cs @@ -376,7 +376,7 @@ public NativeLibraryConfig WithLogCallback(ILogger? logger) /// Whether the running is successful. public bool DryRun(out INativeLibrary? loadedLibrary) { - LogCallback?.Invoke(LLamaLogLevel.Debug, $"Beginning dry run for {this.NativeLibraryName.GetLibraryName()}..."); + LogCallback?.Invoke(LLamaLogLevel.Debug, $"Beginning dry run for {NativeLibraryName.GetLibraryName()}..."); return NativeLibraryUtils.TryLoadLibrary(this, out loadedLibrary) != IntPtr.Zero; } } From aaba4d9e1a355d4e46ed8f617dd89e4c37979f85 Mon Sep 17 00:00:00 2001 From: Lyrcaxis Date: Wed, 26 Feb 2025 16:35:03 +0200 Subject: [PATCH 3/5] Addressed CR (Specified which warnings should be suppressed) --- LLama/LLamaExecutorBase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LLama/LLamaExecutorBase.cs b/LLama/LLamaExecutorBase.cs index daad161a8..48060dbaf 100644 --- a/LLama/LLamaExecutorBase.cs +++ b/LLama/LLamaExecutorBase.cs @@ -395,7 +395,7 @@ protected class InferStateArgs public bool NeedToSaveSession { get; set; } } -#pragma warning disable // Missing XML and irrelevant nullable warnings +#pragma warning disable CS1591, CS8618 // Missing XML and irrelevant nullable warnings [JsonConverter(typeof(PolymorphicJSONConverter))] public class ExecutorBaseState { From d335bff32a721318fa95acbda6255ab3757064cd Mon Sep 17 00:00:00 2001 From: Lyrcaxis <32474602+Lyrcaxis@users.noreply.github.com> Date: Wed, 26 Feb 2025 16:44:51 +0200 Subject: [PATCH 4/5] Update LLama/Native/LLavaImageEmbed.cs Co-authored-by: Martin Evans --- LLama/Native/LLavaImageEmbed.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LLama/Native/LLavaImageEmbed.cs b/LLama/Native/LLavaImageEmbed.cs index 27c1e9068..65eba230c 100644 --- a/LLama/Native/LLavaImageEmbed.cs +++ b/LLama/Native/LLavaImageEmbed.cs @@ -8,7 +8,7 @@ namespace LLama.Native; public unsafe struct LLavaImageEmbed { /// - /// The embeddings of the embedded image. Should be a float[] + /// The embeddings of the embedded image. /// public float* embed; From 02c882a090f353fad6046611f983a95b3007c34e Mon Sep 17 00:00:00 2001 From: Lyrcaxis Date: Wed, 26 Feb 2025 16:49:27 +0200 Subject: [PATCH 5/5] Addressed CR 2 (re-added llama_backend_free extern method) --- LLama/Native/NativeApi.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/LLama/Native/NativeApi.cs b/LLama/Native/NativeApi.cs index 279f2deb4..f17a1bd5e 100644 --- a/LLama/Native/NativeApi.cs +++ b/LLama/Native/NativeApi.cs @@ -7,7 +7,7 @@ namespace LLama.Native /// /// Direct translation of the llama.cpp API /// - public static partial class NativeApi + public static partial class NativeApi { /// /// A method that does nothing. This is a native method, calling it will force the llama native dependencies to be loaded. @@ -18,6 +18,13 @@ public static void llama_empty_call() llama_max_devices(); } +#pragma warning disable CS0626 // Method, operator, or accessor is marked external and has no attributes on it + /// + /// Call once at the end of the program - currently only used for MPI + /// + public static extern void llama_backend_free(); +#pragma warning restore CS0626 // Method, operator, or accessor is marked external and has no attributes on it + /// /// Get the maximum number of devices supported by llama.cpp ///