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

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