okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-3v (aec86299d) into f-own-int: our own Qwen vision tower (mtmd stays the default)0479b72e9

main
7.4 KiB124 linesraw
1// tools/vision/own/vit_geometry.cpp - see vit_geometry.h
2#include "vit_geometry.h"
3
4#include <algorithm>
5#include <cmath>
6
7namespace strata_vit {
8
9namespace {
10
11std::string blk(int il, const char* what, const char* kind) {
12    return "v.blk." + std::to_string(il) + "." + what + "." + kind;
13}
14
15bool need_u32(const Gguf& gg, const char* key, int& out, std::string& err) {
16    uint32_t v = 0;
17    if (!gg.get_u32(key, v) || v == 0 || v > (1u << 24)) { err = std::string("the vision projector has no usable ") + key; return false; }
18    out = (int) v;
19    return true;
20}
21
22// shape check: the tensor exists and has exactly these dimensions (as stored, fastest first)
23bool shape_is(const Gguf& gg, const std::string& name, std::initializer_list<int64_t> ne, std::string& err) {
24    const GgufTensorInfo* t = gg.tensor(name);
25    if (!t) { err = "the vision projector lacks the tensor " + name; return false; }
26    if (t->ne.size() != ne.size() || !std::equal(ne.begin(), ne.end(), t->ne.begin())) {
27        std::string have;
28        for (int64_t d : t->ne) have += (have.empty() ? "" : " x ") + std::to_string(d);
29        err = "tensor " + name + " has shape [" + have + "], not what this tower's geometry needs";
30        return false;
31    }
32    return true;
33}
34
35}  // namespace
36
37bool vit_geometry_from_gguf(const Gguf& gg, VitGeometry& g, std::string& err) {
38    g = VitGeometry{};
39    std::string arch;
40    if (!gg.get_str("general.architecture", arch) || arch != "clip") { err = "not an mmproj file (general.architecture is not clip)"; return false; }
41    bool has_v = false;
42    if (!gg.get_bool("clip.has_vision_encoder", has_v) || !has_v) { err = "the mmproj file has no vision encoder"; return false; }
43    if (!gg.get_str("clip.projector_type", g.projector) || g.projector.empty()) { err = "the mmproj file names no projector type"; return false; }
44    if (!need_u32(gg, "clip.vision.block_count", g.layers, err) || !need_u32(gg, "clip.vision.embedding_length", g.width, err) ||
45        !need_u32(gg, "clip.vision.attention.head_count", g.heads, err) || !need_u32(gg, "clip.vision.feed_forward_length", g.ffn, err) ||
46        !need_u32(gg, "clip.vision.patch_size", g.patch, err) || !need_u32(gg, "clip.vision.projection_dim", g.out_dim, err))
47        return false;
48    if (g.width % g.heads != 0) { err = "the vision projector's width is not a multiple of its head count"; return false; }
49    g.head_dim = g.width / g.heads;
50    uint32_t u = 0;
51    if (gg.get_u32("clip.vision.image_size", u)) g.image_size = (int) u;
52    if (gg.get_u32("clip.vision.spatial_merge_size", u) && u > 0 && u <= 16) g.merge = (int) u;
53    gg.get_f32("clip.vision.attention.layer_norm_epsilon", g.eps);
54    std::vector<float> v3;
55    if (gg.get_f32_array("clip.vision.image_mean", v3) && v3.size() == 3) for (int i = 0; i < 3; ++i) g.mean[i] = v3[i];
56    if (gg.get_f32_array("clip.vision.image_std", v3) && v3.size() == 3) for (int i = 0; i < 3; ++i) g.stdv[i] = v3[i];
57    if (gg.get_u32("clip.vision.n_wa_pattern", u)) g.window_pattern = (int) u;
58
59    bool use_gelu = false, use_silu = false;
60    gg.get_bool("clip.use_gelu", use_gelu);
61    gg.get_bool("clip.use_silu", use_silu);
62    g.act = use_gelu ? ActKind::GeluTanh : use_silu ? ActKind::Silu : ActKind::GeluQuick;   // llama.cpp's own default when neither is set
63    g.norm = g.projector == "qwen2.5vl_merger" ? NormKind::RMSNorm : NormKind::LayerNorm;
64
65    // the layer layout, from which tensors exist
66    g.fused_qkv = gg.tensor(blk(0, "attn_qkv", "weight")) != nullptr;
67    g.gated_ffn = gg.tensor(blk(0, "ffn_gate", "weight")) != nullptr;
68    g.patch_taps = gg.tensor("v.patch_embd.weight.1") != nullptr ? 2 : 1;
69    g.pre_ln = gg.tensor("v.pre_ln.weight") != nullptr;
70    std::vector<int64_t> ds;
71    if (gg.get_int_array("clip.vision.is_deepstack_layers", ds))
72        for (size_t i = 0; i < ds.size(); ++i) if (ds[i]) g.deepstack.push_back((int) i);
73    for (int il = 0; il < g.layers; ++il) {
74        if (gg.tensor("v.deepstack." + std::to_string(il) + ".fc1.weight") && std::find(g.deepstack.begin(), g.deepstack.end(), il) == g.deepstack.end())
75            g.deepstack.push_back(il);
76    }
77
78    const int64_t W = g.width, F = g.ffn;
79    if (!shape_is(gg, "v.patch_embd.weight", {g.patch, g.patch, 3, W}, err) || !shape_is(gg, "v.patch_embd.bias", {W}, err)) return false;
80    if (g.patch_taps == 2 && !shape_is(gg, "v.patch_embd.weight.1", {g.patch, g.patch, 3, W}, err)) return false;
81    const GgufTensorInfo* pos = gg.tensor("v.position_embd.weight");
82    if (!pos || pos->ne.size() != 2 || pos->ne[0] != W) { err = "the vision projector's position table is missing or not [width, rows]"; return false; }
83    g.pos_grid = (int) std::lround(std::sqrt((double) pos->ne[1]));
84    if ((int64_t) g.pos_grid * g.pos_grid != pos->ne[1]) { err = "the position table's rows are not a square grid"; return false; }
85    for (int il = 0; il < g.layers; ++il) {
86        for (const char* n : {"ln1", "ln2"})
87            if (!shape_is(gg, blk(il, n, "weight"), {W}, err) || !shape_is(gg, blk(il, n, "bias"), {W}, err)) return false;
88        if (g.fused_qkv) {
89            if (!shape_is(gg, blk(il, "attn_qkv", "weight"), {W, 3 * W}, err) || !shape_is(gg, blk(il, "attn_qkv", "bias"), {3 * W}, err)) return false;
90        } else {
91            for (const char* n : {"attn_q", "attn_k", "attn_v"})
92                if (!shape_is(gg, blk(il, n, "weight"), {W, W}, err)) return false;
93        }
94        if (!shape_is(gg, blk(il, "attn_out", "weight"), {W, W}, err) || !shape_is(gg, blk(il, "attn_out", "bias"), {W}, err)) return false;
95        if (!shape_is(gg, blk(il, "ffn_up", "weight"), {W, F}, err) || !shape_is(gg, blk(il, "ffn_up", "bias"), {F}, err)) return false;
96        if (!shape_is(gg, blk(il, "ffn_down", "weight"), {F, W}, err) || !shape_is(gg, blk(il, "ffn_down", "bias"), {W}, err)) return false;
97        if (g.gated_ffn && !shape_is(gg, blk(il, "ffn_gate", "weight"), {W, F}, err)) return false;
98    }
99    if (!shape_is(gg, "v.post_ln.weight", {W}, err) || !shape_is(gg, "v.post_ln.bias", {W}, err)) return false;
100    g.merger_in = (int) (W * g.merge * g.merge);
101    const GgufTensorInfo* m0 = gg.tensor("mm.0.weight");
102    if (!m0 || m0->ne.size() != 2 || m0->ne[0] != g.merger_in) { err = "the vision projector's mm.0.weight is missing or not [width * merge^2, hidden]"; return false; }
103    g.merger_hidden = (int) m0->ne[1];
104    if (!shape_is(gg, "mm.0.bias", {g.merger_hidden}, err) || !shape_is(gg, "mm.2.weight", {g.merger_hidden, g.out_dim}, err) ||
105        !shape_is(gg, "mm.2.bias", {g.out_dim}, err))
106        return false;
107    return true;
108}
109
110bool vit_geometry_supported(const VitGeometry& g, std::string& why) {
111    if (g.projector != "qwen3vl_merger") { why = "projector type " + g.projector + " is not one of this tower's families"; return false; }
112    if (g.norm != NormKind::LayerNorm) { why = "RMS norm"; return false; }
113    if (g.act != ActKind::GeluTanh) { why = "an activation other than the tanh GELU"; return false; }
114    if (g.gated_ffn) { why = "a gated FFN"; return false; }
115    if (!g.fused_qkv) { why = "separate q/k/v projections"; return false; }
116    if (g.window_pattern != 0) { why = "window attention"; return false; }
117    if (g.pre_ln) { why = "a norm before the first layer"; return false; }
118    if (!g.deepstack.empty()) { why = "deepstack outputs"; return false; }
119    if (g.head_dim % 4 != 0) { why = "a head size that is not a multiple of 4 (the 2D rotary splits it in 4)"; return false; }
120    if (g.merge != 2) { why = "a merge size other than 2"; return false; }
121    return true;
122}
123
124}  // namespace strata_vit