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.

IModelParamsExtensions.cs 2.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System.IO;
  2. using System;
  3. using System.Buffers;
  4. using LLama.Abstractions;
  5. using LLama.Native;
  6. namespace LLama.Extensions
  7. {
  8. /// <summary>
  9. /// Extention methods to the IModelParams interface
  10. /// </summary>
  11. public static class IModelParamsExtensions
  12. {
  13. /// <summary>
  14. /// Convert the given `IModelParams` into a `LLamaContextParams`
  15. /// </summary>
  16. /// <param name="params"></param>
  17. /// <param name="result"></param>
  18. /// <returns></returns>
  19. /// <exception cref="FileNotFoundException"></exception>
  20. /// <exception cref="ArgumentException"></exception>
  21. public static void ToLlamaContextParams(this IModelParams @params, out LLamaContextParams result)
  22. {
  23. result = NativeApi.llama_context_default_params();
  24. result.n_ctx = @params.ContextSize;
  25. result.n_batch = @params.BatchSize;
  26. result.seed = @params.Seed;
  27. result.f16_kv = @params.UseFp16Memory;
  28. result.logits_all = @params.Perplexity;
  29. result.embedding = @params.EmbeddingMode;
  30. result.rope_freq_base = @params.RopeFrequencyBase;
  31. result.rope_freq_scale = @params.RopeFrequencyScale;
  32. result.mul_mat_q = @params.MulMatQ;
  33. }
  34. /// <summary>
  35. /// Convert the given `IModelParams` into a `LLamaModelParams`
  36. /// </summary>
  37. /// <param name="params"></param>
  38. /// <param name="result"></param>
  39. /// <returns></returns>
  40. /// <exception cref="FileNotFoundException"></exception>
  41. /// <exception cref="ArgumentException"></exception>
  42. public static MemoryHandle ToLlamaModelParams(this IModelParams @params, out LLamaModelParams result)
  43. {
  44. if (@params.TensorSplits != null && @params.TensorSplits.Length != 1)
  45. throw new ArgumentException("Currently multi-gpu support is not supported by both llama.cpp and LLamaSharp.");
  46. result = NativeApi.llama_model_default_params();
  47. result.main_gpu = @params.MainGpu;
  48. result.n_gpu_layers = @params.GpuLayerCount;
  49. result.use_mlock = @params.UseMemoryLock;
  50. result.use_mmap = @params.UseMemorymap;
  51. result.vocab_only = @params.VocabOnly;
  52. var pin = @params.TensorSplits.AsMemory().Pin();
  53. unsafe
  54. {
  55. result.tensor_split = (float*)pin.Pointer;
  56. }
  57. return pin;
  58. }
  59. }
  60. }