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.

LLamaContextTests.cs 2.4 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using LLama.Common;
  2. using LLama.Native;
  3. namespace LLama.Unittest
  4. {
  5. public sealed class LLamaContextTests
  6. : IDisposable
  7. {
  8. private readonly LLamaWeights _weights;
  9. private readonly LLamaContext _context;
  10. public LLamaContextTests()
  11. {
  12. var @params = new ModelParams(Constants.ModelPath)
  13. {
  14. ContextSize = 768,
  15. };
  16. _weights = LLamaWeights.LoadFromFile(@params);
  17. _context = _weights.CreateContext(@params);
  18. }
  19. public void Dispose()
  20. {
  21. _weights.Dispose();
  22. _context.Dispose();
  23. }
  24. [Fact]
  25. public void CheckProperties()
  26. {
  27. Assert.Equal(768u, _context.ContextSize);
  28. Assert.Equal(4096, _context.EmbeddingSize);
  29. Assert.Equal(32000, _context.VocabCount);
  30. }
  31. [Fact]
  32. public void Tokenize()
  33. {
  34. var tokens = _context.Tokenize("The quick brown fox", true);
  35. Assert.Equal(new LLamaToken[] { 1, 450, 4996, 17354, 1701, 29916 }, tokens);
  36. }
  37. [Fact]
  38. public void TokenizeNewline()
  39. {
  40. var tokens = _context.Tokenize("\n", false, false);
  41. Assert.Equal(new LLamaToken[] { 29871, 13 }, tokens);
  42. }
  43. [Fact]
  44. public void TokenizeRoundtripSpecialStrings()
  45. {
  46. var strings = new[]
  47. {
  48. "\t", "\t\t", "\t\t\t",
  49. "\n\n", "\n\n\n", "\n\n\n\n",
  50. "\t\n", "\t\n\t\n\n\n\n\t\t",
  51. "\b", "\v", "\0"
  52. };
  53. foreach (var s in strings)
  54. {
  55. var tokens = _context.Tokenize(s, false, false);
  56. var decoder = new StreamingTokenDecoder(_context);
  57. decoder.AddRange(tokens);
  58. var str = decoder.Read();
  59. Assert.Equal(s, str.TrimStart(' '));
  60. }
  61. }
  62. [Fact]
  63. public void TokenizeWithoutBOS()
  64. {
  65. var tokens = _context.Tokenize("The quick brown fox", false);
  66. Assert.Equal(new LLamaToken[] { 450, 4996, 17354, 1701, 29916 }, tokens);
  67. }
  68. [Fact]
  69. public void TokenizeEmpty()
  70. {
  71. var tokens = _context.Tokenize("", false);
  72. Assert.Equal(Array.Empty<LLamaToken>(), tokens);
  73. }
  74. }
  75. }