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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. /// Dimension of embedding vectors
  34. /// </summary>
  35. public int EmbeddingSize => NativeHandle.EmbeddingSize;
  36. internal LLamaWeights(SafeLlamaModelHandle weights, Encoding encoding)
  37. {
  38. _weights = weights;
  39. Encoding = encoding;
  40. }
  41. /// <summary>
  42. /// Load weights into memory
  43. /// </summary>
  44. /// <param name="params"></param>
  45. /// <returns></returns>
  46. public static LLamaWeights LoadFromFile(IModelParams @params)
  47. {
  48. using var pin = @params.ToLlamaContextParams(out var lparams);
  49. var weights = SafeLlamaModelHandle.LoadFromFile(@params.ModelPath, lparams);
  50. if (!string.IsNullOrEmpty(@params.LoraAdapter))
  51. weights.ApplyLoraFromFile(@params.LoraAdapter, @params.LoraBase, @params.Threads);
  52. return new LLamaWeights(weights, @params.Encoding);
  53. }
  54. /// <inheritdoc />
  55. public void Dispose()
  56. {
  57. _weights.Dispose();
  58. }
  59. /// <summary>
  60. /// Create a llama_context using this model
  61. /// </summary>
  62. /// <param name="params"></param>
  63. /// <returns></returns>
  64. public LLamaContext CreateContext(IModelParams @params)
  65. {
  66. return new LLamaContext(this, @params);
  67. }
  68. }
  69. }