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.

BasicTest.cs 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System.Text;
  2. using LLama.Common;
  3. using LLama.Native;
  4. using Xunit.Abstractions;
  5. namespace LLama.Unittest
  6. {
  7. public sealed class BasicTest
  8. : IDisposable
  9. {
  10. private readonly ITestOutputHelper _testOutputHelper;
  11. private readonly ModelParams _params;
  12. private readonly LLamaWeights _model;
  13. public BasicTest(ITestOutputHelper testOutputHelper)
  14. {
  15. _testOutputHelper = testOutputHelper;
  16. _params = new ModelParams(Constants.ModelPath)
  17. {
  18. ContextSize = 2048
  19. };
  20. _model = LLamaWeights.LoadFromFile(_params);
  21. }
  22. public void Dispose()
  23. {
  24. _model.Dispose();
  25. }
  26. [Fact]
  27. public void BasicModelProperties()
  28. {
  29. Assert.Equal(32000, _model.VocabCount);
  30. Assert.Equal(4096, _model.ContextSize);
  31. Assert.Equal(4096, _model.EmbeddingSize);
  32. }
  33. [Fact]
  34. public void AdvancedModelProperties()
  35. {
  36. var expected = new Dictionary<string, string>
  37. {
  38. { "general.name", "LLaMA v2" },
  39. { "general.architecture", "llama" },
  40. { "general.quantization_version", "2" },
  41. { "general.file_type", "2" },
  42. { "llama.context_length", "4096" },
  43. { "llama.rope.dimension_count", "128" },
  44. { "llama.embedding_length", "4096" },
  45. { "llama.block_count", "32" },
  46. { "llama.feed_forward_length", "11008" },
  47. { "llama.attention.head_count", "32" },
  48. { "llama.attention.head_count_kv", "32" },
  49. { "llama.attention.layer_norm_rms_epsilon", "0.000001" },
  50. { "tokenizer.ggml.eos_token_id", "2" },
  51. { "tokenizer.ggml.model", "llama" },
  52. { "tokenizer.ggml.bos_token_id", "1" },
  53. { "tokenizer.ggml.unknown_token_id", "0" },
  54. };
  55. var metaCount = NativeApi.llama_model_meta_count(_model.NativeHandle);
  56. Assert.Equal(expected.Count, metaCount);
  57. Span<byte> buffer = stackalloc byte[128];
  58. for (var i = 0; i < expected.Count; i++)
  59. {
  60. unsafe
  61. {
  62. fixed (byte* ptr = buffer)
  63. {
  64. var length = NativeApi.llama_model_meta_key_by_index(_model.NativeHandle, i, ptr, 128);
  65. Assert.True(length > 0);
  66. var key = Encoding.UTF8.GetString(buffer[..length]);
  67. length = NativeApi.llama_model_meta_val_str_by_index(_model.NativeHandle, i, ptr, 128);
  68. Assert.True(length > 0);
  69. var val = Encoding.UTF8.GetString(buffer[..length]);
  70. _testOutputHelper.WriteLine($"{key} == {val}");
  71. Assert.True(expected.ContainsKey(key));
  72. Assert.Equal(expected[key], val);
  73. }
  74. }
  75. }
  76. }
  77. }
  78. }