Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade unittest target framework to .NET 8.0 #358

Merged
merged 4 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
4 changes: 2 additions & 2 deletions LLama.Unittest/LLama.Unittest.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\LLama\LLamaSharp.Runtime.targets" />
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>LLama.Unittest</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Platforms>AnyCPU;x64</Platforms>
Expand Down
10 changes: 8 additions & 2 deletions LLama.Unittest/ModelsParamsTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using LLama.Common;
using System.Text.Json;

namespace LLama.Unittest
{
Expand All @@ -16,14 +17,19 @@ public void SerializeRoundTripSystemTextJson()
TensorSplits = { [0] = 3 }
};

var json = System.Text.Json.JsonSerializer.Serialize(expected);
var actual = System.Text.Json.JsonSerializer.Deserialize<ModelParams>(json)!;
var json = JsonSerializer.Serialize(expected);
var actual = JsonSerializer.Deserialize<ModelParams>(json)!;

// Cannot compare splits with default equality, check they are sequence equal and then set to null
Assert.Equal((IEnumerable<float>)expected.TensorSplits, expected.TensorSplits);
actual.TensorSplits = null!;
expected.TensorSplits = null!;

// Check encoding is the same
var b1 = expected.Encoding.GetBytes("Hello");
var b2 = actual.Encoding.GetBytes("Hello");
Assert.True(b1.SequenceEqual(b2));

Assert.Equal(expected, actual);
}

Expand Down
30 changes: 12 additions & 18 deletions LLama/Common/ModelParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,19 @@ public record ModelParams
/// <inheritdoc />
public bool VocabOnly { get; set; }

/// <summary>
/// `Encoding` cannot be directly JSON serialized, instead store the name as a string which can
/// </summary>
[JsonPropertyName("Encoding")]
martindevans marked this conversation as resolved.
Show resolved Hide resolved
private string EncodingName { get; set; } = Encoding.UTF8.WebName;

/// <inheritdoc />
[JsonConverter(typeof(EncodingConverter))]
public Encoding Encoding { get; set; } = Encoding.UTF8;
[JsonIgnore]
public Encoding Encoding
{
get => Encoding.GetEncoding(EncodingName);
set => EncodingName = value.WebName;
}

/// <summary>
///
Expand All @@ -113,22 +123,6 @@ private ModelParams()
}
}

internal class EncodingConverter
: JsonConverter<Encoding>
{
public override Encoding? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var name = reader.GetString();
if (name == null)
return null;
return Encoding.GetEncoding(name);
}

public override void Write(Utf8JsonWriter writer, Encoding value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.WebName);
}
}

internal class TensorSplitsCollectionConverter
: JsonConverter<TensorSplitsCollection>
Expand Down
Loading