use mistralrs_core::*;
use std::num::NonZeroUsize;
use crate::{best_device, Model};
pub struct GgufModelBuilder {
pub(crate) model_id: String,
pub(crate) files: Vec<String>,
pub(crate) tok_model_id: Option<String>,
pub(crate) token_source: TokenSource,
pub(crate) hf_revision: Option<String>,
pub(crate) chat_template: Option<String>,
pub(crate) tokenizer_json: Option<String>,
pub(crate) device_mapping: Option<DeviceMapMetadata>,
pub(crate) prompt_batchsize: Option<NonZeroUsize>,
pub(crate) force_cpu: bool,
pub(crate) topology: Option<Topology>,
pub(crate) paged_attn_cfg: Option<PagedAttentionConfig>,
pub(crate) max_num_seqs: usize,
pub(crate) no_kv_cache: bool,
pub(crate) with_logging: bool,
pub(crate) prefix_cache_n: Option<usize>,
}
impl GgufModelBuilder {
pub fn new(model_id: impl ToString, files: Vec<impl ToString>) -> Self {
Self {
model_id: model_id.to_string(),
files: files.into_iter().map(|f| f.to_string()).collect::<Vec<_>>(),
prompt_batchsize: None,
chat_template: None,
tokenizer_json: None,
force_cpu: false,
token_source: TokenSource::CacheToken,
hf_revision: None,
paged_attn_cfg: None,
max_num_seqs: 32,
no_kv_cache: false,
prefix_cache_n: Some(16),
with_logging: false,
topology: None,
tok_model_id: None,
device_mapping: None,
}
}
pub fn with_tok_model_id(mut self, tok_model_id: impl ToString) -> Self {
self.tok_model_id = Some(tok_model_id.to_string());
self
}
pub fn with_prompt_batchsize(mut self, prompt_batchsize: NonZeroUsize) -> Self {
self.prompt_batchsize = Some(prompt_batchsize);
self
}
pub fn with_topology(mut self, topology: Topology) -> Self {
self.topology = Some(topology);
self
}
pub fn with_chat_template(mut self, chat_template: impl ToString) -> Self {
self.chat_template = Some(chat_template.to_string());
self
}
pub fn with_tokenizer_json(mut self, tokenizer_json: impl ToString) -> Self {
self.tokenizer_json = Some(tokenizer_json.to_string());
self
}
pub fn with_force_cpu(mut self) -> Self {
self.force_cpu = true;
self
}
pub fn with_token_source(mut self, token_source: TokenSource) -> Self {
self.token_source = token_source;
self
}
pub fn with_hf_revision(mut self, revision: impl ToString) -> Self {
self.hf_revision = Some(revision.to_string());
self
}
pub fn with_paged_attn(
mut self,
paged_attn_cfg: impl FnOnce() -> anyhow::Result<PagedAttentionConfig>,
) -> anyhow::Result<Self> {
if paged_attn_supported() {
self.paged_attn_cfg = Some(paged_attn_cfg()?);
} else {
self.paged_attn_cfg = None;
}
Ok(self)
}
pub fn with_max_num_seqs(mut self, max_num_seqs: usize) -> Self {
self.max_num_seqs = max_num_seqs;
self
}
pub fn with_no_kv_cache(mut self) -> Self {
self.no_kv_cache = true;
self
}
pub fn with_prefix_cache_n(mut self, n_seqs: Option<usize>) -> Self {
self.prefix_cache_n = n_seqs;
self
}
pub fn with_logging(mut self) -> Self {
self.with_logging = true;
self
}
pub fn with_device_mapping(mut self, device_mapping: DeviceMapMetadata) -> Self {
self.device_mapping = Some(device_mapping);
self
}
pub async fn build(self) -> anyhow::Result<Model> {
let config = GGUFSpecificConfig {
prompt_batchsize: self.prompt_batchsize,
topology: self.topology,
};
if self.with_logging {
initialize_logging();
}
let loader = GGUFLoaderBuilder::new(
self.chat_template,
self.tok_model_id,
self.model_id,
self.files,
config,
)
.build();
let pipeline = loader.load_model_from_hf(
self.hf_revision,
self.token_source,
&ModelDType::Auto,
&best_device(self.force_cpu)?,
!self.with_logging,
self.device_mapping.unwrap_or(DeviceMapMetadata::dummy()),
None,
self.paged_attn_cfg,
)?;
let scheduler_method = match self.paged_attn_cfg {
Some(_) => {
let config = pipeline
.lock()
.await
.get_metadata()
.cache_config
.as_ref()
.unwrap()
.clone();
SchedulerConfig::PagedAttentionMeta {
max_num_seqs: self.max_num_seqs,
config,
}
}
None => SchedulerConfig::DefaultScheduler {
method: DefaultSchedulerMethod::Fixed(self.max_num_seqs.try_into()?),
},
};
let mut runner = MistralRsBuilder::new(pipeline, scheduler_method)
.with_no_kv_cache(self.no_kv_cache)
.with_gemm_full_precision_f16(true)
.with_no_prefix_cache(self.prefix_cache_n.is_none());
if let Some(n) = self.prefix_cache_n {
runner = runner.with_prefix_cache_n(n)
}
Ok(Model::new(runner.build()))
}
}