From 17c1ba0072da0a9a87b1fda864113563ab036300 Mon Sep 17 00:00:00 2001 From: Martin Evans Date: Sat, 13 Jul 2024 22:06:39 +0100 Subject: [PATCH] - Removed unused field from StatelessExecutor - Renamed `Mirostat` (from `Mirostate`) - Removed unnecessary temporary array allocation in `Conversation.Prompt` - Removed all unused `using` statements - Used [ collection expression ] where possible --- LLama/Abstractions/IModelParams.cs | 3 +- LLama/Abstractions/INativeLibrary.cs | 4 +-- .../INativeLibrarySelectingPolicy.cs | 4 +-- LLama/Batched/Conversation.cs | 34 ++++++------------- LLama/ChatSession.cs | 6 ++-- LLama/Common/PolymorphicJSONConverter.cs | 3 -- LLama/Extensions/EncodingExtensions.cs | 2 +- LLama/Extensions/IReadOnlyListExtensions.cs | 3 +- LLama/Grammars/GBNFGrammarParser.cs | 4 +-- LLama/Grammars/GrammarRule.cs | 2 +- LLama/LLamaContext.cs | 2 -- LLama/LLamaEmbedder.cs | 2 +- LLama/LLamaInstructExecutor.cs | 3 +- LLama/LLamaInteractExecutor.cs | 1 - LLama/LLamaStatelessExecutor.cs | 4 --- LLama/LLamaTemplate.cs | 1 - LLama/LLamaTransforms.cs | 6 ++-- LLama/Native/GroupDisposable.cs | 3 +- LLama/Native/LLamaBatch.cs | 1 - LLama/Native/LLamaBatchEmbeddings.cs | 1 - LLama/Native/LLamaChatMessage.cs | 2 -- LLama/Native/LLamaContextParams.cs | 1 - LLama/Native/LLamaGrammarElement.cs | 1 - LLama/Native/LLamaKvCacheView.cs | 1 - LLama/Native/LLamaModelMetadataOverride.cs | 2 -- LLama/Native/LLamaModelParams.cs | 1 - LLama/Native/LLamaModelQuantizeParams.cs | 3 +- LLama/Native/LLamaNativeBatch.cs | 2 -- LLama/Native/LLamaPos.cs | 2 -- LLama/Native/LLamaSeqId.cs | 2 -- LLama/Native/LLamaTimings.cs | 1 - LLama/Native/LLamaToken.cs | 1 - LLama/Native/LLamaTokenData.cs | 2 -- LLama/Native/LLamaTokenDataArray.cs | 2 -- LLama/Native/LLavaImageEmbed.cs | 2 -- .../DefaultNativeLibrarySelectingPolicy.cs | 3 +- LLama/Native/Load/NativeLibraryUtils.cs | 3 +- LLama/Native/Load/NativeLibraryWithAvx.cs | 3 +- LLama/Native/Load/NativeLibraryWithCuda.cs | 3 +- .../Load/NativeLibraryWithMacOrFallback.cs | 3 +- LLama/Native/Load/NativeLibraryWithVulkan.cs | 3 +- LLama/Native/Load/SystemInfo.cs | 7 ++-- LLama/Native/Load/UnknownNativeLibrary.cs | 5 +-- LLama/Native/NativeApi.Grammar.cs | 1 - LLama/Native/NativeApi.LLava.cs | 2 -- LLama/Native/NativeApi.Load.cs | 1 - LLama/Native/NativeApi.Quantize.cs | 2 -- LLama/Native/NativeApi.Sampling.cs | 3 +- LLama/Native/NativeApi.cs | 1 - LLama/Native/NativeLogConfig.cs | 1 - LLama/Native/SafeLLamaContextHandle.cs | 1 - LLama/Native/SafeLLamaGrammarHandle.cs | 1 - LLama/Native/SafeLLamaHandleBase.cs | 3 +- LLama/Native/SafeLlamaModelHandle.cs | 2 -- LLama/Native/SafeLlavaModelHandle.cs | 1 - LLama/Sampling/DefaultSamplingPipeline.cs | 1 - LLama/Sampling/ISamplingPipeline.cs | 4 +-- LLama/Sampling/Mirostat2SamplingPipeline.cs | 6 ++-- LLama/Sampling/MirostatSamplingPipeline.cs | 6 ++-- LLama/StreamingTokenDecoder.cs | 1 - .../Transformers/PromptTemplateTransformer.cs | 11 ++---- LLama/Usings.cs | 4 ++- 62 files changed, 53 insertions(+), 143 deletions(-) diff --git a/LLama/Abstractions/IModelParams.cs b/LLama/Abstractions/IModelParams.cs index 25280e71b..e6a64c930 100644 --- a/LLama/Abstractions/IModelParams.cs +++ b/LLama/Abstractions/IModelParams.cs @@ -1,5 +1,4 @@ using System; -using System.Buffers; using System.Collections; using System.Collections.Generic; using System.ComponentModel; @@ -214,7 +213,7 @@ public class TensorSplitsCollectionConverter /// public override TensorSplitsCollection Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) { - var arr = JsonSerializer.Deserialize(ref reader, options) ?? Array.Empty(); + var arr = JsonSerializer.Deserialize(ref reader, options) ?? [ ]; return new TensorSplitsCollection(arr); } diff --git a/LLama/Abstractions/INativeLibrary.cs b/LLama/Abstractions/INativeLibrary.cs index a7e00b753..41eb7c0ad 100644 --- a/LLama/Abstractions/INativeLibrary.cs +++ b/LLama/Abstractions/INativeLibrary.cs @@ -1,7 +1,5 @@ -using LLama.Native; -using System; +using LLama.Native; using System.Collections.Generic; -using System.Text; namespace LLama.Abstractions { diff --git a/LLama/Abstractions/INativeLibrarySelectingPolicy.cs b/LLama/Abstractions/INativeLibrarySelectingPolicy.cs index 41335202e..d69ba4857 100644 --- a/LLama/Abstractions/INativeLibrarySelectingPolicy.cs +++ b/LLama/Abstractions/INativeLibrarySelectingPolicy.cs @@ -1,7 +1,5 @@ -using LLama.Native; -using System; +using LLama.Native; using System.Collections.Generic; -using System.Text; namespace LLama.Abstractions { diff --git a/LLama/Batched/Conversation.cs b/LLama/Batched/Conversation.cs index 67ef7aa9b..a6fcf2431 100644 --- a/LLama/Batched/Conversation.cs +++ b/LLama/Batched/Conversation.cs @@ -1,8 +1,6 @@ using System; -using System.Buffers; using System.Collections.Generic; using System.Linq; -using System.Runtime.InteropServices; using System.Text.Json; using LLama.Native; @@ -289,29 +287,19 @@ public void Prompt(SafeLlavaImageEmbedHandle embedding) if (embedding.Model.EmbeddingDimensions != Executor.Model.EmbeddingSize) throw new ArgumentException($"Embedding dimension mismatch between image embedding ({embedding.Model.EmbeddingDimensions}) and model ({Executor.Model.EmbeddingSize})"); - - // Get a temporary array large enough to hold one embedding item - var tempArr = ArrayPool.Shared.Rent(embedding.Model.EmbeddingDimensions); - var tempSpan = tempArr.AsSpan(0, embedding.Model.EmbeddingDimensions); - try + + for (var i = 0; i < embedding.Model.PatchCount; i++) { - for (var i = 0; i < embedding.Model.PatchCount; i++) - { - // Get a batch with space - (var batch, _requiredEpoch) = Executor.GetEmbeddingBatch(); + // Get a batch with space + (var batch, _requiredEpoch) = Executor.GetEmbeddingBatch(); - batch.Add( - (i, embedding), - static (Span dest, (int index, SafeLlavaImageEmbedHandle embedding) tup) => tup.embedding.GetEmbedding(dest, tup.index), - _end++, - ConversationId, - i == embedding.Model.PatchCount - 1 - ); - } - } - finally - { - ArrayPool.Shared.Return(tempArr); + batch.Add( + (i, embedding), + static (Span dest, (int index, SafeLlavaImageEmbedHandle embedding) tup) => tup.embedding.GetEmbedding(dest, tup.index), + _end++, + ConversationId, + i == embedding.Model.PatchCount - 1 + ); } } diff --git a/LLama/ChatSession.cs b/LLama/ChatSession.cs index a55bfedb7..d77a0ffae 100644 --- a/LLama/ChatSession.cs +++ b/LLama/ChatSession.cs @@ -624,7 +624,7 @@ public record SessionState /// /// The input transform pipeline used in this session. /// - public ITextTransform[] InputTransformPipeline { get; set; } = Array.Empty(); + public ITextTransform[] InputTransformPipeline { get; set; } = [ ]; /// /// The output transform used in this session. @@ -639,7 +639,7 @@ public record SessionState /// /// The the chat history messages for this session. /// - public ChatHistory.Message[] History { get; set; } = Array.Empty(); + public ChatHistory.Message[] History { get; set; } = [ ]; /// /// Create a new session state. @@ -741,7 +741,7 @@ public static SessionState Load(string path) inputTransforms = File.Exists(inputTransformFilepath) ? (JsonSerializer.Deserialize(File.ReadAllText(inputTransformFilepath)) ?? throw new ArgumentException("Input transform file is invalid", nameof(path))) - : Array.Empty(); + : [ ]; } catch (JsonException) { diff --git a/LLama/Common/PolymorphicJSONConverter.cs b/LLama/Common/PolymorphicJSONConverter.cs index 1af4011cc..c5f4ee358 100644 --- a/LLama/Common/PolymorphicJSONConverter.cs +++ b/LLama/Common/PolymorphicJSONConverter.cs @@ -1,9 +1,6 @@ -using LLama.Abstractions; using System; -using System.Collections.Generic; using System.Linq; using System.Reflection; -using System.Text; using System.Text.Json; using System.Text.Json.Serialization; diff --git a/LLama/Extensions/EncodingExtensions.cs b/LLama/Extensions/EncodingExtensions.cs index 00b46f213..bd11b7077 100644 --- a/LLama/Extensions/EncodingExtensions.cs +++ b/LLama/Extensions/EncodingExtensions.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Text; namespace LLama.Extensions; diff --git a/LLama/Extensions/IReadOnlyListExtensions.cs b/LLama/Extensions/IReadOnlyListExtensions.cs index f23c54d65..afbd97a90 100644 --- a/LLama/Extensions/IReadOnlyListExtensions.cs +++ b/LLama/Extensions/IReadOnlyListExtensions.cs @@ -1,5 +1,4 @@ -using System; -using System.Buffers; +using System; using System.Collections; using System.Collections.Generic; using System.Text; diff --git a/LLama/Grammars/GBNFGrammarParser.cs b/LLama/Grammars/GBNFGrammarParser.cs index ac915cf2a..7e4ad8ae5 100644 --- a/LLama/Grammars/GBNFGrammarParser.cs +++ b/LLama/Grammars/GBNFGrammarParser.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -19,7 +19,7 @@ internal sealed class GBNFGrammarParser // copied from llama.cpp private static uint DecodeUTF8(ref ReadOnlySpan src) { - int[] lookup = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 4 }; + Span lookup = [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 4 ]; byte firstByte = src[0]; byte highbits = (byte)(firstByte >> 4); diff --git a/LLama/Grammars/GrammarRule.cs b/LLama/Grammars/GrammarRule.cs index c57b7084c..3d18bd722 100644 --- a/LLama/Grammars/GrammarRule.cs +++ b/LLama/Grammars/GrammarRule.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using LLama.Exceptions; using LLama.Native; diff --git a/LLama/LLamaContext.cs b/LLama/LLamaContext.cs index 892ec9f29..b4e23dd1e 100644 --- a/LLama/LLamaContext.cs +++ b/LLama/LLamaContext.cs @@ -7,13 +7,11 @@ using System.IO; using System.IO.MemoryMappedFiles; using LLama.Common; -using System.Runtime.InteropServices; using System.Threading.Tasks; using LLama.Abstractions; using LLama.Sampling; using Microsoft.Extensions.Logging; using System.Threading; -using System.Security.Cryptography; namespace LLama { diff --git a/LLama/LLamaEmbedder.cs b/LLama/LLamaEmbedder.cs index d8d97d0b8..ffbb85cd6 100644 --- a/LLama/LLamaEmbedder.cs +++ b/LLama/LLamaEmbedder.cs @@ -105,7 +105,7 @@ private float[] GetEmbeddingsArray() embeddings = NativeApi.llama_get_embeddings_seq(Context.NativeHandle, LLamaSeqId.Zero); if (embeddings == null) - return Array.Empty(); + return [ ]; return new Span(embeddings, Context.EmbeddingSize).ToArray(); } diff --git a/LLama/LLamaInstructExecutor.cs b/LLama/LLamaInstructExecutor.cs index adc82eb85..057c44c07 100644 --- a/LLama/LLamaInstructExecutor.cs +++ b/LLama/LLamaInstructExecutor.cs @@ -9,7 +9,6 @@ using System.Text.Json.Serialization; using System.Threading.Tasks; using LLama.Exceptions; -using LLama.Extensions; using Microsoft.Extensions.Logging; namespace LLama @@ -119,7 +118,7 @@ protected override Task GetLoopCondition(InferStateArgs args) /// protected override Task PreprocessInputs(string text, InferStateArgs args) { - args.Antiprompts ??= new List(); + args.Antiprompts ??= [ ]; args.Antiprompts.Add(_instructionPrefix); if (_is_prompt_run) { diff --git a/LLama/LLamaInteractExecutor.cs b/LLama/LLamaInteractExecutor.cs index 3f3f4a41e..068cf129c 100644 --- a/LLama/LLamaInteractExecutor.cs +++ b/LLama/LLamaInteractExecutor.cs @@ -9,7 +9,6 @@ using System.Text.Json.Serialization; using System.Threading.Tasks; using LLama.Exceptions; -using LLama.Extensions; using Microsoft.Extensions.Logging; diff --git a/LLama/LLamaStatelessExecutor.cs b/LLama/LLamaStatelessExecutor.cs index ca868b77d..2ba0f7c8f 100644 --- a/LLama/LLamaStatelessExecutor.cs +++ b/LLama/LLamaStatelessExecutor.cs @@ -4,7 +4,6 @@ using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; -using System.Text; using System.Threading; using LLama.Exceptions; using LLama.Native; @@ -28,9 +27,6 @@ public class StatelessExecutor // LLava Section public bool IsMultiModal => false; - /// - public bool MultiModalProject { get; } - /// public LLavaWeights? ClipModel { get; } diff --git a/LLama/LLamaTemplate.cs b/LLama/LLamaTemplate.cs index fb2268ac2..8f9c9daa2 100644 --- a/LLama/LLamaTemplate.cs +++ b/LLama/LLamaTemplate.cs @@ -1,5 +1,4 @@ using System; -using System.Buffers; using System.Collections.Generic; using System.Diagnostics; using System.Text; diff --git a/LLama/LLamaTransforms.cs b/LLama/LLamaTransforms.cs index f50d32c7f..5dfeea6b3 100644 --- a/LLama/LLamaTransforms.cs +++ b/LLama/LLamaTransforms.cs @@ -1,4 +1,4 @@ -using LLama.Abstractions; +using LLama.Abstractions; using LLama.Common; using System.Collections.Generic; using System.Linq; @@ -196,7 +196,7 @@ public KeywordTextOutputStreamTransform( int maxKeywordLength, bool removeAllMatchedTokens) { - _keywords = new(keywords); + _keywords = [ ..keywords ]; _maxKeywordLength = maxKeywordLength; _removeAllMatchedTokens = removeAllMatchedTokens; } @@ -212,7 +212,7 @@ public KeywordTextOutputStreamTransform( /// If set to true, when getting a matched keyword, all the related tokens will be removed. Otherwise only the part of keyword will be removed. public KeywordTextOutputStreamTransform(IEnumerable keywords, int redundancyLength = 3, bool removeAllMatchedTokens = false) { - _keywords = new(keywords); + _keywords = [ ..keywords ]; _maxKeywordLength = _keywords.Max(x => x.Length) + redundancyLength; _maxKeywordLength = _keywords.Select(x => x.Length).Max() + redundancyLength; _removeAllMatchedTokens = removeAllMatchedTokens; diff --git a/LLama/Native/GroupDisposable.cs b/LLama/Native/GroupDisposable.cs index 5238c1ade..82ed6a0bc 100644 --- a/LLama/Native/GroupDisposable.cs +++ b/LLama/Native/GroupDisposable.cs @@ -1,5 +1,4 @@ -using System; -using System.Buffers; +using System; using System.Collections.Generic; namespace LLama.Native; diff --git a/LLama/Native/LLamaBatch.cs b/LLama/Native/LLamaBatch.cs index 183bc1d39..91a2fafbc 100644 --- a/LLama/Native/LLamaBatch.cs +++ b/LLama/Native/LLamaBatch.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Runtime.InteropServices; namespace LLama.Native; diff --git a/LLama/Native/LLamaBatchEmbeddings.cs b/LLama/Native/LLamaBatchEmbeddings.cs index ccfed7402..052a99580 100644 --- a/LLama/Native/LLamaBatchEmbeddings.cs +++ b/LLama/Native/LLamaBatchEmbeddings.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Runtime.CompilerServices; namespace LLama.Native; diff --git a/LLama/Native/LLamaChatMessage.cs b/LLama/Native/LLamaChatMessage.cs index e731901fa..85c567474 100644 --- a/LLama/Native/LLamaChatMessage.cs +++ b/LLama/Native/LLamaChatMessage.cs @@ -1,5 +1,3 @@ -using System.Runtime.InteropServices; - namespace LLama.Native; /// diff --git a/LLama/Native/LLamaContextParams.cs b/LLama/Native/LLamaContextParams.cs index e1d1417c4..4e1d7020f 100644 --- a/LLama/Native/LLamaContextParams.cs +++ b/LLama/Native/LLamaContextParams.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.InteropServices; namespace LLama.Native { diff --git a/LLama/Native/LLamaGrammarElement.cs b/LLama/Native/LLamaGrammarElement.cs index fceeada71..c364d3493 100644 --- a/LLama/Native/LLamaGrammarElement.cs +++ b/LLama/Native/LLamaGrammarElement.cs @@ -1,5 +1,4 @@ using System.Diagnostics; -using System.Runtime.InteropServices; namespace LLama.Native { diff --git a/LLama/Native/LLamaKvCacheView.cs b/LLama/Native/LLamaKvCacheView.cs index 36379bfdd..2fa513324 100644 --- a/LLama/Native/LLamaKvCacheView.cs +++ b/LLama/Native/LLamaKvCacheView.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.InteropServices; namespace LLama.Native; diff --git a/LLama/Native/LLamaModelMetadataOverride.cs b/LLama/Native/LLamaModelMetadataOverride.cs index c0cadd1fe..b9171cb66 100644 --- a/LLama/Native/LLamaModelMetadataOverride.cs +++ b/LLama/Native/LLamaModelMetadataOverride.cs @@ -1,5 +1,3 @@ -using System.Runtime.InteropServices; - namespace LLama.Native; /// diff --git a/LLama/Native/LLamaModelParams.cs b/LLama/Native/LLamaModelParams.cs index cf0861f56..c0437d9db 100644 --- a/LLama/Native/LLamaModelParams.cs +++ b/LLama/Native/LLamaModelParams.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.InteropServices; namespace LLama.Native { diff --git a/LLama/Native/LLamaModelQuantizeParams.cs b/LLama/Native/LLamaModelQuantizeParams.cs index 4a6a4e218..8979d8724 100644 --- a/LLama/Native/LLamaModelQuantizeParams.cs +++ b/LLama/Native/LLamaModelQuantizeParams.cs @@ -1,5 +1,4 @@ -using System; -using System.Runtime.InteropServices; +using System; namespace LLama.Native { diff --git a/LLama/Native/LLamaNativeBatch.cs b/LLama/Native/LLamaNativeBatch.cs index d58c61788..8e9c40603 100644 --- a/LLama/Native/LLamaNativeBatch.cs +++ b/LLama/Native/LLamaNativeBatch.cs @@ -1,5 +1,3 @@ -using System.Runtime.InteropServices; - namespace LLama.Native; /// diff --git a/LLama/Native/LLamaPos.cs b/LLama/Native/LLamaPos.cs index 47564d554..e4394d006 100644 --- a/LLama/Native/LLamaPos.cs +++ b/LLama/Native/LLamaPos.cs @@ -1,5 +1,3 @@ -using System.Runtime.InteropServices; - namespace LLama.Native; /// diff --git a/LLama/Native/LLamaSeqId.cs b/LLama/Native/LLamaSeqId.cs index e3c6e8f43..07527cfec 100644 --- a/LLama/Native/LLamaSeqId.cs +++ b/LLama/Native/LLamaSeqId.cs @@ -1,5 +1,3 @@ -using System.Runtime.InteropServices; - namespace LLama.Native; /// diff --git a/LLama/Native/LLamaTimings.cs b/LLama/Native/LLamaTimings.cs index 63131e5f8..9391bbbf3 100644 --- a/LLama/Native/LLamaTimings.cs +++ b/LLama/Native/LLamaTimings.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.InteropServices; namespace LLama.Native; diff --git a/LLama/Native/LLamaToken.cs b/LLama/Native/LLamaToken.cs index 027030ba0..50183006a 100644 --- a/LLama/Native/LLamaToken.cs +++ b/LLama/Native/LLamaToken.cs @@ -1,5 +1,4 @@ using System.Diagnostics; -using System.Runtime.InteropServices; namespace LLama.Native; diff --git a/LLama/Native/LLamaTokenData.cs b/LLama/Native/LLamaTokenData.cs index d6dd500bc..e61c6b8cb 100644 --- a/LLama/Native/LLamaTokenData.cs +++ b/LLama/Native/LLamaTokenData.cs @@ -1,5 +1,3 @@ -using System.Runtime.InteropServices; - namespace LLama.Native; /// diff --git a/LLama/Native/LLamaTokenDataArray.cs b/LLama/Native/LLamaTokenDataArray.cs index 1a656c42e..cc2515639 100644 --- a/LLama/Native/LLamaTokenDataArray.cs +++ b/LLama/Native/LLamaTokenDataArray.cs @@ -1,6 +1,4 @@ using System; -using System.Buffers; -using System.Runtime.InteropServices; namespace LLama.Native { diff --git a/LLama/Native/LLavaImageEmbed.cs b/LLama/Native/LLavaImageEmbed.cs index 7704b73de..559a245ec 100644 --- a/LLama/Native/LLavaImageEmbed.cs +++ b/LLama/Native/LLavaImageEmbed.cs @@ -1,5 +1,3 @@ -using System.Runtime.InteropServices; - namespace LLama.Native; /// diff --git a/LLama/Native/Load/DefaultNativeLibrarySelectingPolicy.cs b/LLama/Native/Load/DefaultNativeLibrarySelectingPolicy.cs index 690fa706f..6f5ad35fe 100644 --- a/LLama/Native/Load/DefaultNativeLibrarySelectingPolicy.cs +++ b/LLama/Native/Load/DefaultNativeLibrarySelectingPolicy.cs @@ -1,6 +1,5 @@ -using LLama.Abstractions; +using LLama.Abstractions; using System.Collections.Generic; -using System.Runtime.InteropServices; namespace LLama.Native { diff --git a/LLama/Native/Load/NativeLibraryUtils.cs b/LLama/Native/Load/NativeLibraryUtils.cs index 73f407bbf..d5b014ce0 100644 --- a/LLama/Native/Load/NativeLibraryUtils.cs +++ b/LLama/Native/Load/NativeLibraryUtils.cs @@ -1,9 +1,8 @@ -using LLama.Abstractions; +using LLama.Abstractions; using LLama.Exceptions; using System; using System.Collections.Generic; using System.IO; -using System.Runtime.InteropServices; namespace LLama.Native { diff --git a/LLama/Native/Load/NativeLibraryWithAvx.cs b/LLama/Native/Load/NativeLibraryWithAvx.cs index 52e62845f..932c49866 100644 --- a/LLama/Native/Load/NativeLibraryWithAvx.cs +++ b/LLama/Native/Load/NativeLibraryWithAvx.cs @@ -1,6 +1,5 @@ -using LLama.Abstractions; +using LLama.Abstractions; using System.Collections.Generic; -using System.Runtime.InteropServices; namespace LLama.Native { diff --git a/LLama/Native/Load/NativeLibraryWithCuda.cs b/LLama/Native/Load/NativeLibraryWithCuda.cs index 982236771..12da095dc 100644 --- a/LLama/Native/Load/NativeLibraryWithCuda.cs +++ b/LLama/Native/Load/NativeLibraryWithCuda.cs @@ -1,6 +1,5 @@ -using LLama.Abstractions; +using LLama.Abstractions; using System.Collections.Generic; -using System.Runtime.InteropServices; namespace LLama.Native { diff --git a/LLama/Native/Load/NativeLibraryWithMacOrFallback.cs b/LLama/Native/Load/NativeLibraryWithMacOrFallback.cs index 55734fcc4..6bcd55049 100644 --- a/LLama/Native/Load/NativeLibraryWithMacOrFallback.cs +++ b/LLama/Native/Load/NativeLibraryWithMacOrFallback.cs @@ -1,6 +1,5 @@ -using LLama.Abstractions; +using LLama.Abstractions; using System.Collections.Generic; -using System.Runtime.InteropServices; namespace LLama.Native { diff --git a/LLama/Native/Load/NativeLibraryWithVulkan.cs b/LLama/Native/Load/NativeLibraryWithVulkan.cs index 867a52f52..fe4eef01e 100644 --- a/LLama/Native/Load/NativeLibraryWithVulkan.cs +++ b/LLama/Native/Load/NativeLibraryWithVulkan.cs @@ -1,6 +1,5 @@ -using LLama.Abstractions; +using LLama.Abstractions; using System.Collections.Generic; -using System.Runtime.InteropServices; namespace LLama.Native { diff --git a/LLama/Native/Load/SystemInfo.cs b/LLama/Native/Load/SystemInfo.cs index 2429b671c..d5516ef37 100644 --- a/LLama/Native/Load/SystemInfo.cs +++ b/LLama/Native/Load/SystemInfo.cs @@ -1,7 +1,6 @@ -using System; +using System; using System.Diagnostics; using System.IO; -using System.Runtime.InteropServices; using System.Text.Json; using System.Text.RegularExpressions; @@ -170,10 +169,10 @@ private static int GetCudaMajorVersion() { // List of default cuda paths string[] defaultCudaPaths = - { + [ "/usr/local/bin/cuda", "/usr/local/cuda", - }; + ]; // Loop through every default path to find the version foreach (var path in defaultCudaPaths) diff --git a/LLama/Native/Load/UnknownNativeLibrary.cs b/LLama/Native/Load/UnknownNativeLibrary.cs index fa29ac0d4..2d5a8052d 100644 --- a/LLama/Native/Load/UnknownNativeLibrary.cs +++ b/LLama/Native/Load/UnknownNativeLibrary.cs @@ -1,9 +1,6 @@ -using LLama.Abstractions; +using LLama.Abstractions; using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace LLama.Native { diff --git a/LLama/Native/NativeApi.Grammar.cs b/LLama/Native/NativeApi.Grammar.cs index 42a89a3f2..2e9811371 100644 --- a/LLama/Native/NativeApi.Grammar.cs +++ b/LLama/Native/NativeApi.Grammar.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.InteropServices; namespace LLama.Native { diff --git a/LLama/Native/NativeApi.LLava.cs b/LLama/Native/NativeApi.LLava.cs index 183f183a7..246a9d1b2 100644 --- a/LLama/Native/NativeApi.LLava.cs +++ b/LLama/Native/NativeApi.LLava.cs @@ -1,9 +1,7 @@ using System; -using System.Runtime.InteropServices; namespace LLama.Native; -using clip_ctx = IntPtr; public static unsafe partial class NativeApi { /// diff --git a/LLama/Native/NativeApi.Load.cs b/LLama/Native/NativeApi.Load.cs index 939c8a580..5ad30d032 100644 --- a/LLama/Native/NativeApi.Load.cs +++ b/LLama/Native/NativeApi.Load.cs @@ -1,6 +1,5 @@ using LLama.Exceptions; using System; -using System.Runtime.InteropServices; using LLama.Abstractions; namespace LLama.Native diff --git a/LLama/Native/NativeApi.Quantize.cs b/LLama/Native/NativeApi.Quantize.cs index a2a372bc8..0e4225330 100644 --- a/LLama/Native/NativeApi.Quantize.cs +++ b/LLama/Native/NativeApi.Quantize.cs @@ -1,5 +1,3 @@ -using System.Runtime.InteropServices; - namespace LLama.Native { public static partial class NativeApi diff --git a/LLama/Native/NativeApi.Sampling.cs b/LLama/Native/NativeApi.Sampling.cs index 1b30a1cf7..4b73d2e0b 100644 --- a/LLama/Native/NativeApi.Sampling.cs +++ b/LLama/Native/NativeApi.Sampling.cs @@ -1,5 +1,4 @@ -using System; -using System.Runtime.InteropServices; +using System; namespace LLama.Native { diff --git a/LLama/Native/NativeApi.cs b/LLama/Native/NativeApi.cs index 8314249ec..41d3a130b 100644 --- a/LLama/Native/NativeApi.cs +++ b/LLama/Native/NativeApi.cs @@ -1,5 +1,4 @@ using System; -using System.Runtime.InteropServices; #pragma warning disable IDE1006 // Naming Styles diff --git a/LLama/Native/NativeLogConfig.cs b/LLama/Native/NativeLogConfig.cs index 939c4d7f2..cc03bce7d 100644 --- a/LLama/Native/NativeLogConfig.cs +++ b/LLama/Native/NativeLogConfig.cs @@ -1,4 +1,3 @@ -using System.Runtime.InteropServices; using System.Text; using System.Threading; using Microsoft.Extensions.Logging; diff --git a/LLama/Native/SafeLLamaContextHandle.cs b/LLama/Native/SafeLLamaContextHandle.cs index 913a9b750..8636234bb 100644 --- a/LLama/Native/SafeLLamaContextHandle.cs +++ b/LLama/Native/SafeLLamaContextHandle.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Runtime.InteropServices; using System.Text; using LLama.Exceptions; diff --git a/LLama/Native/SafeLLamaGrammarHandle.cs b/LLama/Native/SafeLLamaGrammarHandle.cs index 6dd3fe839..53c8fda28 100644 --- a/LLama/Native/SafeLLamaGrammarHandle.cs +++ b/LLama/Native/SafeLLamaGrammarHandle.cs @@ -1,5 +1,4 @@ using System; -using System.Buffers; using System.Collections.Generic; using System.Diagnostics; using System.Linq; diff --git a/LLama/Native/SafeLLamaHandleBase.cs b/LLama/Native/SafeLLamaHandleBase.cs index 7dcc4ef95..98803151b 100644 --- a/LLama/Native/SafeLLamaHandleBase.cs +++ b/LLama/Native/SafeLLamaHandleBase.cs @@ -1,5 +1,4 @@ -using System; -using System.Runtime.InteropServices; +using System; namespace LLama.Native { diff --git a/LLama/Native/SafeLlamaModelHandle.cs b/LLama/Native/SafeLlamaModelHandle.cs index 4970a4fdf..2dcc09aa4 100644 --- a/LLama/Native/SafeLlamaModelHandle.cs +++ b/LLama/Native/SafeLlamaModelHandle.cs @@ -1,9 +1,7 @@ using System; -using System.Buffers; using System.Collections.Generic; using System.Diagnostics; using System.IO; -using System.Runtime.InteropServices; using System.Text; using LLama.Exceptions; diff --git a/LLama/Native/SafeLlavaModelHandle.cs b/LLama/Native/SafeLlavaModelHandle.cs index c94c071a4..db668c12b 100644 --- a/LLama/Native/SafeLlavaModelHandle.cs +++ b/LLama/Native/SafeLlavaModelHandle.cs @@ -1,6 +1,5 @@ using System; using System.IO; -using System.Runtime.InteropServices; using LLama.Exceptions; diff --git a/LLama/Sampling/DefaultSamplingPipeline.cs b/LLama/Sampling/DefaultSamplingPipeline.cs index 0413dd972..4f87b0030 100644 --- a/LLama/Sampling/DefaultSamplingPipeline.cs +++ b/LLama/Sampling/DefaultSamplingPipeline.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using LLama.Extensions; using LLama.Native; namespace LLama.Sampling; diff --git a/LLama/Sampling/ISamplingPipeline.cs b/LLama/Sampling/ISamplingPipeline.cs index b538d1feb..e3ec22691 100644 --- a/LLama/Sampling/ISamplingPipeline.cs +++ b/LLama/Sampling/ISamplingPipeline.cs @@ -1,7 +1,5 @@ -using System; -using System.Buffers; +using System; using System.Collections.Generic; -using System.Runtime.InteropServices; using LLama.Native; namespace LLama.Sampling; diff --git a/LLama/Sampling/Mirostat2SamplingPipeline.cs b/LLama/Sampling/Mirostat2SamplingPipeline.cs index bdf1a461e..de5de2f40 100644 --- a/LLama/Sampling/Mirostat2SamplingPipeline.cs +++ b/LLama/Sampling/Mirostat2SamplingPipeline.cs @@ -1,4 +1,4 @@ -using System; +using System; using LLama.Native; namespace LLama.Sampling; @@ -6,7 +6,7 @@ namespace LLama.Sampling; /// /// A sampling pipeline which uses mirostat (v2) to select tokens /// -public class Mirostate2SamplingPipeline +public class Mirostat2SamplingPipeline : BaseSamplingPipeline { private const float DEFAULT_TAU = 5; @@ -58,7 +58,7 @@ public override void Reset() /// public override ISamplingPipeline Clone() { - return new Mirostate2SamplingPipeline + return new Mirostat2SamplingPipeline { Grammar = Grammar?.Clone(), diff --git a/LLama/Sampling/MirostatSamplingPipeline.cs b/LLama/Sampling/MirostatSamplingPipeline.cs index 1bb111384..769d19dc9 100644 --- a/LLama/Sampling/MirostatSamplingPipeline.cs +++ b/LLama/Sampling/MirostatSamplingPipeline.cs @@ -1,4 +1,4 @@ -using System; +using System; using LLama.Native; namespace LLama.Sampling; @@ -6,7 +6,7 @@ namespace LLama.Sampling; /// /// A sampling pipeline which uses mirostat (v1) to select tokens /// -public class MirostateSamplingPipeline +public class MirostatSamplingPipeline : BaseSamplingPipeline { private const int MIROSTAT_M = 100; @@ -59,7 +59,7 @@ public override void Reset() /// public override ISamplingPipeline Clone() { - return new MirostateSamplingPipeline + return new MirostatSamplingPipeline { Grammar = Grammar?.Clone(), diff --git a/LLama/StreamingTokenDecoder.cs b/LLama/StreamingTokenDecoder.cs index aef643f43..ead0ee88e 100644 --- a/LLama/StreamingTokenDecoder.cs +++ b/LLama/StreamingTokenDecoder.cs @@ -1,4 +1,3 @@ -using System.Buffers; using System.Diagnostics; using System; using System.Collections.Generic; diff --git a/LLama/Transformers/PromptTemplateTransformer.cs b/LLama/Transformers/PromptTemplateTransformer.cs index 3543f9a1a..d61d3bfa2 100644 --- a/LLama/Transformers/PromptTemplateTransformer.cs +++ b/LLama/Transformers/PromptTemplateTransformer.cs @@ -1,5 +1,3 @@ -using System; -using System.Text; using LLama.Abstractions; using LLama.Common; @@ -56,12 +54,7 @@ public static string ToModelPrompt(LLamaTemplate template) var templateBuffer = template.Apply(); // convert the resulting buffer to a string -#if NET6_0_OR_GREATER - return LLamaTemplate.Encoding.GetString(templateBuffer); -#endif - - // need the ToArray call for netstandard -- avoided in newer runtimes - return LLamaTemplate.Encoding.GetString(templateBuffer.ToArray()); + return LLamaTemplate.Encoding.GetStringFromSpan(templateBuffer); } - #endregion utils +#endregion utils } diff --git a/LLama/Usings.cs b/LLama/Usings.cs index 1510815ab..20971356d 100644 --- a/LLama/Usings.cs +++ b/LLama/Usings.cs @@ -1 +1,3 @@ -global using LLama.Extensions; \ No newline at end of file +global using LLama.Extensions; +global using System.Buffers; +global using System.Runtime.InteropServices; \ No newline at end of file