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 1.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using LLama.Common;
  2. using System.Text.Json;
  3. using LLama.Abstractions;
  4. namespace LLama.Unittest
  5. {
  6. public class ModelsParamsTests
  7. {
  8. [Fact]
  9. public void SerializeRoundTripSystemTextJson()
  10. {
  11. var options = new JsonSerializerOptions()
  12. {
  13. WriteIndented = true,
  14. };
  15. var expected = new ModelParams("abc/123")
  16. {
  17. BatchSize = 17,
  18. ContextSize = 42,
  19. Seed = 42,
  20. GpuLayerCount = 111,
  21. TensorSplits = { [0] = 3 },
  22. MetadataOverrides =
  23. {
  24. new MetadataOverride("hello", true),
  25. new MetadataOverride("world", 17),
  26. new MetadataOverride("cats", 17f),
  27. }
  28. };
  29. var json = JsonSerializer.Serialize(expected, options);
  30. var actual = JsonSerializer.Deserialize<ModelParams>(json, options)!;
  31. // Cannot compare splits with default equality, check they are sequence equal and then set to null
  32. Assert.True(expected.TensorSplits.SequenceEqual(actual.TensorSplits));
  33. actual.TensorSplits = null!;
  34. expected.TensorSplits = null!;
  35. // Cannot compare overrides with default equality, check they are sequence equal and then set to null
  36. Assert.True(expected.MetadataOverrides.SequenceEqual(actual.MetadataOverrides));
  37. actual.MetadataOverrides = null!;
  38. expected.MetadataOverrides = null!;
  39. // Check encoding is the same
  40. var b1 = expected.Encoding.GetBytes("Hello");
  41. var b2 = actual.Encoding.GetBytes("Hello");
  42. Assert.True(b1.SequenceEqual(b2));
  43. Assert.Equal(expected, actual);
  44. }
  45. }
  46. }