您最多选择25个标签 标签必须以中文、字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

LLavaWeights.cs 2.6 kB

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