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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 expected = new ModelParams("abc/123")
  12. {
  13. BatchSize = 17,
  14. ContextSize = 42,
  15. Seed = 42,
  16. GpuLayerCount = 111,
  17. TensorSplits = { [0] = 3 },
  18. MetadataOverrides =
  19. {
  20. MetadataOverride.Create("hello", true),
  21. MetadataOverride.Create("world", 17),
  22. }
  23. };
  24. var json = JsonSerializer.Serialize(expected);
  25. var actual = JsonSerializer.Deserialize<ModelParams>(json)!;
  26. // Cannot compare splits with default equality, check they are sequence equal and then set to null
  27. Assert.Equal((IEnumerable<float>)expected.TensorSplits, expected.TensorSplits);
  28. actual.TensorSplits = null!;
  29. expected.TensorSplits = null!;
  30. // Check encoding is the same
  31. var b1 = expected.Encoding.GetBytes("Hello");
  32. var b2 = actual.Encoding.GetBytes("Hello");
  33. Assert.True(b1.SequenceEqual(b2));
  34. Assert.Equal(expected, actual);
  35. }
  36. //[Fact]
  37. //public void SerializeRoundTripNewtonsoft()
  38. //{
  39. // var expected = new ModelParams("abc/123")
  40. // {
  41. // BatchSize = 17,
  42. // ContextSize = 42,
  43. // Seed = 42,
  44. // GpuLayerCount = 111,
  45. // LoraAdapters =
  46. // {
  47. // new("abc", 1),
  48. // new("def", 0)
  49. // },
  50. // TensorSplits = { [0] = 3 }
  51. // };
  52. // var settings = new Newtonsoft.Json.JsonSerializerSettings();
  53. // var json = Newtonsoft.Json.JsonConvert.SerializeObject(expected, settings);
  54. // var actual = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelParams>(json, settings)!;
  55. // // Cannot compare splits with default equality, check they are sequence equal and then set to null
  56. // Assert.Equal((IEnumerable<float>)expected.TensorSplits, expected.TensorSplits);
  57. // actual.TensorSplits = null!;
  58. // expected.TensorSplits = null!;
  59. // Assert.Equal(expected, actual);
  60. //}
  61. }
  62. }