okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
aff3a132f
main
1"""DeepSeek models that this Strata no longer runs: how a saved config, a folder, a file or a typed name is recognised, and the words 2a person sees. 3 4Strata ran DeepSeek-V4.1-Flash up to 0.2.13 on an engine of its own (strata-dsv41). This version does not: a DeepSeek config is 5refused before anything starts and before any file of it is written, its model files are never touched, and the words name where 6they are and how large, so the person can decide. Every check here is local and bounded (config fields, a file name, the first 7bytes of one GGUF, the sizes of the files directly in one or two folders): it answers at once, also offline, and never raises. 8""" 9from __future__import annotations 10 11import json 12import os 13import re 14import struct 15from pathlib import Path 16from typing import Any ,Iterable ,Mapping 17 18#: the family and arch names setup and the server used for DeepSeek-V4.1-Flash 19FAMILY = "deepseek41" 20ENGINE_NAMES = ("strata-dsv41" ,"strata-dsv41.exe" ) 21#: the model sizes setup and `strata launch` offered under --family deepseek41 (lower case); "UD-IQ1_M" and "UD-IQ1_S" are not here: they are also 22#: Unsloth Qwen names 23MODEL_NAMES = frozenset ({"ist-das-2.00bpw" ,"ist-das-2.25bpw" ,"ist-das-1.75bpw" ,"ista-das-coder" ,"ud-q8_k_xl" , 24"deepseek-v4.1-flash" ,"deepseek41" ,"deepseek" }) 25#: the config file stem setup wrote (strata-deepseek-v4.1-flash-<size>.json) and the model_name in it 26TAG = "deepseek-v4.1-flash" 27#: the published file names of the DeepSeek sets (unsloth, ISTA-DAS, the Engram table, the DSpark drafter) 28_FILE = re .compile (r"^(?:dspark-)?DeepSeek-V4[._]?1-Flash[-_].*\.gguf$" ,re .I ) 29_DIR_ENTRIES = 200 # entries looked at in one folder: a bounded look, never a walk 30_GGUF_HEAD = 1 << 20 # bytes of a GGUF read to find general.architecture 31 32 33# Every sentence below is the owner's approved wording of 11-11 (par/OWNER-WORDING-1111.md): change none without him. 34#: D1: a start of a saved DeepSeek model (the app, `strata start`, a saved launch) 35REFUSED = "{name} is a DeepSeek model. This version of Strata no longer runs DeepSeek models, so it was not started." 36FILES_STAY = "Its files are still on this computer: {size} in {folder}. Nothing was changed or deleted." 37NEXT = ("Choose a Qwen model to run instead. If you no longer need the DeepSeek files, you can delete that folder " 38"to free the space." ) 39#: D2: the model list's line for a DeepSeek row 40ROW = "DeepSeek - no longer supported." 41ROW_FILES = "DeepSeek - no longer supported. {size} in {folder}." 42#: D3: DeepSeek asked for by name (`strata launch --model`, `--family deepseek41`, setup, a DeepSeek-only flag) 43NOT_OFFERED = "Strata no longer offers DeepSeek models. Run `strata launch` to choose one of the Qwen models." 44#: D4: the person's own DeepSeek files (`strata launch --model-path`, the own-files screen, `strata models import`) 45OWN_FILES = ("These are DeepSeek model files ({size} in {folder}). This version of Strata no longer runs DeepSeek models; " 46"nothing in the folder was changed. Choose a Qwen model instead." ) 47OWN_FILES_NOSIZE = ("These are DeepSeek model files (in {folder}). This version of Strata no longer runs DeepSeek models; " 48"nothing in the folder was changed. Choose a Qwen model instead." ) 49#: D5: the server started directly with a DeepSeek config or tokenizer 50SERVER = ("[strata] this config runs a DeepSeek model ({why}). This version of Strata no longer runs DeepSeek models, so the " 51"server stops here. The model files were not changed." ) 52#: D6: setup's update and its list of installed models 53LEFT_AS_IS = "{name}: a DeepSeek model, left as it is (this version of Strata no longer runs DeepSeek models)." 54 55WHY = {"arch" :"arch deepseek41" ,"engine" :"engine strata-dsv41" ,"tokenizer" :"DeepSeek tokenizer" ,"gguf" :"DeepSeek model file" , 56"name" :"DeepSeek model name" } 57 58 59# ----------------------------------------------------------------------------------------------------------- recognising 60def is_name (word :Any )-> bool : 61"""A model, size or family name typed or saved for DeepSeek (`--model IST-DAS-2.00bpw`, `--family deepseek41`, a saved choice).""" 62if not isinstance (word ,str )or not word .strip (): 63return False 64w = word .strip ().lower () 65return w in MODEL_NAMES or w .startswith (TAG )or w .startswith ("deepseek-v4" ) 66 67 68def is_file_name (name :Any )-> bool : 69"""A published DeepSeek-V4.1-Flash file name (a shard, the Engram table, the DSpark drafter).""" 70return isinstance (name ,str )and bool (_FILE .match (Path (name ).name )) 71 72 73def gguf_arch (path :Any )-> str | None : 74"""general.architecture of a GGUF file, read from its first megabyte (None: not a GGUF, unreadable, or not found early).""" 75try : 76with open (path ,"rb" )as f : 77head = f .read (_GGUF_HEAD ) 78except (OSError ,TypeError ,ValueError ): 79return None 80if len (head )< 24 or head [:4 ]!= b"GGUF" : 81return None 82try : 83pos = 8 + 8 # magic, version, tensor count 84 (n_kv ,)= struct .unpack_from ("<Q" ,head ,pos ) 85pos += 8 86sizes = {0 :1 ,1 :1 ,2 :2 ,3 :2 ,4 :4 ,5 :4 ,6 :4 ,7 :1 ,10 :8 ,11 :8 ,12 :8 } 87for _ in range (min (n_kv ,64 )): 88 (klen ,)= struct .unpack_from ("<Q" ,head ,pos ) 89pos += 8 90key = head [pos :pos + klen ].decode ("utf-8" ,"replace" ) 91pos += klen 92 (vtype ,)= struct .unpack_from ("<I" ,head ,pos ) 93pos += 4 94if vtype == 8 :# a string 95 (slen ,)= struct .unpack_from ("<Q" ,head ,pos ) 96pos += 8 97value = head [pos :pos + slen ].decode ("utf-8" ,"replace" ) 98pos += slen 99if key == "general.architecture" : 100return value 101elif vtype in sizes : 102pos += sizes [vtype ] 103else :# an array before the architecture: stop looking 104return None 105if pos > len (head ): 106return None 107except (struct .error ,ValueError ): 108return None 109return None 110 111 112def _tokenizer_pre (cfg :Mapping )-> str | None : 113tok = cfg .get ("tokenizer" ) 114if not isinstance (tok ,str )or not tok : 115return None 116try : 117with open (Path (tok )/ "tokenizer.json" ,"rb" )as f : 118data = f .read (_GGUF_HEAD ) 119pre = json .loads (data .decode ("utf-8" )).get ("pre" ) 120except (OSError ,ValueError ,AttributeError ,TypeError ): 121return None 122return pre if isinstance (pre ,str )else None 123 124 125def config_reason (cfg :Any )-> str | None : 126"""Why a run config is a DeepSeek one ("arch", "engine", "tokenizer", "name"), or None. Reads the config's fields and, only when it 127names no arch, the first megabyte of its tokenizer.json.""" 128if not isinstance (cfg ,Mapping ): 129return None 130arch = cfg .get ("arch" ) 131if isinstance (arch ,str )and arch .strip ().lower ().startswith ("deepseek" ): 132return "arch" 133exe = cfg .get ("exe" ) 134if isinstance (exe ,str )and re .split (r"[\\/]" ,exe )[- 1 ].lower ()in ENGINE_NAMES : 135return "engine" 136if isinstance (cfg .get ("model_name" ),str )and is_name (cfg ["model_name" ]): 137return "name" 138if not arch : 139pre = _tokenizer_pre (cfg ) 140if pre and pre .lower ().startswith ("deepseek" ): 141return "tokenizer" 142return None 143 144 145def read_config (path :Any )-> dict | None : 146try : 147data = json .loads (Path (path ).read_text (encoding = "utf-8-sig" )) 148except (OSError ,ValueError ,TypeError ): 149return None 150return data if isinstance (data ,dict )else None 151 152 153def config_file_reason (path :Any )-> str | None : 154"""config_reason of a run config file (an unreadable file is not DeepSeek's: the usual errors say what is wrong with it).""" 155stem = Path (str (path )).stem .lower ()if path else "" 156if stem .startswith ("strata-" + TAG ): 157return "name" 158return config_reason (read_config (path )) 159 160 161def folder_reason (path :Any )-> str | None : 162"""Why a folder or file the person points at holds a DeepSeek model ("name": a published file name, "gguf": a GGUF whose architecture 163is DeepSeek's), or None. Looks at most at _DIR_ENTRIES names and reads the head of at most one GGUF.""" 164try : 165p = Path (path ) 166if p .is_file (): 167if is_file_name (p .name ): 168return "name" 169arch = gguf_arch (p ) 170return "gguf" if arch and arch .lower ().startswith ("deepseek" )else None 171if not p .is_dir (): 172return None 173first = None 174with os .scandir (p )as it : 175for i ,entry in enumerate (it ): 176if i >= _DIR_ENTRIES : 177break 178if is_file_name (entry .name ): 179return "name" 180if first is None and entry .name .lower ().endswith (".gguf" )and entry .is_file (): 181first = entry .path 182if first : 183arch = gguf_arch (first ) 184return "gguf" if arch and arch .lower ().startswith ("deepseek" )else None 185except OSError : 186return None 187return None 188 189 190# ------------------------------------------------------------------------------------------------------ where and how large 191def _top_files (folder :Path )-> tuple [int ,int ]: 192"""(bytes, files) of the files directly in `folder` (at most _DIR_ENTRIES entries; symlinks followed, folders not entered).""" 193total = count = 0 194try : 195with os .scandir (folder )as it : 196for i ,entry in enumerate (it ): 197if i >= _DIR_ENTRIES : 198break 199try : 200if entry .is_file (): 201total += entry .stat ().st_size 202count += 1 203except OSError : 204continue 205except OSError : 206pass 207return total ,count 208 209 210def _pack_model_dir (pack :Path )-> Path | None : 211"""The model folder a DeepSeek pack names (its ds4.txt line `model_dir <folder>`).""" 212try : 213with open (pack / "ds4.txt" ,"r" ,encoding = "utf-8" ,errors = "replace" )as f : 214for i ,line in enumerate (f ): 215if i > 400 : 216break 217if line .startswith ("model_dir " ): 218return Path (line [len ("model_dir " ):].strip ()) 219except OSError : 220return None 221return None 222 223 224def config_folders (cfg :Mapping )-> list [Path ]: 225"""The folders that hold a DeepSeek config's model: the model folder its pack names, then the pack itself (existing ones only).""" 226args = [str (x )for x in (cfg .get ("args" )or [])if isinstance (x , (str ,int ,float ))] 227out :list [Path ]= [] 228for flag in ("--pack" ,"--model" ): 229if flag in args and args .index (flag )+ 1 < len (args ): 230p = Path (args [args .index (flag )+ 1 ]) 231model_dir = _pack_model_dir (p )if flag == "--pack" else None 232for q in (model_dir ,p if p .is_dir ()else p .parent if p .is_file ()else None ): 233if q is not None and q .is_dir ()and q not in out : 234out .append (q ) 235return out 236 237 238def where (folders :Iterable [Path ])-> tuple [str ,int ]| None : 239"""("<folder>[ and <folder>]", bytes) of the files directly in these folders, or None when none is there.""" 240folders = list (folders ) 241total ,names = 0 , [] 242for f in folders : 243size ,count = _top_files (f ) 244if count : 245total += size 246names .append (str (f )) 247return (" and " .join (names ),total )if names else None 248 249 250def size_words (n :int )-> str : 251gb = max (n ,0 )/ 1e9 252return f" { gb :.0f } GB" if gb >= 10 else f" { gb :.1f } GB" 253 254 255# ----------------------------------------------------------------------------------------------------------------- words 256_LABELS = {"ist-das-2.00bpw" :"ISTA-DAS 2.00 bpw" ,"ist-das-2.25bpw" :"ISTA-DAS 2.25 bpw" ,"ist-das-1.75bpw" :"ISTA-DAS 1.75 bpw" , 257"ista-das-coder" :"ISTA-DAS Coder" ,"ud-q8_k_xl" :"Unsloth UD-Q8_K_XL" ,"ud-iq1_s" :"Unsloth UD-IQ1_S" ,"ud-iq1_m" :"Unsloth UD-IQ1_M" } 258 259 260def title (cfg :Mapping | None = None ,path :Any = None )-> str : 261"""The model's name as setup showed it ("DeepSeek-V4.1-Flash ISTA-DAS 2.00 bpw"), from the config file's name when it has setup's form.""" 262stem = Path (str (path )).stem .lower ()if path else "" 263head = "strata-" + TAG + "-" 264if stem .startswith (head ): 265size = stem [len (head ):] 266return "DeepSeek-V4.1-Flash " + _LABELS .get (size ,size .upper ()) 267return "DeepSeek-V4.1-Flash" 268 269 270def refusal (cfg :Mapping | None ,name :str | None = None )-> str : 271"""D1: the whole refusal for a saved DeepSeek model: what happened, where its files are and how large (when that can be read), the 272next step.""" 273title = name or (cfg or {}).get ("model_name" )or "This model" 274lines = [REFUSED .format (name = title )] 275found = where (config_folders (cfg ))if isinstance (cfg ,Mapping )else None 276if found : 277lines .append (FILES_STAY .format (size = size_words (found [1 ]),folder = found [0 ])) 278lines .append (NEXT ) 279return " " .join (lines ) 280 281 282def row_line (cfg :Mapping | None )-> str : 283"""D2: the model list's line for a DeepSeek row.""" 284found = where (config_folders (cfg ))if isinstance (cfg ,Mapping )else None 285return ROW_FILES .format (size = size_words (found [1 ]),folder = found [0 ])if found else ROW 286 287 288def own_files (path :Any )-> str : 289"""D4: the person pointed at DeepSeek files.""" 290p = Path (path ) 291folder = p if p .is_dir ()else p .parent 292found = where ([folder ]) 293return OWN_FILES .format (size = size_words (found [1 ]),folder = found [0 ])if found else OWN_FILES_NOSIZE .format (folder = folder ) 294 295 296def server_line (reason :str )-> str : 297"""D5.""" 298return SERVER .format (why = WHY .get (reason ,reason )) 299 300 301def left_as_is (name :str )-> str : 302"""D6.""" 303return LEFT_AS_IS .format (name = name )