Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

12.7 KB
Raw Blame
# SPDX-License-Identifier: AGPL-3.0-only
# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0

"""Gates torch.compile on Windows: triton imports without CRT headers, and clang-cl ignores INCLUDE."""

from __future__ import annotations

import os
import sys
import logging

logger = logging.getLogger(__name__)


def _headers_complete(dirs) -> bool:
    """`stdlib.h` alone is a standalone SDK: its ucrt pulls in `vcruntime.h` from the VC toolset,
    so the compile still dies there (measured with the toolset dir removed). Both, or not whole."""

    def found(name: str) -> bool:
        return any(d and os.path.isfile(os.path.join(d, name)) for d in dirs)

    return found("stdlib.h") and found("vcruntime.h")


def _have_crt_headers() -> bool:
    return _headers_complete(os.environ.get("INCLUDE", "").split(os.pathsep))


def _triton_include_dirs() -> list | None:
    """None means the search could not be run, which is not the same as running and finding nothing.
    Only the latter may gate: `find_msvc_winsdk` is private, so a rename or a changed return arity
    would otherwise read as "no headers" and disable torch.compile on a machine with Visual Studio."""
    try:
        from triton.windows_utils import find_msvc_winsdk  # noqa: PLC0415
        _, inc_dirs, _ = find_msvc_winsdk()
    except Exception:  # noqa: BLE001
        logger.debug("Triton's MSVC/WinSDK discovery is unavailable", exc_info = True)
        return None
    return list(inc_dirs)


def _triton_finds_crt_headers() -> bool | None:
    dirs = _triton_include_dirs()
    return None if dirs is None else _headers_complete(dirs)


def _triton_is_triton_windows() -> bool:
    """XPU Triton and triton-windows own the same top-level name; only the distribution says which."""
    try:
        import importlib.metadata as md  # noqa: PLC0415
        dists = md.packages_distributions().get("triton") or ()
    except Exception:  # noqa: BLE001
        logger.debug("Could not resolve which distribution owns `triton`", exc_info = True)
        return False
    return any(d.lower().replace("_", "-") == "triton-windows" for d in dists)


def _rocm_clang_cl_present() -> bool:
    import sysconfig  # noqa: PLC0415
    return os.path.isfile(
        os.path.join(
            sysconfig.get_path("platlib"), "_rocm_sdk_core", "lib", "llvm", "bin", "clang-cl.exe"
        )
    )


def _cc_needs_msvc_headers(cc: str) -> bool:
    """Imported apart from get_cc: is_clang_cl only exists in triton-windows 3.5.1.post23 and later."""
    try:
        from triton.runtime.build import is_clang_cl, is_msvc  # noqa: PLC0415
        return bool(is_msvc(cc) or is_clang_cl(cc))
    except Exception:  # noqa: BLE001
        logger.debug("Triton's compiler predicates are unavailable", exc_info = True)
        return os.path.basename(str(cc)).lower() in ("cl", "cl.exe", "clang-cl", "clang-cl.exe")


def _triton_cc() -> str:
    """Newest triton-windows renamed get_cc to _find_compiler(language); else the wheel layout decides."""
    try:
        from triton.runtime.build import get_cc  # noqa: PLC0415
    except ImportError:
        from triton.runtime.build import _find_compiler  # noqa: PLC0415
        return _find_compiler("c")
    return get_cc()


def _needs_msvc_headers() -> bool:
    """Only cl and clang-cl need MSVC headers; unanswerable means ungated, as gating wrongly breaks it."""
    try:
        cc = _triton_cc()
    except Exception:  # noqa: BLE001
        logger.debug("Triton's compiler selection is unavailable", exc_info = True)
        return _triton_is_triton_windows() and _rocm_clang_cl_present()
    return _cc_needs_msvc_headers(cc)


def _toolchain_summary() -> str:
    """0 include dirs: no Visual Studio. Only vcruntime.h missing: an SDK without VC toolset."""
    try:
        cc = os.path.basename(_triton_cc())
    except Exception:  # noqa: BLE001
        cc = "unknown"
    try:
        from triton.windows_utils import find_msvc_winsdk  # noqa: PLC0415
        _, inc_dirs, _ = find_msvc_winsdk()
    except Exception:  # noqa: BLE001
        inc_dirs = []
    logger.debug("Triton include dirs: %s", list(inc_dirs))
    missing = [
        h
        for h in ("stdlib.h", "vcruntime.h")
        if not any(d and os.path.isfile(os.path.join(d, h)) for d in inc_dirs)
    ]
    return (
        f"compiler={cc}, Triton include dirs={len(inc_dirs)}, "
        f"missing headers={','.join(missing) or 'none'}, "
        f"INCLUDE={'set' if os.environ.get('INCLUDE') else 'unset'}"
    )


def _compiles_a_trivial_translation_unit(cc: str, inc_dirs) -> bool | None:
    """Predicting from directories wrongly disables torch.compile; clang-cl finds MSVC itself."""
    import subprocess  # noqa: PLC0415
    import tempfile  # noqa: PLC0415

    try:
        with tempfile.TemporaryDirectory() as tmp:
            src = os.path.join(tmp, "probe.c")
            with open(src, "w", encoding = "utf-8") as fh:
                fh.write("#include <stdlib.h>\nint main(void){return 0;}\n")
            # Syntax-only: no link, so a missing lib cannot pass as a missing header.
            argv = [cc, "/Zs", src] + [f"/I{d}" for d in inc_dirs if d]
            done = subprocess.run(
                argv,
                cwd = tmp,
                capture_output = True,
                timeout = 90,
            )
    except Exception:  # noqa: BLE001
        logger.debug("The compiler probe could not be run", exc_info = True)
        return None
    if done.returncode != 0:
        logger.debug("Compiler probe failed: %s", (done.stderr or b"")[-400:])
    return done.returncode == 0


def _bundled_tcc() -> str | None:
    import sysconfig  # noqa: PLC0415
    path = os.path.join(sysconfig.get_path("platlib"), "triton", "runtime", "tcc", "tcc.exe")
    return path if os.path.isfile(path) else None


def _clear_compiler_cache() -> None:
    """`_find_compiler` is lru_cached in triton-windows 3.8, so a CC set after its first call is otherwise ignored."""
    try:
        from triton.runtime import build  # noqa: PLC0415
    except Exception:  # noqa: BLE001
        return
    for name in ("_find_compiler", "get_cc"):
        clear = getattr(getattr(build, name, None), "cache_clear", None)
        if callable(clear):
            clear()


def _triton_driver_builds() -> bool:
    """Creating the active driver compiles its C helper (`hip_utils.c` on AMD) with the selected compiler."""
    try:
        _clear_compiler_cache()
        from triton.runtime import driver  # noqa: PLC0415

        driver.active.get_current_target()
        return True
    except Exception:  # noqa: BLE001
        logger.debug("Triton's driver did not build with the bundled TinyCC", exc_info = True)
        return False


def _switch_to_bundled_tcc() -> bool:
    """clang-cl without the CRT headers (#9897): build with the TinyCC triton-windows bundles, which carries its own
    and is what it already picks when no ROCm wheel is installed. Kept only if Triton's driver then builds; an
    explicit CC is the user's choice."""
    if os.environ.get("CC"):
        return False
    tcc = _bundled_tcc()
    if tcc is None:
        return False
    os.environ["CC"] = tcc
    if _triton_driver_builds():
        logger.info(
            "Triton's C toolchain has no CRT headers; building with its bundled TinyCC (%s)", tcc
        )
        return True
    os.environ.pop("CC", None)
    _clear_compiler_cache()
    return False


def crt_headers_reachable() -> bool:
    """Whether Triton's JIT can build its C helpers here, switching to the bundled TinyCC when clang-cl cannot."""
    return _crt_headers_reachable() or _switch_to_bundled_tcc()


def _crt_headers_reachable() -> bool:
    if sys.platform != "win32":
        return True
    if not _needs_msvc_headers():
        return True
    triton_dirs = _triton_include_dirs()

    # Ask the compiler first: directory inference over-gates (clang-cl's own MSVC search) and
    # under-gates (vcruntime.h needs sal.h from the SDK shared dir).
    if triton_dirs is not None:
        try:
            verdict = _compiles_a_trivial_translation_unit(_triton_cc(), triton_dirs)
        except Exception:  # noqa: BLE001
            verdict = None
        if verdict is not None:
            return verdict

    # Fallback when the probe could not run; unknown is not evidence, so fail open.
    env_dirs = os.environ.get("INCLUDE", "").split(os.pathsep)
    if _headers_complete(env_dirs):
        return True
    # INCLUDE is unset outside a Developer Command Prompt; only Triton's empty search may gate.
    if triton_dirs is None:
        return True
    # Judge the union: clang-cl reads INCLUDE and Triton passes its dirs as /I.
    return _headers_complete(triton_dirs + env_dirs)


def _triton_windows_spec() -> str:
    """The triton-windows range for the resident torch, as studio/setup.ps1 installs it.

    Read from package metadata, not `import torch`: this runs before torch is imported. torch 2.12 and
    2.13 pin triton 3.7 (2.12.0 exactly 3.7.0) and 2.14 pins 3.8; anything older (or unreadable)
    keeps the long-standing <3.7. A ROCm torch 2.11 / 2.12 gets 3.8, the build setup.ps1 pairs it with.
    A native ARM64 interpreter gets setup.ps1's Windows-on-ARM floor: no win_arm64 wheel predates it.
    """
    import platform  # noqa: PLC0415

    if platform.machine().lower() == "arm64":
        return "triton-windows>=3.8.0.post28"
    try:
        import re  # noqa: PLC0415
        from importlib.metadata import version  # noqa: PLC0415

        # A dev build's patch ("2.13.0a0+git...") still has the major and minor.
        match = re.match(r"(\d+)\.(\d+)(?:\.(\d+))?", version("torch"))
        major, minor = int(match.group(1)), int(match.group(2))
        patch = int(match.group(3)) if match.group(3) is not None else None
    except Exception:  # noqa: BLE001 -- a hint must never raise
        return "triton-windows<3.7"
    # setup.ps1's ROCm pairing: 3.9 has no block pointers for 2.11 / 2.12's flex_decoding, 3.6 has no HIP headers.
    if major == 2 and minor in (11, 12) and _torch_is_rocm_build():
        return "triton-windows>=3.8,<3.9"
    if (major, minor, patch) == (2, 12, 0):
        return "triton-windows>=3.7,<3.7.1"
    if (major, minor) in ((2, 12), (2, 13)):
        return "triton-windows>=3.7,<3.8"
    if (major, minor) == (2, 14):
        return "triton-windows>=3.8,<3.9"
    return "triton-windows<3.7"


def _torch_is_rocm_build() -> bool:
    # Off disk, no torch import: AMD's untagged Windows wheel (2.8.0a0+git...) is only told by version.py's hip field.
    try:
        from core._torchao_stub import _installed_torch_is_rocm  # noqa: PLC0415
        return _installed_torch_is_rocm() is True
    except Exception:  # noqa: BLE001
        logger.debug("Could not read the installed torch's ROCm signals", exc_info = True)
        return False


def gate_torch_compile_on_windows(log: logging.Logger) -> None:
    if sys.platform != "win32":
        return
    if _torch_is_rocm_build():
        try:
            # triton-windows >= 3.8 imported first loads System32 amdhip64_7.dll, then torch_hip.dll fails with WinError 126.
            import torch  # noqa: F401, PLC0415
        except Exception:  # noqa: BLE001
            logger.debug("The torch preload failed; probing triton anyway", exc_info = True)
    try:
        import triton  # noqa: F401, PLC0415
    except ImportError:
        os.environ["TORCHDYNAMO_DISABLE"] = "1"
        log.warning(
            "Triton not found on Windows — torch.compile disabled. "
            f'Install for better performance: pip install "{_triton_windows_spec()}"'
        )
        return

    # Intel's XPU Triton picks its compiler differently from triton-windows and needs cl set up for it (#14807).
    from ._xpu_triton_toolchain import prepare_xpu_triton_compiler  # noqa: PLC0415

    prepare_xpu_triton_compiler(log)

    try:
        reachable = crt_headers_reachable()
    except Exception:  # noqa: BLE001 -- a probe must not take down the worker it exists to protect
        logger.debug("The toolchain probe raised; leaving torch.compile alone", exc_info = True)
        reachable = True
    if reachable:
        log.info("Triton available — torch.compile enabled")
        return

    os.environ["TORCHDYNAMO_DISABLE"] = "1"
    # This only turns off the compiles we own; Unsloth's @triton.jit kernels still need the toolchain.
    log.warning(
        "Triton is installed but its C toolchain has no CRT headers, so its "
        "clang-cl JIT would fail on 'stdlib.h' (#7595). torch.compile disabled; "
        "directly launched Triton kernels still need the toolchain. "
        "Install Visual Studio Build Tools (C++ workload): winget "
        'install Microsoft.VisualStudio.2022.BuildTools --override "--add '
        'Microsoft.VisualStudio.Workload.VCTools --includeRecommended". '
        "Probe saw: %s.",
        _toolchain_summary(),
    )