From 2830e5755c99f624b1e55e3f7be33a4433fd0ac3 Mon Sep 17 00:00:00 2001 From: Martin Evans Date: Tue, 22 Aug 2023 23:20:13 +0100 Subject: [PATCH] - Applied a lot of minor R# code quality suggestions. Lots of unnecessary imports removed. - Deleted `NativeInfo` (internal class, not used anywhere) --- .../NewVersion/ChatSessionStripRoleName.cs | 1 - .../NewVersion/ChatSessionWithRoleName.cs | 1 - LLama.Examples/NewVersion/InstructModeExecute.cs | 1 - .../NewVersion/InteractiveModeExecute.cs | 1 - LLama.Examples/NewVersion/LoadAndSaveSession.cs | 1 - LLama.Examples/NewVersion/LoadAndSaveState.cs | 1 - LLama.Examples/Program.cs | 5 +---- LLama.Unittest/BasicTest.cs | 1 - LLama.Unittest/GrammarTest.cs | 3 +-- LLama.Unittest/LLamaContextTests.cs | 3 +-- LLama/Abstractions/IHistoryTransform.cs | 3 --- LLama/Abstractions/ILLamaExecutor.cs | 5 +---- LLama/Abstractions/ITextStreamTransform.cs | 5 ++--- LLama/Abstractions/ITextTransform.cs | 6 +----- LLama/Common/ModelParams.cs | 1 - LLama/LLamaExecutorBase.cs | 5 +---- LLama/LLamaInstructExecutor.cs | 7 ++----- LLama/LLamaInteractExecutor.cs | 2 +- LLama/Native/LLamaContextParams.cs | 3 +-- LLama/Native/LLamaModelQuantizeParams.cs | 4 +--- LLama/Native/LLamaTokenData.cs | 5 +---- LLama/Native/NativeApi.Quantize.cs | 7 ++----- LLama/Native/NativeInfo.cs | 15 --------------- LLama/Native/SafeLlamaModelHandle.cs | 3 ++- LLama/Utils.cs | 2 +- 25 files changed, 19 insertions(+), 72 deletions(-) delete mode 100644 LLama/Native/NativeInfo.cs diff --git a/LLama.Examples/NewVersion/ChatSessionStripRoleName.cs b/LLama.Examples/NewVersion/ChatSessionStripRoleName.cs index 46c0505e..274a1bac 100644 --- a/LLama.Examples/NewVersion/ChatSessionStripRoleName.cs +++ b/LLama.Examples/NewVersion/ChatSessionStripRoleName.cs @@ -1,5 +1,4 @@ using LLama.Common; -using System.Text; namespace LLama.Examples.NewVersion { diff --git a/LLama.Examples/NewVersion/ChatSessionWithRoleName.cs b/LLama.Examples/NewVersion/ChatSessionWithRoleName.cs index 5e155252..b04fbcf3 100644 --- a/LLama.Examples/NewVersion/ChatSessionWithRoleName.cs +++ b/LLama.Examples/NewVersion/ChatSessionWithRoleName.cs @@ -1,5 +1,4 @@ using LLama.Common; -using System.Text; namespace LLama.Examples.NewVersion { diff --git a/LLama.Examples/NewVersion/InstructModeExecute.cs b/LLama.Examples/NewVersion/InstructModeExecute.cs index d5b31416..9eabe04a 100644 --- a/LLama.Examples/NewVersion/InstructModeExecute.cs +++ b/LLama.Examples/NewVersion/InstructModeExecute.cs @@ -1,5 +1,4 @@ using LLama.Common; -using System.Text; namespace LLama.Examples.NewVersion { diff --git a/LLama.Examples/NewVersion/InteractiveModeExecute.cs b/LLama.Examples/NewVersion/InteractiveModeExecute.cs index cc7c2891..cbaf8844 100644 --- a/LLama.Examples/NewVersion/InteractiveModeExecute.cs +++ b/LLama.Examples/NewVersion/InteractiveModeExecute.cs @@ -1,5 +1,4 @@ using LLama.Common; -using System.Text; namespace LLama.Examples.NewVersion { diff --git a/LLama.Examples/NewVersion/LoadAndSaveSession.cs b/LLama.Examples/NewVersion/LoadAndSaveSession.cs index 9ca07de0..ff9c1237 100644 --- a/LLama.Examples/NewVersion/LoadAndSaveSession.cs +++ b/LLama.Examples/NewVersion/LoadAndSaveSession.cs @@ -1,5 +1,4 @@ using LLama.Common; -using System.Text; namespace LLama.Examples.NewVersion { diff --git a/LLama.Examples/NewVersion/LoadAndSaveState.cs b/LLama.Examples/NewVersion/LoadAndSaveState.cs index d72fade8..af7e6151 100644 --- a/LLama.Examples/NewVersion/LoadAndSaveState.cs +++ b/LLama.Examples/NewVersion/LoadAndSaveState.cs @@ -1,5 +1,4 @@ using LLama.Common; -using System.Text; namespace LLama.Examples.NewVersion { diff --git a/LLama.Examples/Program.cs b/LLama.Examples/Program.cs index 9e5baaef..1699fa66 100644 --- a/LLama.Examples/Program.cs +++ b/LLama.Examples/Program.cs @@ -1,7 +1,4 @@ -using LLama; -using LLama.Common; -using LLama.Examples; -using LLama.Examples.NewVersion; +using LLama.Examples.NewVersion; using LLama.Examples.Old; Console.WriteLine("======================================================================================================"); diff --git a/LLama.Unittest/BasicTest.cs b/LLama.Unittest/BasicTest.cs index 6ad2e8a9..6fc206ed 100644 --- a/LLama.Unittest/BasicTest.cs +++ b/LLama.Unittest/BasicTest.cs @@ -1,4 +1,3 @@ -using System.Text; using LLama.Common; namespace LLama.Unittest diff --git a/LLama.Unittest/GrammarTest.cs b/LLama.Unittest/GrammarTest.cs index 081fe99c..eebbf1d1 100644 --- a/LLama.Unittest/GrammarTest.cs +++ b/LLama.Unittest/GrammarTest.cs @@ -1,5 +1,4 @@ -using System.Text; -using LLama.Common; +using LLama.Common; using LLama.Native; namespace LLama.Unittest diff --git a/LLama.Unittest/LLamaContextTests.cs b/LLama.Unittest/LLamaContextTests.cs index 1c5fa952..c6d2dc21 100644 --- a/LLama.Unittest/LLamaContextTests.cs +++ b/LLama.Unittest/LLamaContextTests.cs @@ -1,5 +1,4 @@ -using System.Text; -using LLama.Common; +using LLama.Common; namespace LLama.Unittest { diff --git a/LLama/Abstractions/IHistoryTransform.cs b/LLama/Abstractions/IHistoryTransform.cs index 75a78bc5..c9217ae0 100644 --- a/LLama/Abstractions/IHistoryTransform.cs +++ b/LLama/Abstractions/IHistoryTransform.cs @@ -1,7 +1,4 @@ using LLama.Common; -using System; -using System.Collections.Generic; -using System.Text; namespace LLama.Abstractions { diff --git a/LLama/Abstractions/ILLamaExecutor.cs b/LLama/Abstractions/ILLamaExecutor.cs index 43a6092b..a7af0243 100644 --- a/LLama/Abstractions/ILLamaExecutor.cs +++ b/LLama/Abstractions/ILLamaExecutor.cs @@ -1,7 +1,4 @@ -using LLama.Common; -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; using System.Threading; namespace LLama.Abstractions diff --git a/LLama/Abstractions/ITextStreamTransform.cs b/LLama/Abstractions/ITextStreamTransform.cs index af083564..e96febcf 100644 --- a/LLama/Abstractions/ITextStreamTransform.cs +++ b/LLama/Abstractions/ITextStreamTransform.cs @@ -1,6 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Collections.Generic; namespace LLama.Abstractions { @@ -15,6 +13,7 @@ namespace LLama.Abstractions /// /// IEnumerable Transform(IEnumerable tokens); + /// /// Takes a stream of tokens and transforms them, returning a new stream of tokens asynchronously. /// diff --git a/LLama/Abstractions/ITextTransform.cs b/LLama/Abstractions/ITextTransform.cs index c165e807..ac196644 100644 --- a/LLama/Abstractions/ITextTransform.cs +++ b/LLama/Abstractions/ITextTransform.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace LLama.Abstractions +namespace LLama.Abstractions { /// /// An interface for text transformations. diff --git a/LLama/Common/ModelParams.cs b/LLama/Common/ModelParams.cs index 53962b2b..bc26965c 100644 --- a/LLama/Common/ModelParams.cs +++ b/LLama/Common/ModelParams.cs @@ -1,6 +1,5 @@ using LLama.Abstractions; using System; -using System.Text; namespace LLama.Common { diff --git a/LLama/LLamaExecutorBase.cs b/LLama/LLamaExecutorBase.cs index 5f2b129e..173cc10d 100644 --- a/LLama/LLamaExecutorBase.cs +++ b/LLama/LLamaExecutorBase.cs @@ -266,10 +266,7 @@ namespace LLama public virtual IEnumerable Infer(string text, IInferenceParams? inferenceParams = null, CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); - if (inferenceParams is null) - { - inferenceParams = new InferenceParams(); - } + inferenceParams ??= new InferenceParams(); InferStateArgs args = new InferStateArgs() { diff --git a/LLama/LLamaInstructExecutor.cs b/LLama/LLamaInstructExecutor.cs index 3d5a3356..982cea64 100644 --- a/LLama/LLamaInstructExecutor.cs +++ b/LLama/LLamaInstructExecutor.cs @@ -108,10 +108,7 @@ namespace LLama /// protected override void PreprocessInputs(string text, InferStateArgs args) { - if(args.Antiprompts is null) - { - args.Antiprompts = new List(); - } + args.Antiprompts ??= new List(); args.Antiprompts.Add(_instructionPrefix); if (_is_prompt_run) { @@ -160,7 +157,7 @@ namespace LLama if (_pastTokensCount > 0 && args.WaitForInput) { - extraOutputs = new string[] { "\n> " }; + extraOutputs = new[] { "\n> " }; return true; } } diff --git a/LLama/LLamaInteractExecutor.cs b/LLama/LLamaInteractExecutor.cs index a738e981..94aa7c2a 100644 --- a/LLama/LLamaInteractExecutor.cs +++ b/LLama/LLamaInteractExecutor.cs @@ -154,7 +154,7 @@ namespace LLama if (_embeds.Count > 0 && _embeds.Last() == NativeApi.llama_token_eos()) { - extraOutputs = new string[] { " [end of text]\n" }; + extraOutputs = new[] { " [end of text]\n" }; return true; } diff --git a/LLama/Native/LLamaContextParams.cs b/LLama/Native/LLamaContextParams.cs index 5fd900d7..8747dba4 100644 --- a/LLama/Native/LLamaContextParams.cs +++ b/LLama/Native/LLamaContextParams.cs @@ -1,11 +1,10 @@ using System; -using System.Collections.Generic; using System.Runtime.InteropServices; -using System.Text; namespace LLama.Native { public delegate void LlamaProgressCallback(float progress, IntPtr ctx); + [StructLayout(LayoutKind.Sequential)] public struct LLamaContextParams { diff --git a/LLama/Native/LLamaModelQuantizeParams.cs b/LLama/Native/LLamaModelQuantizeParams.cs index 53887ac0..3335918a 100644 --- a/LLama/Native/LLamaModelQuantizeParams.cs +++ b/LLama/Native/LLamaModelQuantizeParams.cs @@ -1,6 +1,4 @@ -using System.Runtime.InteropServices; - -namespace LLama.Native +namespace LLama.Native { /// /// Quantizer parameters used in the native API diff --git a/LLama/Native/LLamaTokenData.cs b/LLama/Native/LLamaTokenData.cs index a5ffda59..0d3a56fc 100644 --- a/LLama/Native/LLamaTokenData.cs +++ b/LLama/Native/LLamaTokenData.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Runtime.InteropServices; -using System.Text; +using System.Runtime.InteropServices; namespace LLama.Native { diff --git a/LLama/Native/NativeApi.Quantize.cs b/LLama/Native/NativeApi.Quantize.cs index 8b201dde..d4ff5cf8 100644 --- a/LLama/Native/NativeApi.Quantize.cs +++ b/LLama/Native/NativeApi.Quantize.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Runtime.InteropServices; -using System.Text; +using System.Runtime.InteropServices; namespace LLama.Native { @@ -16,6 +13,6 @@ namespace LLama.Native /// not great API - very likely to change /// Returns 0 on success [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)] - public unsafe static extern int llama_model_quantize(string fname_inp, string fname_out, LLamaModelQuantizeParams* param); + public static extern unsafe int llama_model_quantize(string fname_inp, string fname_out, LLamaModelQuantizeParams* param); } } diff --git a/LLama/Native/NativeInfo.cs b/LLama/Native/NativeInfo.cs deleted file mode 100644 index 6711db78..00000000 --- a/LLama/Native/NativeInfo.cs +++ /dev/null @@ -1,15 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace LLama.Native -{ - internal class NativeInfo - { - internal static readonly int LLAMA_FILE_VERSION = 1; - internal static readonly string LLAMA_FILE_MAGIC = "ggjt"; - internal static readonly string LLAMA_FILE_MAGIC_UNVERSIONED = "ggml"; - internal static readonly string LLAMA_SESSION_MAGIC = "ggsn"; - internal static readonly int LLAMA_SESSION_VERSION = 1; - } -} diff --git a/LLama/Native/SafeLlamaModelHandle.cs b/LLama/Native/SafeLlamaModelHandle.cs index 4dadee50..d9afb6ee 100644 --- a/LLama/Native/SafeLlamaModelHandle.cs +++ b/LLama/Native/SafeLlamaModelHandle.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics; using System.Text; using LLama.Exceptions; @@ -150,7 +151,7 @@ namespace LLama.Native var tokens = new int[count]; fixed (int* tokensPtr = &tokens[0]) { - count = NativeApi.llama_tokenize_with_model(this, bytesPtr, tokensPtr, count, add_bos); + NativeApi.llama_tokenize_with_model(this, bytesPtr, tokensPtr, count, add_bos); return tokens; } } diff --git a/LLama/Utils.cs b/LLama/Utils.cs index 45acad76..482e1fd1 100644 --- a/LLama/Utils.cs +++ b/LLama/Utils.cs @@ -102,7 +102,7 @@ namespace LLama /// public static bool SignedByteToBool(sbyte value) { - return value > 0 ? true : false; + return value > 0; } }