2026-05-28 23:21:15 +08:00
|
|
|
use crate::models::common::generate::{GenerationDataProvider, PrepareData};
|
|
|
|
|
|
2026-04-02 22:22:52 +08:00
|
|
|
use anyhow::Result;
|
|
|
|
|
use candle_core::{DType, Device};
|
2025-09-25 12:09:25 +08:00
|
|
|
use candle_nn::VarBuilder;
|
|
|
|
|
|
2025-10-15 21:03:49 +08:00
|
|
|
use crate::models::minicpm4::config::MiniCPM4Config;
|
|
|
|
|
use crate::models::minicpm4::model::MiniCPMModel;
|
2026-04-02 22:22:52 +08:00
|
|
|
use crate::utils::{find_type_files, get_device, get_dtype};
|
2026-05-28 23:21:15 +08:00
|
|
|
use crate::{chat_template::ChatTemplate, tokenizer::TokenizerModel};
|
2025-10-15 21:03:49 +08:00
|
|
|
|
2026-05-27 00:50:23 +08:00
|
|
|
pub struct MiniCPM4GenerateModel<'a> {
|
2025-09-25 12:09:25 +08:00
|
|
|
chat_template: ChatTemplate<'a>,
|
|
|
|
|
tokenizer: TokenizerModel,
|
2026-05-28 23:21:15 +08:00
|
|
|
model: MiniCPMModel,
|
2025-09-25 12:09:25 +08:00
|
|
|
device: Device,
|
2025-12-03 17:21:01 +08:00
|
|
|
model_name: String,
|
2025-09-25 12:09:25 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-27 00:50:23 +08:00
|
|
|
impl<'a> MiniCPM4GenerateModel<'a> {
|
2025-09-25 12:44:17 +08:00
|
|
|
pub fn init(path: &str, device: Option<&Device>, dtype: Option<DType>) -> Result<Self> {
|
2025-09-25 12:09:25 +08:00
|
|
|
let chat_template = ChatTemplate::init(path)?;
|
|
|
|
|
let tokenizer = TokenizerModel::init(path)?;
|
|
|
|
|
let config_path = path.to_string() + "/config.json";
|
|
|
|
|
let cfg: MiniCPM4Config = serde_json::from_slice(&std::fs::read(config_path)?)?;
|
|
|
|
|
let device = &get_device(device);
|
|
|
|
|
let cfg_dtype = cfg.torch_dtype.as_str();
|
|
|
|
|
let dtype = get_dtype(dtype, cfg_dtype);
|
2025-10-15 21:03:49 +08:00
|
|
|
let model_list = find_type_files(path, "safetensors")?;
|
2025-09-25 12:09:25 +08:00
|
|
|
let vb = unsafe { VarBuilder::from_mmaped_safetensors(&model_list, dtype, device)? };
|
2026-05-28 23:21:15 +08:00
|
|
|
let model = MiniCPMModel::new(vb, cfg)?;
|
2026-03-30 20:33:48 +08:00
|
|
|
let model_name = std::path::Path::new(path)
|
2026-03-30 20:36:57 +08:00
|
|
|
.file_name()
|
2026-03-30 20:33:48 +08:00
|
|
|
.and_then(|s| s.to_str())
|
|
|
|
|
.unwrap_or("minicpm4")
|
|
|
|
|
.to_string();
|
2026-05-27 00:50:23 +08:00
|
|
|
Ok(MiniCPM4GenerateModel {
|
2025-09-25 12:09:25 +08:00
|
|
|
chat_template,
|
|
|
|
|
tokenizer,
|
2026-05-28 23:21:15 +08:00
|
|
|
model,
|
2025-09-25 12:09:25 +08:00
|
|
|
device: device.clone(),
|
2026-03-30 20:33:48 +08:00
|
|
|
model_name,
|
2025-09-25 12:09:25 +08:00
|
|
|
})
|
|
|
|
|
}
|
2025-09-25 12:44:17 +08:00
|
|
|
}
|
|
|
|
|
|
2026-05-28 23:21:15 +08:00
|
|
|
impl<'a> GenerationDataProvider for MiniCPM4GenerateModel<'a> {
|
|
|
|
|
fn get_data(&self, mes: &crate::params::chat::ChatCompletionParameters) -> Result<PrepareData> {
|
|
|
|
|
let mes_render = self.chat_template.apply_chat_template(mes)?;
|
|
|
|
|
let in_reasoning = self.is_in_reasoning(&mes_render);
|
2026-04-02 22:22:52 +08:00
|
|
|
let input_ids = self.tokenizer.text_encode(mes_render, &self.device)?;
|
2026-05-28 23:21:15 +08:00
|
|
|
let multi_model_data = self.get_multi_model_data();
|
|
|
|
|
Ok(PrepareData {
|
|
|
|
|
in_reasoning,
|
2026-04-02 22:22:52 +08:00
|
|
|
input_ids,
|
2026-05-28 23:21:15 +08:00
|
|
|
multi_model_data,
|
|
|
|
|
})
|
2025-09-25 12:09:25 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-05-28 23:21:15 +08:00
|
|
|
|
|
|
|
|
crate::impl_generate_model!(MiniCPM4GenerateModel<'a>);
|