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

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