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.

LLavaWeights.cs 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. using System;
  2. using System.Threading;
  3. using System.Threading.Tasks;
  4. using LLama.Native;
  5. namespace LLama;
  6. /// <summary>
  7. /// A set of llava model weights (mmproj), loaded into memory.
  8. /// </summary>
  9. public sealed class LLavaWeights : IDisposable
  10. {
  11. /// <summary>
  12. /// The native handle, which is used in the native APIs
  13. /// </summary>
  14. /// <remarks>Be careful how you use this!</remarks>
  15. public SafeLlavaModelHandle NativeHandle { get; }
  16. private LLavaWeights(SafeLlavaModelHandle weights)
  17. {
  18. NativeHandle = weights;
  19. }
  20. /// <summary>
  21. /// Load weights into memory
  22. /// </summary>
  23. /// <param name="mmProject">path to the "mmproj" model file</param>
  24. /// <returns></returns>
  25. public static LLavaWeights LoadFromFile(string mmProject)
  26. {
  27. var weights = SafeLlavaModelHandle.LoadFromFile(mmProject, 1);
  28. return new LLavaWeights(weights);
  29. }
  30. /// <summary>
  31. /// Load weights into memory
  32. /// </summary>
  33. /// <param name="mmProject">path to the "mmproj" model file</param>
  34. /// <param name="token"></param>
  35. /// <returns></returns>
  36. public static Task<LLavaWeights> LoadFromFileAsync(string mmProject, CancellationToken token = default)
  37. {
  38. return Task.Run(() => LoadFromFile(mmProject), token);
  39. }
  40. /// <summary>
  41. /// Create the Image Embeddings from the bytes of an image.
  42. /// </summary>
  43. /// <param name="ctxLlama"></param>
  44. /// <param name="image">Image bytes. Supported formats:
  45. /// <list type="bullet">
  46. /// <item>JPG</item>
  47. /// <item>PNG</item>
  48. /// <item>BMP</item>
  49. /// <item>TGA</item>
  50. /// </list>
  51. /// </param>
  52. /// <returns></returns>
  53. public SafeLlavaImageEmbedHandle CreateImageEmbeddings(LLamaContext ctxLlama, byte[] image )
  54. {
  55. return NativeHandle.CreateImageEmbeddings(ctxLlama, image );
  56. }
  57. /// <summary>
  58. /// Create the Image Embeddings from the bytes of an image.
  59. /// </summary>
  60. /// <param name="ctxLlama"></param>
  61. /// <param name="image">Path to the image file. Supported formats:
  62. /// <list type="bullet">
  63. /// <item>JPG</item>
  64. /// <item>PNG</item>
  65. /// <item>BMP</item>
  66. /// <item>TGA</item>
  67. /// </list>
  68. /// </param>
  69. /// <returns></returns>
  70. /// <exception cref="InvalidOperationException"></exception>
  71. public SafeLlavaImageEmbedHandle CreateImageEmbeddings(LLamaContext ctxLlama, string image )
  72. {
  73. return NativeHandle.CreateImageEmbeddings(ctxLlama, image );
  74. }
  75. /// <summary>
  76. /// Eval the image embeddings
  77. /// </summary>
  78. /// <param name="ctxLlama"></param>
  79. /// <param name="imageEmbed"></param>
  80. /// <param name="n_past"></param>
  81. /// <returns></returns>
  82. public bool EvalImageEmbed(LLamaContext ctxLlama, SafeLlavaImageEmbedHandle imageEmbed, ref int n_past)
  83. {
  84. return NativeHandle.EvalImageEmbed( ctxLlama, imageEmbed, ref n_past );
  85. }
  86. /// <inheritdoc />
  87. public void Dispose()
  88. {
  89. NativeHandle.Dispose();
  90. }
  91. }