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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using LLama.Abstractions;
  3. using LLama.Extensions;
  4. using LLama.Native;
  5. namespace LLama
  6. {
  7. /// <summary>
  8. /// A set of model weights, loaded into memory.
  9. /// </summary>
  10. public sealed class LLamaWeights
  11. : IDisposable
  12. {
  13. private readonly SafeLlamaModelHandle _weights;
  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 => _weights;
  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. /// Dimension of embedding vectors
  37. /// </summary>
  38. public int EmbeddingSize => NativeHandle.EmbeddingSize;
  39. internal LLamaWeights(SafeLlamaModelHandle weights)
  40. {
  41. _weights = weights;
  42. }
  43. /// <summary>
  44. /// Load weights into memory
  45. /// </summary>
  46. /// <param name="params"></param>
  47. /// <returns></returns>
  48. public static LLamaWeights LoadFromFile(IModelParams @params)
  49. {
  50. using var pin = @params.ToLlamaModelParams(out var lparams);
  51. var weights = SafeLlamaModelHandle.LoadFromFile(@params.ModelPath, lparams);
  52. foreach (var adapter in @params.LoraAdapters)
  53. {
  54. if (string.IsNullOrEmpty(adapter.Path))
  55. continue;
  56. if (adapter.Scale <= 0)
  57. continue;
  58. weights.ApplyLoraFromFile(adapter.Path, adapter.Scale, @params.LoraBase, @params.Threads);
  59. }
  60. return new LLamaWeights(weights);
  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(IContextParams @params)
  73. {
  74. return new LLamaContext(this, @params);
  75. }
  76. }
  77. }