okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
8ac8ce32e
main
1// Copyright (c) 2026 Peasant Smith and the Project Maya contributors (MIT; https://github.com/mw00/project-maya, v1.1.17). 2// Ported into Strata (third_party/project-maya/NOTICE.md, LICENSE); changes are in the git history of this file. 3// src/ds4/ds4_vision.cuh - DeepSeek-V4.1's vision tower on one GPU (DeepSeek's inference/vision.py): 4// patches (14 x 14 RGB, scaled to [-1, 1]) -> Linear(588 -> 1024) 5// 32 blocks: x += attn(rmsnorm(x)) (16 heads of 64, full bidirectional attention over the image, a 2D rotary on q 6// and k: the first half of each head's frequencies the patch row, the second its column); 7// x += w2(silu(gate) * up) with gate, up = w1(rmsnorm(x)) 8// rmsnorm, then the aligner: each 3 x 3 block of patches (zero-padded at the right and bottom) as one 9216 row, 9// Linear(9216 -> 5120), GELU, Linear(5120 -> 5120): one LLM embedding row per block, in reading order. 10// The BF16 weights live in pinned host memory and stream to the GPU a block at a time (two slots, the next block's 11// copy under this one's work), so the tower costs the model ~350 MB of the GPU's memory, not ~1 GB; the GEMMs are 12// cuBLAS (BF16 in, FP32 accumulation), the residual stream FP32. 13#pragma once 14 15#include <cstddef> 16#include <cstdint> 17#include <functional> 18#include <string> 19#include <vector> 20 21namespace strata::ds4 { 22 23struct VisionGeo { 24 int layers = 0, dim = 1024, heads = 16, inter = 2816, patch = 14, merge = 3, out = 5120; 25 float theta = 10000.0f, eps = 1e-6f; 26 int max_tokens = 1024; // an image's span in the prompt at most: n_h * (n_w + 1) + 2 tokens 27}; 28 29class VisionTower { 30public: 31 /// read(name, shape, dst): the tensor's shape, and (dst not null) its BF16 data into dst; false: not there 32 using Reader = std::function<bool(const std::string& name, std::vector<int64_t>& shape, void* dst)>; 33 VisionTower() = default; 34 VisionTower(const VisionTower&) = delete; 35 VisionTower& operator=(const VisionTower&) = delete; 36 ~VisionTower(); 37 bool load(const VisionGeo& g, int device, const Reader& read, std::string& err); 38 /// Frees everything load took (a load that failed half way too); loaded() is false after it. 39 void release(); 40 bool loaded() const { return dev_ >= 0; } 41 int device() const { return dev_; } 42 size_t device_bytes() const { return dev_bytes_; } 43 /// The patches an image may have (its aligner grid within max_tokens). 44 int max_patches() const { return max_n_; } 45 /// rgb: height x width x 3 bytes, both sides multiples of the patch size -> the aligner's rows, BF16 46 /// [n_h * n_w][out] in reading order (n_h x n_w: the patch grid over 3, rounded up). 47 bool encode(const uint8_t* rgb, int width, int height, std::vector<uint16_t>& rows, int& n_h, int& n_w, 48 std::string& err); 49 50private: 51 struct Piece { // a weight the encode streams in: host bits, its size 52 const uint16_t* host = nullptr; 53 size_t n = 0; 54 }; 55 VisionGeo g_; 56 int dev_ = -1, at_ = -1, max_n_ = 0, qb_ = 0, rb_ = 0; // (at_: the device its memory is on) 57 size_t dev_bytes_ = 0, slot_n_ = 0; 58 std::vector<void*> dallocs_; 59 uint16_t* host_ = nullptr; // pinned: every block's [wqkv, wo, w1, w2], then aligner w1, w2 60 size_t block_n_ = 0; // a block's weights (BF16 values) 61 size_t al1_off_ = 0, al2_off_ = 0; // the aligner's weights in host_ 62 // resident (small): the patch embedding, the norms and biases 63 uint16_t *pe_w_ = nullptr, *pe_b_ = nullptr, *norm_ = nullptr, *al1_b_ = nullptr, *al2_b_ = nullptr; 64 std::vector<uint16_t*> n1_, n2_, bqkv_, bo_; 65 // work buffers (max_n_ patches) 66 uint8_t* rgb_ = nullptr; 67 uint16_t *patch_ = nullptr, *xn_ = nullptr, *qkv_ = nullptr, *o_ = nullptr, *mid_ = nullptr, *p_ = nullptr, 68 *un_ = nullptr, *a16_ = nullptr, *outb_ = nullptr; 69 float *x_ = nullptr, *y_ = nullptr, *s_ = nullptr, *inv_freq_ = nullptr; 70 uint16_t* slot_[2] = {nullptr, nullptr}; 71 void* blas_ = nullptr; // cublasHandle_t 72 uint8_t* ws_ = nullptr; 73 void *st_ = nullptr, *cs_ = nullptr; // cudaStream_t: the work, the weight copies 74 void *copied_[2] = {nullptr, nullptr}, *used_[2] = {nullptr, nullptr}; // cudaEvent_t per slot 75 template <class T> 76 bool dalloc(T*& p, size_t n, std::string& err); 77}; 78 79} // namespace strata::ds4