Files

113 lines
5.1 KiB
Python

"""The prebuilt wheels of the compiled packages the tools need, by system. STDLIB ONLY.
Windows: files of the checkout - code/whl (Spellcast3D's own) and code/pixal3d/whl (the Pixal3D fork's, where its
install.py also downloads flash_attn and natten).
Linux: built from the same sources at the same versions (code/release/wheels/*/build_*_linux.sh) and kept as assets
of one GitHub release, not in git: each is downloaded once, pinned by size and SHA-256 (linux-wheels.json), into
code/venvs/downloads/wheels. One build serves every card: its GPU code covers GTX 10xx .. RTX 50xx, and the PyTorch
of either CUDA flavour loads it (components.torch_build).
"""
from __future__ import annotations
import json
import os
from pathlib import Path
from typing import Callable, Iterable
from .. import paths
from . import models
from .sdcpp import Release
Log = Callable[[str], None]
WINDOWS = os.name == "nt"
PIXAL3D_WHL = paths.PIXAL3D_DIR / "whl"
# Windows' wheels that a component names by file (the others are found by their folder: Pixal3D's install.py takes
# every wheel of code/pixal3d/whl, Instant Meshes the newest instant_meshes_brush of code/whl).
WINDOWS_FILES: dict[str, Path] = {
"flash_attn": PIXAL3D_WHL / "flash_attn-2.8.3+cu128torch2.8-cp311-cp311-win_amd64.whl",
"o_voxel": PIXAL3D_WHL / "o_voxel-0.0.1-cp311-cp311-win_amd64.whl",
"torchsparse": paths.WHL / "torchsparse-2.1.0+cu128torch2.8-cp311-cp311-win_amd64.whl",
"pyfqmr_triflow": paths.WHL / "pyfqmr_triflow-0.5.0+triflow-cp311-cp311-win_amd64.whl",
"xatlas": paths.WHL / "xatlas-0.0.11+spellcast3d.2-cp311-cp311-win_amd64.whl",
}
# Of WINDOWS_FILES, what is not a file of the fork's checkout: too big for git, Pixal3D's own installer downloads
# it into code/pixal3d/whl, so it is there once Pixal3D is installed.
PIXAL3D_DOWNLOADS: tuple[str, ...] = ("flash_attn",)
# Linux: the release the wheels are assets of, where they are kept once downloaded, and their pins
# ({project: {"file", "bytes", "sha256"}}).
LINUX_RELEASE = Release("IgorAherne/Pixal3D", "wheels")
LINUX_DIR = paths.DOWNLOADS / "wheels"
LINUX_PINS_FILE = Path(__file__).with_name("linux-wheels.json")
# The wheels Pixal3D's venv gets on Linux (its install.py installs every wheel of the folder it is given).
PIXAL3D_LINUX = ("cumesh", "flex_gemm", "nvdiffrast", "nvdiffrec_render", "o_voxel", "pixal3d_kernels",
"sageattention", "flash_attn", "natten")
def _linux_pins() -> dict[str, tuple[str, int, str]]:
try:
raw = json.loads(LINUX_PINS_FILE.read_text(encoding="utf-8"))
return {project: (str(pin["file"]), int(pin["bytes"]), str(pin["sha256"])) for project, pin in raw.items()}
except (OSError, ValueError, KeyError, TypeError, AttributeError):
return {}
LINUX: dict[str, tuple[str, int, str]] = _linux_pins()
def file(project: str) -> Path:
"""This system's wheel of `project`: Windows' in the checkout, Linux' where fetch() puts it."""
if WINDOWS:
return WINDOWS_FILES[project]
return LINUX_DIR / (LINUX[project][0] if project in LINUX else f"{project}-not-pinned-linux_x86_64.whl")
def pin(project: str) -> str:
"""What says which build of `project` this system installs, for a component's fingerprint (Linux: its pin)."""
return "{}={}".format(*LINUX[project][::2]) if project in LINUX else f"{project}=unpinned"
def url(project: str) -> str:
return LINUX_RELEASE.url + LINUX[project][0].replace("+", "%2B")
def missing(projects: Iterable[str]) -> list[str]:
"""Linux: those of `projects` whose wheel is not downloaded at its size (stat calls); elsewhere none."""
if WINDOWS:
return []
found = []
for project in projects:
try:
if project in LINUX and os.stat(file(project)).st_size == LINUX[project][1]:
continue
except OSError:
pass
found.append(project)
return found
def fetch(projects: Iterable[str], log: Log) -> None:
"""Linux: download the wheels of `projects` that are not there (resumed, SHA-256 checked; one already there is
hashed once and remembered). Elsewhere nothing: the wheels are files of the checkout. Raises OSError
(models.ChecksumError: downloaded in full, but not that file)."""
if WINDOWS:
return
for project in projects:
if project not in LINUX:
raise OSError(f"this copy of Spellcast3D names no Linux build of {project} "
f"({paths.shown(LINUX_PINS_FILE)})")
name, size, sha256 = LINUX[project]
path = file(project)
if project not in missing([project]):
if models.is_recorded(path, sha256) or models.sha256_file(path) == sha256:
models.record_verified(path, sha256)
continue
log(f"{name} in {paths.shown(LINUX_DIR)} is not the expected file (its SHA-256 differs): it is "
f"downloaded again.")
path.unlink()
log(f"Downloading {name} ({max(1, round(size / 1e6))} MB) from GitHub ...")
path.parent.mkdir(parents=True, exist_ok=True)
models.download_file(url(project), path, size, sha256, log)