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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Text;
  3. using LLama.Abstractions;
  4. using LLama.Extensions;
  5. using LLama.Native;
  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. private readonly SafeLlamaModelHandle _weights;
  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 => _weights;
  20. /// <summary>
  21. /// Encoding to use to convert text into bytes for the model
  22. /// </summary>
  23. public Encoding Encoding { get; }
  24. /// <summary>
  25. /// Total number of tokens in vocabulary of this model
  26. /// </summary>
  27. public int VocabCount => NativeHandle.VocabCount;
  28. /// <summary>
  29. /// Total number of tokens in the context
  30. /// </summary>
  31. public int ContextSize => NativeHandle.ContextSize;
  32. /// <summary>
  33. /// Get the size of this model in bytes
  34. /// </summary>
  35. public ulong SizeInBytes => NativeHandle.SizeInBytes;
  36. /// <summary>
  37. /// Get the number of parameters in this model
  38. /// </summary>
  39. public ulong ParameterCount => NativeHandle.ParameterCount;
  40. /// <summary>
  41. /// Dimension of embedding vectors
  42. /// </summary>
  43. public int EmbeddingSize => NativeHandle.EmbeddingSize;
  44. internal LLamaWeights(SafeLlamaModelHandle weights, Encoding encoding)
  45. {
  46. _weights = weights;
  47. Encoding = encoding;
  48. }
  49. /// <summary>
  50. /// Load weights into memory
  51. /// </summary>
  52. /// <param name="params"></param>
  53. /// <returns></returns>
  54. public static LLamaWeights LoadFromFile(IModelParams @params)
  55. {
  56. using var pin = @params.ToLlamaModelParams(out var lparams);
  57. var weights = SafeLlamaModelHandle.LoadFromFile(@params.ModelPath, lparams);
  58. if (!string.IsNullOrEmpty(@params.LoraAdapter))
  59. weights.ApplyLoraFromFile(@params.LoraAdapter, @params.LoraAdapterScale, @params.LoraBase, @params.Threads);
  60. return new LLamaWeights(weights, @params.Encoding);
  61. }
  62. /// <inheritdoc />
  63. public void Dispose()
  64. {
  65. _weights.Dispose();
  66. }
  67. /// <summary>
  68. /// Create a llama_context using this model
  69. /// </summary>
  70. /// <param name="params"></param>
  71. /// <returns></returns>
  72. public LLamaContext CreateContext(IModelParams @params)
  73. {
  74. return new LLamaContext(this, @params);
  75. }
  76. }
  77. }