okami.mom/strata

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

Niko1221Merge f-vit-exact: the picture reader follows the float32 reference closer on the CPU and on the GPU60d431e2a

main
2.0 KiB44 linesraw
1// tools/vision/own/vit_cpu.h - the fast CPU tower of the Qwen vision model (own-stack phase 3, the product CPU path; VitRef stays the float32 oracle).
2//
3// The same maths and the same contract as VitRef (vit_ref.h), geometry-driven, no ggml: the weights stay as stored (BF16 or F16, no float32 copy of the
4// matrices), the matrix products are register-tiled float32 kernels with one source per ISA tier (AVX-512, AVX2, portable; cpu_kernels.h), the attention is
5// tiled with an online softmax (no score matrix, any picture size), the work runs on a small persistent fork-join team (cpu_team.h).
6// 16-bit rounding of the matrix inputs and q, k, v (as ggml's CPU path does): OFF by default for every weight type (vit_cpu_round_kind), so the tower
7// follows the float32 VitRef; STRATA_VIT_REF_ROUND=1 turns it on (the same switch as VitRef's, where it is off by default too).
8#pragma once
9
10#include "cpu_kernels.h"
11#include "cpu_team.h"
12#include "gguf_lite.h"
13#include "image_prep.h"
14#include "vit_geometry.h"
15
16#include <memory>
17#include <string>
18#include <vector>
19
20namespace strata_vit {
21
22// The 16-bit rounding the CPU tower applies to its matrix inputs and q, k, v: 0 none, 1 BF16, 2 F16.  weight_kind is the matrices' stored type (1 BF16,
23// 2 F16, anything else: none).  Default: none; env (STRATA_VIT_REF_ROUND's value, may be null) "1" rounds to the weight's type, "0" keeps none.
24int vit_cpu_round_kind(int weight_kind, const char* env);
25
26class VitCpu {
27public:
28    VitCpu();
29    ~VitCpu();
30    // Reads the tower's weights (16-bit matrices as stored).  `g` must be supported() (the caller checks).
31    bool load(const Gguf& gg, const VitGeometry& g, std::string& err);
32
33    // img: a prepared picture.  rows: nx * ny rows of out_dim floats.  threads <= 0: half the hardware threads, at most 16.
34    bool encode(const PreparedImage& img, int threads, std::vector<float>& rows, int& nx, int& ny, std::string& err);
35
36    const VitGeometry& geometry() const;
37    const char* isa_name() const;
38
39private:
40    struct Impl;
41    std::unique_ptr<Impl> impl_;
42};
43
44}  // namespace strata_vit