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.

SafeLlavaModelHandle.cs 4.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using LLama;
  8. using LLama.Exceptions;
  9. namespace LLama.Native
  10. {
  11. /// <summary>
  12. /// A reference to a set of llava model weights.
  13. /// </summary>
  14. public sealed class SafeLlavaModelHandle
  15. : SafeLLamaHandleBase
  16. {
  17. private SafeLlavaModelHandle(IntPtr handle)
  18. : base(handle, true)
  19. {
  20. }
  21. private SafeLlavaModelHandle()
  22. {}
  23. /// <inheritdoc />
  24. protected override bool ReleaseHandle()
  25. {
  26. clip_free(DangerousGetHandle());
  27. SetHandle(IntPtr.Zero);
  28. return true;
  29. }
  30. /// <summary>
  31. /// Load a model from the given file path into memory
  32. /// </summary>
  33. /// <param name="modelPath">MMP File (Multi-Modal Projections)</param>
  34. /// <param name="verbosity">Verbosity level</param>
  35. /// <returns>SafeHandle of the Clip Model</returns>
  36. /// <exception cref="InvalidOperationException"></exception>
  37. /// <exception cref="RuntimeError"></exception>
  38. public static SafeLlavaModelHandle LoadFromFile(string modelPath, int verbosity )
  39. {
  40. // Try to open the model file, this will check:
  41. // - File exists (automatically throws FileNotFoundException)
  42. // - File is readable (explicit check)
  43. // This provides better error messages that llama.cpp, which would throw an access violation exception in both cases.
  44. using (var fs = new FileStream(modelPath, FileMode.Open))
  45. if (!fs.CanRead)
  46. throw new InvalidOperationException($"Llava MMP Model file '{modelPath}' is not readable");
  47. return clip_model_load(modelPath, verbosity)
  48. ?? throw new LoadWeightsFailedException(modelPath);
  49. }
  50. /// <summary>
  51. /// Create the Image Embeddings.
  52. /// </summary>
  53. /// <param name="ctxLlama">LLama Context</param>
  54. /// <param name="image">Image filename (it supports jpeg format only)</param>
  55. /// <returns>return the SafeHandle of these embeddings</returns>
  56. public SafeLlavaImageEmbedHandle CreateImageEmbeddings(LLamaContext ctxLlama, string image)
  57. {
  58. return SafeLlavaImageEmbedHandle.CreateFromFileName(this, ctxLlama, image);
  59. }
  60. /// <summary>
  61. /// Create the Image Embeddings.
  62. /// </summary>
  63. /// <param name="ctxLlama">LLama Context</param>
  64. /// <param name="image">Image in binary format (it supports jpeg format only)</param>
  65. /// <returns>return the SafeHandle of these embeddings</returns>
  66. public SafeLlavaImageEmbedHandle CreateImageEmbeddings(LLamaContext ctxLlama, byte[] image )
  67. {
  68. return SafeLlavaImageEmbedHandle.CreateFromMemory(this, ctxLlama, image );
  69. }
  70. /// <summary>
  71. /// Evaluates the image embeddings.
  72. /// </summary>
  73. /// <param name="ctxLlama">Llama Context</param>
  74. /// <param name="imageEmbed">The current embeddings to evaluate</param>
  75. /// <param name="n_past"></param>
  76. /// <returns>True on success</returns>
  77. public bool EvalImageEmbed(LLamaContext ctxLlama, SafeLlavaImageEmbedHandle imageEmbed, ref int n_past)
  78. {
  79. return NativeApi.llava_eval_image_embed(ctxLlama.NativeHandle, imageEmbed, (int)ctxLlama.Params.BatchSize, ref n_past );
  80. }
  81. /// <summary>
  82. /// Load MULTI MODAL PROJECTIONS model / Clip Model
  83. /// </summary>
  84. /// <param name="mmProj"> Model path/file</param>
  85. /// <param name="verbosity">Verbosity level</param>
  86. /// <returns>SafeLlavaModelHandle</returns>
  87. [DllImport(NativeApi.llavaLibraryName, EntryPoint = "clip_model_load", CallingConvention = CallingConvention.Cdecl)]
  88. private static extern SafeLlavaModelHandle clip_model_load(string mmProj, int verbosity);
  89. /// <summary>
  90. /// Frees MULTI MODAL PROJECTIONS model / Clip Model
  91. /// </summary>
  92. /// <param name="ctx">Internal Pointer to the model</param>
  93. [DllImport(NativeApi.llavaLibraryName, EntryPoint = "clip_free", CallingConvention = CallingConvention.Cdecl)]
  94. private static extern void clip_free(IntPtr ctx);
  95. }
  96. }