okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
736266e87
main
1"""tools/image/install.py - image generation (Qwen-Image-2.1-Turbo) for an existing Strata install, or on its own: 2compiles sd-cli for the backend (setup.py's build_image_engine), downloads and checks the three model files, and 3writes the "images" section into a run config (or prints it). setup.py --images qwen-image does the same inside 4the normal setup; this is the way to add it without running the whole setup again. 5 6python tools/image/install.py --config strata-q2_0.json (backend and size picked for this PC) 7python tools/image/install.py --backend vulkan --quant Q8_0 --models-dir ~/models --print 8python tools/image/install.py --backend hip --arch gfx1103 --build-only 9 10--gpu-policy auto|keep|shrink|unload: what the text model does during an image job (serve/images.py). 11""" 12from __future__import annotations 13 14import argparse 15import json 16import sys 17from pathlib import Path 18 19ROOT = Path (__file__ ).resolve ().parents [2 ] 20sys .path .insert (0 ,str (ROOT )) 21sys .path .insert (0 ,str (ROOT / "tools" )) 22sys .path .insert (0 ,str (ROOT / "tools" / "image" )) 23import qimage as QI # noqa: E402 24 25 26def detect_gpu (S )-> tuple [dict | None ,bool ]: 27"""setup.py's view of this PC's GPU: (gpu dict, AMD?) - an NVIDIA card first, else an AMD one, else none.""" 28try : 29g = S .gpu_info () 30if g : 31return {** g ,"vendor" :"nvidia" },False 32except Exception :# noqa: BLE001 - no NVIDIA driver 33pass 34try : 35amd = S .amd_gpus_win ()if S .WIN else S .amd_gpus () 36if amd : 37g = min (amd ,key = S .amd_rank ) 38return S .amd_apply_uma (dict (g )),True 39except Exception :# noqa: BLE001 40pass 41return None ,False 42 43 44def main (argv = None )-> int : 45ap = argparse .ArgumentParser (description = __doc__ ,formatter_class = argparse .RawDescriptionHelpFormatter ) 46ap .add_argument ("--backend" ,choices = QI .BACKENDS ) 47ap .add_argument ("--arch" ,help = "the GPU architecture to compile for (CUDA: 86, ...; HIP: gfx1103, ...)" ) 48ap .add_argument ("--quant" ,choices = QI .QUANTS ) 49ap .add_argument ("--gpu-gb" ,type = float ,help = "the GPU memory the quant is picked for (default: detected)" ) 50ap .add_argument ("--models-dir" ,help = "where the model files go (default: the data folder's models)" ) 51ap .add_argument ("--config" ,help = "a run config (strata-<model>.json) to write the \"images\" section into" ) 52ap .add_argument ("--gpu-policy" ,choices = ["auto" ,"keep" ,"shrink" ,"unload" ],default = "auto" ) 53ap .add_argument ("--gpu" ,type = int ,help = "the GPU's number for the image job (default: the backend's first)" ) 54ap .add_argument ("--native" ,choices = ["on" ,"off" ],default = "off" , 55help = "EXPERIMENTAL: Strata's own DiT runner on CUDA / HIP (the config's \"native\")" ) 56ap .add_argument ("--noise" ,choices = ["on" ,"off" ],default = "off" ,help = argparse .SUPPRESS ) 57ap .add_argument ("--vae" ,choices = ["on" ,"off" ],default = "off" ,help = argparse .SUPPRESS ) 58ap .add_argument ("--build-only" ,action = "store_true" ,help = "compile sd-cli only" ) 59ap .add_argument ("--print" ,action = "store_true" ,help = "print the images section instead of writing it" ) 60ap .add_argument ("--yes" ,action = "store_true" ) 61a = ap .parse_args (argv ) 62import setup as S 63gpu ,amd = detect_gpu (S )if not (a .backend and (a .arch or a .backend in ("vulkan" ,"cpu" )))else (None ,False ) 64gpu = dict (gpu or {}) 65if a .arch : 66gpu ["arch" ]= a .arch 67if a .gpu_gb is not None : 68gpu ["vram_gb" ]= a .gpu_gb 69ns = argparse .Namespace (images = "qwen-image" ,image_backend = a .backend ,image_quant = a .quant , 70image_gpu_policy = a .gpu_policy ,yes = a .yes ) 71backend ,quant ,gb = S .image_backend_choice (ns ,gpu or None ,amd or a .backend == "hip" ) 72if backend in ("cuda" ,"hip" )and not gpu .get ("arch" ): 73S .fail (f"the { backend } build needs the GPU's architecture" ,"pass --arch (e.g. 86 or gfx1103)" ) 74S .ok (f"image generation: { quant } on { backend } " + (f" ( { gb :.0f } GB GPU memory)" if gb else "" )) 75exe ,lib_dirs = S .build_image_engine (backend ,gpu ,a .noise == "on" ,a .vae == "on" ) 76if a .build_only : 77return 0 78if a .models_dir : 79models = Path (a .models_dir ).expanduser () 80else : 81data ,_ = S .data_folder (None ) 82models = data / "models" 83S .say (" " + QI .LICENSE_NOTE ) 84for name in (quant ,"vae" ,"llm" ): 85repo ,path ,size ,sha = QI .FILES [name ] 86dst = QI .local_path (models ,name ) 87S .download (QI .file_url (name ,S .hf_endpoint ()),dst ,f"image model: { Path ( path ). name } " ) 88S .verify_sha256 (dst ,size ,sha ) 89section = QI .image_config (backend ,quant ,exe ,models ,uma = QI .is_apu (gpu ),gpu = a .gpu ,lib_dirs = lib_dirs , 90policy = a .gpu_policy ,native = a .native == "on" ,noise = a .noise == "on" , 91vae = a .vae == "on" ) 92if a .or not a .config : 93json .dumps ({"images" :section },indent = 1 )) 94return 0 95path = Path (a .config ) 96cfg = json .loads (path .read_text (encoding = "utf-8-sig" ))if path .is_file ()else {} 97cfg ["images" ]= section 98S .write_config (path ,cfg ) 99S .ok (f" { path . name } : image generation added (POST /v1/images/generations once the server starts again)" ) 100return 0 101 102 103if __name__ == "__main__" : 104sys .exit (main ())