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.9 KiB97 linesraw
1// tools/vision/own/gguf_lite.h - a small GGUF reader for Strata's own vision tower (no ggml, no llama.cpp).
2//
3// Reads the header (key/value pairs, tensor directory) of a GGUF v2/v3 file and the data of single tensors, converted to float32:
4// F32, F16, BF16 and Q8_0 are handled (what an mmproj file comes in); any other tensor type is an error that names it.
5// Shapes are kept as the file stores them (ne[0] is the fastest dimension: a Linear weight of "[in, out]" is `out` rows of `in`
6// contiguous values, y = W x).
7#pragma once
8
9#include <cstdint>
10#include <cstdio>
11#include <cstring>
12#include <map>
13#include <string>
14#include <vector>
15
16namespace strata_vit {
17
18enum GgufValueType : uint32_t {
19    GV_UINT8 = 0, GV_INT8 = 1, GV_UINT16 = 2, GV_INT16 = 3, GV_UINT32 = 4, GV_INT32 = 5, GV_FLOAT32 = 6, GV_BOOL = 7,
20    GV_STRING = 8, GV_ARRAY = 9, GV_UINT64 = 10, GV_INT64 = 11, GV_FLOAT64 = 12,
21};
22
23// ggml's tensor type ids (the ones this reader knows)
24enum GgufTensorType : uint32_t { GT_F32 = 0, GT_F16 = 1, GT_Q8_0 = 8, GT_BF16 = 30 };
25
26struct GgufTensorInfo {
27    std::string name;
28    std::vector<int64_t> ne;   // as stored: ne[0] fastest
29    uint32_t type = 0;
30    uint64_t offset = 0;       // from the start of the data section
31    uint64_t nbytes = 0;       // 0 when the type is not one this reader handles
32    int64_t nelem() const {
33        int64_t n = 1;
34        for (int64_t d : ne) n *= d;
35        return n;
36    }
37};
38
39class Gguf {
40public:
41    Gguf() = default;
42    Gguf(const Gguf&) = delete;
43    Gguf& operator=(const Gguf&) = delete;
44    ~Gguf();
45
46    // Reads the header and the tensor directory (never the tensor data).  False and `err` when the file is not a GGUF this reader handles.
47    bool open(const std::string& path, std::string& err);
48
49    const std::string& path() const { return path_; }
50    uint64_t file_size() const { return file_size_; }
51
52    bool has(const std::string& key) const { return kv_.count(key) != 0; }
53    bool get_u32(const std::string& key, uint32_t& v) const;       // any integer type holding a value in 0 .. 2^32-1 (bool counts)
54    bool get_f32(const std::string& key, float& v) const;          // a float or an integer
55    bool get_bool(const std::string& key, bool& v) const;          // a bool or an integer
56    bool get_str(const std::string& key, std::string& v) const;
57    bool get_f32_array(const std::string& key, std::vector<float>& v) const;     // an array of floats or integers
58    bool get_int_array(const std::string& key, std::vector<int64_t>& v) const;   // an array of integers or bools
59
60    const GgufTensorInfo* tensor(const std::string& name) const;
61    const std::vector<GgufTensorInfo>& tensors() const { return tensors_; }
62
63    // the tensor's values as float32 (t.nelem() of them); false and `err` on a type this reader does not handle or a short file
64    bool read_f32(const GgufTensorInfo& t, float* dst, std::string& err) const;
65    // the tensor's bytes as stored (t.nbytes of them)
66    bool read_raw(const GgufTensorInfo& t, void* dst, std::string& err) const;
67
68private:
69    struct Value {
70        uint32_t type = 0, elem = 0;       // elem: the array's element type
71        int64_t i = 0;
72        double d = 0;
73        std::string s;
74        std::vector<int64_t> ai;
75        std::vector<double> ad;
76        std::vector<std::string> as;
77        uint64_t n = 0;                    // array length
78    };
79    bool read_at(uint64_t pos, void* dst, uint64_t n, std::string& err) const;
80    std::string path_;
81    uint64_t file_size_ = 0, data_start_ = 0;
82    std::map<std::string, Value> kv_;
83    std::vector<GgufTensorInfo> tensors_;
84    std::map<std::string, size_t> by_name_;
85};
86
87// half and bfloat16 to float, one value
88float gguf_f16_to_f32(uint16_t h);
89inline float gguf_bf16_to_f32(uint16_t b) {
90    const uint32_t u = (uint32_t) b << 16;
91    float f;
92    static_assert(sizeof f == sizeof u, "float is 32 bits");
93    std::memcpy(&f, &u, sizeof f);
94    return f;
95}
96
97}  // namespace strata_vit