You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ModelsParamsTests.cs 3.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. using System.Text;
  2. using LLama.Common;
  3. using Newtonsoft.Json;
  4. namespace LLama.Unittest
  5. {
  6. public class ModelsParamsTests
  7. {
  8. [Fact]
  9. public void SerializeRoundTripSystemTextJson()
  10. {
  11. var expected = new ModelParams("abc/123")
  12. {
  13. BatchSize = 17,
  14. ContextSize = 42,
  15. LoraAdapter = "adapter",
  16. GroupedQueryAttention = 7,
  17. Seed = 42,
  18. GpuLayerCount = 111
  19. };
  20. var options = new System.Text.Json.JsonSerializerOptions();
  21. options.Converters.Add(new SystemTextJsonEncodingConverter());
  22. var json = System.Text.Json.JsonSerializer.Serialize(expected, options);
  23. var actual = System.Text.Json.JsonSerializer.Deserialize<ModelParams>(json, options);
  24. Assert.Equal(expected, actual);
  25. }
  26. private class SystemTextJsonEncodingConverter
  27. : System.Text.Json.Serialization.JsonConverter<Encoding>
  28. {
  29. public override Encoding? Read(ref System.Text.Json.Utf8JsonReader reader, Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
  30. {
  31. var name = reader.GetString();
  32. if (name == null)
  33. return null;
  34. return Encoding.GetEncoding(name);
  35. }
  36. public override void Write(System.Text.Json.Utf8JsonWriter writer, Encoding value, System.Text.Json.JsonSerializerOptions options)
  37. {
  38. writer.WriteStringValue(value.WebName);
  39. }
  40. }
  41. [Fact]
  42. public void SerializeRoundTripNewtonsoft()
  43. {
  44. var expected = new ModelParams("abc/123")
  45. {
  46. BatchSize = 17,
  47. ContextSize = 42,
  48. LoraAdapter = "adapter",
  49. GroupedQueryAttention = 7,
  50. Seed = 42,
  51. GpuLayerCount = 111
  52. };
  53. var settings = new Newtonsoft.Json.JsonSerializerSettings();
  54. settings.Converters.Add(new NewtsonsoftEncodingConverter());
  55. var json = Newtonsoft.Json.JsonConvert.SerializeObject(expected, settings);
  56. var actual = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelParams>(json, settings);
  57. Assert.Equal(expected, actual);
  58. }
  59. private class NewtsonsoftEncodingConverter
  60. : Newtonsoft.Json.JsonConverter<Encoding>
  61. {
  62. public override void WriteJson(JsonWriter writer, Encoding? value, JsonSerializer serializer)
  63. {
  64. writer.WriteValue((string?)value?.WebName);
  65. }
  66. public override Encoding? ReadJson(JsonReader reader, Type objectType, Encoding? existingValue, bool hasExistingValue, JsonSerializer serializer)
  67. {
  68. var name = (string?)reader.Value;
  69. if (name == null)
  70. return null;
  71. return Encoding.GetEncoding(name);
  72. }
  73. }
  74. }
  75. }