okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0479b72e9
main
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 { 20std ::string projector ;// clip.projector_type 21int layers = 0 ;// clip.vision.block_count 22int width = 0 ;// clip.vision.embedding_length 23int heads = 0 ;// clip.vision.attention.head_count 24int head_dim = 0 ;// width / heads 25int ffn = 0 ;// clip.vision.feed_forward_length 26int patch = 0 ;// clip.vision.patch_size 27int merge = 2 ;// clip.vision.spatial_merge_size 28int image_size = 0 ;// clip.vision.image_size (the trained side: the position grid is image_size / patch squared) 29int pos_grid = 0 ;// the side of the position table (sqrt of its rows) 30int out_dim = 0 ;// clip.vision.projection_dim: the width of an output row 31float eps = 1e-6f ;// clip.vision.attention.layer_norm_epsilon 32NormKind norm = NormKind ::LayerNorm ; 33ActKind act = ActKind ::GeluTanh ; 34bool gated_ffn = false;// an ffn_gate tensor 35bool fused_qkv = true;// attn_qkv (else attn_q / attn_k / attn_v) 36int patch_taps = 2 ;// v.patch_embd.weight and .1 (the two temporal taps of the 3D patch embed, summed for a still image) 37bool pre_ln = false;// a v.pre_ln.* norm before the first layer 38int window_pattern = 0 ;// clip.vision.n_wa_pattern (0: every layer attends globally) 39std ::vector < int > deepstack ;// layer indices that feed an extra output (clip.vision.is_deepstack_layers) 40float rope_theta = 10000.0f ; 41float mean [3 ]= {0.5f ,0.5f ,0.5f }; 42float stdv [3 ]= {0.5f ,0.5f ,0.5f }; 43int min_tokens = 8 ;// the image's token limits (Qwen's preprocessor_config; --min-tokens / --max-tokens override) 44int max_tokens = 4096 ; 45int merger_in = 0 ;// width * merge^2 46int 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 49int 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