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.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. using System;
  2. using LLama.Abstractions;
  3. using LLama.Extensions;
  4. using LLama.Native;
  5. using Microsoft.Extensions.Logging;
  6. namespace LLama
  7. {
  8. /// <summary>
  9. /// A set of model weights, loaded into memory.
  10. /// </summary>
  11. public sealed class LLamaWeights
  12. : IDisposable
  13. {
  14. /// <summary>
  15. /// The native handle, which is used in the native APIs
  16. /// </summary>
  17. /// <remarks>Be careful how you use this!</remarks>
  18. public SafeLlamaModelHandle NativeHandle { get; }
  19. /// <summary>
  20. /// Total number of tokens in vocabulary of this model
  21. /// </summary>
  22. public int VocabCount => NativeHandle.VocabCount;
  23. /// <summary>
  24. /// Total number of tokens in the context
  25. /// </summary>
  26. public int ContextSize => NativeHandle.ContextSize;
  27. /// <summary>
  28. /// Get the size of this model in bytes
  29. /// </summary>
  30. public ulong SizeInBytes => NativeHandle.SizeInBytes;
  31. /// <summary>
  32. /// Get the number of parameters in this model
  33. /// </summary>
  34. public ulong ParameterCount => NativeHandle.ParameterCount;
  35. /// <summary>
  36. /// Get the newline token for this model
  37. /// </summary>
  38. public int NewlineToken => NativeApi.llama_token_nl(NativeHandle);
  39. /// <summary>
  40. /// Get the "end of sentence" token for this model
  41. /// </summary>
  42. public int EndOfSentenceToken => NativeApi.llama_token_eos(NativeHandle);
  43. /// <summary>
  44. /// Get the "beginning of sentence" token for this model
  45. /// </summary>
  46. public int BeginningOfSentenceToken => NativeApi.llama_token_bos(NativeHandle);
  47. /// <summary>
  48. /// Dimension of embedding vectors
  49. /// </summary>
  50. public int EmbeddingSize => NativeHandle.EmbeddingSize;
  51. internal LLamaWeights(SafeLlamaModelHandle weights)
  52. {
  53. NativeHandle = weights;
  54. }
  55. /// <summary>
  56. /// Load weights into memory
  57. /// </summary>
  58. /// <param name="params"></param>
  59. /// <returns></returns>
  60. public static LLamaWeights LoadFromFile(IModelParams @params)
  61. {
  62. using var pin = @params.ToLlamaModelParams(out var lparams);
  63. var weights = SafeLlamaModelHandle.LoadFromFile(@params.ModelPath, lparams);
  64. foreach (var adapter in @params.LoraAdapters)
  65. {
  66. if (string.IsNullOrEmpty(adapter.Path))
  67. continue;
  68. if (adapter.Scale <= 0)
  69. continue;
  70. weights.ApplyLoraFromFile(adapter.Path, adapter.Scale, @params.LoraBase);
  71. }
  72. return new LLamaWeights(weights);
  73. }
  74. /// <inheritdoc />
  75. public void Dispose()
  76. {
  77. NativeHandle.Dispose();
  78. }
  79. /// <summary>
  80. /// Create a llama_context using this model
  81. /// </summary>
  82. /// <param name="params"></param>
  83. /// <param name="logger"></param>
  84. /// <returns></returns>
  85. public LLamaContext CreateContext(IContextParams @params, ILogger? logger = null)
  86. {
  87. return new LLamaContext(this, @params, logger);
  88. }
  89. }
  90. }