clean up comments

This commit is contained in:
Xuan Son Nguyen 2026-11-25 16:59:20 +01:00
parent 3b5211fe4c
commit 80bfd75e7e
4 changed files with 8 additions and 18 deletions

View file

@ -5,7 +5,7 @@ ggml_cgraph * mtmd_graph_hy_v4::build() {
const int pw = n_patches_x;
const int ph = n_patches_y;
// position embedding: a graph input, interpolated on CPU in mtmd_subcontext::encode() (same name and interpolation as hunyuanvl)
// filled on CPU by mtmd_subcontext::encode(), same as hunyuanvl
ggml_tensor * pos_embd = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_embd, ph * pw);
ggml_set_name(pos_embd, "hunyuanvl_pos_embd");
ggml_set_input(pos_embd);
@ -13,7 +13,6 @@ ggml_cgraph * mtmd_graph_hy_v4::build() {
ggml_tensor * inp = build_inp();
ggml_tensor * cur = build_vit(inp, n_patches, NORM_TYPE_NORMAL, hparams.ffn_op, pos_embd, nullptr);
// rmsnorm mlp adapter
cur = build_norm(cur, model.mm_pre_norm_w, nullptr, NORM_TYPE_RMS, eps, -1);
// [C, W*H] -> [W, H, C] for conv2d
@ -21,7 +20,6 @@ ggml_cgraph * mtmd_graph_hy_v4::build() {
cur = ggml_permute(ctx0, cur, 2, 0, 1, 3);
cur = ggml_cont(ctx0, cur);
// conv2d(k=2, s=2) + GELU + conv2d(k=1, s=1)
cur = ggml_conv_2d(ctx0, model.mm_0_w, cur, merge, merge, 0, 0, 1, 1);
cur = ggml_add(ctx0, cur, ggml_reshape_3d(ctx0, model.mm_0_b, 1, 1, model.mm_0_b->ne[0]));
cur = ggml_gelu_erf(ctx0, cur);
@ -30,9 +28,8 @@ ggml_cgraph * mtmd_graph_hy_v4::build() {
const int ow = pw / merge;
const int oh = ph / merge;
const int idim = (int)cur->ne[2]; // OC = n_mmproj_embd
const int idim = (int)cur->ne[2];
// append newline along W (dim 0)
ggml_tensor * nl = ggml_reshape_4d(ctx0, model.image_newline, 1, 1, idim, 1);
nl = ggml_repeat_4d(ctx0, nl, 1, oh, idim, 1);
cur = ggml_concat(ctx0, cur, nl, 0);

View file

@ -1265,13 +1265,11 @@ struct mtmd_model_loader {
} break;
case PROJECTOR_TYPE_HY_V4_FLASH:
{
// the HF processor runs torchvision bicubic (it replaces lanczos) and Python round()
hparams.n_merge = 2;
hparams.set_image_resize_algo(RESIZE_ALGO_BICUBIC, true);
hparams.set_image_resize_algo(RESIZE_ALGO_BICUBIC, true); // HF processor replaces lanczos with bicubic
hparams.image_round_half_even = true;
hparams.image_resize_pad = PAD_NONE;
// the encoder fuses its ffn bias with the tanh gelu (bias_activation_fusion)
hparams.ffn_op = FFN_GELU;
hparams.ffn_op = FFN_GELU; // tanh gelu (bias_activation_fusion)
log_ffn_op = "gelu";
get_u32(KEY_SPATIAL_MERGE_SIZE, hparams.n_merge, false);
if (hparams.n_merge == 0) {
@ -1280,7 +1278,7 @@ struct mtmd_model_loader {
hparams.set_limit_image_tokens(4, 4096);
get_u32(KEY_IMAGE_MIN_PIXELS, hparams.image_min_pixels, false);
get_u32(KEY_IMAGE_MAX_PIXELS, hparams.image_max_pixels, false);
hparams.set_warmup_n_tokens(32*32); // vision_config.min_image_size
hparams.set_warmup_n_tokens(46*46); // avoid OOM on warmup
} break;
case PROJECTOR_TYPE_LFM2A:
case PROJECTOR_TYPE_D1OMNI_A:
@ -3910,14 +3908,12 @@ struct mtmd_model_loader {
} break;
case PROJECTOR_TYPE_HY_V4_FLASH:
{
// perceive.proj.0 -> mm.0 (conv1), perceive.proj.2 -> mm.2 (conv2)
model.mm_0_w = get_tensor(mtmd_format(TN_LLAVA_PROJ, 0, "weight"));
model.mm_0_b = get_tensor(mtmd_format(TN_LLAVA_PROJ, 0, "bias"));
model.mm_1_w = get_tensor(mtmd_format(TN_LLAVA_PROJ, 2, "weight"));
model.mm_1_b = get_tensor(mtmd_format(TN_LLAVA_PROJ, 2, "bias"));
model.mm_pre_norm_w = get_tensor(mtmd_format(TN_MM_PRE_NORM, "weight"));
model.image_newline = get_tensor(TN_IMAGE_NEWLINE);
// the position embedding is resampled on the host as F32
if (!model.position_embeddings || model.position_embeddings->type != GGML_TYPE_F32 || !ggml_is_matrix(model.position_embeddings) || model.position_embeddings->ne[0] != hparams.n_embd) {
throw std::runtime_error(mtmd_format("%s: %s requires a 2D F32 %s\n", __func__,
PROJECTOR_TYPE_NAMES[model.proj_type].c_str(), mtmd_format(TN_POS_EMBD, prefix).c_str()));
@ -4725,8 +4721,7 @@ int mtmd_model::n_output_tokens_x(const mtmd_image_f32 * img) const {
case PROJECTOR_TYPE_LFM3VL:
return (img->nx() / params.patch_size) / 2;
case PROJECTOR_TYPE_HY_V4_FLASH:
// the projector appends one newline embedding per token row
return (img->nx() / params.patch_size) / params.n_merge + 1;
return (img->nx() / params.patch_size) / params.n_merge + 1; // +1 for newline
case PROJECTOR_TYPE_STEP3VL:
case PROJECTOR_TYPE_MINICPMV4_6:
case PROJECTOR_TYPE_MINICPMV4_7:
@ -5039,7 +5034,6 @@ int mtmd_model::n_output_tokens(const mtmd_image_f32 * img) const {
} break;
case PROJECTOR_TYPE_HY_V4_FLASH:
{
// no begin/end embeddings, the prompt carries the image start/end tokens
int merge = hparams.n_merge;
int ow = (img->nx() / patch_size) / merge;
int oh = (img->ny() / patch_size) / merge;
@ -5293,7 +5287,7 @@ int mtmd_model::n_mmproj_embd() const {
case PROJECTOR_TYPE_HUNYUANVL:
return mm_model_proj->ne[1];
case PROJECTOR_TYPE_HY_V4_FLASH:
return mm_1_w->ne[3]; // conv2d kernel is [1, 1, IC, OC]
return mm_1_w->ne[3];
case PROJECTOR_TYPE_COGVLM:
return mm_4h_to_h_w->ne[1];
case PROJECTOR_TYPE_DEEPSEEKOCR:

View file

@ -2113,7 +2113,7 @@ bool mtmd_subcontext::encode(mtmd_subctx_encode_params * params) {
case PROJECTOR_TYPE_HUNYUANVL:
case PROJECTOR_TYPE_HY_V4_FLASH:
{
// Compute the 2D position embedding on CPU (with the custom sf=(target+0.1)/n_grid bilinear sampling that the reference implementation uses) and upload it to the graph input declared in mtmd_graph_hunyuanvl::build() / mtmd_graph_hy_v4::build().
// resize position embedding on CPU, using bilinear with sf=(target+0.1)/n_grid
GGML_ASSERT(model.position_embeddings != nullptr);
ggml_tensor * src_t = model.position_embeddings;
GGML_ASSERT(ggml_is_matrix(src_t) && src_t->type == GGML_TYPE_F32);

View file

@ -1397,7 +1397,6 @@ struct mtmd_context {
if (text_model && pos_type != MTMD_POS_TYPE_MROPE) {
throw std::runtime_error("the hy_v4_flash projector needs a text model with M-RoPE rope sections");
}
// image start/end markers use fullwidth bar and U+2581 like hunyuanvl
img_beg = "<|hy_image▁start:opensource|>";
img_end = "<|hy_image▁end:opensource|>";
image_preproc = std::make_unique<mtmd_image_preprocessor_dyn_size>(models.v.get());