46 lines
4.0 KiB
Python
46 lines
4.0 KiB
Python
#!/usr/bin/env python3
|
|
"""Native Strata build. No Docker, driver changes, root requirement or service restart."""
|
|
import argparse,json,os,re,shutil,subprocess,sys
|
|
from pathlib import Path
|
|
ROOT=Path(__file__).resolve().parent.parent
|
|
RECIPE=json.loads((ROOT/'strata_sources.json').read_text())
|
|
def run(args,cwd=None,env=None):subprocess.run([str(x) for x in args],cwd=cwd,env=env,check=True)
|
|
def install(destination,nvcc=None,architectures='86;120',update=False):
|
|
destination=Path(destination).resolve()
|
|
nvcc=nvcc or os.environ.get('STRATA_NVCC') or shutil.which('nvcc')
|
|
if not nvcc or not re.search(r'release 13\.0\b',subprocess.check_output([nvcc,'--version'],text=True)):
|
|
from strata_toolkit import toolkit
|
|
nvcc=str(toolkit(destination/'cuda'))
|
|
version=subprocess.check_output([nvcc,'--version'],text=True)
|
|
if not re.search(r'release 13\.0\b',version):raise ValueError('Strata-Rezept benötigt CUDA Toolkit 13.0; vorhandene andere Toolkits bleiben unverändert.')
|
|
for tool in ('git','g++','make'):
|
|
if not shutil.which(tool):raise ValueError('Native Build-Abhängigkeit fehlt: '+tool)
|
|
if not re.fullmatch(r'\d+(;\d+)*',architectures):raise ValueError('Ungültige CUDA-Architekturen.')
|
|
destination.mkdir(parents=True,exist_ok=True)
|
|
source=destination/'source'
|
|
if not source.exists():
|
|
run(['git','init',source]);run(['git','-C',source,'fetch','--depth','1','https://github.com/Niko1221/Strata.git',RECIPE['code']]);run(['git','-C',source,'checkout','--detach','FETCH_HEAD'])
|
|
if subprocess.check_output(['git','-C',source,'rev-parse','HEAD'],text=True).strip()!=RECIPE['code']:
|
|
if not update:raise ValueError('Strata-Quellversion stimmt nicht mit Rezept überein. Für bewusstes direktes Update --update verwenden.')
|
|
if subprocess.check_output(['git','-C',source,'status','--porcelain','--untracked-files=no'],text=True).strip():raise ValueError('Lokale Strata-Quelländerungen zuerst prüfen.')
|
|
(destination/'ready.json').unlink(missing_ok=True)
|
|
run(['git','-C',source,'fetch','--depth','1','https://github.com/Niko1221/Strata.git',RECIPE['code']])
|
|
run(['git','-C',source,'checkout','--detach','FETCH_HEAD'])
|
|
# This source is downloaded by setup, not model data. Replace it with the new pinned dependency.
|
|
shutil.rmtree(source/'third_party/llama.cpp',ignore_errors=True)
|
|
|
|
python=source/'.venv/bin/python'
|
|
if not python.is_file():run([sys.executable,'-m','venv',source/'.venv'])
|
|
run([python,'-m','pip','install','--no-cache-dir','-r',source/'requirements.txt'])
|
|
env=dict(os.environ,PATH=str(python.parent)+os.pathsep+str(Path(nvcc).parent)+os.pathsep+os.environ.get('PATH',''),CUDAToolkit_ROOT=str(Path(nvcc).parent.parent),LD_LIBRARY_PATH=str(Path(nvcc).parent.parent/'lib')+os.pathsep+str(Path(nvcc).parent.parent/'lib64')+os.pathsep+os.environ.get('LD_LIBRARY_PATH',''),STRATA_NVCC=str(nvcc),CMAKE_BUILD_PARALLEL_LEVEL='2')
|
|
run([python,'-c','import setup;setup.get_llama_cpp()'],source,env)
|
|
# Upstream owns compiler options, companion libraries and BUILD.json. This
|
|
# prevents a later official package check from treating our local build as
|
|
# unknown and trying to install another toolkit or rebuilding it differently.
|
|
run([python,'-c','import json,setup,sys; archs=json.loads(sys.argv[1]); setup.build_engine({"arch":archs[0],"archs":archs},"none",False,setup.get_llama_cpp(),toolkit=13)',json.dumps([int(x) for x in architectures.split(';')])],source,env)
|
|
version_output=subprocess.check_output([source/'engine/strata','--version'],text=True,stderr=subprocess.STDOUT)
|
|
if RECIPE['version'] not in version_output:raise ValueError('Gebautes Strata-Binary meldet nicht die erwartete Version.')
|
|
(destination/'ready.json').write_text(json.dumps(dict(commit=RECIPE['code'],cuda='13.0',architectures=architectures,origin='native-build')))
|
|
if __name__=='__main__':
|
|
p=argparse.ArgumentParser();p.add_argument('--destination',required=True);p.add_argument('--nvcc');p.add_argument('--architectures',default='86;120');p.add_argument('--update',action='store_true');a=p.parse_args();install(a.destination,a.nvcc,a.architectures,a.update)
|