okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b1594fbbe
main
1#!/usr/bin/env python3 2"""Package the Windows HIP engine as the ready-made zip setup.py downloads (strata-windows-x64-hip.zip). 3 4python tools/hip/package_windows.py --build build-hip-win --rocm <rocm-sdk path --root> \\ 5--archs "gfx1100;gfx1201" --rocm-version 10.2.0a20260930 --out dist 6 7tools/hip/build_windows.bat runs it after the build. The zip holds: 8 9strata.exe, strata-device.exe, BUILD.json (backend "hip", the archs, the ROCm and hipBLASLt versions) 10strata-vision.exe, strata-vision-hip.exe the picture readers when --vision-cpu / --vision-hip are given: the CPU reader 11(the fallback; starts on any PC) and the GPU reader (tools/vision, 12STRATA_VISION_HIP; imports the same ROCm DLLs as strata.exe) 13amdhip64_7.dll, amd_comgr.dll + what they import the HIP runtime beside the exes too (#468 #461: before System32) 14rocm/bin/*.dll the ROCm DLLs the two programs load (their import tables, followed 15through the ROCm DLLs, + amd_comgr.dll, which the HIP runtime loads 16by name) and the Microsoft C++ runtime they import 17rocm/bin/rocblas/library, rocm/bin/hipblaslt/library the GEMM kernels, for these archs only 18tuning/<arch>-hipblaslt-<version>.txt the hipBLASLt tuning tables of tools/hip for these archs and this hipBLASLt version 19(BUILD.json hipblaslt_version); the engine start uses the one for the card 20rocm/.kpack/ rocBLAS's own device code for these archs (rocblas.dll finds it at 21../.kpack from its folder) 22rocm/licenses/ the shipped components' licenses, and NOTICE.txt 23 24setup.py puts rocm/bin first on the engine's PATH (the config's lib_dirs). The AMD display driver (AMD Software: 25Adrenalin Edition) is the only thing the user installs: TheRock's HIP runtime runs on top of it. 26""" 27from __future__import annotations 28 29import argparse 30import json 31import os 32import re 33import shutil 34import subprocess 35import sys 36import zipfile 37from pathlib import Path 38 39ROOT = Path (__file__ ).resolve ().parents [2 ] 40sys .path .insert (0 ,str (ROOT )) 41from strata_control import hipblaslt_tuning as ht # noqa: E402 42 43ASSET = "strata-windows-x64-hip.zip" 44PROGRAMS = ("strata.exe" ,"strata-device.exe" ) 45DYNAMIC = ("amd_comgr.dll" ,)# LoadLibrary'd by amdhip64_7.dll: not in any import table 46CRT = ("msvcp140.dll" ,"msvcp140_1.dll" ,"msvcp140_2.dll" ,"vcruntime140.dll" ,"vcruntime140_1.dll" , 47"msvcp140_atomic_wait.dll" ,"msvcp140_codecvt_ids.dll" ,"vcruntime140_threads.dll" ,"concrt140.dll" )# strata.exe imports msvcp140_atomic_wait.dll (VS 2022 std::atomic wait) 48KPACKS = ("blas_lib" ,)# rocblas.dll's own device code; rocSOLVER's (27 MB per arch) is never launched by Strata 49LICENSES = ("rocblas" ,"rocsolver" ,"hipblas" ,"hipblas-common" ,"hipblaslt" ,"amd_comgr" ,"rocm-core" ) 50NOTICE = """Strata's Windows HIP engine ships these AMD ROCm components unmodified, from AMD's TheRock Python wheels 51(ROCm {rocm}, https://nightly.repo.amd.com/rocm/whl-next/, built from https://github.com/ROCm/TheRock): 52 53{dlls} 54 55Licenses: rocBLAS, hipBLAS, hipBLAS-common, hipBLASLt (and its TensileLite host and origami libraries), rocm-core: MIT; 56rocSOLVER: BSD 2-clause; amd_comgr: Apache 2.0 with LLVM exceptions (files in this folder). The HIP runtime 57(amdhip64_7.dll, https://github.com/ROCm/rocm-systems/tree/develop/projects/clr) and rocm_kpack.dll (TheRock) are MIT 58licensed; their wheels carry no license file. The Microsoft Visual C++ runtime DLLs (msvcp140*.dll, 59vcruntime140*.dll) are redistributable under the Visual Studio license (Distributable Code). 60""" 61 62 63def imports (objdump :Path ,dll :Path )-> list [str ]: 64text = subprocess .run ([str (objdump ),"-p" ,str (dll )],capture_output = True ,text = True ,check = True ).stdout 65names = [m .group (1 )for m in re .finditer (r"DLL Name:\s*(\S+)" ,text )] 66# 0.2.1.2: strata.exe delay-loads hipblas / libhipblaslt / rocblas, which objdump -p does not list; dll_closure reads the delay-import table too 67sys .path .insert (0 ,str (Path (__file__ ).resolve ().parent )) 68from dll_closure import pe_imports 69seen = {n .lower ()for n in names } 70return names + [n for n in pe_imports (dll )if n .lower ()not in seen ] 71 72 73def crt_dir ()-> Path | None : 74"""The MSVC redist folder with the C++ runtime DLLs (vcvars sets VCToolsRedistDir).""" 75base = os .environ .get ("VCToolsRedistDir" ) 76if not base : 77return None 78hits = sorted (Path (base ,"x64" ).glob ("Microsoft.VC*.CRT" )) 79return hits [- 1 ]if hits else None 80 81 82def main ()-> int : 83ap = argparse .ArgumentParser (description = __doc__ ,formatter_class = argparse .RawDescriptionHelpFormatter ) 84ap .add_argument ("--build" ,required = True ,type = Path ) 85ap .add_argument ("--rocm" ,required = True ,type = Path ,help = "the ROCm root (rocm-sdk path --root)" ) 86ap .add_argument ("--archs" ,required = True ,help = "the archs the engine was compiled for, ';'-separated" ) 87ap .add_argument ("--rocm-version" ,required = True ) 88ap .add_argument ("--out" ,required = True ,type = Path ) 89ap .add_argument ("--vision-cpu" ,type = Path ,help = "strata-vision.exe built for the CPU (tools/vision, STRATA_PORTABLE=ON)" ) 90ap .add_argument ("--vision-hip" ,type = Path ,help = "strata-vision-hip.exe built for the same archs (STRATA_VISION_HIP=ON)" ) 91a = ap .parse_args () 92archs = [x for x in re .split (r"[;, ]+" ,a .archs )if x ] 93rbin = a .rocm / "bin" 94objdump = a .rocm / "lib" / "llvm" / "bin" / "llvm-objdump.exe" 95stage = a .out / "strata-windows-x64-hip" 96shutil .rmtree (stage ,ignore_errors = True ) 97 (stage / "rocm" / "bin" ).mkdir (parents = True ) 98 99for p in PROGRAMS : 100shutil .copy2 (a .build / p ,stage / p ) 101readers = [(src ,src .name )for src in (a .vision_cpu ,a .vision_hip )if src is not None ] 102for src ,name in readers : 103shutil .copy2 (src ,stage / name ) 104 105# the ROCm DLLs: the programs' imports, followed through the ROCm DLLs themselves 106rocm_dlls = {p .name .lower ():p for p in rbin .glob ("*.dll" )} 107need ,todo ,crt = [], [stage / p for p in PROGRAMS ]+ [stage / n for _ ,n in readers ]+ [rbin / d for d in DYNAMIC ],set () 108while todo : 109f = todo .pop () 110for name in imports (objdump ,f ): 111low = name .lower () 112if low in CRT : 113crt .add (low ) 114elif low in rocm_dlls and low not in need : 115need .append (low ) 116todo .append (rocm_dlls [low ]) 117for d in DYNAMIC : 118if d not in need : 119need .append (d ) 120for d in sorted (need ): 121shutil .copy2 (rocm_dlls [d ],stage / "rocm" / "bin" / rocm_dlls [d ].name ) 122cdir = crt_dir () 123if cdir is None : 124sys .exit ("VCToolsRedistDir is not set: run this from a Visual Studio x64 developer prompt (vcvars64.bat)" ) 125for d in sorted (crt ): 126shutil .copy2 (cdir / d ,stage / "rocm" / "bin" / d ) 127shutil .copy2 (cdir / d ,stage / d )# and beside the exes: the folder Windows searches first, with no PATH needed 128# #468 #461: the HIP runtime (and the compiler it loads by name) next to the exes as well - Windows searches the 129# exe's folder before System32, where an AMD driver may install its own amdhip64_7.dll (found before PATH's 130# rocm/bin); rocBLAS/hipBLASLt stay in rocm/bin, which they resolve their kernel libraries and ../.kpack from 131# #461: and everything those two import (amdhip64_7.dll needs rocm_kpack.dll and the C++ runtime, or its full-path 132# load fails with error 126 and Windows falls back to System32's copy): the closure of their PE import tables 133sys .path .insert (0 ,str (Path (__file__ ).resolve ().parent )) 134from dll_closure import dll_closure 135roots = sorted (f .name for pat in ("amdhip64_*.dll" ,"amd_comgr*.dll" )for f in (stage / "rocm" / "bin" ).glob (pat )) 136for name in dll_closure (stage / "rocm" / "bin" ,roots ): 137shutil .copy2 (stage / "rocm" / "bin" / name ,stage / name ) 138 139# the GEMM kernels and the libraries' own device code, for these archs 140lib = stage / "rocm" / "bin" / "rocblas" / "library" 141lib .mkdir (parents = True ) 142for f in (rbin / "rocblas" / "library" ).iterdir (): 143m = re .search (r"gfx[0-9a-f]+" ,f .name ) 144if m is None or m .group ()in archs : 145if f .is_dir ():# the gfx1151 wheel keeps its rocBLAS kernels in library/gfx1151/ 146shutil .copytree (f ,lib / f .name ) 147else : 148shutil .copy2 (f ,lib / f .name ) 149for arch in archs : 150src = rbin / "hipblaslt" / "library" / arch 151if src .is_dir ():# hipBLASLt has no RDNA2 (gfx1030) kernels 152shutil .copytree (src ,stage / "rocm" / "bin" / "hipblaslt" / "library" / arch ) 153kp = stage / "rocm" / ".kpack" 154kp .mkdir () 155kpack_src = a .rocm / ".kpack" 156for arch in archs : 157for k in KPACKS : 158f = kpack_src / f" { k } _ { arch } .kpack" 159if not f .exists (): 160sys .exit (f"missing { f } : install the rocm-sdk-device- { arch } wheel and run rocm-sdk init" ) 161shutil .copy2 (f ,kp / f .name ) 162 163lic = stage / "rocm" / "licenses" 164lic .mkdir () 165for comp in LICENSES : 166for f in (a .rocm / "share" / "doc" / comp ).glob ("LICENSE*" ): 167shutil .copy2 (f ,lic / f" { comp } - { f . name } " ) 168shipped = sorted (p .name for p in (stage / "rocm" / "bin" ).glob ("*.dll" )) 169 (lic / "NOTICE.txt" ).write_text (NOTICE .format (rocm = a .rocm_version ,dlls = "\n" .join (" " + d for d in shipped )), 170encoding = "utf-8" ) 171 172version = re .search (r"project\(strata VERSION ([\d.]+)" , (ROOT / "CMakeLists.txt" ).read_text (encoding = "utf-8" )).group (1 ) 173hl = (a .rocm / "include" / "hipblaslt" / "hipblaslt-version.h" ).read_text (encoding = "utf-8" ) 174hlv = [int (re .search (rf"#define\s+HIPBLASLT_VERSION_ { k } \s+(\d+)" ,hl ).group (1 ))for k in ("MAJOR" ,"MINOR" ,"PATCH" )] 175meta = {"source" :"prebuilt" ,"backend" :"hip" ,"platform" :"windows-x64" ,"version" :version ,"archs" :archs , 176"rocm" :a .rocm_version ,"hipblaslt_version" :hlv [0 ]* 100000 + hlv [1 ]* 100 + hlv [2 ],"vision" :"none" , 177"lib_dirs" : ["rocm/bin" ]} 178if a .vision_cpu is not None : 179meta .update ({"vision" :"cpu" ,"vision_cpu" :a .vision_cpu .name }) 180if a .vision_hip is not None : 181meta .update ({"vision" :"gpu" ,"vision_gpu" :a .vision_hip .name ,"vision_archs" :archs }) 182 (stage / "BUILD.json" ).write_text (json .dumps (meta ,indent = 1 ),encoding = "utf-8" ) 183# the hipBLASLt tuning tables of tools/hip for these archs and exactly this hipBLASLt version, in tuning/ (strata_control/hipblaslt_tuning.py): 184# the engine start points STRATA_HIPBLASLT_TUNING at the one for the card; an arch without a table keeps plain hipBLAS 185tables = ht .stage_tables (stage ,archs ,meta ["hipblaslt_version" ],ROOT / "tools" / "hip" ) 186for arch ,have in ht .missing_report (archs ,meta ["hipblaslt_version" ],ROOT / "tools" / "hip" ).items (): 187f"no hipBLASLt table for { arch } with hipBLASLt { meta [ 'hipblaslt_version' ] } (the repo has: { ', ' . join ( map ( str , have )) or 'none' } ): plain hipBLAS there" ) 188"hipBLASLt tuning tables:" ,", " .join (tables )or "none" ) 189 190z = a .out / ASSET 191z .unlink (missing_ok = True ) 192with zipfile .ZipFile (z ,"w" ,zipfile .ZIP_DEFLATED ,compresslevel = 9 )as f : 193for p in sorted (stage .rglob ("*" )): 194if p .is_file (): 195f .write (p ,p .relative_to (stage ).as_posix ()) 196size = sum (p .stat ().st_size for p in stage .rglob ("*" )if p .is_file ()) 197f" { z } : { z . stat (). st_size / 2 ** 20 :.0f } MiB ( { size / 2 ** 20 :.0f } MiB unpacked), engine { version } , " 198f" { ', ' . join ( archs ) } , ROCm { a . rocm_version } , DLLs: { ', ' . join ( shipped ) } " ) 199return 0 200 201 202if __name__ == "__main__" : 203sys .exit (main ())