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 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. /// Get the newline token for this model
  39. /// </summary>
  40. public LLamaToken NewlineToken => NativeApi.llama_token_nl(NativeHandle);
  41. /// <summary>
  42. /// Get the "end of sentence" token for this model
  43. /// </summary>
  44. public LLamaToken EndOfSentenceToken => NativeApi.llama_token_eos(NativeHandle);
  45. /// <summary>
  46. /// Get the "beginning of sentence" token for this model
  47. /// </summary>
  48. public LLamaToken BeginningOfSentenceToken => NativeApi.llama_token_bos(NativeHandle);
  49. /// <summary>
  50. /// Dimension of embedding vectors
  51. /// </summary>
  52. public int EmbeddingSize => NativeHandle.EmbeddingSize;
  53. /// <summary>
  54. /// All metadata keys in this model
  55. /// </summary>
  56. public IReadOnlyDictionary<string, string> Metadata { get; set; }
  57. private LLamaWeights(SafeLlamaModelHandle weights)
  58. {
  59. NativeHandle = weights;
  60. Metadata = weights.ReadMetadata();
  61. }
  62. /// <summary>
  63. /// Load weights into memory
  64. /// </summary>
  65. /// <param name="params"></param>
  66. /// <returns></returns>
  67. public static LLamaWeights LoadFromFile(IModelParams @params)
  68. {
  69. using var pin = @params.ToLlamaModelParams(out var lparams);
  70. var weights = SafeLlamaModelHandle.LoadFromFile(@params.ModelPath, lparams);
  71. foreach (var adapter in @params.LoraAdapters)
  72. {
  73. if (string.IsNullOrEmpty(adapter.Path))
  74. continue;
  75. if (adapter.Scale <= 0)
  76. continue;
  77. weights.ApplyLoraFromFile(adapter.Path, adapter.Scale, @params.LoraBase);
  78. }
  79. return new LLamaWeights(weights);
  80. }
  81. /// <inheritdoc />
  82. public void Dispose()
  83. {
  84. NativeHandle.Dispose();
  85. }
  86. /// <summary>
  87. /// Create a llama_context using this model
  88. /// </summary>
  89. /// <param name="params"></param>
  90. /// <param name="logger"></param>
  91. /// <returns></returns>
  92. public LLamaContext CreateContext(IContextParams @params, ILogger? logger = null)
  93. {
  94. return new LLamaContext(this, @params, logger);
  95. }
  96. /// <summary>
  97. /// Convert a string of text into tokens
  98. /// </summary>
  99. /// <param name="text"></param>
  100. /// <param name="add_bos"></param>
  101. /// <param name="encoding"></param>
  102. /// <param name="special">Allow tokenizing special and/or control tokens which otherwise are not exposed and treated as plaintext.</param>
  103. /// <returns></returns>
  104. public LLamaToken[] Tokenize(string text, bool add_bos, bool special, Encoding encoding)
  105. {
  106. return NativeHandle.Tokenize(text, add_bos, special, encoding);
  107. }
  108. }
  109. }