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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. private LLamaWeights(SafeLlamaModelHandle weights)
  21. {
  22. _weights = weights;
  23. }
  24. /// <summary>
  25. /// Load weights into memory
  26. /// </summary>
  27. /// <param name="params"></param>
  28. /// <returns></returns>
  29. public static LLamaWeights LoadFromFile(IModelParams @params)
  30. {
  31. using var pin = @params.ToLlamaContextParams(out var lparams);
  32. var weights = SafeLlamaModelHandle.LoadFromFile(@params.ModelPath, lparams);
  33. if (!string.IsNullOrEmpty(@params.LoraAdapter))
  34. weights.ApplyLoraFromFile(@params.LoraAdapter, @params.LoraBase, @params.Threads);
  35. return new LLamaWeights(weights);
  36. }
  37. /// <inheritdoc />
  38. public void Dispose()
  39. {
  40. _weights.Dispose();
  41. }
  42. /// <summary>
  43. /// Create a llama_context using this model
  44. /// </summary>
  45. /// <param name="params"></param>
  46. /// <param name="encoding"></param>
  47. /// <returns></returns>
  48. public LLamaContext CreateContext(IModelParams @params, Encoding encoding)
  49. {
  50. return new LLamaContext(this, @params, encoding);
  51. }
  52. }
  53. }