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

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