add hunyuan_ocr

This commit is contained in:
jhqxxx
2025-12-03 17:21:01 +08:00
parent 7d72cb3baf
commit 697484cf23
29 changed files with 1756 additions and 190 deletions
+4 -2
View File
@@ -23,6 +23,7 @@ pub struct MiniCPMGenerateModel<'a> {
device: Device,
endoftext_id: u32,
im_end_id: u32,
model_name: String,
}
impl<'a> MiniCPMGenerateModel<'a> {
@@ -47,6 +48,7 @@ impl<'a> MiniCPMGenerateModel<'a> {
device: device.clone(),
endoftext_id,
im_end_id,
model_name: "minicpm4".to_string(),
})
}
}
@@ -78,7 +80,7 @@ impl<'a> GenerateModel for MiniCPMGenerateModel<'a> {
}
let res = self.tokenizer.token_decode(generate)?;
self.minicpm.clear_kv_cache();
let response = build_completion_response(res, "minicpm");
let response = build_completion_response(res, &self.model_name);
Ok(response)
}
fn generate_stream(
@@ -128,7 +130,7 @@ impl<'a> GenerateModel for MiniCPMGenerateModel<'a> {
continue;
}
error_tokens.clear();
let chunk = build_completion_chunk_response(decoded_token, "minicpm", None, None);
let chunk = build_completion_chunk_response(decoded_token, &self.model_name, None, None);
yield Ok(chunk);
if next_token == self.endoftext_id || next_token == self.im_end_id {
break;
+8 -6
View File
@@ -4,7 +4,7 @@ use candle_nn::{Embedding, Linear, Module, RmsNorm, VarBuilder, embedding, rms_n
use crate::{
models::{
common::{AttentionNobias, MLPNoBias},
common::{GateUpDownMLP, NaiveAttention},
minicpm4::config::MiniCPM4Config,
},
position_embed::rope::compute_default_rope_parameters,
@@ -93,8 +93,8 @@ impl MiniCPMLongRoPE {
}
pub struct MiniCPMDecoderLayer {
self_attn: AttentionNobias,
mlp: MLPNoBias,
self_attn: NaiveAttention,
mlp: GateUpDownMLP,
input_layernorm: RmsNorm,
post_attention_layernorm: RmsNorm,
scale_depth: f32,
@@ -103,17 +103,19 @@ pub struct MiniCPMDecoderLayer {
impl MiniCPMDecoderLayer {
pub fn new(vb: VarBuilder, cfg: &MiniCPM4Config) -> Result<Self> {
let self_attn = AttentionNobias::new(
let self_attn = NaiveAttention::new(
vb.pp("self_attn"),
cfg.hidden_size,
cfg.num_attention_heads,
cfg.num_key_value_heads,
false,
)?;
let mlp = MLPNoBias::new(
let mlp = GateUpDownMLP::new(
vb.pp("mlp"),
cfg.hidden_size,
cfg.intermediate_size,
cfg.hidden_act,
false,
)?;
let input_layernorm =
rms_norm(cfg.hidden_size, cfg.rms_norm_eps, vb.pp("input_layernorm"))?;
@@ -143,7 +145,7 @@ impl MiniCPMDecoderLayer {
let xs = self.input_layernorm.forward(xs)?;
let xs = self
.self_attn
.forward(&xs, cos, sin, attention_mask, true)?;
.forward(&xs, Some(cos), Some(sin), attention_mask, true)?;
let xs = (residual
+ xs.affine(
self.scale_depth as f64 / (self.num_hidden_layers as f64).sqrt(),