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.

SafeLlamaModelHandle.cs 1.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using System;
  2. using LLama.Exceptions;
  3. namespace LLama.Native
  4. {
  5. /// <summary>
  6. /// A reference to a set of llama model weights
  7. /// </summary>
  8. public class SafeLlamaModelHandle
  9. : SafeLLamaHandleBase
  10. {
  11. internal SafeLlamaModelHandle(IntPtr handle)
  12. : base(handle)
  13. {
  14. }
  15. /// <inheritdoc />
  16. protected override bool ReleaseHandle()
  17. {
  18. NativeApi.llama_free_model(handle);
  19. SetHandle(IntPtr.Zero);
  20. return true;
  21. }
  22. /// <summary>
  23. /// Load a model from the given file path into memory
  24. /// </summary>
  25. /// <param name="modelPath"></param>
  26. /// <param name="lparams"></param>
  27. /// <returns></returns>
  28. /// <exception cref="RuntimeError"></exception>
  29. public static SafeLlamaModelHandle LoadFromFile(string modelPath, LLamaContextParams lparams)
  30. {
  31. var model_ptr = NativeApi.llama_load_model_from_file(modelPath, lparams);
  32. if (model_ptr == IntPtr.Zero)
  33. throw new RuntimeError($"Failed to load model {modelPath}.");
  34. return new SafeLlamaModelHandle(model_ptr);
  35. }
  36. }
  37. }