Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 48 additions & 6 deletions CorridorKeyModule/inference_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,31 @@
logger = logging.getLogger(__name__)


def _try_activate_msvc() -> None:
"""Find and activate MSVC (cl.exe) on Windows if installed but not in PATH.

Searches common Visual Studio install locations for the latest cl.exe
and adds its directory to PATH.
"""
import glob

patterns = [
r"C:\Program Files\Microsoft Visual Studio\2022\*\VC\Tools\MSVC\*\bin\Hostx64\x64\cl.exe",
r"C:\Program Files (x86)\Microsoft Visual Studio\2022\*\VC\Tools\MSVC\*\bin\Hostx64\x64\cl.exe",
r"C:\Program Files\Microsoft Visual Studio\2019\*\VC\Tools\MSVC\*\bin\Hostx64\x64\cl.exe",
]

for pattern in patterns:
matches = sorted(glob.glob(pattern), reverse=True) # newest version first
if matches:
cl_dir = os.path.dirname(matches[0])
os.environ["PATH"] = cl_dir + os.pathsep + os.environ.get("PATH", "")
logger.info("Auto-detected MSVC: %s", matches[0])
return

logger.debug("MSVC not found in standard locations")


class CorridorKeyEngine:
def __init__(
self,
Expand Down Expand Up @@ -58,12 +83,29 @@ def __init__(
self._is_rocm = hasattr(torch.version, "hip") and torch.version.hip
self.model = self._load_model()

# torch.compile is tested on CUDA (Windows + Linux) and ROCm (Linux).
# ROCm on Windows hangs during Triton kernel compilation — skip it.
# CORRIDORKEY_SKIP_COMPILE=1 forces eager mode (useful for testing).
skip_compile = (self._is_rocm and sys.platform == "win32") or os.environ.get("CORRIDORKEY_SKIP_COMPILE") == "1"
if skip_compile:
logger.info("Skipping torch.compile (eager mode)")
# torch.compile needs: cl.exe (Windows), gcc (Linux), and Triton.
# Check prerequisites and skip with a helpful message if missing.
import shutil

# Auto-detect MSVC on Windows — it's installed but not in PATH by default
if sys.platform == "win32" and not shutil.which("cl"):
_try_activate_msvc()

skip_reason = None
if self._is_rocm and sys.platform == "win32":
skip_reason = "ROCm on Windows — Triton compilation hangs"
elif os.environ.get("CORRIDORKEY_SKIP_COMPILE") == "1":
skip_reason = "CORRIDORKEY_SKIP_COMPILE=1"
elif sys.platform == "win32" and not shutil.which("cl"):
skip_reason = (
"MSVC (cl.exe) not found. Install Visual Studio Build Tools "
"for ~30% faster inference: https://visualstudio.microsoft.com/visual-cpp-build-tools/"
)
elif sys.platform == "linux" and not shutil.which("gcc") and not shutil.which("cc"):
skip_reason = "no C compiler found — install gcc for faster inference"

if skip_reason:
logger.info("Skipping torch.compile (%s)", skip_reason)
elif sys.platform == "linux" or sys.platform == "win32":
self._compile()

Expand Down