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.

UnderstandLLamaContext.md 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # Understand LLamaSharp context
  2. `LLamaContext` is the most important component as a link between native APIs and higher-level APIs. It contains the basic settings for model inference and holds the kv-cache, which could significantly accelerate the model inference. Since `LLamaContext` is not coupled with `LLamaWeights`, it's possible to create multiple context based on one piece of model weight. Each `ILLamaExecutor` will hold a `LLamaContext` instance, but it's possible to switch to different context in an executor.
  3. If your application has multiple sessions, please take care of managing `LLamaContext`.
  4. `LLamaContext` takes the following parameters as its settings. Note that the parameters could not be changed once the context has been created.
  5. ```cs
  6. public interface IContextParams
  7. {
  8. /// <summary>
  9. /// Model context size (n_ctx)
  10. /// </summary>
  11. uint? ContextSize { get; }
  12. /// <summary>
  13. /// batch size for prompt processing (must be >=32 to use BLAS) (n_batch)
  14. /// </summary>
  15. uint BatchSize { get; }
  16. /// <summary>
  17. /// Seed for the random number generator (seed)
  18. /// </summary>
  19. uint Seed { get; }
  20. /// <summary>
  21. /// Whether to use embedding mode. (embedding) Note that if this is set to true,
  22. /// The LLamaModel won't produce text response anymore.
  23. /// </summary>
  24. bool EmbeddingMode { get; }
  25. /// <summary>
  26. /// RoPE base frequency (null to fetch from the model)
  27. /// </summary>
  28. float? RopeFrequencyBase { get; }
  29. /// <summary>
  30. /// RoPE frequency scaling factor (null to fetch from the model)
  31. /// </summary>
  32. float? RopeFrequencyScale { get; }
  33. /// <summary>
  34. /// The encoding to use for models
  35. /// </summary>
  36. Encoding Encoding { get; }
  37. /// <summary>
  38. /// Number of threads (null = autodetect) (n_threads)
  39. /// </summary>
  40. uint? Threads { get; }
  41. /// <summary>
  42. /// Number of threads to use for batch processing (null = autodetect) (n_threads)
  43. /// </summary>
  44. uint? BatchThreads { get; }
  45. /// <summary>
  46. /// YaRN extrapolation mix factor (null = from model)
  47. /// </summary>
  48. float? YarnExtrapolationFactor { get; }
  49. /// <summary>
  50. /// YaRN magnitude scaling factor (null = from model)
  51. /// </summary>
  52. float? YarnAttentionFactor { get; }
  53. /// <summary>
  54. /// YaRN low correction dim (null = from model)
  55. /// </summary>
  56. float? YarnBetaFast { get; }
  57. /// <summary>
  58. /// YaRN high correction dim (null = from model)
  59. /// </summary>
  60. float? YarnBetaSlow { get; }
  61. /// <summary>
  62. /// YaRN original context length (null = from model)
  63. /// </summary>
  64. uint? YarnOriginalContext { get; }
  65. /// <summary>
  66. /// YaRN scaling method to use.
  67. /// </summary>
  68. RopeScalingType? YarnScalingType { get; }
  69. /// <summary>
  70. /// Override the type of the K cache
  71. /// </summary>
  72. GGMLType? TypeK { get; }
  73. /// <summary>
  74. /// Override the type of the V cache
  75. /// </summary>
  76. GGMLType? TypeV { get; }
  77. /// <summary>
  78. /// Whether to disable offloading the KQV cache to the GPU
  79. /// </summary>
  80. bool NoKqvOffload { get; }
  81. /// <summary>
  82. /// defragment the KV cache if holes/size &gt; defrag_threshold, Set to &lt; 0 to disable (default)
  83. /// </summary>
  84. float DefragThreshold { get; }
  85. /// <summary>
  86. /// Whether to pool (sum) embedding results by sequence id (ignored if no pooling layer)
  87. /// </summary>
  88. bool DoPooling { get; }
  89. }
  90. ```
  91. `LLamaContext` has its state, which could be saved and loaded.
  92. ```cs
  93. LLamaContext.SaveState(string filename)
  94. LLamaContext.GetState()
  95. ```