okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
747642c2c
main
1"""Which picture reader an engine folder gets: the GPU one (`strata-vision-hip` on AMD, `strata-vision-sycl` on Intel Arc, `strata-vision --gpu` 2on a Mac) or the CPU one (`strata-vision`). 3 4An AMD engine package carries two programs for pictures. `strata-vision` is the CPU reader: it needs nothing from the GPU 5runtime, so it starts on every PC. `strata-vision-hip` is the same reader on the card (llama.cpp's HIP backend), a 6fraction of a second per picture instead of seconds. Setup writes the run config with the GPU reader first and the CPU 7reader as its fallback (`vision.cpu_exe`); the server (serve/server.py, Vision) starts the CPU reader by itself, with one 8log line, when the GPU reader does not start: the HIP runtime does not load, the card is not one the build has code for 9(the reader says so before it runs a kernel), or the card is busy. Nothing is asked of the user. 10 11A Mac has one program for both: `strata-vision` is built with Metal, `--gpu` reads on the Mac's GPU and without it on the CPU, so the 12CPU reader is the same file and the fallback needs no second program. Unified memory changes the 24 GB rule: nothing is taken from a 13separate card, the reader's ~1.1 GB is the mmproj file itself on either side (measured on an M4, footprint 1,050 MB on the CPU, 1,179 MB 14on the GPU), and the CPU reader takes ~50 s for a 260-token picture where the GPU one takes 0.45 s (ggml has no BF16 vector kernel on 15ARM). So a Mac reads on the GPU unless the person asks for the CPU. 16 17An Intel Arc engine folder (engine-sycl/) carries the CPU reader; `strata-vision-sycl` (llama.cpp's SYCL backend, tools/vision with 18STRATA_VISION_SYCL, built by `setup --backend sycl --vision gpu` on a PC that has the oneAPI compiler) is the GPU reader there. 19 20`choose` is the whole decision; setup.py calls it, and tools/test_vision_reader.py checks every branch. 21""" 22from __future__import annotations 23 24import re 25from dataclasses import dataclass 26from pathlib import Path 27 28CPU_READER = "strata-vision" 29GPU_READER = "strata-vision-hip" 30SYCL_READER = "strata-vision-sycl" 31GPU_READERS = (GPU_READER ,SYCL_READER )# the GPU reader of an engine folder: the first one it carries 32 33#: Owner decision 2026-10-18: pictures are ON by default, and the reader runs on the CPU for a card under 24 GB (the GPU reader keeps about 34#: 1-1.4 GB of VRAM that the experts would use: a few % of decode, more on some PCs, #2406) and on the GPU for 24 GB and above - on every make 35#: of card. A person can switch it per model (--vision gpu / --vision cpu, the Pictures step); what was asked for is kept. 36#: Owner rule 2026-11-01: the reader lives in RAM, not VRAM. The GPU reader of an NVIDIA card with one GPU is therefore started by the first picture and 37#: stopped when idle (serve/server.py apply_vision_defaults), with the CPU reader (the same program without --gpu) when the card has no room. 38GPU_MIN_VRAM_GB = 23.5 39 40#: The one exception to that rule. An Intel Arc of 24 GB or more keeps the CPU reader by default: the SYCL GPU reader works (tested on an A750, 41#: forced with --vision gpu: 0.16 s a picture against 1.7 s on the CPU, embeddings cosine >= 0.995 to the CPU reader's) but it needs the oneAPI 42#: runtime on the PC and has not run on a 24 GB+ card (an Arc Pro B70 is allowed since 2026-10-22; the CPU reader is what it gets). --vision gpu asks for it. Set True when a 24 GB+ Arc has run it. 43INTEL_GPU_READER_BY_DEFAULT = False 44 45 46def default_mode (vram_gb :float | None ,make :str = "" ,* ,unified :bool = False )-> str : 47""""cpu" or "gpu": the reader an unspecific "pictures on" gets on a card with this much memory (unknown memory: "gpu"). `make` is "intel" 48for an Arc, which stays on the CPU reader until INTEL_GPU_READER_BY_DEFAULT. unified: a Mac, where the chip's memory is the system's - 49always "gpu" (see the module doc).""" 50if unified : 51return "gpu" 52vram = float (vram_gb or 0.0 ) 53if make == "intel" and not INTEL_GPU_READER_BY_DEFAULT : 54return "cpu" 55return "cpu" if 0 < vram < GPU_MIN_VRAM_GB else "gpu" 56 57 58def wanted (asked :str | None ,vram_gb :float | None ,make :str = "" ,* ,unified :bool = False )-> str : 59"""What is wanted for `asked` (None, "yes", "on", "gpu", "cpu", "no"/"none"/"off"): "gpu", "cpu" or "none". Nothing asked and "yes" mean 60the default for this card; "gpu" and "cpu" are kept as asked.""" 61a = (asked or "" ).lower () 62if a in ("no" ,"none" ,"off" ): 63return "none" 64if a in ("gpu" ,"cpu" ): 65return a 66return default_mode (vram_gb ,make ,unified = unified ) 67 68GFX = re .compile (r"gfx[0-9a-f]+" ,re .IGNORECASE ) 69 70 71def reader_name (base :str ,windows :bool )-> str : 72return base + (".exe" if windows else "" ) 73 74 75def find (folder :Path | str ,windows :bool ,* ,mac :bool = False )-> dict : 76"""The readers in an engine folder: {"cpu": Path | None, "gpu": Path | None}. On a Mac (mac=True) `strata-vision` is both: it has 77Metal built in and `--gpu` picks the GPU.""" 78folder = Path (folder ) 79found = {} 80for key ,bases in (("cpu" , (CPU_READER ,)), ("gpu" ,GPU_READERS )): 81paths = [folder / reader_name (base ,windows )for base in bases ] 82found [key ]= next ((p for p in paths if p .is_file ()),None ) 83if mac : 84found ["gpu" ]= found ["cpu" ] 85return found 86 87 88def gfx_of (text :object )-> str : 89""""gfx1201" out of a gcnArchName ("gfx1201:sramecc-:xnack-"), a device line or a plain arch; "" when there is none.""" 90m = GFX .search (str (text or "" )) 91return m .group (0 ).lower ()if m else "" 92 93 94@dataclass 95class Reader : 96mode :str # "gpu" | "cpu" | "none" 97exe :Path | None = None # the program the run config starts 98cpu_exe :Path | None = None # the CPU reader to fall back on when a GPU reader does not start (mode "gpu" only) 99note :str = "" # one plain sentence for the setup output when it is not what was asked for 100 101 102MAC_CPU_NOTE = ("the picture reader on the CPU takes about 50 seconds for one picture on a Mac (the GPU reader: under a second): kept as " 103"you chose; --vision gpu reads on the Mac's GPU" ) 104 105 106def choose (folder :Path | str ,asked :str ,* ,windows :bool ,gpu_arch :str = "" ,archs :list | None = None ,mac :bool = False )-> Reader : 107"""The reader for `asked` ("yes"/"on"/"gpu" = the best the folder has, "cpu", "no"/"none"/"off"). 108 109gpu_arch: the card's architecture when setup knows it ("gfx1201"); archs: what the GPU reader was built for 110(BUILD.json `vision_archs`, else the package's `archs`). A card the build does not list gets the CPU reader here; 111when the card is unknown the GPU reader is tried and falls back by itself (see the module doc). 112mac: the Mac's one program, `strata-vision`: "gpu" runs it with --gpu and keeps the same file as the CPU reader to fall back on.""" 113asked = (asked or "" ).lower () 114if asked in ("no" ,"none" ,"off" ,"" ): 115return Reader ("none" ) 116have = find (folder ,windows ,mac = mac ) 117if mac : 118if have ["cpu" ]is None : 119return Reader ("none" ,note = "this engine has no picture reader: pictures off" ) 120if asked == "cpu" : 121return Reader ("cpu" ,have ["cpu" ],note = MAC_CPU_NOTE ) 122return Reader ("gpu" ,have ["gpu" ],have ["cpu" ]) 123if asked == "cpu" : 124if have ["cpu" ]: 125return Reader ("cpu" ,have ["cpu" ]) 126return Reader ("none" ,note = "this engine has no picture reader: pictures off" ) 127# yes / on / gpu 128gfx = gfx_of (gpu_arch ) 129built = [gfx_of (a )for a in (archs or [])if gfx_of (a )] 130if have ["gpu" ]is None : 131if have ["cpu" ]: 132return Reader ("cpu" ,have ["cpu" ],note = "this engine has no GPU picture reader: pictures are read on the CPU " 133"(a few seconds each)" if asked == "gpu" else "" ) 134return Reader ("none" ,note = "this engine has no picture reader: pictures off" ) 135if gfx and built and gfx not in built : 136if have ["cpu" ]: 137return Reader ("cpu" ,have ["cpu" ],note = f"the GPU picture reader has no code for your card ( { gfx } ): pictures " 138"are read on the CPU (a few seconds each)" ) 139return Reader ("none" ,note = f"the GPU picture reader has no code for your card ( { gfx } ): pictures off" ) 140return Reader ("gpu" ,have ["gpu" ],have ["cpu" ]) 141 142 143def vision_section (reader :Reader ,base :dict )-> dict : 144"""The run config's "vision" section for `reader` on top of `base` (mmproj, model, max_tokens ...): exe, gpu, and for the 145GPU reader the CPU reader to fall back on.""" 146out = dict (base ) 147out ["exe" ]= str (reader .exe ) 148out ["gpu" ]= reader .mode == "gpu" 149if reader .mode == "gpu" and reader .cpu_exe : 150out ["cpu_exe" ]= str (reader .cpu_exe ) 151else : 152out .pop ("cpu_exe" ,None ) 153return out 154 155 156def shared_memory_nvidia_host ()-> bool : 157"""Linux on Arm: the DGX Spark class (GB10: the GPU shares the system RAM, 121 GB), where the NVIDIA picture reader stays loaded instead of starting with the first 158picture (owner 2026-11-11). Measured on a Spark (IQ3_S, two starts per arm): the first picture after a start takes 0.84-0.89 s resident against 3.0-7.1 s on demand 159(the reader's start, and its context retry when the kernel has no free 2 MiB blocks), and again 2.2 s after every idle stop; the resident reader costs about 5 s of start 160time and about 1 GiB of the shared RAM, and no expert slot (every expert is resident there). The same switch as the AMD reader and the Mac's: the reader starts with the 161server, before the engine; an explicit "elastic" key in the vision section wins either way.""" 162import platform 163import sys 164return sys .platform .startswith ("linux" )and platform .machine ().lower ()in ("aarch64" ,"arm64" ) 165 166 167#: A GPU reader's start that failed while it made its CUDA context on a Linux Arm machine with shared CPU / GPU memory (DGX Spark, GB10) is tried again. Such a context needs 168#: physically contiguous memory (2 MiB blocks); when the kernel has almost none free, ggml's cudaSetDevice answers "CUDA error: out of memory ... in function 169#: ggml_cuda_init" and the reader aborts, although 20+ GiB are free. The same start 1 to 5 s later works (the kernel compacts memory after the failed try; measured 170#: on a Spark: three to five failed spawns in a row, then success: first failure to first success 9.6 to 14.6 s; one failed spawn takes 1.4 s, the pause is 1 s). Without the 171#: retry the picture reader falls back to the CPU for good (90 s for a large picture, 9.7 s against 2.0 s at 1,024 tokens on the card). 172#: Bounded by time and by count: a new spawn may start only while less than CONTEXT_RETRY_BUDGET_S seconds have passed since the first spawn started (about 8 spawns at 173#: 2.4 s a cycle; the five measured failures end at 12 s), and at most CONTEXT_RETRIES retries (fast attempts). The worst wait before the CPU reader takes over is the 174#: budget plus the last failed spawn, about 19.5 to 20 s. 175CONTEXT_RETRIES = 12 176CONTEXT_RETRY_WAIT_S = 1.0 177CONTEXT_RETRY_BUDGET_S = 18.0 178 179 180def retry_gpu_start (* ,linux :bool ,arm :bool ,hip :bool ,text :str ,retries_done :int ,next_start_s :float = 0.0 )-> bool : 181"""Should a failed GPU reader start be tried again? Only for the context failure ("out of memory" in ggml_cuda_init / cudaSetDevice), only for the NVIDIA reader on 182Linux on Arm, at most CONTEXT_RETRIES times, and only if the next spawn would start less than CONTEXT_RETRY_BUDGET_S seconds after the first one (`next_start_s`: that 183time, the pause included). A card another program filled says "cudaMalloc failed: out of memory" (a retry cannot help: reader_room reports it), every 184other error (no device, no kernel image, busy) is not this case. `text`: what the failed start said (its message and the tail of the reader's log).""" 185if not linux or not arm or hip or retries_done >= CONTEXT_RETRIES or next_start_s >= CONTEXT_RETRY_BUDGET_S : 186return False 187t = text .lower () 188if "out of memory" not in t or "cudamalloc failed" in t or "failed to allocate" in t : 189return False 190return "ggml_cuda_init" in t or "cudasetdevice" in t or "cannot use gpu" in t 191 192 193def cpu_fallback (vision_cfg :dict ,same_program :bool = False )-> dict | None : 194"""The same section for the CPU reader, when the GPU reader has one to fall back on; None otherwise. AMD and Intel name it (`cpu_exe`); 195the NVIDIA reader (`strata-vision`) is the CPU reader too, without --gpu - `same_program` takes that one (the on-demand reader's 196start, which must read the picture whatever the card has left).""" 197if not vision_cfg .get ("gpu" ): 198return None 199cpu = vision_cfg .get ("cpu_exe" ) 200if not cpu and same_program : 201# the NVIDIA reader and the Mac's are one program: without --gpu it reads on the CPU (same picture tokens, so the same answers) 202exe = str (vision_cfg .get ("exe" )or "" ) 203if Path (exe ).name .lower ()not in (CPU_READER ,CPU_READER + ".exe" ): 204return None 205cpu = exe 206if not cpu : 207return None 208out = {k :v for k ,v in vision_cfg .items ()if k not in ("cpu_exe" ,"elastic" ,"elastic_mib" ,"elastic_idle_s" )} 209out ["exe" ],out ["gpu" ]= cpu ,False 210return out