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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. return clip_model_load(modelPath, verbosity)
  38. ?? throw new LoadWeightsFailedException(modelPath);
  39. }
  40. /// <summary>
  41. /// Create the Image Embeddings.
  42. /// </summary>
  43. /// <param name="ctxLlama">LLama Context</param>
  44. /// <param name="image">Image filename (it supports jpeg format only)</param>
  45. /// <returns>return the SafeHandle of these embeddings</returns>
  46. public SafeLlavaImageEmbedHandle CreateImageEmbeddings(LLamaContext ctxLlama, string image)
  47. {
  48. return SafeLlavaImageEmbedHandle.CreateFromFileName(this, ctxLlama, image);
  49. }
  50. /// <summary>
  51. /// Create the Image Embeddings.
  52. /// </summary>
  53. /// <param name="ctxLlama">LLama Context</param>
  54. /// <param name="image">Image in binary format (it supports jpeg format only)</param>
  55. /// <returns>return the SafeHandle of these embeddings</returns>
  56. public SafeLlavaImageEmbedHandle CreateImageEmbeddings(LLamaContext ctxLlama, byte[] image )
  57. {
  58. return SafeLlavaImageEmbedHandle.CreateFromMemory(this, ctxLlama, image );
  59. }
  60. /// <summary>
  61. /// Evaluates the image embeddings.
  62. /// </summary>
  63. /// <param name="ctxLlama">Llama Context</param>
  64. /// <param name="imageEmbed">The current embeddings to evaluate</param>
  65. /// <param name="n_past"></param>
  66. /// <returns>True on success</returns>
  67. public bool EvalImageEmbed(LLamaContext ctxLlama, SafeLlavaImageEmbedHandle imageEmbed, ref int n_past)
  68. {
  69. return NativeApi.llava_eval_image_embed(ctxLlama.NativeHandle, imageEmbed, (int)ctxLlama.Params.BatchSize, ref n_past );
  70. }
  71. /// <summary>
  72. /// Load MULTI MODAL PROJECTIONS model / Clip Model
  73. /// </summary>
  74. /// <param name="mmProj"> Model path/file</param>
  75. /// <param name="verbosity">Verbosity level</param>
  76. /// <returns>SafeLlavaModelHandle</returns>
  77. [DllImport(NativeApi.llavaLibraryName, EntryPoint = "clip_model_load", CallingConvention = CallingConvention.Cdecl)]
  78. private static extern SafeLlavaModelHandle clip_model_load(string mmProj, int verbosity);
  79. /// <summary>
  80. /// Frees MULTI MODAL PROJECTIONS model / Clip Model
  81. /// </summary>
  82. /// <param name="ctx">Internal Pointer to the model</param>
  83. [DllImport(NativeApi.llavaLibraryName, EntryPoint = "clip_free", CallingConvention = CallingConvention.Cdecl)]
  84. private static extern void clip_free(IntPtr ctx);
  85. }
  86. }