62 lines
3.2 KiB
Python
62 lines
3.2 KiB
Python
#!/usr/bin/env python3
|
|
"""Install official Strata CPU image encoder; never restart or change a worker."""
|
|
import argparse
|
|
import hashlib
|
|
import json
|
|
import os
|
|
from pathlib import Path
|
|
import shutil
|
|
import subprocess
|
|
import urllib.request
|
|
|
|
SOURCE = json.loads((Path(__file__).resolve().parent.parent / 'strata_sources.json').read_text())['vision']
|
|
|
|
def digest(path):
|
|
with path.open('rb') as stream:
|
|
return hashlib.file_digest(stream, 'sha256').hexdigest()
|
|
|
|
def install(runtime, storage):
|
|
runtime, storage = Path(runtime).resolve(), Path(storage).resolve()
|
|
source = runtime / 'source'
|
|
recipe = json.loads((Path(__file__).resolve().parent.parent / 'strata_sources.json').read_text())
|
|
revision = subprocess.check_output(['git', '-C', source, 'rev-parse', 'HEAD'], text=True).strip()
|
|
if revision != recipe['code']:
|
|
raise ValueError('Strata source revision differs from the Deck recipe')
|
|
target = storage / 'vision' / SOURCE['file']
|
|
target.parent.mkdir(parents=True, exist_ok=True)
|
|
valid = target.is_file() and target.stat().st_size == SOURCE['size'] and digest(target) == SOURCE['sha256']
|
|
if not valid:
|
|
partial = target.with_suffix('.gguf.partial')
|
|
url = 'https://huggingface.co/{repo}/resolve/{revision}/{file}'.format(**SOURCE)
|
|
with urllib.request.urlopen(url, timeout=120) as response, partial.open('wb') as out:
|
|
shutil.copyfileobj(response, out, 1024 * 1024)
|
|
if partial.stat().st_size != SOURCE['size'] or digest(partial) != SOURCE['sha256']:
|
|
partial.unlink(missing_ok=True)
|
|
raise ValueError('Projector size or checksum differs from original source')
|
|
os.replace(partial, target)
|
|
# Upstream may reuse system CMake instead of installing it into the venv.
|
|
python = source / '.venv/bin/python'
|
|
env = dict(os.environ, PATH=str(python.parent) + os.pathsep + os.environ.get('PATH', ''))
|
|
tools = subprocess.check_output([str(python), '-c',
|
|
'import setup,json; print(json.dumps(list(map(str,setup.ensure_build_tools()))))'],
|
|
cwd=source, env=env, text=True)
|
|
cmake, ninja = map(Path, json.loads(tools.strip().splitlines()[-1]))
|
|
build = source / 'build-vision-cpu'
|
|
subprocess.run([str(cmake), '-S', str(source / 'tools/vision'), '-B', str(build),
|
|
'-G', 'Ninja', '-DCMAKE_MAKE_PROGRAM=' + str(ninja), '-DCMAKE_BUILD_TYPE=Release',
|
|
'-DLLAMA_DIR=' + str(source / 'third_party/llama.cpp'),
|
|
'-DSTRATA_VISION_CUDA=OFF', '-DSTRATA_PORTABLE=ON'], check=True, env=env)
|
|
subprocess.run([str(cmake), '--build', str(build), '--target', 'strata-vision', '-j', '2'], check=True, env=env)
|
|
executable = source / 'engine/strata-vision'
|
|
shutil.copy2(build / 'bin/strata-vision', executable)
|
|
manifest = dict(SOURCE, code=revision, device='cpu', executable=str(executable), path=str(target))
|
|
(runtime / 'vision-ready.json').write_text(json.dumps(manifest, indent=2))
|
|
print(json.dumps(manifest))
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--runtime', required=True)
|
|
parser.add_argument('--storage', required=True)
|
|
args = parser.parse_args()
|
|
install(args.runtime, args.storage)
|