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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using LLama.Common;
  2. using System.Text.Json;
  3. namespace LLama.Unittest
  4. {
  5. public class ModelsParamsTests
  6. {
  7. [Fact]
  8. public void SerializeRoundTripSystemTextJson()
  9. {
  10. var expected = new ModelParams("abc/123")
  11. {
  12. BatchSize = 17,
  13. ContextSize = 42,
  14. Seed = 42,
  15. GpuLayerCount = 111,
  16. TensorSplits = { [0] = 3 }
  17. };
  18. var json = JsonSerializer.Serialize(expected);
  19. var actual = JsonSerializer.Deserialize<ModelParams>(json)!;
  20. // Cannot compare splits with default equality, check they are sequence equal and then set to null
  21. Assert.Equal((IEnumerable<float>)expected.TensorSplits, expected.TensorSplits);
  22. actual.TensorSplits = null!;
  23. expected.TensorSplits = null!;
  24. // Check encoding is the same
  25. var b1 = expected.Encoding.GetBytes("Hello");
  26. var b2 = actual.Encoding.GetBytes("Hello");
  27. Assert.True(b1.SequenceEqual(b2));
  28. Assert.Equal(expected, actual);
  29. }
  30. //[Fact]
  31. //public void SerializeRoundTripNewtonsoft()
  32. //{
  33. // var expected = new ModelParams("abc/123")
  34. // {
  35. // BatchSize = 17,
  36. // ContextSize = 42,
  37. // Seed = 42,
  38. // GpuLayerCount = 111,
  39. // LoraAdapters =
  40. // {
  41. // new("abc", 1),
  42. // new("def", 0)
  43. // },
  44. // TensorSplits = { [0] = 3 }
  45. // };
  46. // var settings = new Newtonsoft.Json.JsonSerializerSettings();
  47. // var json = Newtonsoft.Json.JsonConvert.SerializeObject(expected, settings);
  48. // var actual = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelParams>(json, settings)!;
  49. // // Cannot compare splits with default equality, check they are sequence equal and then set to null
  50. // Assert.Equal((IEnumerable<float>)expected.TensorSplits, expected.TensorSplits);
  51. // actual.TensorSplits = null!;
  52. // expected.TensorSplits = null!;
  53. // Assert.Equal(expected, actual);
  54. //}
  55. }
  56. }