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

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