diff --git a/.ci/manywheel/build.sh b/.ci/manywheel/build.sh index 8a0b0dbd825c..47d35ce561b9 100755 --- a/.ci/manywheel/build.sh +++ b/.ci/manywheel/build.sh @@ -27,6 +27,36 @@ case "${GPU_ARCH_TYPE:-BLANK}" in cd "${PYTORCH_ROOT}" python3 "${SCRIPTPATH}/build_wheel.py" "${RAW_WHEEL_DIR}" + + # native-AOT stage 2, BEFORE the repair so the relinked libtorch_cuda goes + # through patchelf like the rest of the wheel. The kernel builders import + # torch, so the raw wheel is installed first. + # + # CUDA arch types only: this arm also serves cpu/xpu/rocm, where stage 2 can + # only skip. + if [[ "${GPU_ARCH_TYPE}" == cuda* ]]; then + # nullglob so an empty directory counts as zero, not the pattern. + naot_wheels=() + shopt -s nullglob + naot_wheels=("${RAW_WHEEL_DIR}"/*.whl) + shopt -u nullglob + if [[ ${#naot_wheels[@]} -ne 1 ]]; then + echo "native-AOT: expected one raw wheel in ${RAW_WHEEL_DIR}, found ${#naot_wheels[@]}" >&2 + exit 1 + fi + RAW_WHEEL="${naot_wheels[0]}" + # --no-deps --no-index: Requires-Dist would pull GBs of CUDA wheels. + python3 -m pip install --progress-bar off --no-deps --no-index "${RAW_WHEEL}" + # Sourced for install_cutlass_dsl, so the DSL pin lives in one place. + source "${PYTORCH_ROOT}/.ci/pytorch/common_utils.sh" + # Stage 2 owns the verdict (see .ci/pytorch/build.sh for why a separate + # probe here was wrong). + if [[ "$(python3 tools/native_aot/build_stage2.py --print-verdict)" == "RUN" ]]; then + install_cutlass_dsl + fi + python3 tools/native_aot/build_stage2.py --wheel "${RAW_WHEEL}" + fi # GPU_ARCH_TYPE == cuda* + python3 "${SCRIPTPATH}/repair_wheel.py" "${RAW_WHEEL_DIR}" "${PYTORCH_FINAL_PACKAGE_DIR}" ;; cpu-s390x) diff --git a/.ci/pytorch/build.sh b/.ci/pytorch/build.sh index 87e8cc67b9f1..7e69839965b4 100755 --- a/.ci/pytorch/build.sh +++ b/.ci/pytorch/build.sh @@ -283,6 +283,32 @@ if [[ "$BUILD_ENVIRONMENT" != *libtorch* ]]; then fi pip_install_whl "$(echo dist/*.whl)" + # native-AOT stage 2: export DSL kernels, relink torch_cuda with them embedded, and + # patch the library back into the wheel test jobs get (tools/native_aot/build_stage2.py). + # + # CUDA-only, as in .ci/manywheel/build.sh: --wheel makes stage 2 refuse a torch that + # does not import, and in the ASan and TSan images `import torch` cannot work. + if [[ "$BUILD_ENVIRONMENT" == *cuda* ]]; then + # Installed HERE, not in .ci/docker/requirements-ci.txt, which every image + # shares: that would put ~190 MB of CUDA-only tooling into the CPU/ROCm/XPU images. + # + # ONE owner of the decision: stage 2 prints the verdict, we install only on RUN. + if [[ "$(python tools/native_aot/build_stage2.py --print-verdict)" == "RUN" ]]; then + install_cutlass_dsl + fi + # One wheel expected; a stale second would be glued into one argument. nullglob + # so an empty dist/ counts as zero. + naot_wheels=() + shopt -s nullglob + naot_wheels=(dist/*.whl) + shopt -u nullglob + if [[ ${#naot_wheels[@]} -ne 1 ]]; then + echo "native-AOT: expected exactly one wheel in dist/, found ${#naot_wheels[@]}" >&2 + exit 1 + fi + python tools/native_aot/build_stage2.py --wheel "${naot_wheels[0]}" + fi # BUILD_ENVIRONMENT == *cuda* + # Smoke-test tools/build_with_debinfo.py against the real build tree: it must # still emit a debug-rebuild plan with a -g compile and the libtorch_python # relink. This guards against build-system changes (e.g. a new diff --git a/.spin/cmds.py b/.spin/cmds.py index 24b252af9fa9..0993bcb79e12 100644 --- a/.spin/cmds.py +++ b/.spin/cmds.py @@ -572,6 +572,14 @@ def _pip_install_cmd(editable): return cmd + [".", "-v", "--no-build-isolation"] +def _native_aot_stage2(): + # Post-install: the kernel builders import the installed torch, and + # scikit-build-core has no post-build hook inside the PEP 517 backend. Skips when + # AOT does not apply, but a machine with an exportable GPU needs the DSL wheels + # installed, or TORCH_NATIVE_AOT=0. + spin.util.run([sys.executable, "tools/native_aot/build_stage2.py"]) + + @click.command() def develop(): """Build PyTorch (editable install). @@ -582,6 +590,7 @@ def develop(): CMakeLists.txt and the supported env vars in cmake/EnvVarForwarding.cmake. """ spin.util.run(_pip_install_cmd(editable=True)) + _native_aot_stage2() # Alias so `spin editable` also works. @@ -599,6 +608,7 @@ def install(): CMakeLists.txt and the supported env vars in cmake/EnvVarForwarding.cmake. """ spin.util.run(_pip_install_cmd(editable=False)) + _native_aot_stage2() PYREFLY_LINTER_SCRIPT = CWD / "tools" / "linter" / "adapters" / "pyrefly_linter.py" diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 081810abb4ab..a7007d69ee13 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -819,6 +819,7 @@ On the initial build, you can also speed things up by disabling the features you - `USE_PYTORCH_QNNPACK=0` will disable PyTorch's internal QNNPACK quantized kernels. - `USE_CPU_VECTORIZATION=0` will disable building vectorized CPU kernel variants (AVX2, AVX512, VSX, ZVECTOR, SVE). Only the scalar DEFAULT kernels are built. Fine for correctness/dispatch work; not for CPU benchmarking. - `USE_COLORIZE_OUTPUT=1` will colorize compiler output for easier reading. +- `TORCH_NATIVE_AOT=0` will disable the native-AOT stage-2 step (exporting the DSL kernels and embedding them into `libtorch_cuda`; see `tools/native_aot/build_stage2.py`, whose module docstring lists these in the order they are checked). Stage 2 already skips itself when the platform is not Linux, when the built torch does not import or was built without CUDA, when no toolchain targets this backend, when CUDA is older than 13 or cannot be determined, when the interpreter has no published DSL wheel and none is installed, when `BUILD_SHARED_LIBS=OFF` leaves a static `torch_cuda` that cannot take the version script, when nothing declares kernels, and when no supported arch is targeted -- note that with `TORCH_CUDA_ARCH_LIST` unset it exports for whatever GPU is present, so a machine with a supported GPU does not hit that last one. Once it decides it *will* export, a missing DSL wheel is a hard error rather than a skip, so this is the switch to use when you want a build without the DSL toolchain installed. The full list of build environment variables, what each one does, and how it reaches CMake is documented at the top of [`cmake/EnvVarForwarding.cmake`](./cmake/EnvVarForwarding.cmake). diff --git a/caffe2/CMakeLists.txt b/caffe2/CMakeLists.txt index 8527697898af..df6745517241 100644 --- a/caffe2/CMakeLists.txt +++ b/caffe2/CMakeLists.txt @@ -1038,6 +1038,11 @@ elseif(USE_CUDA) torch_compile_options(torch_cuda) # see cmake/public/utils.cmake target_compile_definitions(torch_cuda PRIVATE USE_CUDA) + # native-AOT: embed the AOT-exported DSL kernels into torch_cuda. ONE include and no + # logic here: tools/native_aot/gen_aot_lib.py decides everything and emits this file. + # OPTIONAL, so a tree that embeds nothing does nothing. + include("${CMAKE_BINARY_DIR}/native_aot/native_aot.cmake" OPTIONAL) + if(USE_CUFILE) target_link_libraries(torch_cuda PRIVATE torch::cufile) target_compile_definitions(torch_cuda PRIVATE USE_CUFILE) diff --git a/cmake/EnvVarForwarding.cmake b/cmake/EnvVarForwarding.cmake index 038bd0479a52..45c0be15d24f 100644 --- a/cmake/EnvVarForwarding.cmake +++ b/cmake/EnvVarForwarding.cmake @@ -26,6 +26,11 @@ # is set) # USE_CUDA=0, BUILD_TEST=0, ... feature toggles, next section # TORCH_CUDA_ARCH_LIST CUDA arches to build for, e.g. "8.0;9.0" +# TORCH_NATIVE_AOT=0 skip the native-AOT stage-2 step (export DSL +# kernels, relink them into libtorch_cuda). Honored by +# both tools/native_aot/build_stage2.py and the +# generated build/native_aot/native_aot.cmake. Not +# forwarded: both sides read the environment directly # # Feature toggles (USE_*/BUILD_*, forwarded by prefix): # USE_CUDA=0 disables CUDA build diff --git a/tools/native_aot/build_stage2.py b/tools/native_aot/build_stage2.py index e9bf138886cd..5d03d77d4582 100644 --- a/tools/native_aot/build_stage2.py +++ b/tools/native_aot/build_stage2.py @@ -3,10 +3,16 @@ generate the stub sources, and relink torch_cuda with them embedded. A post-install step because the kernel builders package-import torch, and scikit-build-core has no post-build hook inside the PEP 517 backend (the wheel -is assembled before torch is importable). +is assembled before torch is importable): + + * CI: .ci/pytorch/build.sh, after installing the built wheel, with --wheel so + the relinked library is patched back into it before test jobs get it + * dev: chained by `spin develop` / `spin install`; after a raw + `pip install -e .`, run `python tools/native_aot/build_stage2.py` Skips -- leaving a normal artifacts-free build -- when AOT kernels are not -applicable. Keep this list in sync with should_run(), which reports each one: +applicable. Keep this list in sync with should_run(), which reports each one, +and with CONTRIBUTING.md, which states them for users: * TORCH_NATIVE_AOT=0 (the environment, else the CMake cache) * not Linux: everything downstream is ELF @@ -28,13 +34,18 @@ Two modes, split by the skip list. TORCH_NATIVE_AOT=0 opts out, and every entry point honours it; it is expected to stay constant across a build's phases. + +Assumes the torch it finds installed is the one this tree just built, which every +caller above arranges, and verifies that rather than repairing it. """ import glob import os +import shutil import subprocess import sys import sysconfig +import zipfile HERE = os.path.dirname(os.path.abspath(__file__)) @@ -44,6 +55,9 @@ BUILD_DIR = os.path.join(REPO, "build") # directory: a contract with cmake/Codegen.cmake, so the path is spelled once. # Joined onto BUILD_DIR at call time, like _cmake_cache_value, so a test can move it. ARCH_LIST_RECORD = os.path.join("native_aot", "arch_list.txt") +# Where caffe2/CMakeLists.txt include()s native_aot.cmake from, as +# ${CMAKE_BINARY_DIR}/native_aot: a change on either side has to move both. +NATIVE_AOT_ARTIFACTS_DIR = os.path.join(BUILD_DIR, "native_aot") # See export.py: as a script sys.path[0] is this directory, so the repo root # has to go on the path for `tools.native_aot` to import from any cwd. Appended, # not inserted, so the source torch/ tree never shadows the installed wheel. @@ -521,3 +535,465 @@ def _backend() -> str: """ "cuda" or "rocm", as torch reports it. backends.cuda.is_built() is True on ROCm too, so torch.version.hip is the discriminator.""" return "rocm" if _torch_probe("torch.version.hip is not None") else "cuda" + + +def _cache_entries() -> dict[str, tuple[str, str]]: + """This build's cache as name -> (TYPE=value, the doc line above it). + + Last assignment wins, as in _cmake_cache_value. The doc line names the source of an + entry EnvVarForwarding wrote ("From environment", "From env "). + """ + entries: dict[str, tuple[str, str]] = {} + doc = "" + try: + with open(os.path.join(BUILD_DIR, "CMakeCache.txt"), errors="replace") as f: + for line in f: + line = line.rstrip("\n") + if line.startswith("//"): + doc = line[2:] + continue + key, sep, value = line.partition("=") + if sep and not line.startswith("#") and ":" in key: + name, _, kind = key.partition(":") + entries[name] = (f"{kind}={value}", doc) + doc = "" + except OSError: + return {} + return entries + + +# The doc lines EnvVarForwarding writes above an entry it sourced from the environment. +_ENV_SOURCED = ("From environment", "From env ") + + +def _refuse_cache_drift(before: dict[str, tuple[str, str]]) -> None: + """Refuse a reconfigure that changed this build's configuration. + + EnvVarForwarding writes every BUILD_*/USE_*/CMAKE_* environment variable (and the + names in its alias lists, TORCH_CUDA_ARCH_LIST among them) into the cache when it + differs from the cached value, so a stage-2 run in another environment than the + build silently relinks torch_cuda against other settings. It CREATES the entry when + the build had none, so an added env-sourced one is drift too; additions CMake makes + itself are not. + """ + drift = [] + for name, (value, doc) in _cache_entries().items(): + if name in before: + if before[name][0] != value: + drift.append((name, before[name][0], value, doc)) + elif doc.startswith(_ENV_SOURCED): + drift.append((name, "absent", value, doc)) + if not drift: + return + changed = "\n".join( + f" {name}: {old} -> {new}" + (f" ({doc})" if doc else "") + for name, old, new, doc in drift + ) + raise RuntimeError( + f"native-AOT stage 2: reconfiguring {BUILD_DIR} changed this build's " + f"configuration, so torch_cuda would be relinked against different settings " + f"than the rest of this install:\n{changed}\n" + f"Run stage 2 in the same environment as the build (the CI shells and " + f"`spin develop` do), or re-run the build with the settings you want." + ) + + +def _lib_snapshot() -> dict[str, tuple[int, int]]: + """(mtime, size) for every file in the build tree's lib/ except torch_cuda's. + + Stage 2 installs libtorch_cuda.so and nothing else, so these are exactly the + libraries a relink may rebuild but this script will not ship. + """ + lib = os.path.join(BUILD_DIR, "lib") + out = {} + with os.scandir(lib) as entries: + for e in entries: + if e.name == "libtorch_cuda.so" or not e.is_file(follow_symlinks=False): + continue + st = e.stat(follow_symlinks=False) + out[e.name] = (st.st_mtime_ns, st.st_size) + return out + + +def _refuse_unshipped_rebuilds(before: dict[str, tuple[int, int]]) -> None: + """Refuse a relink that rebuilt a library stage 2 does not install. + + `--target torch_cuda` builds its DEPENDENCIES too, so a source edited since the + last full build lands in libtorch_cpu.so (or c10) as well -- and only + libtorch_cuda.so is copied over the installed torch, leaving the two mismatched. + Every supported caller builds everything immediately before this, so a difference + here means stage 2 was run by hand against an edited tree. + """ + after = _lib_snapshot() + rebuilt = sorted(n for n, v in after.items() if before.get(n, v) != v) + if not rebuilt: + return + raise RuntimeError( + f"native-AOT stage 2: relinking torch_cuda also rebuilt " + f"{', '.join(rebuilt[:4])}{' and others' if len(rebuilt) > 4 else ''}, which " + f"stage 2 does not install -- the tree has sources newer than the last full " + f"build, and installing libtorch_cuda.so alone would leave the two " + f"mismatched. Re-run the build (`spin develop`, or `pip install -e .` " + f"followed by this script), which installs them together." + ) + + +def _cmake_for_this_build() -> str: + """The cmake that configured this build tree, else cmake from PATH. + + Recorded as CMAKE_COMMAND, and not necessarily on PATH: a build uses whatever cmake + its environment had, often a pip wheel's rather than /usr/local/bin/cmake. + """ + cached = _cmake_cache_value("CMAKE_COMMAND") + if cached and os.path.exists(cached): + return cached + if cached: + _report(f"{cached} configured this build but is gone; using cmake from PATH") + return "cmake" + + +def _run_child(cmd: list[str], what: str, **kw: object) -> None: + """Run one of stage 2's own steps, naming the step and a signal death on failure, + neither of which check_call reports. The child's own output is inherited.""" + code = subprocess.call(cmd, **kw) # type: ignore[arg-type] + if code == 0: + return + raise RuntimeError( + f"native-AOT stage 2: {what} " + + (f"was killed by signal {-code}" if code < 0 else f"exited {code}") + + " (its output is above). Command: " + + " ".join(cmd) + ) + + +def _installed_lib_dir() -> str: + """The lib/ directory of the INSTALLED torch package. + + Anchored on the compiled _C extension, not torch.__file__: an editable install + serves Python from the source tree while the artifacts live in site-packages.""" + code = ( + "import importlib.util, os\n" + "spec = importlib.util.find_spec('torch._C')\n" + "if spec is not None and spec.origin:\n" + " print('NAOT_VALUE:' + os.path.dirname(spec.origin), flush=True)\n" + ) + # find_spec imports the PARENT package, so this is a full `import torch`: bounded + # and guarded through _run_probe like every other one here. + probe = _run_probe(code, "find_spec('torch._C')") + if probe is not None: + # Marked rather than taking stdout whole: this is joined into a path, so any + # other line the child writes (a warning, a site hook) becomes part of it. + for line in probe.stdout.splitlines(): + if line.startswith("NAOT_VALUE:"): + return os.path.join(line[len("NAOT_VALUE:") :], "lib") + _report_probe_failure("find_spec('torch._C')", probe.stderr, probe.returncode) + why = f"exited {probe.returncode} having printed no path" + ( + " (its stderr is above)" if probe.stderr.strip() else " and nothing at all" + ) + else: + why = "could not be run at all (reported above)" + raise RuntimeError( + f"native-AOT stage 2: cannot locate the installed torch._C, so there is no " + f"torch/lib to copy the relinked library into. The probe {why}. Install the " + f"wheel from this build first." + ) + + +def _copied_member(info: zipfile.ZipInfo) -> zipfile.ZipInfo: + """A ZipInfo for a member copied into a new archive. + + Field by field, not the source's ZipInfo: `info.extra` carries its ZIP64 header + offset, so above zipfile's ZIP64_LIMIT (2 GiB) a copy inherits stale offsets.""" + out = zipfile.ZipInfo(info.filename, info.date_time) + out.compress_type = info.compress_type + out.external_attr = info.external_attr + out.internal_attr = info.internal_attr + out.create_system = info.create_system + out.comment = info.comment + # file_size decides whether open(..., "w") writes a ZIP64 header, so left at 0 it + # raises on exactly the members above the limit. + out.file_size = info.file_size + return out + + +def _write_hashed(dst: zipfile.ZipFile, info: zipfile.ZipInfo, path: str) -> str: + """Stream `path` into `dst` as `info`; return its RECORD `sha256=...,size` fields. + + Hashed from the bytes actually archived, in the same pass: a second read would + double a ~400 MiB read.""" + import base64 + import hashlib + + info.file_size = os.path.getsize(path) # see _copied_member: sizes the header + h = hashlib.sha256() + size = 0 + with open(path, "rb") as f, dst.open(info, "w") as d: + for chunk in iter(lambda: f.read(1 << 20), b""): + h.update(chunk) + size += len(chunk) + d.write(chunk) + digest = base64.urlsafe_b64encode(h.digest()).rstrip(b"=").decode() + return f"sha256={digest},{size}" + + +def patch_wheel(wheel_path: str, lib_path: str) -> None: + """Replace torch/lib/libtorch_cuda.so inside an already-built wheel and fix its + RECORD entry. + + zipfile, not the zip CLI (absent from the manywheel images) or `wheel pack`, which + emits invalid ZIP64 above 4GB (pytorch#189748). + """ + + lib_rel = "torch/lib/libtorch_cuda.so" + with zipfile.ZipFile(wheel_path) as zf: + names = zf.namelist() + records = [n for n in names if n.endswith(".dist-info/RECORD")] + if lib_rel not in names or len(records) != 1: + raise RuntimeError(f"{wheel_path}: not a torch wheel ({lib_rel}/RECORD)") + record_rel = records[0] + record_lines = zf.read(record_rel).decode().splitlines() + + # Located before a byte is written, so a wheel this cannot patch costs nothing. + for lib_line, line in enumerate(record_lines): + if line.startswith(lib_rel + ","): + break + else: + raise RuntimeError(f"{wheel_path}: RECORD has no entry for {lib_rel}") + + # Rebuilt beside the original and renamed over it, so an interrupted rewrite + # cannot replace a valid wheel. + tmp_whl = f"{wheel_path}.naot.{os.getpid()}.tmp" + try: + with ( + zipfile.ZipFile(wheel_path) as src, + zipfile.ZipFile(tmp_whl, "w", allowZip64=True) as dst, + ): + entry = "" + for info in src.infolist(): + # The RECORD last, once the digest is known; every other member + # keeps its position, so .dist-info stays where archivers expect it. + if info.filename == record_rel: + continue + out = _copied_member(info) + if info.filename == lib_rel: + entry = _write_hashed(dst, out, lib_path) + continue + # Streamed, not read()+writestr(): several members run to hundreds + # of MiB and the read form holds each twice. + with src.open(info) as s, dst.open(out, "w") as d: + shutil.copyfileobj(s, d) + record_lines[lib_line] = f"{lib_rel},{entry}" + # The RECORD keeps its original compression: writestr() with a plain + # name would take dst's default, which is none, and store it uncompressed. + dst.writestr( + _copied_member(src.getinfo(record_rel)), + "\n".join(record_lines) + "\n", + ) + shutil.move(tmp_whl, wheel_path) + finally: + if os.path.exists(tmp_whl): + os.remove(tmp_whl) + + +def _invalidate_stale_include() -> None: + """Stop this build tree from embedding what a PREVIOUS run wired up. + + caffe2/CMakeLists.txt include()s the generated file unconditionally, and + .ci/manywheel/build_all.sh shares one build/ across eight interpreters. OVERWRITTEN + rather than deleted: CMake registers an include()d file as a configure dependency + only if it existed at configure time.""" + from tools.native_aot.gen_aot_lib import CMAKE_INCLUDE, write_nothing_to_embed + + art = NATIVE_AOT_ARTIFACTS_DIR + if not os.path.exists(os.path.join(art, CMAKE_INCLUDE)): + return + write_nothing_to_embed(art) + _report(f"a previous run left kernels wired up in {art}; disabled them") + + +def main(argv: list[str] | None = None) -> int: + import argparse + + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument( + "--wheel", + default=None, + help="also embed the relinked libtorch_cuda into this built wheel " + "(CI: the dist/*.whl handed to test jobs must carry the kernels)", + ) + parser.add_argument( + "--print-verdict", + action="store_true", + help="print RUN or SKIP and exit; the shell callers install the DSL " + "runtimes only when this says RUN, so one place owns the decision (their " + "own probe disagreed with this one on GPU-less CUDA builders)", + ) + args = parser.parse_args(argv) + if args.print_verdict: + print("RUN" if should_run() else "SKIP", flush=True) + return 0 + # The explicit opt-out first, and only that: it needs no torch, so + # TORCH_NATIVE_AOT=0 is a kill switch even on the binary-build path. + if _opted_out(): + return 0 # _opted_out() reports the value and where it came from + # --wheel means torch was installed on the line above, so "not importable" is a + # broken build, not "not applicable"; hence ahead of the gates that need torch. + if args.wheel and not _torch_probe("True"): + raise RuntimeError( + "native-AOT stage 2: --wheel was given, so torch was just installed, " + "but it does not import (stderr above). Refusing to patch a wheel " + "without kernels. Fix the install, or drop --wheel for a build where " + "`import torch` cannot work (the ASan and TSan images need LD_PRELOAD " + "or a different interpreter). TORCH_NATIVE_AOT=0 also exempts it." + ) + # ...and only then that it exists, so a typo costs a second rather than a full + # export, reconfigure, relink and copy. + if args.wheel and not os.path.exists(args.wheel): + raise RuntimeError(f"native-AOT stage 2: --wheel {args.wheel} does not exist") + if not should_run(): + _invalidate_stale_include() + return 0 + require_runtimes() + py = sys.executable + art = NATIVE_AOT_ARTIFACTS_DIR + _report("exporting kernels") + export = [py, os.path.join(HERE, "export.py"), "--out-dir", art] + # Read ONCE for both children below, so they cannot be told different arch lists, + # and into the export child's environment even if this build only cached it. + arch_list = _arch_list() + env = dict(os.environ) + if arch_list: + env["TORCH_CUDA_ARCH_LIST"] = arch_list + _run_child(export, "exporting kernels", cwd=REPO, env=env) + _report("generating stub sources") + gen = [py, os.path.join(HERE, "gen_aot_lib.py"), "--artifacts-dir", art] + # The archive the generator names in the CMake it emits. + if archive := _dsl_runtime_archive(): + gen += ["--dsl-runtime", archive] + # Name the arches THIS build targets, so a tree left by a build with a different + # TORCH_CUDA_ARCH_LIST is ignored. Omitted for an on-device export. + if arch_list: + from tools.native_aot import export as export_mod + + # Both: --archs filters the trees, --arch-list is the raw value recorded + # in the emitted CMake. Only this caller knows they are one request. + gen += ["--archs", *export_mod.archs_from_cuda_arch_list(arch_list)] + gen += ["--arch-list", arch_list] + _run_child(gen, "generating stub sources", cwd=REPO) + # Nothing generated is legitimate: no declaration ships kernels for this arch. + # Stop rather than relink unchanged and then assert kernels are in it. + sources = glob.glob(os.path.join(art, "*", "aot_*.cpp")) + if not sources: + _report("no declaration ships kernels for this build; nothing embedded") + return 0 + # The count, and the size delta after the relink, rather than parsing the generated + # CMake: these bytes scale with declarations x precompile points x arches. + _report(f"embedding kernels from {len(sources)} generated source(s)") + # Reconfigure explicitly: the generated file registers itself in + # CMAKE_CONFIGURE_DEPENDS only from the reconfigure that first reads it. + # Keyed on the CACHE, not the directory: `cmake -B` on a missing directory exits 0 + # and configures FROM SCRATCH, without scikit-build-core's -D flags. + if not os.path.exists(os.path.join(BUILD_DIR, "CMakeCache.txt")): + raise RuntimeError( + f"native-AOT stage 2: {BUILD_DIR} holds no CMakeCache.txt, so it is not " + f"the build this torch came from. It must match pyproject.toml's " + f"build-dir; re-run the build from the repo root, or set " + f"TORCH_NATIVE_AOT=0 to skip stage 2." + ) + _report("reconfiguring to pick up the generated CMake") + # The cmake that configured this tree, and its cache as it stands, so the + # reconfigure can be held to both (see _refuse_cache_drift). + cmake_exe = _cmake_for_this_build() + cache_before = _cache_entries() + # Captured because CMake prints its failure context on stdout, and the STATUS line + # the generated file emits is the only pre-relink evidence that it will embed. + # --log-level, because that marker is a message(STATUS) and a cached + # CMAKE_MESSAGE_LOG_LEVEL=WARNING (EnvVarForwarding FORCEs it) would hide it. + configure = subprocess.run( + [cmake_exe, "--log-level=STATUS", "-S", REPO, "-B", BUILD_DIR], + capture_output=True, + text=True, + ) + if configure.returncode != 0: + print(configure.stdout.rstrip(), file=sys.stderr, flush=True) + print(configure.stderr.rstrip(), file=sys.stderr, flush=True) + raise RuntimeError( + f"native-AOT stage 2: reconfiguring {BUILD_DIR} failed (exit " + f"{configure.returncode}, output above)." + ) + # Ahead of the relink, so a drifting configure cannot reach the installed torch. + _refuse_cache_drift(cache_before) + from tools.native_aot.gen_aot_lib import EMBED_STATUS + + if EMBED_STATUS not in configure.stdout: + raise RuntimeError( + f"native-AOT stage 2: generation wrote " + f"{os.path.join(art, 'native_aot.cmake')}, but the reconfigure did not " + f"report embedding it. The build has declined the kernels this run just " + f"exported -- most often TORCH_NATIVE_AOT is falsy in " + f"{BUILD_DIR}/CMakeCache.txt (a -D from an earlier configure) while stage " + f"2 was told to run. Reconfigure with -DTORCH_NATIVE_AOT=1, or set " + f"TORCH_NATIVE_AOT=0 to skip stage 2 entirely." + ) + # Relink JUST torch_cuda: `--target install` walks the whole manifest (~15 min). + # The copy below matches it only because the CMake sets BUILD_WITH_INSTALL_RPATH. + _report("relinking torch_cuda with embedded kernels") + # pyproject.toml aliases MAX_JOBS only inside scikit-build-core's subprocess. + relink = [cmake_exe, "--build", ".", "--target", "torch_cuda"] + jobs = os.getenv("MAX_JOBS") or os.getenv("CMAKE_BUILD_PARALLEL_LEVEL") + if jobs and jobs.isdigit(): + relink += ["--parallel", jobs] + build_lib = os.path.join(BUILD_DIR, "lib", "libtorch_cuda.so") + # Taken across the relink, for the size delta reported below. + before = os.path.getsize(build_lib) if os.path.exists(build_lib) else 0 + siblings = _lib_snapshot() + _run_child(relink, "relinking torch_cuda", cwd=BUILD_DIR) + # Before the copy, so a mismatched pair never reaches the installed torch. + _refuse_unshipped_rebuilds(siblings) + + if not os.path.exists(build_lib): + raise RuntimeError(f"expected relinked library at {build_lib}") + installed = os.path.join(_installed_lib_dir(), "libtorch_cuda.so") + if not os.path.exists(installed): + # Refuse to create it: _installed_lib_dir found *a* torch, so a layout that + # never held the library means we are pointed at the wrong environment. + raise RuntimeError( + f"native-AOT stage 2: {installed} does not exist, so the torch on " + f"sys.path is not the one this tree built. Install the wheel from " + f"this build first, or set TORCH_NATIVE_AOT=0." + ) + # Temp file + rename: copying in place truncates a library others may be mapping, + # and one os.replace means `installed` never stops existing. + staged = f"{installed}.naot.{os.getpid()}.tmp" + try: + shutil.copy2(build_lib, staged) + os.replace(staged, installed) + finally: + if os.path.exists(staged): + os.remove(staged) + grew = (os.path.getsize(build_lib) - before) >> 20 + _report( + f"{os.path.getsize(build_lib) >> 20} MiB relinked into {installed} " + f"({grew:+d} MiB of embedded kernels)" + ) + # Size is not evidence: this script's artifacts dir must agree with the one + # caffe2/CMakeLists.txt include()s, or the relink embeds nothing and still exits 0. + if not _torch_probe("torch._native._native_aot_embedded()"): + raise RuntimeError( + "native-AOT stage 2: relinked libtorch_cuda reports no embedded " + "kernels (torch._native._native_aot_embedded() is False). The " + f"artifacts in {art} were not linked in -- check that this build's " + f"CMAKE_BINARY_DIR is {BUILD_DIR} (it must match pyproject.toml's " + f"build-dir), and that {art}/native_aot.cmake exists and is the file " + f"caffe2/CMakeLists.txt includes. {installed} now holds that library, " + f"so reinstall the wheel from this build to get back to a known state." + ) + if args.wheel: + _report(f"embedding into {args.wheel}") + patch_wheel(args.wheel, build_lib) + _report("done") + return 0 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/tools/native_aot/gen_aot_lib.py b/tools/native_aot/gen_aot_lib.py index fb392b8bfe02..90c83600379c 100644 --- a/tools/native_aot/gen_aot_lib.py +++ b/tools/native_aot/gen_aot_lib.py @@ -648,7 +648,17 @@ def _cmake_str(value: str) -> str: def _write_atomic(path: str, text: str) -> str: """Write via a temp file + rename, so a crash cannot leave a partial file - that CMake would read as authoritative.""" + that CMake would read as authoritative. + + Identical content is left alone: these files are build inputs, and restamping them + recompiles the generated sources and relinks torch_cuda on every run, which dirties + every target that consumes it.""" + try: + with open(path) as f: + if f.read() == text: + return path + except OSError: + pass # The pid in the name, not just the target's: nothing locks the artifacts dir, so # two generations in flight (a hand stage-2 run beside `spin develop`) shared one # staging file -- the second truncated it under the first, and the first published @@ -960,7 +970,15 @@ def main(argv: list[str] | None = None) -> None: # write_nothing_to_embed states, and what the other two invalidation sites do. # The nothing-to-embed form names no sources, so it invalidates as completely as # unlinking without dropping CMake's configure dependency. - if os.path.exists(os.path.join(args.artifacts_dir, CMAKE_INCLUDE)): + include = os.path.join(args.artifacts_dir, CMAKE_INCLUDE) + original = None + if os.path.exists(include): + # Kept so the timestamp can be restored below: this write and the final one + # both differ from what is on disk, so the pair restamps the include even when + # generation changes nothing, and it is a configure dependency. + st = os.stat(include) + with open(include) as f: + original = (f.read(), st.st_atime_ns, st.st_mtime_ns) write_nothing_to_embed(args.artifacts_dir) # decl_id -> its arch dirs, so one declaration generates once however many @@ -1219,9 +1237,18 @@ def main(argv: list[str] | None = None) -> None: print(f"wrote {out} ({n} kernels)") ver = write_version_script(args.artifacts_dir, all_prefixes) print(f"wrote {ver}") - print( - f"wrote {write_cmake_include(args.artifacts_dir, [p[1] for p in pending], link_objects, ver, args.dsl_runtime, args.arch_list)}" + written = write_cmake_include( + args.artifacts_dir, + [p[1] for p in pending], + link_objects, + ver, + args.dsl_runtime, + args.arch_list, ) + print(f"wrote {written}") + with open(written) as f: + if original is not None and f.read() == original[0]: + os.utime(written, ns=original[1:]) if __name__ == "__main__": diff --git a/tools/test/test_native_aot_tools.py b/tools/test/test_native_aot_tools.py index aa7e5d1b62e9..9176099f0167 100644 --- a/tools/test/test_native_aot_tools.py +++ b/tools/test/test_native_aot_tools.py @@ -10,9 +10,12 @@ import re import subprocess import sys import tempfile +import time import types import unittest import unittest.mock as mock +import zipfile +from typing import Any # Ordinary package imports: these modules keep their scope torch-free so the # Test tools job, which runs in the linter image, can import them without torch. @@ -213,7 +216,7 @@ class TestExportJobs(unittest.TestCase): # worker serve several arches, appended to any builder-supplied options. import sys import types - from typing import Any, cast + from typing import cast seen = {} @@ -1947,6 +1950,53 @@ class TestEndToEndGeneration(unittest.TestCase): err = e yield art, err + def test_a_second_generation_over_the_same_inputs_touches_nothing(self): + # These files are build inputs: restamping them recompiles the generated + # sources and relinks torch_cuda, dirtying all ~110 targets that consume it. + with tempfile.TemporaryDirectory() as art, tempfile.TemporaryDirectory() as ops: + art_op = os.path.join(art, "sm_100a", "fakeop") + os.makedirs(art_op) + _touch_artifacts(art_op, SIDECAR["prefix"]) + sidecar = dict( + SIDECAR, + spec={"N": 1024, "K": 8}, + sources=_current_sources(), + runtimes=_RUNTIMES, + version=export.SIDECAR_VERSION, + ) + with open(os.path.join(art_op, SIDECAR["prefix"] + ".json"), "w") as f: + json.dump(sidecar, f) + os.makedirs(os.path.join(ops, "fakeop")) + with open(os.path.join(ops, "fakeop", "aot.py"), "w") as f: + f.write(self._DECL) + + # By INODE, not mtime: os.replace gives a rewritten file a new inode, while + # two runs inside one filesystem timestamp tick share an mtime. The include + # is exempt -- it is rewritten by construction (invalidated first, then + # written) and only its timestamp is restored, which is what the build reads. + def stamps(): + out = {} + for root, _, files in os.walk(art): + for name in files: + p = os.path.join(root, name) + st = os.stat(p) + rel = os.path.relpath(p, art) + keep = ( + st.st_mtime_ns + if name == gen_aot_lib.CMAKE_INCLUDE + else st.st_ino + ) + out[rel] = (keep, st.st_size) + return out + + with _patched_generation(ops, declarations=None): + gen_aot_lib.main(["--artifacts-dir", art]) + first = stamps() + # Past a timestamp tick, so a restamped file is visible as one. + time.sleep(0.05) + gen_aot_lib.main(["--artifacts-dir", art]) + self.assertEqual(stamps(), first) + def test_main_writes_aot_source(self): # Artifacts live at ///. _generated writes them for real, # since generation refuses a sidecar describing a file that is not there. @@ -2028,6 +2078,240 @@ class TestEndToEndGeneration(unittest.TestCase): sys.argv = argv +def _has_zip64_extra(info: zipfile.ZipInfo) -> bool: + """Whether a member's central-directory extra carries a 0x0001 (ZIP64) field.""" + extra, i = info.extra, 0 + while i + 4 <= len(extra): + tag = int.from_bytes(extra[i : i + 2], "little") + length = int.from_bytes(extra[i + 2 : i + 4], "little") + if tag == 1: + return True + i += 4 + length + return False + + +class TestWheelPatch(unittest.TestCase): + LIB = "torch/lib/libtorch_cuda.so" + + @staticmethod + def _rec(name, data): + """A RECORD line, implemented here rather than imported from the code + under test -- that is what lets the digest assertion actually fail.""" + import base64 + import hashlib + + h = base64.urlsafe_b64encode(hashlib.sha256(data).digest()) + return f"{name},sha256={h.rstrip(b'=').decode()},{len(data)}" + + def _make_wheel(self, d: str, record_entry: bool = True) -> str: + rec = self._rec + whl = os.path.join(d, "torch-0.0-cp310-linux_x86_64.whl") + record = "torch-0.0.dist-info/RECORD" + lines = [rec("torch/__init__.py", b"x"), f"{record},,"] + if record_entry: + lines.insert(0, rec(self.LIB, b"OLD")) + with zipfile.ZipFile(whl, "w") as zf: + zf.writestr(self.LIB, b"OLD") + zf.writestr("torch/__init__.py", b"x") + zf.writestr(record, "\n".join(lines) + "\n") + return whl + + def test_replaces_lib_and_record(self): + with tempfile.TemporaryDirectory() as d: + whl = self._make_wheel(d) + lib = os.path.join(d, "libtorch_cuda.so") + with open(lib, "wb") as f: + f.write(b"NEW" * 64) + build_stage2.patch_wheel(whl, lib) + with zipfile.ZipFile(whl) as zf: + self.assertEqual(zf.read(self.LIB), b"NEW" * 64) + lines = zf.read("torch-0.0.dist-info/RECORD").decode().splitlines() + # An independent expectation, since computing it as the code does + # would pass with the digest switched to md5. + self.assertIn(self._rec(self.LIB, b"NEW" * 64), lines) + self.assertRegex(self._rec(self.LIB, b"NEW" * 64), r"[-_]") + # untouched members keep their RECORD entries; no dup names + self.assertTrue(any(x.startswith("torch/__init__.py,") for x in lines)) + self.assertEqual(len(zf.namelist()), len(set(zf.namelist()))) + + def test_compression_is_preserved(self): + # The replaced member is the biggest thing in the wheel, which every test shard + # downloads. Untouched members must keep their compression too. + with tempfile.TemporaryDirectory() as d: + whl = os.path.join(d, "torch-0.0-cp310-linux_x86_64.whl") + record = "torch-0.0.dist-info/RECORD" + # Deliberately mixed: DEFLATED for the lib and one member, STORED + # for another, so "preserved" cannot be satisfied by a constant. + with zipfile.ZipFile(whl, "w") as zf: + zf.writestr(self.LIB, b"OLD" * 999, zipfile.ZIP_DEFLATED) + zf.writestr("torch/big.py", b"y" * 4096, zipfile.ZIP_DEFLATED) + zf.writestr("torch/raw.bin", b"z" * 4096, zipfile.ZIP_STORED) + zf.writestr(record, f"{self.LIB},,\n{record},,\n") + lib = os.path.join(d, "libtorch_cuda.so") + with open(lib, "wb") as f: + f.write(b"NEW" * 999) + build_stage2.patch_wheel(whl, lib) + with zipfile.ZipFile(whl) as zf: + self.assertEqual( + zf.getinfo(self.LIB).compress_type, zipfile.ZIP_DEFLATED + ) + self.assertEqual( + zf.getinfo("torch/big.py").compress_type, zipfile.ZIP_DEFLATED + ) + self.assertEqual( + zf.getinfo("torch/raw.bin").compress_type, zipfile.ZIP_STORED + ) + self.assertEqual(zf.read(self.LIB), b"NEW" * 999) + self.assertEqual(zf.read("torch/raw.bin"), b"z" * 4096) + + def test_members_are_streamed_not_read_whole(self): + # A CUDA wheel holds members in the hundreds of MiB, and read()+writestr holds + # each twice. Asserted by watching for the read that must not happen. + with tempfile.TemporaryDirectory() as d: + whl = self._make_wheel(d) + lib = os.path.join(d, "libtorch_cuda.so") + with open(lib, "wb") as f: + f.write(b"NEW") + real_read = zipfile.ZipFile.read + reads = [] + + def spy(self, name, *a, **kw): + reads.append(name if isinstance(name, str) else name.filename) + return real_read(self, name, *a, **kw) + + with mock.patch.object(zipfile.ZipFile, "read", spy): + build_stage2.patch_wheel(whl, lib) + # RECORD is read deliberately (it has to be rewritten); the payload + # members must not be. + self.assertNotIn("torch/__init__.py", reads) + + def test_a_failed_rewrite_leaves_the_original_wheel_untouched(self): + # tmp + rename: an interrupted rewrite must not leave a half-written wheel + # where a valid one was, nor a .naot.tmp beside it. + with tempfile.TemporaryDirectory() as d: + whl = self._make_wheel(d) + with open(whl, "rb") as f: + before = f.read() + lib = os.path.join(d, "libtorch_cuda.so") + with open(lib, "wb") as f: + f.write(b"NEW") + with mock.patch.object( + build_stage2.shutil, "move", side_effect=OSError("simulated failure") + ): + with self.assertRaises(OSError): + build_stage2.patch_wheel(whl, lib) + with open(whl, "rb") as f: + self.assertEqual(f.read(), before, "original wheel was modified") + self.assertEqual( + [f for f in os.listdir(d) if f.endswith(".tmp")], + [], + "left a temp wheel behind", + ) + # ...and it is still a readable zip. + with zipfile.ZipFile(whl) as zf: + self.assertIsNone(zf.testzip()) + + def test_zip64_members_survive_the_copy(self): + # ZIP64_LIMIT is patched rather than writing 2 GiB, once either side of it. + with tempfile.TemporaryDirectory() as d: + with mock.patch.object(zipfile, "ZIP64_LIMIT", 2): + whl = self._make_wheel(d) # every member gets a real ZIP64 extra + with zipfile.ZipFile(whl) as zf: + self.assertTrue( + all(_has_zip64_extra(i) for i in zf.infolist()), + "the fixture is supposed to need ZIP64 everywhere", + ) + lib = os.path.join(d, "libtorch_cuda.so") + with open(lib, "wb") as f: + f.write(b"NEW" * 64) + build_stage2.patch_wheel(whl, lib) # ...rewritten below the limit + with zipfile.ZipFile(whl) as zf: + self.assertIsNone(zf.testzip()) + self.assertEqual(zf.read(self.LIB), b"NEW" * 64) + self.assertEqual( + [n for n in zf.namelist() if _has_zip64_extra(zf.getinfo(n))], + [], + "a copied member kept the source's ZIP64 extra field", + ) + # ...and the library keeps its position, so no later member moves and + # .dist-info stays where an archiver expects it. + self.assertEqual(zf.namelist()[0], self.LIB) + # ...and again with every member above the limit, which is what (b) + # needs. + with mock.patch.object(zipfile, "ZIP64_LIMIT", 0): + build_stage2.patch_wheel(whl, lib) + with zipfile.ZipFile(whl) as zf: + self.assertIsNone(zf.testzip()) + self.assertEqual(zf.read(self.LIB), b"NEW" * 64) + + def test_the_staging_name_belongs_to_this_process(self): + # Two concurrent stage-2 runs would otherwise share one .naot.tmp path, and + # the loser would rename its half-written archive over the finished wheel. + seen = [] + with tempfile.TemporaryDirectory() as d: + whl = self._make_wheel(d) + lib = os.path.join(d, "libtorch_cuda.so") + with open(lib, "wb") as f: + f.write(b"NEW") + real_move = build_stage2.shutil.move + + def spy(src, dst): + seen.append(src) + return real_move(src, dst) + + with mock.patch.object(build_stage2.shutil, "move", spy): + build_stage2.patch_wheel(whl, lib) + self.assertEqual(len(seen), 1, seen) + self.assertIn(str(os.getpid()), os.path.basename(seen[0])) + + def test_the_rewritten_record_keeps_its_compression(self): + # writestr() with a plain name takes the ZipFile default, which the destination + # has none of, so the RECORD would be the one member stored uncompressed. + with tempfile.TemporaryDirectory() as d: + whl = os.path.join(d, "torch-0.0.0-cp310-cp310-linux_x86_64.whl") + lib = "torch/lib/libtorch_cuda.so" + rec = "torch-0.0.0.dist-info/RECORD" + body = "\n".join(f"torch/f{i}.py,sha256=x,{i}" for i in range(4000)) + with zipfile.ZipFile(whl, "w", zipfile.ZIP_DEFLATED) as z: + z.writestr(lib, b"old") + z.writestr(rec, f"{lib},sha256=old,3\n{body}\n") + new_lib = os.path.join(d, "new.so") + with open(new_lib, "wb") as f: + f.write(b"new kernels") + build_stage2.patch_wheel(whl, new_lib) + with zipfile.ZipFile(whl) as z: + info = z.getinfo(rec) + self.assertEqual(info.compress_type, zipfile.ZIP_DEFLATED) + self.assertLess(info.compress_size, info.file_size) + # ...and the rewrite is still correct: sizes recomputed, entry + # updated, every other line kept, archive readable. + text = z.read(rec).decode() + self.assertNotIn("sha256=old", text) + self.assertEqual(text.count("\n"), 4001) + + def test_non_torch_wheel_is_refused(self): + # A wrong --wheel argument, or a renamed member, must say so rather than + # silently produce a wheel with no kernels in it. + with tempfile.TemporaryDirectory() as d: + whl = os.path.join(d, "nottorch-0.0-py3-none-any.whl") + with zipfile.ZipFile(whl, "w") as zf: + zf.writestr("nottorch/__init__.py", b"x") + zf.writestr("notorch-0.0.dist-info/RECORD", "") + lib = os.path.join(d, "libtorch_cuda.so") + open(lib, "wb").close() + with self.assertRaisesRegex(RuntimeError, "not a torch wheel"): + build_stage2.patch_wheel(whl, lib) + + def test_record_without_the_lib_entry_is_refused(self): + with tempfile.TemporaryDirectory() as d: + whl = self._make_wheel(d, record_entry=False) + lib = os.path.join(d, "libtorch_cuda.so") + with open(lib, "wb") as f: + f.write(b"NEW") + with self.assertRaisesRegex(RuntimeError, "RECORD has no entry"): + build_stage2.patch_wheel(whl, lib) + + class TestDeclarationArchs(unittest.TestCase): def test_a_malformed_archs_entry_is_refused_at_load(self): # _SM_RE accepts "sm_9" and "sm_1000", which name no capability. Refused by the @@ -2373,6 +2657,64 @@ class TestShouldRun(unittest.TestCase): # requiring them here fails the run on the very runtimes it should request. self.assertTrue(self._run(self.CUDA, self.ARCH, missing=("cutlass",))) + def _verdict(self, env): + """(stdout, stderr) of `--print-verdict`, captured separately.""" + out, err = io.StringIO(), io.StringIO() + # All-true would mean ROCm, which skips before the arch logic under test. + probe = lambda e: e != "torch.version.hip is not None" # noqa: E731 + with contextlib.ExitStack() as stack: + # Through main() rather than _run, so it needs the same REPO redirect, or + # the verdict depends on whether the checked-out commit declares anything. + repo = stack.enter_context(tempfile.TemporaryDirectory()) + ops = os.path.join(repo, "torch", "_native", "ops") + os.makedirs(os.path.join(ops, "fakeop")) + open(os.path.join(ops, "fakeop", "aot.py"), "w").close() + stack.enter_context(mock.patch.object(build_stage2, "REPO", repo)) + stack.enter_context(mock.patch.object(export, "OPS_DIR", ops)) + # ...and the same platform patch, since should_run checks sys.platform + # first: without it this test failed on a non-Linux box. + stack.enter_context(mock.patch.object(sys, "platform", "linux")) + # ...and the same BUILD_DIR redirect, carrying the CUDA major: an empty + # cache falls through to the installed torch. + build = stack.enter_context(tempfile.TemporaryDirectory()) + with open(os.path.join(build, "CMakeCache.txt"), "w") as f: + f.write("CUDAToolkit_VERSION_MAJOR:STRING=13\n") + stack.enter_context(mock.patch.object(build_stage2, "BUILD_DIR", build)) + # ...and the same env neutralization as _run, applied before the caller's + # env, which overrides it. + stack.enter_context( + mock.patch.dict( + os.environ, + {"TORCH_NATIVE_AOT": "", "TORCH_CUDA_ARCH_LIST": ""}, + clear=False, + ) + ) + stack.enter_context(mock.patch.object(build_stage2, "_torch_probe", probe)) + stack.enter_context( + mock.patch.object( + toolchains.Toolchain, + "missing_runtimes", + classmethod(lambda cls: []), + ) + ) + stack.enter_context(mock.patch.dict(os.environ, env, clear=False)) + stack.enter_context(contextlib.redirect_stdout(out)) + stack.enter_context(contextlib.redirect_stderr(err)) + build_stage2.main(["--print-verdict"]) + return out.getvalue(), err.getvalue() + + def test_verdict_stdout_carries_only_the_verdict(self): + # The shells compare with ==, so any other line on stdout breaks them, and + # multi-arch is the one case that reports and still proceeds. + out, err = self._verdict({"TORCH_CUDA_ARCH_LIST": "9.0;10.0"}) + self.assertEqual(out, "RUN\n") + self.assertIn("multi-arch", err) + + def test_verdict_stdout_is_clean_when_skipping(self): + out, err = self._verdict({"TORCH_NATIVE_AOT": "0"}) + self.assertEqual(out, "SKIP\n") + self.assertIn("TORCH_NATIVE_AOT=0", err) + def test_skips_when_no_declarations_exist(self): # No aot.py in the tree means stage 2 would export nothing, so demanding # ~190MB of DSL wheels for it is wrong. It is also the state of a bisect. @@ -2427,13 +2769,17 @@ class TestShouldRun(unittest.TestCase): def fake_run(cmd, **kw): seen.append(kw.get("cwd")) return subprocess.CompletedProcess( - args=cmd, returncode=0, stdout="PROBE_OK\nNAOT_VALUE:2\n", stderr="" + args=cmd, + returncode=0, + stdout="PROBE_OK\nNAOT_VALUE:/x/torch\n", + stderr="", ) with mock.patch.object(build_stage2.subprocess, "run", fake_run): build_stage2._torch_probe("True") build_stage2._torch_value("1") - self.assertEqual(len(seen), 2, "both probes should have run") + build_stage2._installed_lib_dir() + self.assertEqual(len(seen), 3, "all three probes should have run") for cwd in seen: self.assertEqual(cwd, build_stage2.HERE) self.assertNotEqual(cwd, build_stage2.REPO) @@ -2996,6 +3342,47 @@ class TestProbeDiagnostics(unittest.TestCase): self.assertEqual(out.getvalue(), "") +class TestInstalledLibDir(unittest.TestCase): + def test_the_path_comes_from_a_marked_line(self): + # Unmarked, any other line the child writes -- a warning, a site hook -- + # becomes part of a filesystem path. + done = subprocess.CompletedProcess( + [], 0, stdout="a warning\nNAOT_VALUE:/x/site-packages/torch\n", stderr="" + ) + with mock.patch.object(subprocess, "run", return_value=done): + self.assertEqual( + build_stage2._installed_lib_dir(), + os.path.join("/x/site-packages/torch", "lib"), + ) + + def test_a_probe_that_cannot_run_at_all_is_still_framed(self): + # The SPAWN, not the child: a fork returning EAGAIN after a 32-way relink would + # surface as a bare OSError, and with no timeout the call hangs the build. + with ( + mock.patch.object( + subprocess, "run", side_effect=BlockingIOError(11, "try again") + ), + contextlib.redirect_stderr(io.StringIO()) as err, + ): + with self.assertRaisesRegex(RuntimeError, "cannot locate the installed"): + build_stage2._installed_lib_dir() + self.assertIn("could not run", err.getvalue()) + + def test_a_failure_reports_the_childs_stderr(self): + # The traceback was captured and thrown away -- after a full export, + # generation and relink. + done = subprocess.CompletedProcess( + [], 1, stdout="", stderr="ImportError: libcudart.so.13" + ) + with ( + mock.patch.object(subprocess, "run", return_value=done), + contextlib.redirect_stderr(io.StringIO()) as err, + ): + with self.assertRaisesRegex(RuntimeError, "cannot locate the installed"): + build_stage2._installed_lib_dir() + self.assertIn("libcudart.so.13", err.getvalue()) + + class TestProbeSpawnFailures(unittest.TestCase): """A probe that never runs must degrade like one that answers: should_run() prints a word the CI shells compare with ==, so a traceback there is neither @@ -3048,7 +3435,10 @@ class TestProbeSpawnFailures(unittest.TestCase): with mock.patch.object(build_stage2.subprocess, "run", fake_run): build_stage2._torch_probe("True") build_stage2._torch_value("1") - self.assertEqual(seen, [build_stage2._PROBE_TIMEOUT_SECONDS] * 2) + # ...including the probe that locates the installed torch: find_spec + # imports the parent package, so that call is a full `import torch` too. + build_stage2._installed_lib_dir() + self.assertEqual(seen, [build_stage2._PROBE_TIMEOUT_SECONDS] * 3) class TestRegistryConsistency(unittest.TestCase): @@ -3502,6 +3892,36 @@ class TestSizeGateIsPerKindNotPerFile(unittest.TestCase): self.assertIn("_naot_dim_too_big", src) +class TestWheelRefusal(unittest.TestCase): + def test_wheel_requires_an_importable_torch(self): + # --wheel means a CI caller installed torch on the line above, so "not + # importable" is a broken build rather than "not applicable". + with ( + mock.patch.object(build_stage2, "_torch_probe", lambda e: False), + mock.patch.object(build_stage2, "should_run", lambda: False), + ): + with self.assertRaisesRegex(RuntimeError, "does not import"): + build_stage2.main(["--wheel", "/tmp/nonexistent.whl"]) + + def test_the_refusal_is_ahead_of_every_applicability_gate(self): + # The refusal cannot consult should_run, which needs torch: hence the guard. + called = [] + with ( + mock.patch.object(build_stage2, "_torch_probe", lambda e: False), + mock.patch.object( + build_stage2, "should_run", lambda: called.append(1) or False + ), + ): + with self.assertRaises(RuntimeError): + build_stage2.main(["--wheel", "/tmp/nonexistent.whl"]) + self.assertEqual(called, []) + + def test_without_wheel_an_unimportable_torch_is_a_clean_skip(self): + # The other half of the same contract: an ordinary build must degrade. + with mock.patch.object(build_stage2, "_torch_probe", lambda e: False): + self.assertEqual(build_stage2.main([]), 0) + + class TestExportMain(unittest.TestCase): """export.main() had no coverage at all, including the TORCH_CUDA_ARCH_LIST translation that another comment in the same file depends on: _CLOSURE_EXCLUDED @@ -3785,6 +4205,612 @@ class TestCollectJobsRefusals(unittest.TestCase): self.assertEqual(job[4], "sm_100a") +class TestCiAndCMakeWiring(unittest.TestCase): + """Text assertions over files this suite cannot execute, so a change to the build + wiring fails here rather than in a build.""" + + def _read(self, rel): + with open(os.path.join(REPO, rel)) as f: + return f.read() + + def test_every_install_path_runs_stage_two(self): + # stage 2 is a post-install step for want of a hook inside the PEP 517 backend, + # so an install path that skips it builds a torch with no AOT ops at all. + src = self._read(".spin/cmds.py") + self.assertIn("def _native_aot_stage2():", src) + # ...and it has to still RUN stage 2: gutting the body is invisible to a + # check that only looks for the call sites. + hook = src[src.index("def _native_aot_stage2():") :].split("\n\n\n")[0] + self.assertIn("tools/native_aot/build_stage2.py", hook) + for cmd in ("def develop():", "def install():"): + with self.subTest(cmd=cmd): + body = src[src.index(cmd) :].split("\n\n\n")[0] + # AFTER the install, not merely present: the kernel builders import + # the installed torch, so swapped, stage 2 relinks the previous one. + self.assertLess( + body.index("_pip_install_cmd("), + body.index("_native_aot_stage2()"), + "stage 2 has to run after the install it depends on", + ) + # ...and the same order in the CI shell. Located through the closing marker, + # since the guard line itself occurs five times in that file. + sh = self._read(".ci/pytorch/build.sh") + block_at = sh.rindex( + 'if [[ "$BUILD_ENVIRONMENT" == *cuda* ]]; then', + 0, + sh.index("fi # BUILD_ENVIRONMENT == *cuda*"), + ) + self.assertLess( + sh.index("pip_install_whl "), + block_at, + "the wheel must be installed before stage 2 runs", + ) + + def test_manywheel_installs_the_raw_wheel_before_stage_two(self): + # The kernel builders import the installed torch, so the raw wheel goes first -- + # inside the cuda guard, or a cpu container installs it for nothing. + text = self._read(".ci/manywheel/build.sh") + marker = "fi # GPU_ARCH_TYPE == cuda*" + self.assertIn(marker, text) + block = text.partition(marker)[0] + block = block[block.rindex('if [[ "${GPU_ARCH_TYPE}" == cuda* ]]; then') :] + self.assertLess( + block.index("pip install"), + block.index("build_stage2.py --wheel"), + "the raw wheel must be installed before stage 2 runs", + ) + # Same single owner of the install decision as .ci/pytorch/build.sh. + self.assertIn("--print-verdict", block) + self.assertIn("install_cutlass_dsl", block) + + def test_the_verdict_word_the_shells_compare_is_the_one_stage_two_prints(self): + # Both shells install the DSL wheels only when stage 2 says RUN, comparing with + # ==, so renaming the word either side skips the install and fails the build. + with ( + mock.patch.object(build_stage2, "should_run", return_value=True), + contextlib.redirect_stdout(io.StringIO()) as printed, + ): + build_stage2.main(["--print-verdict"]) + word = printed.getvalue().strip() + self.assertTrue(word, "--print-verdict printed nothing") + for rel in (".ci/pytorch/build.sh", ".ci/manywheel/build.sh"): + with self.subTest(rel=rel): + self.assertIn(f'--print-verdict)" == "{word}"', self._read(rel)) + + def test_the_wheel_is_patched_before_it_is_repaired(self): + # repair_wheel.py produces the PUBLISHED artifact, so stage 2 patches the raw + # wheel first: reversed, every release wheel ships kernel-free and green. + sh = self._read(".ci/manywheel/build.sh") + self.assertLess( + sh.index("build_stage2.py --wheel"), + sh.index("repair_wheel.py"), + "stage 2 must patch the raw wheel before repair_wheel.py copies it out", + ) + + def test_stage_two_is_cuda_guarded_in_both_ci_shells(self): + # Structure, not the literal: the call must sit between the guard and its fi. + text = self._read(".ci/pytorch/build.sh") + # The closing marker must exist: partitioning on a missing separator returns + # the whole string, and the assertions below would match an unrelated guard. + marker = "fi # BUILD_ENVIRONMENT == *cuda*" + self.assertIn(marker, text) + block = text.partition(marker)[0] + guard_at = block.rindex('if [[ "$BUILD_ENVIRONMENT" == *cuda* ]]; then') + self.assertIn("build_stage2.py --wheel", block[guard_at:]) + self.assertIn("--print-verdict", block[guard_at:]) + self.assertIn( + 'if [[ "${GPU_ARCH_TYPE}" == cuda* ]]; then', + self._read(".ci/manywheel/build.sh"), + ) + + def test_both_shells_count_wheels_with_nullglob(self): + # Without it a non-matching glob yields the literal pattern, and the + # message said "found 1" for an EMPTY directory. + for rel, pattern in ( + (".ci/pytorch/build.sh", "naot_wheels=(dist/*.whl)"), + (".ci/manywheel/build.sh", 'naot_wheels=("${RAW_WHEEL_DIR}"/*.whl)'), + ): + with self.subTest(rel=rel): + text = self._read(rel) + # Order, with all three indices from the START of the file: searching + # from the enable's own offset is tautological. + enable = text.index("shopt -s nullglob") + at = text.index(pattern) + disable = text.index("shopt -u nullglob") + self.assertLess(enable, at, "nullglob must be set BEFORE the glob") + self.assertLess(at, disable, "and unset only AFTER it has expanded") + + def test_the_documented_skip_reasons_match_should_run(self): + # The count is what keeps docstring, CONTRIBUTING.md and should_run() in step. + doc = build_stage2.__doc__ + contributing = self._read("CONTRIBUTING.md") + arms = ( + ("TORCH_NATIVE_AOT=0", "`TORCH_NATIVE_AOT=0`"), + ("not Linux", "not Linux"), + ("does not import", "does not import"), + ("no toolchain targets this backend", "no toolchain targets this backend"), + ("CUDA older than", "older than 13 or cannot be determined"), + ("no published DSL wheel", "no published DSL wheel"), + ("a static torch_cuda", "`BUILD_SHARED_LIBS=OFF`"), + ("nothing declares kernels", "nothing declares kernels"), + ("names no exportable arch", "no supported arch is targeted"), + ) + # The first bullet block after the lead-in, delimited by blank lines: keyed on + # a following paragraph's wording, this counted that paragraph's bullets too. + blocks = doc.partition("Keep this list in sync")[2].split("\n\n") + skips = next(b for b in blocks if b.strip().startswith("* ")) + bullets = [ln for ln in skips.splitlines() if ln.strip().startswith("* ")] + self.assertEqual(len(bullets), len(arms), f"docstring skip bullets: {bullets}") + for in_doc, in_contributing in arms: + with self.subTest(arm=in_doc): + self.assertIn(in_doc, doc) + self.assertIn(in_contributing, contributing) + + +class TestStageTwoArgvContract(unittest.TestCase): + """What main() passes to its two children, and the invariant that ties them: + a tree export creates must be one generation was told about.""" + + @contextlib.contextmanager + def _run_main(self, arch_list, cache=None, archive=None): + """main() with both children captured; returns the recorded calls.""" + calls = [] + + def fake_call(cmd, **kw): + calls.append((cmd, kw.get("env") or {})) + return 0 + + with contextlib.ExitStack() as stack: + build = stack.enter_context(tempfile.TemporaryDirectory()) + art = os.path.join(build, "native_aot") + os.makedirs(art) + if cache is not None: + with open(os.path.join(build, "CMakeCache.txt"), "w") as f: + f.write(cache) + env = {"TORCH_CUDA_ARCH_LIST": arch_list or "", "TORCH_NATIVE_AOT": ""} + stack.enter_context(mock.patch.dict(os.environ, env, clear=False)) + stack.enter_context(mock.patch.object(build_stage2, "BUILD_DIR", build)) + stack.enter_context( + mock.patch.object(build_stage2, "NATIVE_AOT_ARTIFACTS_DIR", art) + ) + stack.enter_context( + mock.patch.object(build_stage2, "should_run", lambda: True) + ) + stack.enter_context( + mock.patch.object(build_stage2, "require_runtimes", lambda: None) + ) + # Discovery is a separate subject: unpatched, these reach the real DSL + # wheel and a real torch probe. What they are about is main()'s argv. + stack.enter_context( + mock.patch.object(build_stage2, "_dsl_runtime_archive", lambda: archive) + ) + stack.enter_context(mock.patch.object(subprocess, "call", fake_call)) + # No generated source, so main() stops before the relink -- the argv is + # the whole subject here. + self.assertEqual(build_stage2.main([]), 0) + yield calls + + def _args_of(self, calls, script): + ((cmd, env),) = [(c, e) for c, e in calls if any(script in str(a) for a in c)] + return cmd, env + + def test_both_children_are_given_the_one_artifacts_directory(self): + # Both children must get the same tree, or export writes A, generation reads B. + with self._run_main(None) as calls: + export_cmd, _ = self._args_of(calls, "export.py") + gen_cmd, _ = self._args_of(calls, "gen_aot_lib.py") + out_dir = export_cmd[export_cmd.index("--out-dir") + 1] + artifacts = gen_cmd[gen_cmd.index("--artifacts-dir") + 1] + self.assertEqual(out_dir, build_stage2.NATIVE_AOT_ARTIFACTS_DIR) + self.assertEqual(artifacts, out_dir, "both children need one tree") + + def test_the_arch_list_reaches_the_export_child(self): + # export.py reads TORCH_CUDA_ARCH_LIST itself, so the value has to be in the + # child's environment even for a -D build where this process never had it. + with self._run_main( + None, cache="TORCH_CUDA_ARCH_LIST:STRING=9.0;10.0a\n" + ) as calls: + _, env = self._args_of(calls, "export.py") + self.assertEqual(env.get("TORCH_CUDA_ARCH_LIST"), "9.0;10.0a") + + def test_generation_is_told_both_the_filter_and_the_recorded_list(self): + with self._run_main("9.0;10.0a") as calls: + cmd, _ = self._args_of(calls, "gen_aot_lib.py") + self.assertIn("--archs", cmd) + self.assertIn("--arch-list", cmd) + # --arch-list is recorded verbatim for CMake to compare against. + self.assertEqual(cmd[cmd.index("--arch-list") + 1], "9.0;10.0a") + + def test_the_discovered_archive_is_passed_to_generation(self): + # The first of the two hops between _dsl_runtime_archive() and the emitted + # target_link_libraries, each tested on its own: this pins the forwarding. + archive = "/x/libcuda_dialect_runtime_static.a" + with self._run_main(None, archive=archive) as calls: + cmd, _ = self._args_of(calls, "gen_aot_lib.py") + self.assertIn("--dsl-runtime", cmd) + self.assertEqual(cmd[cmd.index("--dsl-runtime") + 1], archive) + + def test_an_on_device_run_passes_no_arch_filter(self): + # Nothing to filter by: the local GPU is the whole arch list, and passing + # a filter here would be comparing a resolved spelling against itself. + with self._run_main(None) as calls: + cmd, _ = self._args_of(calls, "gen_aot_lib.py") + self.assertNotIn("--archs", cmd) + self.assertNotIn("--arch-list", cmd) + + def test_every_tree_export_creates_is_one_generation_was_told_about(self): + # Directory, sidecar, --archs filter and recorded ARCH_LIST must follow from + # one resolution. The lists are the real ones (manywheel, and the b200 job). + for arch_list in ( + "7.5;8.0;9.0;10.0;12.0", + "7.5;8.0;8.6;9.0", + "10.0a", + "9.0", + "9.0a;10.0", + # Both spellings collapse to one arch; the two sides must agree which. + "10.0;10.0a", + ): + with self.subTest(arch_list=arch_list): + with self._run_main(arch_list) as calls: + cmd, _ = self._args_of(calls, "gen_aot_lib.py") + told = cmd[cmd.index("--archs") + 1 : cmd.index("--arch-list")] + with ( + tempfile.TemporaryDirectory() as ops, + tempfile.TemporaryDirectory() as out, + ): + _write_fake_decl(ops) + with ( + mock.patch.object(export, "OPS_DIR", ops), + _no_ambient_arch(device=None), + ): + jobs = export._collect_jobs( + None, out, export.archs_from_cuda_arch_list(arch_list) + ) + trees = sorted( + {os.path.basename(os.path.dirname(j[3])) for j in jobs} + ) + self.assertTrue(trees, "the fixture should export something") + for tree in trees: + self.assertIn( + tree, + told, + f"export created {tree}/ but generation was told to filter " + f"on {told}, so that tree would be ignored and nothing " + f"embedded", + ) + + +class TestRelinkNeverStrandsTheInstalledTorch(unittest.TestCase): + """main()'s relink half, which copies over the INSTALLED torch. + + A separate fixture from TestStageTwoArgvContract._run_main, which supplies no + generated source and so stops before the relink.""" + + OLD, NEW = b"PREVIOUSLY-INSTALLED", b"RELINKED" + + @contextlib.contextmanager + def _main( + self, + cache=True, + configure_embeds=True, + configure_rc=0, + embedded_after=True, + kill_swap=False, + wheel=False, + verdict=True, + runtimes=None, + child_rc=0, + stale_include=False, + built=True, + installed=True, + grew=0, + cache_after=None, + cmake_command=None, + rebuild_sibling=False, + ): + """main() with every child faked, so the ORDER is the production one. + + Yields `outcome` (exception message or return code), `content` (bytes in + site-packages), `children` (commands launched), `listing` (the installed lib + dir), `argv` (the reconfigure's command line), `include` (the emitted CMake) + and `reported` (stderr); one keyword per arm of main().""" + children = [] + commands = [] + patched = [] + argv = [] + kwargs = {} + + def fake_call(cmd, **kw): + commands.append(list(cmd)) + children.append(os.path.basename(str(cmd[1])) if len(cmd) > 1 else cmd[0]) + if rebuild_sibling and "--target" in cmd: + with open(os.path.join(build, "lib", "libtorch_cpu.so"), "wb") as f: + f.write(b"REBUILT-DEPENDENCY") + if grew and "--target" in cmd: + with open(os.path.join(build, "lib", "libtorch_cuda.so"), "wb") as f: + f.truncate(grew) # sparse: only the reported size matters + return child_rc + + def fake_run(cmd, **kw): + children.append("reconfigure") + commands.append(list(cmd)) + argv.append(list(cmd)) + kwargs.update(kw) + if cache_after is not None: + with open(os.path.join(build, "CMakeCache.txt"), "w") as f: + f.write(cache_after) + out = ( + "-- native-AOT: embedding 1 object(s)\n" if configure_embeds else "--\n" + ) + return subprocess.CompletedProcess(cmd, configure_rc, out, "cmake said no") + + def require_runtimes(): + if runtimes is not None: + raise RuntimeError(runtimes) + + with contextlib.ExitStack() as stack: + d = stack.enter_context(tempfile.TemporaryDirectory()) + build = os.path.join(d, "build") + art = os.path.join(build, "native_aot") + libdir = os.path.join(d, "site-packages", "torch", "lib") + os.makedirs(os.path.join(art, "fakeop")) + os.makedirs(os.path.join(build, "lib")) + os.makedirs(libdir) + # A generated source, so main() gets past the "nothing to embed" return. + open(os.path.join(art, "fakeop", "aot_fakeop_cuda.cpp"), "w").close() + include = os.path.join(art, gen_aot_lib.CMAKE_INCLUDE) + if stale_include: + # What a PREVIOUS run wired up. caffe2/CMakeLists.txt include()s this + # file unconditionally, so it is linked by every later configure. + with open(include, "w") as f: + f.write('target_sources(torch_cuda PRIVATE "/x/k.o")\n') + # A dependency of torch_cuda, which the relink may rebuild and stage 2 + # does not install. + with open(os.path.join(build, "lib", "libtorch_cpu.so"), "wb") as f: + f.write(b"AS-BUILT") + if built: + with open(os.path.join(build, "lib", "libtorch_cuda.so"), "wb") as f: + f.write(self.NEW) + target = os.path.join(libdir, "libtorch_cuda.so") + if installed: + with open(target, "wb") as f: + f.write(self.OLD) + if cache: + with open(os.path.join(build, "CMakeCache.txt"), "w") as f: + f.write("CUDAToolkit_VERSION_MAJOR:STRING=13\n") + if cmake_command: + f.write(f"CMAKE_COMMAND:INTERNAL={cmake_command}\n") + for obj, name, value in ( + (build_stage2, "BUILD_DIR", build), + (build_stage2, "NATIVE_AOT_ARTIFACTS_DIR", art), + (build_stage2, "should_run", lambda: verdict), + (build_stage2, "require_runtimes", require_runtimes), + (build_stage2, "_installed_lib_dir", lambda: libdir), + (build_stage2, "_arch_list", lambda: ""), + (build_stage2, "_dsl_runtime_archive", lambda: None), + (build_stage2, "_torch_probe", lambda e: embedded_after), + (build_stage2, "patch_wheel", lambda w, lib: patched.append((w, lib))), + (subprocess, "call", fake_call), + (subprocess, "run", fake_run), + ): + stack.enter_context(mock.patch.object(obj, name, value)) + if kill_swap: + real_replace = os.replace + + def failing_replace(a, b): + if str(a).endswith(".tmp"): + raise KeyboardInterrupt("killed at the swap") + return real_replace(a, b) + + stack.enter_context(mock.patch.object(os, "replace", failing_replace)) + err = stack.enter_context(contextlib.redirect_stderr(io.StringIO())) + args = [] + if wheel: + whl = os.path.join(d, "torch-0.0.0-cp312-cp312-linux_x86_64.whl") + open(whl, "w").close() + args = ["--wheel", whl] + try: + outcome = f"returned {build_stage2.main(args)}" + except (RuntimeError, KeyboardInterrupt) as e: + outcome = str(e) + content = None + if os.path.exists(target): + with open(target, "rb") as f: + content = f.read() + children += [f"wheel:{w}|{lib}" for w, lib in patched] + yield types.SimpleNamespace( + outcome=outcome, + content=content, + children=children, + commands=commands, + listing=sorted(os.listdir(libdir)), + argv=argv[-1] if argv else [], + kwargs=kwargs, + include=include, + reported=err.getvalue(), + ) + + def test_a_declined_verdict_does_nothing_at_all(self): + # Every other fixture patches should_run() True, so this pins the call itself. + with self._main(verdict=False) as run: + self.assertEqual(run.outcome, "returned 0") + self.assertEqual(run.children, []) + self.assertEqual(run.content, self.OLD) + + def test_a_declining_run_disables_what_a_previous_one_wired_up(self): + # build_all.sh shares one build/ across eight interpreters (see the docstring). + with self._main(verdict=False, stale_include=True) as run: + self.assertEqual(run.outcome, "returned 0") + with open(run.include) as f: + emitted = f.read() + self.assertIn("Nothing to embed", emitted) + self.assertNotIn("target_sources", emitted) + + def test_a_declining_run_creates_no_include_where_there_was_none(self): + # A file created where none existed is invisible until the NEXT configure. + with self._main(verdict=False) as run: + self.assertFalse(os.path.exists(run.include)) + + def test_a_missing_runtime_stops_before_the_export(self): + # The stack's one deliberate build failure: past the gates with no DSL runtime, + # failing beats shipping a wheel missing its declared kernels. + with self._main(runtimes="cutlass is not installed") as run: + self.assertIn("cutlass is not installed", run.outcome) + self.assertEqual(run.children, []) + self.assertEqual(run.content, self.OLD) + + def test_a_build_dir_without_a_cache_is_refused_before_any_cmake(self): + # `cmake -B` on a missing directory exits 0 and configures FROM SCRATCH. + with self._main(cache=False) as run: + self.assertIn("holds no CMakeCache.txt", run.outcome) + self.assertEqual(run.content, self.OLD) + self.assertNotIn("reconfigure", run.children) + + def test_a_failing_reconfigure_shows_what_cmake_said(self): + # CMake prints most of its failure context on stdout, so this arm captures both + # streams and echoes them; DEVNULL leaves a failing configure unreadable. + with self._main(configure_rc=1) as run: + self.assertIn("reconfiguring", run.outcome) + self.assertIn("exit 1", run.outcome) + self.assertIn("embedding 1 object(s)", run.reported) + self.assertIn("cmake said no", run.reported) + self.assertEqual(run.content, self.OLD) + + def test_a_reconfigure_that_does_not_embed_is_refused_before_the_relink(self): + # The STATUS line the generated CMake prints is the only pre-relink evidence that + # the build agrees it should embed, so a disagreement must stop here. + with self._main(configure_embeds=False) as run: + self.assertIn("did not report embedding", run.outcome) + self.assertEqual(run.content, self.OLD) + self.assertEqual(run.children[-1], "reconfigure") + + def test_a_child_that_dies_names_the_step_and_the_signal(self): + # check_call's CalledProcessError names neither, and a 32-way export is what an + # OOM killer reaches for first. + with self._main(child_rc=-9) as run: + self.assertIn("exporting kernels", run.outcome) + self.assertIn("signal 9", run.outcome) + self.assertEqual(run.content, self.OLD) + + def test_a_relink_that_produced_no_library_says_so(self): + with self._main(built=False) as run: + self.assertIn("expected relinked library", run.outcome) + self.assertEqual(run.content, self.OLD) + + def test_a_torch_whose_lib_never_held_the_library_is_refused(self): + # _installed_lib_dir found *a* torch; writing a library into a layout that + # never had one means we are pointed at the wrong environment. + with self._main(installed=False) as run: + self.assertIn("not the one this tree built", run.outcome) + self.assertEqual(run.listing, []) + + def test_the_happy_path_replaces_the_installed_library(self): + # ...and the guards above do not block the case they exist to protect. + with self._main() as run: + self.assertEqual(run.outcome, "returned 0") + self.assertEqual(run.content, self.NEW) + self.assertIn("reconfigure", run.children) + # NOTHING else left behind: a stray copy in site-packages carries a name + # no RECORD lists, which `pip uninstall` cannot clear. + self.assertEqual(run.listing, ["libtorch_cuda.so"]) + + def test_a_kill_at_the_swap_leaves_the_library_and_no_temporary(self): + # One os.replace, so the installed library never stops existing. + with self._main(kill_swap=True) as run: + self.assertIn("killed at the swap", run.outcome) + self.assertEqual(run.content, self.OLD) + self.assertEqual(run.listing, ["libtorch_cuda.so"]) + + def test_a_failed_verification_says_what_state_it_leaves(self): + # No restore copy is kept, so the error has to say how to get back. + with self._main(embedded_after=False) as run: + self.assertIn("reports no embedded kernels", run.outcome) + self.assertIn("reinstall the wheel", run.outcome) + self.assertEqual(run.content, self.NEW) + self.assertEqual(run.listing, ["libtorch_cuda.so"]) + + def test_the_report_names_the_bytes_the_kernels_added(self): + # The delta is what the line is for: one added arch can grow a wheel by tens of + # MiB, and nothing else in the build log states it. + with self._main(grew=(3 << 20) + len(self.NEW)) as run: + self.assertEqual(run.outcome, "returned 0") + self.assertIn("3 MiB relinked into", run.reported) + self.assertIn("+3 MiB of embedded kernels", run.reported) + + def test_the_reconfigure_forces_status_messages(self): + # A cached CMAKE_MESSAGE_LOG_LEVEL would hide the marker; the flag wins. + with self._main() as run: + self.assertEqual(run.outcome, "returned 0") + self.assertIn("--log-level=STATUS", run.argv) + # ...and BOTH streams are captured, or configure.stdout is None and the + # `in` test below raises TypeError. + self.assertIs(run.kwargs.get("capture_output"), True) + + def test_a_reconfigure_that_changes_the_cache_is_refused(self): + # EnvVarForwarding FORCEs env vars into the cache, so a stage-2 run in another + # environment would relink torch_cuda against different settings. + drifted = "//From environment\nCUDAToolkit_VERSION_MAJOR:STRING=12\n" + with self._main(cache_after=drifted) as run: + self.assertIn("changed this build's configuration", run.outcome) + self.assertIn("CUDAToolkit_VERSION_MAJOR", run.outcome) + self.assertIn("From environment", run.outcome) + # Refused BEFORE the relink, so nothing reached the installed torch. + self.assertNotIn("--build", run.children) + self.assertEqual(run.content, self.OLD) + + def test_cache_entries_the_reconfigure_adds_are_not_drift(self): + # Only a CHANGED value is drift: a reconfigure legitimately adds entries. + grown = "CUDAToolkit_VERSION_MAJOR:STRING=13\nNEW_ENTRY:BOOL=ON\n" + with self._main(cache_after=grown) as run: + self.assertEqual(run.outcome, "returned 0") + self.assertEqual(run.content, self.NEW) + + def test_a_relink_that_rebuilt_a_dependency_is_refused(self): + # --target torch_cuda builds its dependencies too, and only torch_cuda ships. + with self._main(rebuild_sibling=True) as run: + self.assertIn("also rebuilt libtorch_cpu.so", run.outcome) + self.assertEqual(run.content, self.OLD) + + def test_a_new_env_sourced_cache_entry_is_drift(self): + # EnvVarForwarding creates the entry when the build had none. + added = "//From environment\nWERROR:STRING=1\n" + with self._main(cache_after=added) as run: + self.assertIn("WERROR: absent -> STRING=1", run.outcome) + self.assertNotIn("--build", run.children) + + def test_both_children_run_the_cmake_that_configured_the_build(self): + # CMAKE_COMMAND is often a pip wheel's cmake, not the one on PATH. + with self._main(cmake_command=sys.executable) as run: + self.assertEqual(run.outcome, "returned 0") + cmakes = [c[0] for c in run.commands if c[1].startswith("--")] + self.assertEqual(cmakes, [sys.executable, sys.executable], run.commands) + + def test_the_wheel_is_patched_with_the_relinked_library(self): + # Nothing downstream re-checks the wheel: the AOT tests skip without kernels. + with self._main(wheel=True) as run: + self.assertEqual(run.outcome, "returned 0") + patched = [c for c in run.children if c.startswith("wheel:")] + self.assertEqual(len(patched), 1, f"main() did not patch: {run.children}") + whl, lib = patched[0][len("wheel:") :].split("|") + self.assertTrue(whl.endswith(".whl"), whl) + # The RELINKED library, not the installed copy: same basename, so a test + # asserting only the name would pass on either. + self.assertEqual(lib.split(os.sep)[-3:], ["build", "lib", "libtorch_cuda.so"]) + + def test_a_wheel_that_does_not_exist_is_refused_before_the_export(self): + # should_run is FALSE deliberately: this check sits ahead of it. + with tempfile.TemporaryDirectory() as d: + with ( + mock.patch.object(build_stage2, "_torch_probe", lambda e: True), + mock.patch.object(build_stage2, "should_run", lambda: False), + mock.patch.dict(os.environ, {"TORCH_NATIVE_AOT": ""}, clear=False), + mock.patch.object(build_stage2, "NATIVE_AOT_ARTIFACTS_DIR", d), + contextlib.redirect_stderr(io.StringIO()), + ): + with self.assertRaisesRegex(RuntimeError, "does not exist"): + build_stage2.main(["--wheel", os.path.join(d, "nope.whl")]) + + class TestStaticBuildSkips(unittest.TestCase): def test_build_shared_libs_off_skips_cleanly(self): # caffe2/CMakeLists.txt refuses to embed into a static torch_cuda, CMake @@ -3969,6 +4995,18 @@ class TestOrphanCheckIsCalled(unittest.TestCase): self.assertTrue(jobs, "the export still runs; the orphan is only disk") +class TestModulePathOrder(unittest.TestCase): + def test_the_repo_root_is_appended_never_prepended(self): + # The repo root's torch/ SOURCE tree shadows the installed wheel if inserted. + for mod in ("build_stage2.py", "export.py", "gen_aot_lib.py"): + with self.subTest(module=mod): + with open(os.path.join(REPO, "tools", "native_aot", mod)) as f: + src = f.read() + self.assertIn("sys.path.append(REPO)", src) + # Space-stripped, so both spellings are one check. + self.assertNotIn("sys.path.insert(0,REPO)", src.replace(" ", "")) + + class TestEmittedCMake(unittest.TestCase): """caffe2/CMakeLists.txt is one include() and holds no logic, so every decision is emitted here. These assert the emitted CALLS, which is what CMake consumes.""" @@ -3994,6 +5032,34 @@ class TestEmittedCMake(unittest.TestCase): with open(path) as f: return f.read() + def test_caffe2_cmakelists_holds_no_logic(self): + # The point of the restructure: one include, and nothing to get wrong. If a + # future change starts deriving things in CMake again, this fails. + with open(os.path.join(REPO, "caffe2", "CMakeLists.txt")) as f: + cmake = f.read() + naot = [ + l + for l in cmake.splitlines() + if "native_aot" in l and not l.strip().startswith("#") + ] + self.assertEqual( + naot, + [' include("${CMAKE_BINARY_DIR}/native_aot/native_aot.cmake" OPTIONAL)'], + "caffe2/CMakeLists.txt should contain exactly one native-AOT line", + ) + for gone in ("file(STRINGS", "ARCH_LIST_ABSENT", "_native_aot_normalize"): + with self.subTest(construct=gone): + self.assertNotIn(gone, cmake) + + def test_the_include_line_agrees_with_the_python_constants(self): + # Derived from both constants, so moving either fails the literal above. + rel = os.path.relpath( + build_stage2.NATIVE_AOT_ARTIFACTS_DIR, build_stage2.BUILD_DIR + ) + want = f'"${{CMAKE_BINARY_DIR}}/{rel}/{gen_aot_lib.CMAKE_INCLUDE}"' + with open(os.path.join(REPO, "caffe2", "CMakeLists.txt")) as f: + self.assertIn(want, f.read()) + def test_the_blocks_are_separated_by_one_blank_line(self): # The layout lives in the join, not the templates: gluing the blocks together # still emits valid CMake, so nothing else here would notice.