Files
Spellcast3d/code/spellcast/install/preflight.py

401 lines
21 KiB
Python

"""Checks before installing: Python, GPU + driver, disk, install path, the system's runtime libraries (Windows: the
VC++ runtime; Linux: its C library and the shared libraries the tools load), internet. STDLIB ONLY.
A problem either stops everything (fatal) or blocks only the components it concerns
(blocked, e.g. no NVIDIA GPU blocks the image generator and Pixal3D but not the hub), so the hub can
still start and show what is missing. "Uses the GPU" is not "needs PyTorch": Qwen-Image 2.1 runs on
stable-diffusion.cpp, so download.pytorch.org and pip's room for PyTorch do not concern it.
"""
from __future__ import annotations
import math
import os
import platform
import shutil
import subprocess
import sys
import sysconfig
import tempfile
import urllib.error
import urllib.request
from dataclasses import dataclass, field
from pathlib import Path
from .. import certs, gpu, paths
from . import submodules, venvs
from .components import PROBE_URLS, Component, git_exe, torch_build
from .runner import disk_of, pip_cache_dir
# Deepest installed files sit ~170 characters below the root; Windows' limit is 260.
MAX_ROOT_LEN = 60
DISK_MARGIN_GB = 1.0
SIMPLE_PATH = "C:\\spellcast3d" if os.name == "nt" else "~/spellcast3d" # (what the messages suggest)
CACHE_MIN_GB = 5.0 # pip keeps the 3.5 GB PyTorch download in its cache (%LOCALAPPDATA%\pip\cache)
VC_REDIST_URL = "https://aka.ms/vs/17/release/vc_redist.x64.exe"
DRIVER_URL = "https://www.nvidia.com/drivers"
# Linux: the prebuilt programs and wheels are built on Ubuntu 22.04, whose C library this is.
GLIBC_MIN = (2, 35)
# Linux: shared libraries a tool loads from the system - the library, the Debian / Ubuntu package that brings it,
# the tools that cannot work without it. Only what a tool's own binaries name as needed (Qwen-Image's engine: GCC's
# OpenMP runtime; Forge Neo: its OpenCV's desktop libraries) or what a tool was SEEN to fail for on a minimal system
# (TriFlow: the open3d that TRELLIS imports loads X11 and OpenGL, the pyglet under its renderer loads GLU) belongs
# here: a library named on a guess keeps a tool from installing for nothing.
SYSTEM_LIBRARIES: tuple[tuple[str, str, tuple[str, ...]], ...] = (
("libgomp.so.1", "libgomp1", (paths.QWEN,)),
("libX11.so.6", "libx11-6", (paths.TRIFLOW, paths.FORGE)),
("libGL.so.1", "libgl1", (paths.TRIFLOW, paths.FORGE)),
("libGLU.so.1", "libglu1-mesa", (paths.TRIFLOW,)),
("libglib-2.0.so.0", "libglib2.0-0", (paths.FORGE,)),
)
# Linux: the tools whose GPU kernels are Triton's (PyTorch's own dependency there). Triton builds a small launcher in
# C for each kernel at its first use, with the system's compiler, and links it to the NVIDIA driver's library, so
# these tools cannot work without a compiler - and the Debian / Ubuntu package that brings one with the C library's
# headers. (LATO.2 has Triton in its venv and never calls it.)
TRITON_TOOLS = (paths.PIXAL3D, paths.TRIFLOW)
C_COMPILER_PACKAGE = "build-essential"
DRIVER_LIBRARY = "libcuda.so.1"
@dataclass
class Report:
info: list[str] = field(default_factory=list)
warnings: list[str] = field(default_factory=list)
fatal: list[str] = field(default_factory=list)
blocked: dict[str, str] = field(default_factory=dict) # component id -> reason
def block(self, components: list[Component], reason: str) -> None:
for c in components:
self.blocked.setdefault(c.id, reason)
def run(todo: list[Component], gpu_info: gpu.GpuInfo | None, *, network: bool,
reinstall: tuple[str, ...] = ()) -> Report:
"""todo = the components that are about to be installed; reinstall = the ids of those whose venvs are deleted
and made again (--reinstall)."""
r = Report()
_check_python(r)
_check_folder(r, todo)
if os.name == "nt":
_check_vc_runtime(r)
else:
_check_system(r, todo)
_check_gpu(r, todo, gpu_info)
_check_disk(r, todo, reinstall)
if network and todo:
_check_internet(r, todo)
return r
def _check_python(r: Report) -> None:
r.info.append(f"Python {platform.python_version()} ({sys.executable})")
if sys.version_info[:2] != (3, 11) or sys.maxsize <= 2**32:
r.fatal.append(f"Spellcast3D needs 64-bit Python 3.11, but this is Python {platform.python_version()} "
f"({platform.architecture()[0]}). Start Spellcast3D with {paths.LAUNCHER}: it uses the "
f"Python in {paths.shown(paths.PORTABLE_PYTHON_DIR)}.")
def _long_paths_enabled() -> bool:
try:
import winreg
with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, r"SYSTEM\CurrentControlSet\Control\FileSystem") as key:
return winreg.QueryValueEx(key, "LongPathsEnabled")[0] == 1
except OSError:
return False
def ansi_safe(path: str) -> bool:
"""True when `path` survives Windows' ANSI code page ("language for non-Unicode programs").
Programs that take narrow (char) paths - Pixal3D's GPU kernel compilers (triton's tcc.exe and
ptxas.exe) among them - see other letters replaced by '?' or a look-alike ('Łukasz' becomes
'Lukasz' on English Windows) and cannot find their files. Accented letters of the user's own
language (José on English Windows) are fine.
"""
try:
path.encode("mbcs", "strict")
except UnicodeEncodeError:
return False
except LookupError: # not Windows
return True
return True
def _check_folder(r: Report, todo: list[Component]) -> None:
root = str(paths.ROOT)
r.info.append(f"Install folder: {root}")
if not ansi_safe(root):
problem = (f"The install folder path ({root}) contains letters that Windows' 'language for non-Unicode "
f"programs' cannot represent. Pixal3D cannot build its GPU code there, so making 3D models "
f"would fail. Move the Spellcast3D folder to a simple path such as {SIMPLE_PATH} and start "
f"again.")
# Fatal before anything is downloaded: the venvs cannot simply be moved afterwards (they record
# their absolute path), so everything would have to be installed again.
if any(c.id == paths.PIXAL3D for c in todo):
r.fatal.append(problem)
else:
r.warnings.append(problem)
odd = sorted({ch for ch in root if ch in ("&%!^" if os.name == "nt" else "\"'`$\\")})
if odd:
r.warnings.append(f"The install folder path contains special characters ({' '.join(odd)}). Some of the "
f"tools cannot handle them, even when the installation itself works, and Spellcast3D "
f"may not start by double-click. Move the Spellcast3D folder to a simple path such as "
f"{SIMPLE_PATH} and start again.")
if os.name == "nt" and len(root) > MAX_ROOT_LEN and not _long_paths_enabled():
r.fatal.append(f"The install folder path is too long ({len(root)} characters; at most {MAX_ROOT_LEN} "
f"work). Some files are installed about 170 characters deeper, and Windows cannot handle "
f"paths longer than 260 characters. Move the Spellcast3D folder to a short path such as "
f"{SIMPLE_PATH} and start again.")
_check_triflow_path(r, todo)
try:
with tempfile.TemporaryFile(dir=paths.CODE):
pass
except OSError:
r.fatal.append(f"Spellcast3D cannot write into its own folder ({paths.CODE}). Do not put it inside "
f"'Program Files' or another protected folder; move it to e.g. {SIMPLE_PATH}.")
def _check_triflow_path(r: Report, todo: list[Component]) -> None:
"""TriFlow's parts are downloaded with git into folders ~165 characters below the root (submodules.py): in a git
copy with a longer root path than TRIFLOW_MAX_ROOT_LEN that download fails, so TriFlow alone is blocked."""
triflow = [c for c in todo if c.id == paths.TRIFLOW]
root = str(paths.ROOT)
if (os.name != "nt" or not triflow or len(root) <= submodules.TRIFLOW_MAX_ROOT_LEN
or not (paths.ROOT / ".git").exists()
or not submodules.triflow_checkout_work(git_exe())):
return
r.block(triflow, f"TriFlow cannot be downloaded into this folder because its path is too long ({len(root)} "
f"characters). Git keeps TriFlow's innermost part about {len(submodules.TRIFLOW_GIT_DEEPEST)} "
f"characters deeper, and Windows cannot handle paths longer than 260 characters there. Move the "
f"Spellcast3D folder to a shorter path (at most {submodules.TRIFLOW_MAX_ROOT_LEN} characters, "
f"such as C:\\spellcast3d) and start again. The other tools are not affected.")
def _check_vc_runtime(r: Report) -> None:
folders = [paths.PORTABLE_PYTHON_DIR, Path(sys.executable).parent,
Path(os.environ.get("SystemRoot", r"C:\Windows")) / "System32"]
if not any((folder / "msvcp140.dll").exists() for folder in folders):
r.fatal.append(f"The Microsoft Visual C++ Redistributable is not installed (msvcp140.dll is missing); "
f"PyTorch needs it. Install it from {VC_REDIST_URL} and start again.")
def system_libraries() -> dict[str, str] | None:
"""The shared libraries the system's loader knows (ldconfig's cache): {name: its file, the first listed}; None
when it cannot be asked."""
try:
listed = subprocess.run(["ldconfig", "-p"], capture_output=True, text=True, timeout=30,
env={**os.environ, "PATH": os.environ.get("PATH", "") + os.pathsep + "/sbin:/usr/sbin"})
except (OSError, subprocess.SubprocessError):
return None
if listed.returncode != 0:
return None
known: dict[str, str] = {}
for line in listed.stdout.splitlines()[1:]:
words = line.split()
if words:
known.setdefault(words[0], words[-1])
return known
def c_compiler_problem(driver_library: str | None = None) -> str | None:
"""Linux: why Triton could not build its launchers here, with what to do about it (None: it can). The compiler
is found in Triton's own order - $CC, gcc, clang - and tried on what Triton builds: a shared object from C that
includes Python's header and is linked to the NVIDIA driver's library by its plain name (driver_library: that
library's file as the loader knows it; None: no driver is found, which the card's check reports)."""
compiler = os.environ.get("CC") or shutil.which("gcc") or shutil.which("clang")
install = f"Install one (Ubuntu / Debian: sudo apt install {C_COMPILER_PACKAGE})"
if not compiler:
return f"no C compiler (gcc or clang) was found. {install}"
with tempfile.TemporaryDirectory(prefix="spellcast-cc-") as folder:
source = Path(folder) / "probe.c"
source.write_text("#include <Python.h>\nint probe(void) { return 0; }\n", encoding="utf-8")
argv = [compiler, str(source), "-O3", "-shared", "-fPIC", "-o", str(Path(folder) / "probe.so"),
f"-I{sysconfig.get_paths()['include']}"]
folders = Path(driver_library).parent if driver_library else None
try:
built = subprocess.run(argv, capture_output=True, text=True, timeout=120)
linked = (subprocess.run([*argv, "-lcuda", f"-L{folders}"], capture_output=True, text=True, timeout=120)
if built.returncode == 0 and folders is not None else built)
except (OSError, subprocess.SubprocessError) as e:
return f"the C compiler {compiler} could not be run ({e}). {install}"
if built.returncode != 0:
said = (built.stderr.strip().splitlines() or ["no message"])[0]
return f"the C compiler {compiler} could not build a small test ({said[:200]}). {install}"
if linked.returncode != 0:
plain = Path(driver_library).with_name("libcuda.so") # type: ignore[arg-type]
return (f"the linker does not find the NVIDIA driver's library by its plain name ({plain} is missing beside "
f"{Path(driver_library).name}). Add that name (sudo ln -s {Path(driver_library).name} {plain})") # type: ignore[arg-type]
return None
def _check_system(r: Report, todo: list[Component]) -> None:
"""Linux: a 64-bit Intel / AMD processor, a C library as new as the prebuilt files need, a C compiler for the
tools that run Triton's kernels, and the shared libraries the tools load. A missing compiler or library blocks
only the tools that need it."""
machine = platform.machine().lower()
if machine not in ("x86_64", "amd64"):
r.fatal.append(f"Spellcast3D's tools are built for 64-bit Intel / AMD processors (x86_64), but this computer "
f"has a {machine or 'different'} one.")
name, version = platform.libc_ver()
numbers = tuple(int(part) for part in version.split(".")[:2] if part.isdigit())
r.info.append(f"System: {platform.system()} {platform.release()}, {name or 'C library'} {version or 'unknown'}")
if name != "glibc" or len(numbers) < 2 or numbers < GLIBC_MIN:
r.fatal.append(f"Spellcast3D's tools need a Linux with glibc {GLIBC_MIN[0]}.{GLIBC_MIN[1]} or newer (Ubuntu "
f"22.04, Debian 12, Fedora 36 or later), but this system has {name or 'another C library'} "
f"{version}.".rstrip(" ."))
return
known = system_libraries()
compiling = [c.id for c in todo if c.id in TRITON_TOOLS]
problem = c_compiler_problem((known or {}).get(DRIVER_LIBRARY)) if compiling else None
for c in todo: # a tool is told everything it lacks at once
needs = []
if problem and c.id in compiling:
needs.append(f"This tool prepares its GPU programs with a C compiler at their first use, and {problem}.")
lacking = [(library, package) for library, package, tools in SYSTEM_LIBRARIES
if known is not None and c.id in tools and library not in known]
if lacking:
libraries, packages = (" ".join(names) for names in zip(*lacking))
needs.append(f"Missing on this system: {libraries}. Install (Ubuntu / Debian: sudo apt install "
f"{packages}).")
if needs:
r.block([c], " ".join(needs) + " Then start Spellcast3D again.")
def _check_gpu(r: Report, todo: list[Component], gpu_info: gpu.GpuInfo | None) -> None:
needs_gpu = [c for c in todo if c.uses_gpu]
if gpu_info is None:
msg = ("No NVIDIA graphics card was found (or its driver is not installed). Image and 3D generation "
f"need an NVIDIA GPU. If you have one, install its latest driver from {DRIVER_URL} and start again.")
if needs_gpu:
r.block(needs_gpu, msg)
else:
r.warnings.append(msg)
return
r.info.append(f"GPU: {gpu_info.name}, {gpu_info.vram_total_mb / 1024:.0f} GB, "
f"compute capability {gpu_info.compute_cap:.1f}, driver {gpu_info.driver_version}")
others = [card for card in gpu.query_gpus() if card.index != gpu_info.index]
if others:
decides = (f"This computer's CUDA_VISIBLE_DEVICES setting ({gpu.own_devices()}) decides that."
if gpu.own_devices() else f"graphics_card in {paths.CONFIG_FILE.name} picks another.")
r.info.append(f" (this PC has {len(others) + 1} NVIDIA cards; Spellcast3D uses card {gpu_info.index}, "
f"not {', '.join(f'{card.index}: {card.name}' for card in others)}. {decides})")
ok, why = gpu.driver_ok(gpu_info)
if not ok:
r.block(needs_gpu, f"{why} Download it from {DRIVER_URL}, install it, and start again.")
if gpu_info.vram_total_mb < gpu.MADE_FOR_VRAM_MB:
r.warnings.append(f"Your GPU has {gpu_info.vram_total_mb / 1024:.0f} GB of video memory. Spellcast3D is "
f"made for 8 GB or more; generating may fail or be very slow.")
def _check_disk(r: Report, todo: list[Component], reinstall: tuple[str, ...] = ()) -> None:
usage = shutil.disk_usage(paths.ROOT)
free = usage.free / 1e9
disk = disk_of(paths.ROOT)
total_need = 0.0
# A new venv shares its identical files (the same torch) with a venv that has them by hard links: it keeps
# less disk than it needs while installing. That venv may also be one installed earlier in this run.
torch_twin = any(venvs.torch_dist(paths.venv_dir(v)) == torch_build().dist for v in paths.ALL_VENV_IDS)
for c in todo:
if c.id in r.blocked:
continue
# A venv made again needs as much as a first install: its files other venvs share stay on disk through them.
fresh = c.id in reinstall
need = c.disk_needed_gb(fresh)
if need + DISK_MARGIN_GB > free:
# Rounded apart (need up, free down; the margin included): never "needs about 10 GB, but only 10 GB".
r.block([c], f"Not enough free disk space: {c.title} needs about {math.ceil(need + DISK_MARGIN_GB)} GB, "
f"but only {math.floor(max(0.0, free))} GB is free on {disk}. Free up space (or move "
f"the Spellcast3D folder to a bigger drive) and start again.")
else:
kept = c.disk_kept_gb(torch_twin, fresh)
free -= kept
total_need += kept
torch_twin = torch_twin or c.needs_torch
need_text = (f" (this install needs about {total_need:.0f} GB)" if total_need >= 1
else " (this install needs less than 1 GB)" if total_need > 0 else "")
r.info.append(f"Free disk space on {disk} {usage.free / 1e9:.0f} GB{need_text}")
# Temporary files go to code\tmp on this drive (runner.Ctx.env). pip's download cache is elsewhere:
cache = pip_cache_dir()
if any(c.needs_torch and c.id not in r.blocked for c in todo) and not _same_disk(cache, paths.ROOT):
try:
cache_free = shutil.disk_usage(_existing(cache)).free / 1e9
except OSError:
return
if cache_free < CACHE_MIN_GB:
r.warnings.append(f"Only {cache_free:.1f} GB is free on {disk_of(cache)}, where pip keeps its "
f"downloads ({cache}). PyTorch (3.5 GB) cannot be kept there, so it is downloaded "
f"once for each tool that needs it, and a partial file may stay there. Free up "
f"about {CACHE_MIN_GB:.0f} GB there to download it only once.")
def _existing(path: Path) -> Path:
"""The folder itself, or the nearest one above it that exists."""
while not path.exists() and path != path.parent:
path = path.parent
return path
def _same_disk(a: Path, b: Path) -> bool:
if os.name == "nt":
return a.drive.lower() == b.drive.lower()
try:
return os.stat(_existing(a)).st_dev == os.stat(_existing(b)).st_dev
except OSError:
return True
def _reachable(url: str) -> str | None:
"""None when url answers, else a short description of the problem."""
try:
with certs.urlopen(urllib.request.Request(url, method="HEAD"), timeout=20):
return None
except urllib.error.HTTPError as e:
# 404/405 still prove the server is there; 403/407/429/5xx usually mean a proxy or firewall.
return None if e.code in (404, 405) else f"the server answered HTTP {e.code}"
except (urllib.error.URLError, OSError) as e:
return str(getattr(e, "reason", e))
def _advice(problem: str, host: str) -> str:
"""What to do about a server that did not answer: a refused certificate is no connection problem."""
return (certs.refused_words(host) if certs.refused(problem)
else "Check your connection, VPN or firewall and start again.")
def _check_internet(r: Report, todo: list[Component]) -> None:
problem = _reachable("https://pypi.org/simple/pip/")
if problem:
r.fatal.append(f"Could not reach pypi.org, where the Python packages come from ({problem}). Installing "
f"needs the internet. {_advice(problem, 'pypi.org')}")
return
reached = ["pypi.org"]
# Qwen-Image 2.1 uses the GPU without PyTorch: download.pytorch.org must not block it.
needs_torch = [c for c in todo if c.needs_torch and c.id not in r.blocked]
if needs_torch:
problem = _reachable("https://download.pytorch.org/whl/cu128/")
if problem:
r.block(needs_torch, f"Could not reach download.pytorch.org, where PyTorch comes from ({problem}). "
f"{_advice(problem, 'download.pytorch.org')}")
else:
reached.append("download.pytorch.org")
# The other servers the components must download from now (e.g. huggingface.co for AI models, github.com for
# stable-diffusion.cpp): each one checked once, and only for what it is needed for.
by_host: dict[str, list[Component]] = {}
for c in todo:
if c.id not in r.blocked:
for host in sorted(c.download_hosts()):
by_host.setdefault(host, []).append(c)
for host, needing in by_host.items():
if host not in PROBE_URLS:
continue
problem = _reachable(PROBE_URLS[host])
if problem:
r.block(needing, f"Could not reach {host}, where {_WHAT_FROM.get(host, 'some of its files')} come from "
f"({problem}). {_advice(problem, host)}")
else:
reached.append(host)
r.info.append(f"Internet: {', '.join(reached[:-1]) + ' and ' if len(reached) > 1 else ''}{reached[-1]} reachable")
_WHAT_FROM = {"huggingface.co": "its AI models", "github.com": "its program files"}