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
3.6 KiB60 linesraw
1// tools/vision/own/vit_geometry.h - the shape of a vision tower, read from its mmproj GGUF (own-stack phase 3).
2//
3// Nothing about a Qwen tower is a constant in the code: layers, widths, heads, patch and merge sizes, the position grid, the output
4// width, the norm, the activation and the image limits all come from the file.  What the code does know is the list of *operations* a
5// tower may be made of (see supported()): today the Qwen3-VL family ("qwen3vl_merger": LayerNorm, fused QKV, 2D rotary, global
6// attention, a plain tanh-GELU MLP, a 2x2 merger).  A tower of that family with other numbers is a new file, not new code.
7#pragma once
8
9#include "gguf_lite.h"
10
11#include <string>
12#include <vector>
13
14namespace strata_vit {
15
16enum class NormKind { LayerNorm, RMSNorm };
17enum class ActKind { GeluTanh, GeluErf, Silu, GeluQuick };
18
19struct VitGeometry {
20    std::string projector;            // clip.projector_type
21    int layers = 0;                   // clip.vision.block_count
22    int width = 0;                    // clip.vision.embedding_length
23    int heads = 0;                    // clip.vision.attention.head_count
24    int head_dim = 0;                 // width / heads
25    int ffn = 0;                      // clip.vision.feed_forward_length
26    int patch = 0;                    // clip.vision.patch_size
27    int merge = 2;                    // clip.vision.spatial_merge_size
28    int image_size = 0;               // clip.vision.image_size (the trained side: the position grid is image_size / patch squared)
29    int pos_grid = 0;                 // the side of the position table (sqrt of its rows)
30    int out_dim = 0;                  // clip.vision.projection_dim: the width of an output row
31    float eps = 1e-6f;                // clip.vision.attention.layer_norm_epsilon
32    NormKind norm = NormKind::LayerNorm;
33    ActKind act = ActKind::GeluTanh;
34    bool gated_ffn = false;           // an ffn_gate tensor
35    bool fused_qkv = true;            // attn_qkv (else attn_q / attn_k / attn_v)
36    int patch_taps = 2;               // v.patch_embd.weight and .1 (the two temporal taps of the 3D patch embed, summed for a still image)
37    bool pre_ln = false;              // a v.pre_ln.* norm before the first layer
38    int window_pattern = 0;           // clip.vision.n_wa_pattern (0: every layer attends globally)
39    std::vector<int> deepstack;       // layer indices that feed an extra output (clip.vision.is_deepstack_layers)
40    float rope_theta = 10000.0f;
41    float mean[3] = {0.5f, 0.5f, 0.5f};
42    float stdv[3] = {0.5f, 0.5f, 0.5f};
43    int min_tokens = 8;               // the image's token limits (Qwen's preprocessor_config; --min-tokens / --max-tokens override)
44    int max_tokens = 4096;
45    int merger_in = 0;                // width * merge^2
46    int merger_hidden = 0;            // mm.0's output width
47
48    // tokens a patch grid gives: (w / merge) * (h / merge) for a w x h patch grid
49    int align() const { return patch * merge; }   // image sides are multiples of this
50};
51
52// Fills `g` from the file's keys and tensor shapes.  False and `err` when the file is not a vision projector this reader understands at all
53// (no projector type, a missing key, a tensor of the wrong shape).  A tower that is well formed but uses an operation this code does not
54// have is NOT an error here: from_gguf succeeds and supported() says why not.
55bool vit_geometry_from_gguf(const Gguf& gg, VitGeometry& g, std::string& err);
56
57// True when the operations this geometry needs are all implemented; otherwise `why` says which one is not.
58bool vit_geometry_supported(const VitGeometry& g, std::string& why);
59
60}  // namespace strata_vit