refactor(项目结构) 重构为 workspace 多成员项目

- 将项目拆分为三个 crate:minicpm-core(核心模型)、minicpm-convert(转换功能)、minicpm-inference(推理功能)
- 添加两个示例:minimal-inference(最小推理)和 convert(模型转换)
- 转换后自动拷贝 config.json 和 tokenizer.json 到 model 目录
- 更新 README 说明 workspace 结构和使用方式
This commit is contained in:
2026-07-01 14:33:28 +08:00
parent 7e0585421f
commit 69b37ced07
29 changed files with 654446 additions and 421 deletions
+9
View File
@@ -0,0 +1,9 @@
[package]
name = "convert"
version = "0.1.0"
edition = "2021"
[dependencies]
minicpm-convert = { path = "../../crates/minicpm-convert" }
burn = { version = "0.21", features = ["std", "wgpu"] }
anyhow = "1.0"
+24
View File
@@ -0,0 +1,24 @@
use burn::backend::Wgpu;
use minicpm_convert::export_model;
use std::path::Path;
fn main() -> anyhow::Result<()> {
let device = Default::default();
let safetensors_path = Path::new("MiniCPM5-1B/model-00000-of-00001.safetensors");
let config_path = Path::new("MiniCPM5-1B/config.json");
let tokenizer_path = Path::new("MiniCPM5-1B/tokenizer.json");
let output_dir = Path::new("model");
export_model::<Wgpu>(
safetensors_path,
config_path,
tokenizer_path,
output_dir,
&device,
)?;
println!("模型转换完成!");
Ok(())
}
+9
View File
@@ -0,0 +1,9 @@
[package]
name = "minimal-inference"
version = "0.1.0"
edition = "2021"
[dependencies]
minicpm-inference = { path = "../../crates/minicpm-inference" }
burn = { version = "0.21", features = ["std", "wgpu"] }
anyhow = "1.0"
+46
View File
@@ -0,0 +1,46 @@
// 运行方式:
// 1. 确保已在项目根目录
// 2. 运行:cargo run --release -p minimal-inference
// 3. 确保模型文件存在:
// - model/model.mpk (模型权重)
// - MiniCPM5-1B/config.json (模型配置)
// - MiniCPM5-1B/tokenizer.json (分词器)
use burn::backend::Wgpu;
use minicpm_inference::{GenerationConfig, MiniCPM};
use std::time::Instant;
fn main() -> anyhow::Result<()> {
let device = Default::default();
println!("正在加载模型...");
let start = Instant::now();
let model = MiniCPM::<Wgpu>::load(
"model/model",
"MiniCPM5-1B/config.json",
"MiniCPM5-1B/tokenizer.json",
&device,
)?;
println!("模型加载完成,耗时: {:.2?}", start.elapsed());
let prompt = "你知道今天的天气不";
println!("\n用户: {}", prompt);
println!("Assistant: ");
let config = GenerationConfig {
max_new_tokens: Some(200),
temperature: 0.7,
top_p: 0.95,
};
let start = Instant::now();
let response = model.generate(prompt, false, &config)?;
let elapsed = start.elapsed();
println!("{}", response);
println!("\n生成耗时: {:.2?}", elapsed);
Ok(())
}