okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
150c2394f
main
1"""OWN-5: build sd-cli on a test box the way setup.py's build_image_engine does (tools/image/qimage.py's CMake 2definitions, Release, the native runner applied on CUDA / HIP), in this lane's own folder. 3 4python3 -I -B build_sd.py <root> <backend cuda|hip|vulkan|cpu> [arch] [--min-free-gb N] [--jobs N] 5 6<root> holds repo.tgz (git archive of f-own-5) and sdsrc.tgz (the pinned stable-diffusion.cpp tree, .strata-pin 7inside). Results: <root>/repo, <root>/sd-<backend>, <root>/build-<backend>/bin/sd-cli, <root>/build-<backend>.json. 8HIP: ROCm from the pip SDK in /data/Strata/.venv (as the engine builds on p620) unless ROCM_PATH is set. 9Vulkan: the LunarG SDK (qimage.VULKAN_SDK_*) unpacked into <root>/vulkan-sdk when the box has no glslc.""" 10from __future__import annotations 11 12import hashlib 13import json 14import os 15import shutil 16import subprocess 17import sys 18import tarfile 19import time 20import urllib .request 21from pathlib import Path 22 23 24def free_gb (p :Path )-> float : 25return shutil .disk_usage (p ).free / 1e9 26 27 28def sh (cmd ,env = None ,log = None ): 29"+" ," " .join (str (c )for c in cmd ),flush = True ) 30if log : 31with open (log ,"w" )as f : 32r = subprocess .run ([str (c )for c in cmd ],env = env ,stdout = f ,stderr = subprocess .STDOUT ) 33if r .returncode : 34open (log ,errors = "replace" ).read ()[- 6000 :]) 35else : 36r = subprocess .run ([str (c )for c in cmd ],env = env ) 37if r .returncode : 38raise SystemExit (f"FAILED ( { r . returncode } ): { cmd [ 0 ] } " ) 39 40 41def main ()-> int : 42a = sys .argv [1 :] 43root ,backend = Path (a [0 ]).expanduser ().resolve (),a [1 ] 44arch = a [2 ]if len (a )> 2 and not a [2 ].startswith ("--" )else None 45min_free = float (a [a .index ("--min-free-gb" )+ 1 ])if "--min-free-gb" in a else 15.0 46jobs = a [a .index ("--jobs" )+ 1 ]if "--jobs" in a else str (max (2 , (os .cpu_count ()or 4 )// 2 )) 47t0 = time .time () 48f"free before: { free_gb ( root ):.1f } GB (floor { min_free } )" ,flush = True ) 49if free_gb (root )< min_free + 3 : 50raise SystemExit ("ABORT: not enough disk" ) 51repo = root / "repo" 52if not (repo / "tools" / "image" / "qimage.py" ).is_file (): 53repo .mkdir (exist_ok = True ) 54with tarfile .open (root / "repo.tgz" )as t : 55t .extractall (repo ) 56sys .path .insert (0 ,str (repo / "tools" / "image" )) 57import qimage as QI 58src = root / f"sd- { backend } " 59if not QI .source_ok (src ): 60shutil .rmtree (src ,ignore_errors = True ) 61tmp = root / f"_sd- { backend } " 62shutil .rmtree (tmp ,ignore_errors = True ) 63tmp .mkdir () 64with tarfile .open (root / "sdsrc.tgz" )as t : 65t .extractall (tmp ) 66shutil .move (str (next (tmp .iterdir ())),str (src )) 67tmp .rmdir () 68if not QI .source_ok (src ): 69raise SystemExit ("the unpacked source does not carry the pinned .strata-pin" ) 70if backend in QI .NATIVE_BACKENDS : 71sh ([sys .executable ,"-I" ,"-B" ,repo / "tools" / "image" / "sdcpp_native" / "apply.py" ,src ]) 72env = dict (os .environ ) 73extra = [] 74if backend == "hip" : 75hp = Path (env .get ("ROCM_PATH" )or "/data/Strata/.venv/lib/python3.12/site-packages/_rocm_sdk_devel" ) 76site = hp .parent 77libw = next ((p for p in site .glob ("_rocm_sdk_libraries_*" )),None ) 78core = site / "_rocm_sdk_core" 79env .update (HIP_PLATFORM = "amd" ,ROCM_PATH = str (hp ),HIP_PATH = str (hp )) 80env ["LD_LIBRARY_PATH" ]= os .pathsep .join (str (d )for d in (hp / "lib" ,libw / "lib" if libw else hp ,core / "lib" ) 81 )+ os .pathsep + env .get ("LD_LIBRARY_PATH" ,"" ) 82env ["PATH" ]= f" { hp / 'bin' } { os . pathsep } { env [ 'PATH' ] } " 83defs = QI .cmake_defs ("hip" ,arch = arch ,rocm = hp ) 84if libw : 85defs = [d .replace ("-DCMAKE_PREFIX_PATH=" ,f"-DCMAKE_PREFIX_PATH= { libw } ;" )for d in defs ] 86elif backend == "cuda" : 87nvcc = shutil .which ("nvcc" )or "/usr/local/cuda/bin/nvcc" 88cuda_root = Path (nvcc ).resolve ().parent .parent 89env ["PATH" ]= f" { cuda_root / 'bin' } { os . pathsep } { env [ 'PATH' ] } " 90defs = QI .cmake_defs ("cuda" ,arch = arch ,cuda_root = cuda_root )+ [f"-DCMAKE_CUDA_COMPILER= { nvcc } " ] 91elif backend == "vulkan" : 92defs = QI .cmake_defs ("vulkan" ) 93if not shutil .which ("glslc" ): 94sdk = root / "vulkan-sdk" / QI .VULKAN_SDK_VERSION / "x86_64" 95if not (sdk / "bin" / "glslc" ).exists (): 96tar = root / f"vulkansdk- { QI . VULKAN_SDK_VERSION } .tar.xz" 97if not tar .exists ()or tar .stat ().st_size != QI .VULKAN_SDK_SIZE : 98"downloading the LunarG Vulkan SDK" ,flush = True ) 99# urllib's default User-Agent gets 403 from sdk.lunarg.com; curl's is accepted 100sh (["curl" ,"-L" ,"--fail" ,"--retry" ,"5" ,"-s" ,"-S" ,"-o" ,tar ,QI .VULKAN_SDK_URL ]) 101h = hashlib .sha256 () 102with open (tar ,"rb" )as f : 103for b in iter (lambda :f .read (1 << 20 ),b"" ): 104h .update (b ) 105if tar .stat ().st_size != QI .VULKAN_SDK_SIZE or h .hexdigest ()!= QI .VULKAN_SDK_SHA256 : 106raise SystemExit ("the Vulkan SDK download does not match its pinned size / sha256" ) 107with tarfile .open (tar )as t : 108t .extractall (root / "vulkan-sdk" ,filter = "data" ) 109tar .unlink () 110env ["VULKAN_SDK" ]= str (sdk ) 111env ["PATH" ]= f" { sdk / 'bin' } { os . pathsep } { env [ 'PATH' ] } " 112loader = sdk / "lib" / "VulkanLoader" / "lib" 113env ["LD_LIBRARY_PATH" ]= f" { loader } { os . pathsep } { env . get ( 'LD_LIBRARY_PATH' , '' ) } " 114extra = [f"-DVulkan_LIBRARY= { loader / 'libvulkan.so' } " ,f"-DVulkan_INCLUDE_DIR= { sdk / 'include' } " , 115f"-DVulkan_GLSLC_EXECUTABLE= { sdk / 'bin' / 'glslc' } " ] 116else : 117defs = QI .cmake_defs ("cpu" ) 118bdir = root / f"build- { backend } " 119gen = ["-G" ,"Ninja" ]if shutil .which ("ninja" )else ["-G" ,"Unix Makefiles" ] 120conf = ["cmake" ,* gen ,"-S" ,src ,"-B" ,bdir ,"-DCMAKE_BUILD_TYPE=Release" , 121"-DCMAKE_EXPORT_COMPILE_COMMANDS=ON" ,* defs ,* extra ] 122sh (conf ,env = env ,log = root / f"cfg- { backend } .log" ) 123sh (["nice" ,"-n" ,"15" ,"cmake" ,"--build" ,bdir ,"--target" ,"sd-cli" ,"-j" ,jobs ],env = env , 124log = root / f"build- { backend } .log" ) 125exe = bdir / "bin" / "sd-cli" 126if not exe .exists (): 127raise SystemExit (f"no { exe } " ) 128info = {** QI .build_stamp (backend ,arch ),"configure" : [str (c )for c in conf ],"generator" :gen [1 ], 129"minutes" :round ((time .time ()- t0 )/ 60 ,1 ),"exe" :str (exe ),"exe_bytes" :exe .stat ().st_size , 130"env_ld" :env .get ("LD_LIBRARY_PATH" ,"" ),"env_path" :env ["PATH" ]} 131 (root / f"build- { backend } .json" ).write_text (json .dumps (info ,indent = 1 )) 132json .dumps ({k :info [k ]for k in ("backend" ,"arch" ,"minutes" ,"exe_bytes" ,"generator" )})) 133f"free after: { free_gb ( root ):.1f } GB" ,flush = True ) 134"BUILD-OK" ) 135return 0 136 137 138if __name__ == "__main__" : 139sys .exit (main ())