CoolFace
Datasetpublic

echodict/llama.cpp

version https://git-lfs.github.com/spec/v1 oid sha256:cfc44b7ba25614df70e6b65e3341cae0310163bd32fd31a6b928a542df433faf size 30786

sourceHugging Faceupdated 5mo agoView on Hugging Face
0likes773downloads
llama4.cpp97 linesDownload Raw Back to models
1#include "models.h"2 3ggml_cgraph * clip_graph_llama4::build() {4    GGML_ASSERT(model.class_embedding != nullptr);5    GGML_ASSERT(model.position_embeddings != nullptr);6 7    const int n_pos = n_patches + 1; // +1 for [CLS]8 9    // 2D input positions10    ggml_tensor * pos_h = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_pos);11    ggml_set_name(pos_h, "pos_h");12    ggml_set_input(pos_h);13 14    ggml_tensor * pos_w = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_pos);15    ggml_set_name(pos_w, "pos_w");16    ggml_set_input(pos_w);17 18    ggml_tensor * inp = build_inp_raw();19 20    // Llama4UnfoldConvolution21    {22        ggml_tensor * kernel = ggml_reshape_4d(ctx0, model.patch_embeddings_0,23                                                patch_size, patch_size, 3, n_embd);24        inp = ggml_im2col(ctx0, kernel, inp, patch_size, patch_size, 0, 0, 1, 1, true, inp->type);25        inp = build_mm(model.patch_embeddings_0, inp);26        inp = ggml_reshape_2d(ctx0, inp, n_embd, n_patches);27        cb(inp, "patch_conv", -1);28    }29 30    // add CLS token31    inp = ggml_concat(ctx0, inp, model.class_embedding, 1);32 33    // build ViT with 2D position embeddings34    auto add_pos = [&](ggml_tensor * cur, const clip_layer &) {35        // first half is X axis and second half is Y axis36        // ref: https://github.com/huggingface/transformers/blob/40a493c7ed4f19f08eadb0639cf26d49bfa5e180/src/transformers/models/llama4/modeling_llama4.py#L131237        // ref: https://github.com/Blaizzy/mlx-vlm/blob/a57156aa87b33cca6e5ee6cfc14dd4ef8f611be6/mlx_vlm/models/llama4/vision.py#L44138        return build_rope_2d(ctx0, cur, pos_w, pos_h, hparams.rope_theta, false);39    };40    ggml_tensor * cur = build_vit(41                            inp, n_pos,42                            NORM_TYPE_NORMAL,43                            hparams.ffn_op,44                            model.position_embeddings,45                            add_pos);46 47    // remove CLS token48    cur = ggml_view_2d(ctx0, cur,49        n_embd, n_patches,50        ggml_row_size(cur->type, n_embd), 0);51 52    // pixel shuffle53    // based on Llama4VisionPixelShuffleMLP54    // https://github.com/huggingface/transformers/blob/2932f318a20d9e54cc7aea052e040164d85de7d6/src/transformers/models/llama4/modeling_llama4.py#L115155    {56        const int scale_factor = model.hparams.n_merge;57        const int bsz = 1; // batch size, always 1 for now since we don't support batching58        GGML_ASSERT(scale_factor > 0);59        GGML_ASSERT(n_patches_x == n_patches_y); // llama4 only supports square images60        cur = ggml_reshape_4d(ctx0, cur,61            n_embd * scale_factor,62            n_patches_x / scale_factor,63            n_patches_y,64            bsz);65        cur = ggml_permute(ctx0, cur, 0, 2, 1, 3);66        cur = ggml_cont_4d(ctx0, cur,67            n_embd * scale_factor * scale_factor,68            n_patches_x / scale_factor,69            n_patches_y / scale_factor,70            bsz);71        //cur = ggml_permute(ctx0, cur, 0, 2, 1, 3);72        // flatten to 2D73        cur = ggml_cont_2d(ctx0, cur,74            n_embd * scale_factor * scale_factor,75            n_patches / scale_factor / scale_factor);76        cb(cur, "pixel_shuffle", -1);77    }78 79    // based on Llama4VisionMLP2 (always uses GELU activation, no bias)80    {81        cur = build_mm(model.mm_model_mlp_1_w, cur);82        cur = ggml_gelu(ctx0, cur);83        cur = build_mm(model.mm_model_mlp_2_w, cur);84        cur = ggml_gelu(ctx0, cur);85        cb(cur, "adapter_mlp", -1);86    }87 88    // Llama4MultiModalProjector89    cur = build_mm(model.mm_model_proj, cur);90    cb(cur, "projected", -1);91 92    // build the graph93    ggml_build_forward_expand(gf, cur);94 95    return gf;96}97