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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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"></param>
  34. /// <param name="lparams"></param>
  35. /// <returns></returns>
  36. /// <exception cref="RuntimeError"></exception>
  37. public static SafeLlavaModelHandle LoadFromFile(string modelPath, int verbosity )
  38. {
  39. // Try to open the model file, this will check:
  40. // - File exists (automatically throws FileNotFoundException)
  41. // - File is readable (explicit check)
  42. // This provides better error messages that llama.cpp, which would throw an access violation exception in both cases.
  43. using (var fs = new FileStream(modelPath, FileMode.Open))
  44. if (!fs.CanRead)
  45. throw new InvalidOperationException($"Llava MMP Model file '{modelPath}' is not readable");
  46. return clip_model_load(modelPath, verbosity)
  47. ?? throw new RuntimeError($"Failed to load LLaVa model {modelPath}.");
  48. }
  49. /// <summary>
  50. /// Embed the image from file in llama context
  51. /// </summary>
  52. /// <param name="ctxLlama"></param>
  53. /// <param name="image"></param>
  54. /// <param name="n_past"></param>
  55. /// <returns></returns>
  56. public bool EmbedImage(LLamaContext ctxLlama, string image, ref int n_past)
  57. {
  58. var ImageEmbed = SafeLlavaImageEmbedHandle.CreateFromFileName(this, ctxLlama, image);
  59. bool result = NativeApi.llava_eval_image_embed(ctxLlama.NativeHandle, ImageEmbed, (int)ctxLlama.Params.BatchSize, ref n_past );
  60. return result;
  61. }
  62. /// <summary>
  63. /// Embed the image from binary in llama context
  64. /// </summary>
  65. /// <param name="ctxLlama"></param>
  66. /// <param name="image">jpeg image</param>
  67. /// <param name="n_past"></param>
  68. /// <returns></returns>
  69. public bool EmbedImage(LLamaContext ctxLlama, Byte[] image, ref int n_past )
  70. {
  71. var ImageEmbed = SafeLlavaImageEmbedHandle.CreateFromMemory(this, ctxLlama, image );
  72. bool result = NativeApi.llava_eval_image_embed(ctxLlama.NativeHandle, ImageEmbed, (int)ctxLlama.Params.BatchSize, ref n_past );
  73. return result;
  74. }
  75. /// <summary>
  76. /// Load MULTI MODAL PROJECTIONS model / Clip Model
  77. /// </summary>
  78. /// <param name="mmProj"> Model path/file</param>
  79. /// <param name="verbosity">Verbosity level</param>
  80. /// <returns>SafeLlavaModelHandle</returns>
  81. [DllImport(NativeApi.llavaLibraryName, EntryPoint = "clip_model_load", CallingConvention = CallingConvention.Cdecl)]
  82. private static extern SafeLlavaModelHandle clip_model_load(string mmProj, int verbosity);
  83. /// <summary>
  84. /// Frees MULTI MODAL PROJECTIONS model / Clip Model
  85. /// </summary>
  86. /// <param name="ctx"></param>
  87. [DllImport(NativeApi.llavaLibraryName, EntryPoint = "clip_free", CallingConvention = CallingConvention.Cdecl)]
  88. private static extern void clip_free(IntPtr ctx);
  89. }
  90. }