use std::num::NonZeroU32;
use ik_llama_cpp_sys as sys;
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum LlamaContextType {
Default,
Mtp,
}
#[derive(Debug, Clone)]
pub struct LlamaContextParams {
pub(crate) params: sys::llama_context_params,
}
impl Default for LlamaContextParams {
fn default() -> Self {
Self {
params: unsafe { sys::llama_context_default_params() },
}
}
}
impl LlamaContextParams {
#[must_use]
pub fn with_n_ctx(mut self, n_ctx: Option<NonZeroU32>) -> Self {
self.params.n_ctx = n_ctx.map_or(0, NonZeroU32::get);
self
}
#[must_use]
pub fn with_n_batch(mut self, n_batch: u32) -> Self {
self.params.n_batch = n_batch;
self
}
#[must_use]
pub fn with_n_ubatch(mut self, n_ubatch: u32) -> Self {
self.params.n_ubatch = n_ubatch;
self
}
#[must_use]
pub fn with_n_rs_seq(mut self, n_rs_seq: u32) -> Self {
self.params.n_seq_max = n_rs_seq;
self
}
#[must_use]
pub fn with_context_type(mut self, context_type: LlamaContextType) -> Self {
self.params.mtp = matches!(context_type, LlamaContextType::Mtp);
self
}
#[must_use]
pub fn with_seed(mut self, seed: u32) -> Self {
self.params.seed = seed;
self
}
#[must_use]
pub fn with_n_threads(mut self, n_threads: u32) -> Self {
self.params.n_threads = n_threads;
self
}
#[must_use]
pub fn with_n_threads_batch(mut self, n_threads_batch: u32) -> Self {
self.params.n_threads_batch = n_threads_batch;
self
}
#[must_use]
pub fn with_flash_attn(mut self, flash_attn: bool) -> Self {
self.params.flash_attn = flash_attn;
self
}
#[must_use]
pub fn with_mtp(mut self, mtp: bool) -> Self {
self.params.mtp = mtp;
self
}
#[must_use]
pub fn with_embeddings(mut self, embeddings: bool) -> Self {
self.params.embeddings = embeddings;
self
}
#[must_use]
pub fn with_pooling_type(mut self, pooling_type: sys::llama_pooling_type) -> Self {
self.params.pooling_type = pooling_type;
self
}
#[must_use]
pub fn as_raw(&self) -> &sys::llama_context_params {
&self.params
}
}