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.5 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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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.GenerativeModelPath)
  13. {
  14. ContextSize = 768,
  15. GpuLayerCount = Constants.CIGpuLayerCount,
  16. };
  17. _weights = LLamaWeights.LoadFromFile(@params);
  18. _context = _weights.CreateContext(@params);
  19. }
  20. public void Dispose()
  21. {
  22. _weights.Dispose();
  23. _context.Dispose();
  24. }
  25. [Fact]
  26. public void CheckProperties()
  27. {
  28. Assert.Equal(768u, _context.ContextSize);
  29. Assert.Equal(4096, _context.EmbeddingSize);
  30. Assert.Equal(32000, _context.VocabCount);
  31. }
  32. [Fact]
  33. public void Tokenize()
  34. {
  35. var tokens = _context.Tokenize("The quick brown fox", true);
  36. Assert.Equal(new LLamaToken[] { 1, 450, 4996, 17354, 1701, 29916 }, tokens);
  37. }
  38. [Fact]
  39. public void TokenizeNewline()
  40. {
  41. var tokens = _context.Tokenize("\n", false, false);
  42. Assert.Equal(new LLamaToken[] { 29871, 13 }, tokens);
  43. }
  44. [Fact]
  45. public void TokenizeRoundtripSpecialStrings()
  46. {
  47. var strings = new[]
  48. {
  49. "\t", "\t\t", "\t\t\t",
  50. "\n\n", "\n\n\n", "\n\n\n\n",
  51. "\t\n", "\t\n\t\n\n\n\n\t\t",
  52. "\b", "\v", "\0"
  53. };
  54. foreach (var s in strings)
  55. {
  56. var tokens = _context.Tokenize(s, false, false);
  57. var decoder = new StreamingTokenDecoder(_context);
  58. decoder.AddRange(tokens);
  59. var str = decoder.Read();
  60. Assert.Equal(s, str.TrimStart(' '));
  61. }
  62. }
  63. [Fact]
  64. public void TokenizeWithoutBOS()
  65. {
  66. var tokens = _context.Tokenize("The quick brown fox", false);
  67. Assert.Equal(new LLamaToken[] { 450, 4996, 17354, 1701, 29916 }, tokens);
  68. }
  69. [Fact]
  70. public void TokenizeEmpty()
  71. {
  72. var tokens = _context.Tokenize("", false);
  73. Assert.Equal(Array.Empty<LLamaToken>(), tokens);
  74. }
  75. }
  76. }