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.

TokenTests.cs 5.9 kB

April 2024 Binary Update (#662) * Updated binaries, using [this build](https://github.com/SciSharp/LLamaSharp/actions/runs/8654672719/job/23733195669) for llama.cpp commit `f7001ccc5aa359fcf41bba19d1c99c3d25c9bcc7`. - Added all new functions. - Moved some functions (e.g. `SafeLlamaModelHandle` specific functions) into `SafeLlamaModelHandle.cs` - Exposed tokens on `SafeLlamaModelHandle` and `LLamaWeights` through a `Tokens` property. As new special tokens are added in the future they can be added here. - Changed all token properties to return nullable tokens, to handle some models not having some tokens. - Fixed `DefaultSamplingPipeline` to handle no newline token in some models. * Moved native methods to more specific locations. - Context specific things have been moved into `SafeLLamaContextHandle.cs` and made private - they're exposed through C# properties and methods already. - Checking that GPU layer count is zero if GPU offload is not supported. - Moved methods for creating default structs (`llama_model_quantize_default_params` and `llama_context_default_params`) into relevant structs. * Removed exception if `GpuLayerCount > 0` when GPU is not supported. * - Added low level wrapper methods for new per-sequence state load/save in `SafeLLamaContextHandle` - Added high level wrapper methods (save/load with `State` object or memory mapped file) in `LLamaContext` - Moved native methods for per-sequence state load/save into `SafeLLamaContextHandle` * Added update and defrag methods for KV cache in `SafeLLamaContextHandle` * Updated submodule to `f7001ccc5aa359fcf41bba19d1c99c3d25c9bcc7` * Passing the sequence ID when saving a single sequence state
1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System.Text;
  2. using LLama.Common;
  3. using LLama.Extensions;
  4. namespace LLama.Unittest;
  5. public sealed class TokenTests
  6. : IDisposable
  7. {
  8. private readonly ModelParams _params;
  9. private readonly LLamaWeights _model;
  10. public TokenTests()
  11. {
  12. _params = new ModelParams(Constants.GenerativeModelPath)
  13. {
  14. ContextSize = 2048,
  15. GpuLayerCount = Constants.CIGpuLayerCount,
  16. };
  17. _model = LLamaWeights.LoadFromFile(_params);
  18. }
  19. public void Dispose()
  20. {
  21. _model.Dispose();
  22. }
  23. [Fact]
  24. public void TokensEndWith()
  25. {
  26. var tokens = _model.NativeHandle.Tokenize("The cat sat on the edge of the mat", false, true, Encoding.UTF8);
  27. var result = tokens.TokensEndsWithAnyString(new[]
  28. {
  29. "a fish",
  30. "the mat",
  31. "this is an improbably long query to be using for this method"
  32. }, _model.NativeHandle, Encoding.UTF8);
  33. Assert.True(result);
  34. }
  35. [Fact]
  36. public void TokensEndSubstring()
  37. {
  38. var tokens = _model.NativeHandle.Tokenize("The cat sat on the edge of the mat", false, true, Encoding.UTF8);
  39. var result = tokens.TokensEndsWithAnyString((IList<string>)new[]
  40. {
  41. "at",
  42. }, _model.NativeHandle, Encoding.UTF8);
  43. Assert.True(result);
  44. }
  45. [Fact]
  46. public void TokensNotEndWith()
  47. {
  48. var tokens = _model.NativeHandle.Tokenize("The cat sat on the edge of the mat", false, true, Encoding.UTF8);
  49. var result = tokens.TokensEndsWithAnyString((IList<string>)new[]
  50. {
  51. "a fish",
  52. "The cat sat on the edge of the ma",
  53. "this is an improbably long query to be using for this method"
  54. }, _model.NativeHandle, Encoding.UTF8);
  55. Assert.False(result);
  56. }
  57. [Fact]
  58. public void TokensNotEndWithNothing()
  59. {
  60. var tokens = _model.NativeHandle.Tokenize("The cat sat on the edge of the mat", false, true, Encoding.UTF8);
  61. var result = tokens.TokensEndsWithAnyString((IList<string>)Array.Empty<string>(), _model.NativeHandle, Encoding.UTF8);
  62. Assert.False(result);
  63. }
  64. [Fact]
  65. public void TokensEndWith2()
  66. {
  67. var tokens = _model.NativeHandle.Tokenize("The cat sat on the edge of the mat", false, true, Encoding.UTF8);
  68. var decoder = new StreamingTokenDecoder(Encoding.UTF8, _model);
  69. decoder.AddRange(tokens);
  70. var processor = new AntipromptProcessor(new[]
  71. {
  72. "a fish",
  73. "the mat",
  74. "this is an improbably long query to be using for this method"
  75. });
  76. var result = processor.Add(decoder.Read());
  77. Assert.True(result);
  78. }
  79. [Fact]
  80. public void TokensEndSubstring2()
  81. {
  82. var tokens = _model.NativeHandle.Tokenize("The cat sat on the edge of the mat", false, true, Encoding.UTF8);
  83. var decoder = new StreamingTokenDecoder(Encoding.UTF8, _model);
  84. decoder.AddRange(tokens);
  85. var processor = new AntipromptProcessor(new[] { "at" });
  86. var result = processor.Add(decoder.Read());
  87. Assert.True(result);
  88. }
  89. [Fact]
  90. public void TokensNotEndWith2()
  91. {
  92. var tokens = _model.NativeHandle.Tokenize("The cat sat on the edge of the mat", false, true, Encoding.UTF8);
  93. var decoder = new StreamingTokenDecoder(Encoding.UTF8, _model);
  94. decoder.AddRange(tokens);
  95. var processor = new AntipromptProcessor(new[]
  96. {
  97. "a fish",
  98. "The cat sat on the edge of the ma",
  99. "this is an improbably long query to be using for this method"
  100. });
  101. var result = processor.Add(decoder.Read());
  102. Assert.False(result);
  103. }
  104. [Fact]
  105. public void TokensNotEndWithNothing2()
  106. {
  107. var tokens = _model.NativeHandle.Tokenize("The cat sat on the edge of the mat", false, true, Encoding.UTF8);
  108. var decoder = new StreamingTokenDecoder(Encoding.UTF8, _model);
  109. decoder.AddRange(tokens);
  110. var processor = new AntipromptProcessor();
  111. var result = processor.Add(decoder.Read());
  112. Assert.False(result);
  113. }
  114. [Fact]
  115. public void RoundTrip()
  116. {
  117. var strings = new[]
  118. {
  119. "Hello world",
  120. "철수",
  121. "😀 😃 😄 😁 😆철수😅 😂 😊 😇 🙂 ",
  122. };
  123. var charsArr = new char[1024];
  124. foreach (var input in strings)
  125. {
  126. // Convert into llama tokens
  127. var tokens = _model.NativeHandle.Tokenize(input, false, false, Encoding.UTF8);
  128. // Convert tokens back into characters
  129. var chars = _model.NativeHandle.TokensToSpan(tokens, charsArr.AsSpan(), Encoding.UTF8);
  130. // llama.cpp adds a space to the start of strings, remove that
  131. var output = new string(chars).TrimStart(' ');
  132. // Check that the input equals the output
  133. Assert.Equal(input, output);
  134. }
  135. }
  136. [Fact]
  137. public void StreamingDecoderRoundTrip()
  138. {
  139. var decoder = new StreamingTokenDecoder(Encoding.UTF8, _model);
  140. var strings = new[]
  141. {
  142. "Hello world",
  143. "철수",
  144. "😀 😃 😄 😁 😆철수😅 😂 😊 😇 🙂 ",
  145. };
  146. foreach (var input in strings)
  147. {
  148. decoder.Reset();
  149. // Convert into llama tokens
  150. var tokens = _model.NativeHandle.Tokenize(input, false, false, Encoding.UTF8);
  151. // Add tokens to decoder
  152. foreach (var token in tokens)
  153. decoder.Add(token);
  154. // llama.cpp adds a space to the start of strings, remove that
  155. var output = decoder.Read().TrimStart(' ');
  156. // Check that the input equals the output
  157. Assert.Equal(input, output);
  158. }
  159. }
  160. }