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.

ModelParams.cs 7.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using LLama.Abstractions;
  2. using System;
  3. using System.Text;
  4. using System.Text.Json;
  5. using System.Text.Json.Serialization;
  6. using LLama.Native;
  7. namespace LLama.Common
  8. {
  9. /// <summary>
  10. /// The parameters for initializing a LLama model.
  11. /// </summary>
  12. public record ModelParams
  13. : ILLamaParams
  14. {
  15. /// <inheritdoc />
  16. public uint? ContextSize { get; set; }
  17. /// <inheritdoc />
  18. public int MainGpu { get; set; } = 0;
  19. /// <inheritdoc />
  20. public int GpuLayerCount { get; set; } = 20;
  21. /// <inheritdoc />
  22. public uint Seed { get; set; } = 0xFFFFFFFF;
  23. /// <inheritdoc />
  24. public bool UseFp16Memory { get; set; } = true;
  25. /// <inheritdoc />
  26. public bool UseMemorymap { get; set; } = true;
  27. /// <inheritdoc />
  28. public bool UseMemoryLock { get; set; }
  29. /// <inheritdoc />
  30. public bool Perplexity { get; set; }
  31. /// <inheritdoc />
  32. public string ModelPath { get; set; }
  33. /// <inheritdoc />
  34. public AdapterCollection LoraAdapters { get; set; } = new();
  35. /// <inheritdoc />
  36. public string LoraBase { get; set; } = string.Empty;
  37. /// <inheritdoc />
  38. public uint? Threads { get; set; }
  39. /// <inheritdoc />
  40. public uint? BatchThreads { get; set; }
  41. /// <inheritdoc />
  42. public uint BatchSize { get; set; } = 512;
  43. /// <inheritdoc />
  44. public bool EmbeddingMode { get; set; }
  45. /// <inheritdoc />
  46. [JsonConverter(typeof(TensorSplitsCollectionConverter))]
  47. public TensorSplitsCollection TensorSplits { get; set; } = new();
  48. /// <inheritdoc />
  49. public float? RopeFrequencyBase { get; set; }
  50. /// <inheritdoc />
  51. public float? RopeFrequencyScale { get; set; }
  52. /// <inheritdoc />
  53. public float? YarnExtrapolationFactor { get; set; }
  54. /// <inheritdoc />
  55. public float? YarnAttentionFactor { get; set; }
  56. /// <inheritdoc />
  57. public float? YarnBetaFast { get; set; }
  58. /// <inheritdoc />
  59. public float? YarnBetaSlow { get; set; }
  60. /// <inheritdoc />
  61. public uint? YarnOriginalContext { get; set; }
  62. /// <inheritdoc />
  63. public RopeScalingType? YarnScalingType { get; set; }
  64. /// <summary>
  65. /// Use experimental mul_mat_q kernels
  66. /// </summary>
  67. public bool MulMatQ { get; set; }
  68. /// <inheritdoc />
  69. public bool VocabOnly { get; set; }
  70. /// <inheritdoc />
  71. [JsonConverter(typeof(EncodingConverter))]
  72. public Encoding Encoding { get; set; } = Encoding.UTF8;
  73. /// <summary>
  74. ///
  75. /// </summary>
  76. /// <param name="modelPath">The model path.</param>
  77. [JsonConstructor]
  78. public ModelParams(string modelPath)
  79. {
  80. ModelPath = modelPath;
  81. }
  82. private ModelParams()
  83. {
  84. // This constructor (default parameterless constructor) is used by Newtonsoft to deserialize!
  85. ModelPath = "";
  86. }
  87. /// <summary>
  88. ///
  89. /// </summary>
  90. /// <param name="modelPath">The model path.</param>
  91. /// <param name="contextSize">Model context size (n_ctx)</param>
  92. /// <param name="gpuLayerCount">Number of layers to run in VRAM / GPU memory (n_gpu_layers)</param>
  93. /// <param name="seed">Seed for the random number generator (seed)</param>
  94. /// <param name="useFp16Memory">Whether to use f16 instead of f32 for memory kv (memory_f16)</param>
  95. /// <param name="useMemorymap">Whether to use mmap for faster loads (use_mmap)</param>
  96. /// <param name="useMemoryLock">Whether to use mlock to keep model in memory (use_mlock)</param>
  97. /// <param name="perplexity">Thether to compute perplexity over the prompt (perplexity)</param>
  98. /// <param name="loraAdapter">Lora adapter path (lora_adapter)</param>
  99. /// <param name="loraBase">Base model path for the lora adapter (lora_base)</param>
  100. /// <param name="threads">Number of threads (-1 = autodetect) (n_threads)</param>
  101. /// <param name="batchSize">Batch size for prompt processing (must be >=32 to use BLAS) (n_batch)</param>
  102. /// <param name="embeddingMode">Whether to use embedding mode. (embedding) Note that if this is set to true, The LLamaModel won't produce text response anymore.</param>
  103. /// <param name="ropeFrequencyBase">RoPE base frequency.</param>
  104. /// <param name="ropeFrequencyScale">RoPE frequency scaling factor</param>
  105. /// <param name="mulMatQ">Use experimental mul_mat_q kernels</param>
  106. /// <param name="encoding">The encoding to use to convert text for the model</param>
  107. [Obsolete("Use object initializer to set all optional parameters")]
  108. public ModelParams(string modelPath, uint contextSize = 512, int gpuLayerCount = 20,
  109. uint seed = 1337, bool useFp16Memory = true,
  110. bool useMemorymap = true, bool useMemoryLock = false, bool perplexity = false,
  111. string loraAdapter = "", string loraBase = "", int threads = -1, uint batchSize = 512,
  112. bool embeddingMode = false,
  113. float? ropeFrequencyBase = null, float? ropeFrequencyScale = null, bool mulMatQ = false,
  114. string encoding = "UTF-8")
  115. {
  116. ContextSize = contextSize;
  117. GpuLayerCount = gpuLayerCount;
  118. Seed = seed;
  119. UseFp16Memory = useFp16Memory;
  120. UseMemorymap = useMemorymap;
  121. UseMemoryLock = useMemoryLock;
  122. Perplexity = perplexity;
  123. ModelPath = modelPath;
  124. LoraBase = loraBase;
  125. Threads = threads < 1 ? null : (uint)threads;
  126. BatchSize = batchSize;
  127. EmbeddingMode = embeddingMode;
  128. RopeFrequencyBase = ropeFrequencyBase;
  129. RopeFrequencyScale = ropeFrequencyScale;
  130. MulMatQ = mulMatQ;
  131. Encoding = Encoding.GetEncoding(encoding);
  132. LoraAdapters.Add(new LoraAdapter(loraAdapter, 1));
  133. }
  134. }
  135. internal class EncodingConverter
  136. : JsonConverter<Encoding>
  137. {
  138. public override Encoding? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
  139. {
  140. var name = reader.GetString();
  141. if (name == null)
  142. return null;
  143. return Encoding.GetEncoding(name);
  144. }
  145. public override void Write(Utf8JsonWriter writer, Encoding value, JsonSerializerOptions options)
  146. {
  147. writer.WriteStringValue(value.WebName);
  148. }
  149. }
  150. internal class TensorSplitsCollectionConverter
  151. : JsonConverter<TensorSplitsCollection>
  152. {
  153. public override TensorSplitsCollection? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
  154. {
  155. var arr = JsonSerializer.Deserialize<float[]>(ref reader, options) ?? Array.Empty<float>();
  156. return new TensorSplitsCollection(arr);
  157. }
  158. public override void Write(Utf8JsonWriter writer, TensorSplitsCollection value, JsonSerializerOptions options)
  159. {
  160. JsonSerializer.Serialize(writer, value.Splits, options);
  161. }
  162. }
  163. }