MCPcopy Create free account
hub / github.com/appdevforall/CodeOnTheGo / build

Method build

subprojects/llama.cpp/tools/mtmd/models/llava.cpp:5–374  ·  view source on GitHub ↗

this graph is used by llava, granite and glm due to having embedding_stack (used by granite), we cannot reuse build_vit

Source from the content-addressed store, hash-verified

3// this graph is used by llava, granite and glm
4// due to having embedding_stack (used by granite), we cannot reuse build_vit
5ggml_cgraph * clip_graph_llava::build() {
6 const int batch_size = 1;
7 const int n_pos = n_patches + (model.class_embedding ? 1 : 0);
8
9 GGML_ASSERT(n_patches_x == n_patches_y && "only square images supported");
10
11 // Calculate the deepest feature layer based on hparams and projector type
12 int max_feature_layer = n_layer;
13 {
14 // Get the index of the second to last layer; this is the default for models that have a llava projector
15 int il_last = hparams.n_layer - 1;
16 int deepest_feature_layer = -1;
17
18 if (proj_type == PROJECTOR_TYPE_MINICPMV || proj_type == PROJECTOR_TYPE_GLM_EDGE) {
19 il_last += 1;
20 }
21
22 // If we set explicit vision feature layers, only go up to the deepest one
23 // NOTE: only used by granite-vision models for now
24 for (const auto & feature_layer : hparams.vision_feature_layer) {
25 if (feature_layer > deepest_feature_layer) {
26 deepest_feature_layer = feature_layer;
27 }
28 }
29 max_feature_layer = deepest_feature_layer < 0 ? il_last : deepest_feature_layer;
30 }
31
32 ggml_tensor * inp = build_inp();
33
34 // concat class_embeddings and patch_embeddings
35 if (model.class_embedding) {
36 inp = ggml_concat(ctx0, inp, model.class_embedding, 1);
37 }
38
39 ggml_tensor * positions = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_pos);
40 ggml_set_name(positions, "positions");
41 ggml_set_input(positions);
42
43 inp = ggml_add(ctx0, inp, ggml_get_rows(ctx0, model.position_embeddings, positions));
44
45 ggml_tensor * inpL = inp;
46
47 // pre-layernorm
48 if (model.pre_ln_w) {
49 inpL = build_norm(inpL, model.pre_ln_w, model.pre_ln_b, NORM_TYPE_NORMAL, eps, -1);
50 cb(inpL, "pre_ln", -1);
51 }
52
53 std::vector<ggml_tensor *> embedding_stack;
54 const auto & vision_feature_layer = hparams.vision_feature_layer;
55
56 // loop over layers
57 for (int il = 0; il < max_feature_layer; il++) {
58 auto & layer = model.layers[il];
59 ggml_tensor * cur = inpL; // inpL = residual, cur = hidden_states
60
61 // If this is an embedding feature layer, save the output.
62 // NOTE: 0 index here refers to the input to the encoder.

Callers

nothing calls this directly

Calls 15

ggml_concatFunction · 0.85
ggml_new_tensor_1dFunction · 0.85
ggml_set_nameFunction · 0.85
ggml_set_inputFunction · 0.85
ggml_addFunction · 0.85
ggml_get_rowsFunction · 0.85
ggml_mul_matFunction · 0.85
ggml_reshape_3dFunction · 0.85
ggml_reshape_2dFunction · 0.85
ggml_geluFunction · 0.85
ggml_normFunction · 0.85
ggml_mulFunction · 0.85

Tested by

no test coverage detected