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.

LLamaWeights.cs 3.8 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
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
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
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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using LLama.Abstractions;
  5. using LLama.Extensions;
  6. using LLama.Native;
  7. using Microsoft.Extensions.Logging;
  8. namespace LLama
  9. {
  10. /// <summary>
  11. /// A set of model weights, loaded into memory.
  12. /// </summary>
  13. public sealed class LLamaWeights
  14. : IDisposable
  15. {
  16. /// <summary>
  17. /// The native handle, which is used in the native APIs
  18. /// </summary>
  19. /// <remarks>Be careful how you use this!</remarks>
  20. public SafeLlamaModelHandle NativeHandle { get; }
  21. /// <summary>
  22. /// Total number of tokens in vocabulary of this model
  23. /// </summary>
  24. public int VocabCount => NativeHandle.VocabCount;
  25. /// <summary>
  26. /// Total number of tokens in the context
  27. /// </summary>
  28. public int ContextSize => NativeHandle.ContextSize;
  29. /// <summary>
  30. /// Get the size of this model in bytes
  31. /// </summary>
  32. public ulong SizeInBytes => NativeHandle.SizeInBytes;
  33. /// <summary>
  34. /// Get the number of parameters in this model
  35. /// </summary>
  36. public ulong ParameterCount => NativeHandle.ParameterCount;
  37. /// <summary>
  38. /// Dimension of embedding vectors
  39. /// </summary>
  40. public int EmbeddingSize => NativeHandle.EmbeddingSize;
  41. /// <summary>
  42. /// Get the special tokens of this model
  43. /// </summary>
  44. public SafeLlamaModelHandle.ModelTokens Tokens => NativeHandle.Tokens;
  45. /// <summary>
  46. /// All metadata keys in this model
  47. /// </summary>
  48. public IReadOnlyDictionary<string, string> Metadata { get; set; }
  49. private LLamaWeights(SafeLlamaModelHandle weights)
  50. {
  51. NativeHandle = weights;
  52. Metadata = weights.ReadMetadata();
  53. }
  54. /// <summary>
  55. /// Load weights into memory
  56. /// </summary>
  57. /// <param name="params"></param>
  58. /// <returns></returns>
  59. public static LLamaWeights LoadFromFile(IModelParams @params)
  60. {
  61. using var pin = @params.ToLlamaModelParams(out var lparams);
  62. var weights = SafeLlamaModelHandle.LoadFromFile(@params.ModelPath, lparams);
  63. foreach (var adapter in @params.LoraAdapters)
  64. {
  65. if (string.IsNullOrEmpty(adapter.Path))
  66. continue;
  67. if (adapter.Scale <= 0)
  68. continue;
  69. weights.ApplyLoraFromFile(adapter.Path, adapter.Scale, @params.LoraBase);
  70. }
  71. return new LLamaWeights(weights);
  72. }
  73. /// <inheritdoc />
  74. public void Dispose()
  75. {
  76. NativeHandle.Dispose();
  77. }
  78. /// <summary>
  79. /// Create a llama_context using this model
  80. /// </summary>
  81. /// <param name="params"></param>
  82. /// <param name="logger"></param>
  83. /// <returns></returns>
  84. public LLamaContext CreateContext(IContextParams @params, ILogger? logger = null)
  85. {
  86. return new LLamaContext(this, @params, logger);
  87. }
  88. /// <summary>
  89. /// Convert a string of text into tokens
  90. /// </summary>
  91. /// <param name="text"></param>
  92. /// <param name="add_bos"></param>
  93. /// <param name="encoding"></param>
  94. /// <param name="special">Allow tokenizing special and/or control tokens which otherwise are not exposed and treated as plaintext.</param>
  95. /// <returns></returns>
  96. public LLamaToken[] Tokenize(string text, bool add_bos, bool special, Encoding encoding)
  97. {
  98. return NativeHandle.Tokenize(text, add_bos, special, encoding);
  99. }
  100. }
  101. }