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

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