char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Xuan-Son Nguyenmtmd: support dots.ocr (#17575)501aeed18

char/aliceai-t5-moe
1.7 KiB49 linesraw
1#include "models.h"
2
3ggml_cgraph * clip_graph_dotsocr::build() {
4    const int n_pos            = n_patches;
5    const int num_position_ids = n_pos * 4; // m-rope requires 4 dim per position
6
7    // note: similar to PaddleOCR
8    int mrope_sections[4] = {d_head/4, d_head/4, d_head/4, d_head/4};
9
10    ggml_tensor * positions = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, num_position_ids);
11    ggml_set_name(positions, "positions");
12    ggml_set_input(positions);
13
14    auto add_pos = [&](ggml_tensor * cur, const clip_layer &) {
15        return ggml_rope_multi(
16                    ctx0, cur, positions, nullptr,
17                    d_head/2, mrope_sections, GGML_ROPE_TYPE_VISION,
18                    32768, 10000, 1, 0, 1, 32, 1);
19    };
20
21    ggml_tensor * inp = build_inp();
22    ggml_tensor * cur = build_vit(
23                            inp, n_patches,
24                            NORM_TYPE_RMS,
25                            hparams.ffn_op,
26                            nullptr,
27                            add_pos);
28
29    cb(cur, "vit_out", -1);
30
31    // dots.ocr patch merger + projector
32    {
33        GGML_ASSERT(hparams.n_merge > 0);
34        cur = build_norm(cur, model.mm_input_norm_w, model.mm_input_norm_b, NORM_TYPE_NORMAL, 1e-6, -1);
35        cur = build_patch_merge_permute(cur, hparams.n_merge);
36        cb(cur, "after_patch_merger", -1);
37        cur = build_ffn(cur,
38            model.mm_0_w, model.mm_0_b,
39            nullptr, nullptr, // no gate
40            model.mm_2_w, model.mm_2_b,
41            FFN_GELU_ERF, -1); // nn.GELU() defaults to exact erf-based GELU
42        cb(cur, "after_projector", -1);
43    }
44
45    // build the graph
46    ggml_build_forward_expand(gf, cur);
47
48    return gf;
49}