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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. /// <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; } = 0xFFFFFFFF;
  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; }
  43. /// <summary>
  44. /// Compute perplexity over the prompt (perplexity)
  45. /// </summary>
  46. public bool Perplexity { get; set; }
  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 (null = autodetect) (n_threads)
  61. /// </summary>
  62. public uint? Threads { get; set; }
  63. /// <summary>
  64. /// Number of threads to use for batch processing (null = autodetect) (n_threads)
  65. /// </summary>
  66. public uint? BatchThreads { get; set; }
  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; }
  76. /// <summary>
  77. /// how split tensors should be distributed across GPUs.
  78. /// </summary>
  79. /// <remarks>"[ 3, 2 ]" will assign 60% of the data to GPU 0 and 40% to GPU 1.</remarks>
  80. [JsonConverter(typeof(TensorSplitsCollectionConverter))]
  81. public TensorSplitsCollection TensorSplits { get; set; } = new();
  82. /// <summary>
  83. /// RoPE base frequency
  84. /// </summary>
  85. public float? RopeFrequencyBase { get; set; }
  86. /// <summary>
  87. /// RoPE frequency scaling factor
  88. /// </summary>
  89. public float? RopeFrequencyScale { get; set; }
  90. /// <inheritdoc />
  91. public float? YarnExtrapolationFactor { get; set; }
  92. /// <inheritdoc />
  93. public float? YarnAttentionFactor { get; set; }
  94. /// <inheritdoc />
  95. public float? YarnBetaFast { get; set; }
  96. /// <inheritdoc />
  97. public float? YarnBetaSlow { get; set; }
  98. /// <inheritdoc />
  99. public uint? YarnOriginalContext { get; set; }
  100. /// <inheritdoc />
  101. public RopeScalingType? YarnScalingType { get; set; }
  102. /// <summary>
  103. /// Use experimental mul_mat_q kernels
  104. /// </summary>
  105. public bool MulMatQ { get; set; }
  106. /// <summary>
  107. /// Load vocab only (no weights)
  108. /// </summary>
  109. public bool VocabOnly { get; set; }
  110. /// <summary>
  111. /// The encoding to use to convert text for the model
  112. /// </summary>
  113. [JsonConverter(typeof(EncodingConverter))]
  114. public Encoding Encoding { get; set; } = Encoding.UTF8;
  115. /// <summary>
  116. ///
  117. /// </summary>
  118. /// <param name="modelPath">The model path.</param>
  119. [JsonConstructor]
  120. public ModelParams(string modelPath)
  121. {
  122. ModelPath = modelPath;
  123. }
  124. private ModelParams()
  125. {
  126. // This constructor (default parameterless constructor) is used by Newtonsoft to deserialize!
  127. ModelPath = "";
  128. }
  129. /// <summary>
  130. ///
  131. /// </summary>
  132. /// <param name="modelPath">The model path.</param>
  133. /// <param name="contextSize">Model context size (n_ctx)</param>
  134. /// <param name="gpuLayerCount">Number of layers to run in VRAM / GPU memory (n_gpu_layers)</param>
  135. /// <param name="seed">Seed for the random number generator (seed)</param>
  136. /// <param name="useFp16Memory">Whether to use f16 instead of f32 for memory kv (memory_f16)</param>
  137. /// <param name="useMemorymap">Whether to use mmap for faster loads (use_mmap)</param>
  138. /// <param name="useMemoryLock">Whether to use mlock to keep model in memory (use_mlock)</param>
  139. /// <param name="perplexity">Thether to compute perplexity over the prompt (perplexity)</param>
  140. /// <param name="loraAdapter">Lora adapter path (lora_adapter)</param>
  141. /// <param name="loraBase">Base model path for the lora adapter (lora_base)</param>
  142. /// <param name="threads">Number of threads (-1 = autodetect) (n_threads)</param>
  143. /// <param name="batchSize">Batch size for prompt processing (must be >=32 to use BLAS) (n_batch)</param>
  144. /// <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>
  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, uint contextSize = 512, int gpuLayerCount = 20,
  151. uint seed = 1337, bool useFp16Memory = true,
  152. bool useMemorymap = true, bool useMemoryLock = false, bool perplexity = false,
  153. string loraAdapter = "", string loraBase = "", int threads = -1, uint batchSize = 512,
  154. bool embeddingMode = false,
  155. float? ropeFrequencyBase = null, float? ropeFrequencyScale = null, 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. LoraBase = loraBase;
  167. Threads = threads < 1 ? null : (uint)threads;
  168. BatchSize = batchSize;
  169. EmbeddingMode = embeddingMode;
  170. RopeFrequencyBase = ropeFrequencyBase;
  171. RopeFrequencyScale = ropeFrequencyScale;
  172. MulMatQ = mulMatQ;
  173. Encoding = Encoding.GetEncoding(encoding);
  174. LoraAdapters.Add(new LoraAdapter(loraAdapter, 1));
  175. }
  176. }
  177. internal class EncodingConverter
  178. : JsonConverter<Encoding>
  179. {
  180. public override Encoding? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
  181. {
  182. var name = reader.GetString();
  183. if (name == null)
  184. return null;
  185. return Encoding.GetEncoding(name);
  186. }
  187. public override void Write(Utf8JsonWriter writer, Encoding value, JsonSerializerOptions options)
  188. {
  189. writer.WriteStringValue(value.WebName);
  190. }
  191. }
  192. internal class TensorSplitsCollectionConverter
  193. : JsonConverter<TensorSplitsCollection>
  194. {
  195. public override TensorSplitsCollection? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
  196. {
  197. var arr = JsonSerializer.Deserialize<float[]>(ref reader, options) ?? Array.Empty<float>();
  198. return new TensorSplitsCollection(arr);
  199. }
  200. public override void Write(Utf8JsonWriter writer, TensorSplitsCollection value, JsonSerializerOptions options)
  201. {
  202. JsonSerializer.Serialize(writer, value.Splits, options);
  203. }
  204. }
  205. }