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

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