Skip to content

Commit

Permalink
Merge pull request SciSharp#853 from martindevans/general_cleanup
Browse files Browse the repository at this point in the history
General Code Cleanup
  • Loading branch information
martindevans authored Jul 13, 2024
2 parents e907146 + 17c1ba0 commit e66b375
Show file tree
Hide file tree
Showing 62 changed files with 53 additions and 143 deletions.
3 changes: 1 addition & 2 deletions LLama/Abstractions/IModelParams.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Buffers;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
Expand Down Expand Up @@ -214,7 +213,7 @@ public class TensorSplitsCollectionConverter
/// <inheritdoc/>
public override TensorSplitsCollection Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var arr = JsonSerializer.Deserialize<float[]>(ref reader, options) ?? Array.Empty<float>();
var arr = JsonSerializer.Deserialize<float[]>(ref reader, options) ?? [ ];
return new TensorSplitsCollection(arr);
}

Expand Down
4 changes: 1 addition & 3 deletions LLama/Abstractions/INativeLibrary.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using LLama.Native;
using System;
using LLama.Native;
using System.Collections.Generic;
using System.Text;

namespace LLama.Abstractions
{
Expand Down
4 changes: 1 addition & 3 deletions LLama/Abstractions/INativeLibrarySelectingPolicy.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
using LLama.Native;
using System;
using LLama.Native;
using System.Collections.Generic;
using System.Text;

namespace LLama.Abstractions
{
Expand Down
34 changes: 11 additions & 23 deletions LLama/Batched/Conversation.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<float>.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<float> dest, (int index, SafeLlavaImageEmbedHandle embedding) tup) => tup.embedding.GetEmbedding(dest, tup.index),
_end++,
ConversationId,
i == embedding.Model.PatchCount - 1
);
}
}
finally
{
ArrayPool<float>.Shared.Return(tempArr);
batch.Add(
(i, embedding),
static (Span<float> dest, (int index, SafeLlavaImageEmbedHandle embedding) tup) => tup.embedding.GetEmbedding(dest, tup.index),
_end++,
ConversationId,
i == embedding.Model.PatchCount - 1
);
}
}

Expand Down
6 changes: 3 additions & 3 deletions LLama/ChatSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,7 @@ public record SessionState
/// <summary>
/// The input transform pipeline used in this session.
/// </summary>
public ITextTransform[] InputTransformPipeline { get; set; } = Array.Empty<ITextTransform>();
public ITextTransform[] InputTransformPipeline { get; set; } = [ ];

/// <summary>
/// The output transform used in this session.
Expand All @@ -639,7 +639,7 @@ public record SessionState
/// <summary>
/// The the chat history messages for this session.
/// </summary>
public ChatHistory.Message[] History { get; set; } = Array.Empty<ChatHistory.Message>();
public ChatHistory.Message[] History { get; set; } = [ ];

/// <summary>
/// Create a new session state.
Expand Down Expand Up @@ -741,7 +741,7 @@ public static SessionState Load(string path)
inputTransforms = File.Exists(inputTransformFilepath) ?
(JsonSerializer.Deserialize<ITextTransform[]>(File.ReadAllText(inputTransformFilepath))
?? throw new ArgumentException("Input transform file is invalid", nameof(path)))
: Array.Empty<ITextTransform>();
: [ ];
}
catch (JsonException)
{
Expand Down
3 changes: 0 additions & 3 deletions LLama/Common/PolymorphicJSONConverter.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
2 changes: 1 addition & 1 deletion LLama/Extensions/EncodingExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Text;

namespace LLama.Extensions;
Expand Down
3 changes: 1 addition & 2 deletions LLama/Extensions/IReadOnlyListExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Buffers;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
Expand Down
4 changes: 2 additions & 2 deletions LLama/Grammars/GBNFGrammarParser.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
Expand All @@ -19,7 +19,7 @@ internal sealed class GBNFGrammarParser
// copied from llama.cpp
private static uint DecodeUTF8(ref ReadOnlySpan<byte> src)
{
int[] lookup = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 4 };
Span<int> 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);
Expand Down
2 changes: 1 addition & 1 deletion LLama/Grammars/GrammarRule.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using LLama.Exceptions;
using LLama.Native;
Expand Down
2 changes: 0 additions & 2 deletions LLama/LLamaContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion LLama/LLamaEmbedder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private float[] GetEmbeddingsArray()
embeddings = NativeApi.llama_get_embeddings_seq(Context.NativeHandle, LLamaSeqId.Zero);

if (embeddings == null)
return Array.Empty<float>();
return [ ];

return new Span<float>(embeddings, Context.EmbeddingSize).ToArray();
}
Expand Down
3 changes: 1 addition & 2 deletions LLama/LLamaInstructExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -119,7 +118,7 @@ protected override Task<bool> GetLoopCondition(InferStateArgs args)
/// <inheritdoc />
protected override Task PreprocessInputs(string text, InferStateArgs args)
{
args.Antiprompts ??= new List<string>();
args.Antiprompts ??= [ ];
args.Antiprompts.Add(_instructionPrefix);
if (_is_prompt_run)
{
Expand Down
1 change: 0 additions & 1 deletion LLama/LLamaInteractExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using LLama.Exceptions;
using LLama.Extensions;
using Microsoft.Extensions.Logging;


Expand Down
4 changes: 0 additions & 4 deletions LLama/LLamaStatelessExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,9 +27,6 @@ public class StatelessExecutor
// LLava Section
public bool IsMultiModal => false;

/// <inheritdoc />
public bool MultiModalProject { get; }

/// <inheritdoc />
public LLavaWeights? ClipModel { get; }

Expand Down
1 change: 0 additions & 1 deletion LLama/LLamaTemplate.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
Expand Down
6 changes: 3 additions & 3 deletions LLama/LLamaTransforms.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using LLama.Abstractions;
using LLama.Abstractions;
using LLama.Common;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -196,7 +196,7 @@ public KeywordTextOutputStreamTransform(
int maxKeywordLength,
bool removeAllMatchedTokens)
{
_keywords = new(keywords);
_keywords = [ ..keywords ];
_maxKeywordLength = maxKeywordLength;
_removeAllMatchedTokens = removeAllMatchedTokens;
}
Expand All @@ -212,7 +212,7 @@ public KeywordTextOutputStreamTransform(
/// <param name="removeAllMatchedTokens">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.</param>
public KeywordTextOutputStreamTransform(IEnumerable<string> 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;
Expand Down
3 changes: 1 addition & 2 deletions LLama/Native/GroupDisposable.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Buffers;
using System;
using System.Collections.Generic;

namespace LLama.Native;
Expand Down
1 change: 0 additions & 1 deletion LLama/Native/LLamaBatch.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace LLama.Native;

Expand Down
1 change: 0 additions & 1 deletion LLama/Native/LLamaBatchEmbeddings.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace LLama.Native;

Expand Down
2 changes: 0 additions & 2 deletions LLama/Native/LLamaChatMessage.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Runtime.InteropServices;

namespace LLama.Native;

/// <summary>
Expand Down
1 change: 0 additions & 1 deletion LLama/Native/LLamaContextParams.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Runtime.InteropServices;

namespace LLama.Native
{
Expand Down
1 change: 0 additions & 1 deletion LLama/Native/LLamaGrammarElement.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace LLama.Native
{
Expand Down
1 change: 0 additions & 1 deletion LLama/Native/LLamaKvCacheView.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Runtime.InteropServices;

namespace LLama.Native;

Expand Down
2 changes: 0 additions & 2 deletions LLama/Native/LLamaModelMetadataOverride.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Runtime.InteropServices;

namespace LLama.Native;

/// <summary>
Expand Down
1 change: 0 additions & 1 deletion LLama/Native/LLamaModelParams.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Runtime.InteropServices;

namespace LLama.Native
{
Expand Down
3 changes: 1 addition & 2 deletions LLama/Native/LLamaModelQuantizeParams.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Runtime.InteropServices;
using System;

namespace LLama.Native
{
Expand Down
2 changes: 0 additions & 2 deletions LLama/Native/LLamaNativeBatch.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Runtime.InteropServices;

namespace LLama.Native;

/// <summary>
Expand Down
2 changes: 0 additions & 2 deletions LLama/Native/LLamaPos.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Runtime.InteropServices;

namespace LLama.Native;

/// <summary>
Expand Down
2 changes: 0 additions & 2 deletions LLama/Native/LLamaSeqId.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Runtime.InteropServices;

namespace LLama.Native;

/// <summary>
Expand Down
1 change: 0 additions & 1 deletion LLama/Native/LLamaTimings.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Runtime.InteropServices;

namespace LLama.Native;

Expand Down
1 change: 0 additions & 1 deletion LLama/Native/LLamaToken.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace LLama.Native;

Expand Down
2 changes: 0 additions & 2 deletions LLama/Native/LLamaTokenData.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Runtime.InteropServices;

namespace LLama.Native;

/// <summary>
Expand Down
2 changes: 0 additions & 2 deletions LLama/Native/LLamaTokenDataArray.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Buffers;
using System.Runtime.InteropServices;

namespace LLama.Native
{
Expand Down
2 changes: 0 additions & 2 deletions LLama/Native/LLavaImageEmbed.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Runtime.InteropServices;

namespace LLama.Native;

/// <summary>
Expand Down
3 changes: 1 addition & 2 deletions LLama/Native/Load/DefaultNativeLibrarySelectingPolicy.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using LLama.Abstractions;
using LLama.Abstractions;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace LLama.Native
{
Expand Down
3 changes: 1 addition & 2 deletions LLama/Native/Load/NativeLibraryUtils.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
3 changes: 1 addition & 2 deletions LLama/Native/Load/NativeLibraryWithAvx.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using LLama.Abstractions;
using LLama.Abstractions;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace LLama.Native
{
Expand Down
3 changes: 1 addition & 2 deletions LLama/Native/Load/NativeLibraryWithCuda.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using LLama.Abstractions;
using LLama.Abstractions;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace LLama.Native
{
Expand Down
Loading

0 comments on commit e66b375

Please sign in to comment.