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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. using LLama.Abstractions;
  2. using System;
  3. namespace LLama.Common
  4. {
  5. /// <summary>
  6. /// The parameters for initializing a LLama model.
  7. /// </summary>
  8. public class ModelParams
  9. : IModelParams
  10. {
  11. /// <summary>
  12. /// Model context size (n_ctx)
  13. /// </summary>
  14. public int ContextSize { get; set; } = 512;
  15. /// <summary>
  16. /// the GPU that is used for scratch and small tensors
  17. /// </summary>
  18. public int MainGpu { get; set; } = 0;
  19. /// <summary>
  20. /// if true, reduce VRAM usage at the cost of performance
  21. /// </summary>
  22. public bool LowVram { get; set; } = false;
  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 int 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. /// model alias
  53. /// </summary>
  54. public string ModelAlias { get; set; } = "unknown";
  55. /// <summary>
  56. /// lora adapter path (lora_adapter)
  57. /// </summary>
  58. public string LoraAdapter { get; set; } = string.Empty;
  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 int BatchSize { get; set; } = 512;
  71. /// <summary>
  72. /// Whether to convert eos to newline during the inference.
  73. /// </summary>
  74. public bool ConvertEosToNewLine { get; set; } = false;
  75. /// <summary>
  76. /// Whether to use embedding mode. (embedding) Note that if this is set to true,
  77. /// The LLamaModel won't produce text response anymore.
  78. /// </summary>
  79. public bool EmbeddingMode { get; set; } = false;
  80. /// <summary>
  81. /// how split tensors should be distributed across GPUs
  82. /// </summary>
  83. public float[]? TensorSplits { get; set; }
  84. /// <summary>
  85. /// Grouped-Query Attention
  86. /// </summary>
  87. public int GroupedQueryAttention { get; set; } = 1;
  88. /// <summary>
  89. /// RMS Norm Epsilon
  90. /// </summary>
  91. public float RmsNormEpsilon { get; set; } = 5e-6f;
  92. /// <summary>
  93. /// RoPE base frequency
  94. /// </summary>
  95. public float RopeFrequencyBase { get; set; } = 10000.0f;
  96. /// <summary>
  97. /// RoPE frequency scaling factor
  98. /// </summary>
  99. public float RopeFrequencyScale { get; set; } = 1.0f;
  100. /// <summary>
  101. /// Use experimental mul_mat_q kernels
  102. /// </summary>
  103. public bool MulMatQ { get; set; }
  104. /// <summary>
  105. /// The encoding to use to convert text for the model
  106. /// </summary>
  107. public string Encoding { get; set; } = "UTF-8";
  108. /// <summary>
  109. ///
  110. /// </summary>
  111. /// <param name="modelPath">The model path.</param>
  112. /// <param name="contextSize">Model context size (n_ctx)</param>
  113. /// <param name="gpuLayerCount">Number of layers to run in VRAM / GPU memory (n_gpu_layers)</param>
  114. /// <param name="seed">Seed for the random number generator (seed)</param>
  115. /// <param name="useFp16Memory">Whether to use f16 instead of f32 for memory kv (memory_f16)</param>
  116. /// <param name="useMemorymap">Whether to use mmap for faster loads (use_mmap)</param>
  117. /// <param name="useMemoryLock">Whether to use mlock to keep model in memory (use_mlock)</param>
  118. /// <param name="perplexity">Thether to compute perplexity over the prompt (perplexity)</param>
  119. /// <param name="loraAdapter">Lora adapter path (lora_adapter)</param>
  120. /// <param name="loraBase">Base model path for the lora adapter (lora_base)</param>
  121. /// <param name="threads">Number of threads (-1 = autodetect) (n_threads)</param>
  122. /// <param name="batchSize">Batch size for prompt processing (must be >=32 to use BLAS) (n_batch)</param>
  123. /// <param name="convertEosToNewLine">Whether to convert eos to newline during the inference.</param>
  124. /// <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>
  125. /// <param name="groupedQueryAttention">Grouped-Query Attention</param>
  126. /// <param name="rmsNormEpsilon">RMS Norm Epsilon</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. public ModelParams(string modelPath, int contextSize = 512, int gpuLayerCount = 20,
  132. int seed = 1337, bool useFp16Memory = true,
  133. bool useMemorymap = true, bool useMemoryLock = false, bool perplexity = false,
  134. string loraAdapter = "", string loraBase = "", int threads = -1, int batchSize = 512,
  135. bool convertEosToNewLine = false, bool embeddingMode = false,
  136. int groupedQueryAttention = 1, float rmsNormEpsilon = 5e-6f, float ropeFrequencyBase = 10000.0f, float ropeFrequencyScale = 1f, bool mulMatQ = false,
  137. string encoding = "UTF-8")
  138. {
  139. ContextSize = contextSize;
  140. GpuLayerCount = gpuLayerCount;
  141. Seed = seed;
  142. UseFp16Memory = useFp16Memory;
  143. UseMemorymap = useMemorymap;
  144. UseMemoryLock = useMemoryLock;
  145. Perplexity = perplexity;
  146. ModelPath = modelPath;
  147. LoraAdapter = loraAdapter;
  148. LoraBase = loraBase;
  149. Threads = threads == -1 ? Math.Max(Environment.ProcessorCount / 2, 1) : threads;
  150. BatchSize = batchSize;
  151. ConvertEosToNewLine = convertEosToNewLine;
  152. EmbeddingMode = embeddingMode;
  153. GroupedQueryAttention = groupedQueryAttention;
  154. RmsNormEpsilon = rmsNormEpsilon;
  155. RopeFrequencyBase = ropeFrequencyBase;
  156. RopeFrequencyScale = ropeFrequencyScale;
  157. MulMatQ = mulMatQ;
  158. Encoding = encoding;
  159. }
  160. }
  161. }