Skip to content

Studio: give Intel XPU Triton a working C++ toolchain on Windows - #14814

Merged
danielhanchen merged 10 commits into
unslothai:mainfrom
debojyoti-dev:fix/xpu-windows-triton-compiler-14807
Oct 23, 2026
Merged

danielhanchen merged 10 commits into
unslothai:mainfrom
debojyoti-dev:fix/xpu-windows-triton-compiler-14807

Conversation

@debojyoti-dev

@debojyoti-dev debojyoti-dev commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #14807

Before: on Windows with an Intel GPU, chat with a local model through the Transformers path (for example a LoRA adapter) failed with "An internal error occurred." Intel's XPU Triton builds small SYCL helpers on first use with icpx, else cl from PATH, else $CXX. Studio is not started from a Developer Command Prompt, so cl is not on PATH and the build stopped at "Failed to find C++ compiler". Setting CXX did not help: Triton picks MSVC flags only when the path contains the literal cl.EXE, so cl.exe received GNU flags (D8021 on /Wno-psabi), and clang++ received the MSVC /LIBPATH: flags Triton always adds on Windows. Past the compiler, the helpers also include level_zero/ze_api.h and link ze_loader.lib, which neither the Intel GPU driver nor the pip wheels ship.

After: when XPU Triton is installed on Windows, Studio

  • loads the Visual Studio C++ environment (found with vswhere, then vcvars64.bat) if it is not already loaded, and sets CXX to cl spelled the way Triton recognises;
  • if no Level Zero SDK is found (LEVEL_ZERO_V1_SDK_PATH, ZE_PATH, or INCLUDE), fetches the official level-zero-win-sdk-1.34.0.zip (317 KB, MIT) once into Studio's cache, checks its SHA-256, and points LEVEL_ZERO_V1_SDK_PATH at it. Under UV_OFFLINE or an operator-set HF offline mode it uses only a cached copy and otherwise logs how to install the SDK by hand.

It leaves things alone when icpx is available or CXX already names icpx, does nothing for triton-windows (NVIDIA / AMD) or off Windows, and never raises. A CXX naming a GNU driver such as clang++ is left as set, with a warning. No Visual Studio found logs how to install the Build Tools. The discovery subprocesses run without a console window, and vcvars output is read as UTF-16 so non-ASCII PATH entries survive.

How: core/_xpu_triton_toolchain.py, called from gate_torch_compile_on_windows, which the inference, training and export workers already run on Windows. Only PATH, INCLUDE, LIB and LIBPATH are taken from vcvars. Telling Intel's Triton from triton-windows is a directory check on the already-imported package (backends/intel), so other Windows users pay no package scan.

Evidence (windows-latest, torch 2.10.0+xpu, triton-xpu 3.6.0, building Triton's real arch_utils and spirv_utils helpers; the runner has no Intel GPU, so Triton's is_xpu() is forced on, which is the only branch a real Arc machine takes):

setup main this PR
Studio as launched Failed to find C++ compiler both helpers compile and link; SDK fetched into the Studio cache
second start same cached SDK reused, no download
CXX=cl.exe D8021 invalid numeric argument '/Wno-psabi' MSVC flags, builds
CXX=clang++ no such file or directory: '/LIBPATH:...' left as set, warning
offline same warning naming the SDK and where to get it
triton-windows CXX and PATH untouched CXX and PATH untouched

spirv_utils cannot be loaded on the runner because ze_loader.dll comes with the Intel GPU driver; it compiles and links.

Tests: studio/backend/tests/test_xpu_triton_toolchain_14807.py (discovery stubbed, runs on Linux CI) covers loading the environment and pointing Triton at cl, re-spelling cl.exe, leaving clang++ and icpx alone, the Intel backend check, vcvars UTF-16 parsing, SDK detection and provisioning, archive unpacking with backslash member names, a bad hash or unsafe member leaving nothing behind, offline mode, hidden console windows, and the Windows gate calling the new step. tests/test_msvc_env_7595.py still passes. Staging CI is green on Linux, macOS and Windows. Not covered: a real Intel Arc GPU running a kernel end to end.

Intel's XPU Triton builds its SYCL helpers with icpx, else cl from PATH,
else $CXX. Studio is not started from a Developer Command Prompt, so cl
is not on PATH and every chat on an Intel GPU failed with "Failed to
find C++ compiler". Setting CXX did not help either: Triton picks MSVC
flags only when the path contains the literal "cl.EXE", so "cl.exe" got
GNU flags (D8021 on /Wno-psabi), and clang++ got the MSVC /LIBPATH:
flags Triton always adds on Windows.

When XPU Triton is installed on Windows, load the Visual Studio C++
environment through vswhere and vcvars64.bat if it is missing, and set
CXX to cl spelled the way Triton recognises. A GNU driver in CXX is left
as the user set it, with a warning that explains why it cannot work.

Refs unslothai#14807
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@danielhanchen

Copy link
Copy Markdown
Member

@codex security review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-23T09:18:49.164910Z 8a9b537 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5d699054bb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

root = os.environ.get("LEVEL_ZERO_V1_SDK_PATH") or os.environ.get("ZE_PATH")
dirs = [os.path.join(root, "include")] if root else []
dirs += os.environ.get("INCLUDE", "").split(os.pathsep)
return any(d and os.path.isfile(os.path.join(d, "level_zero", "ze_api.h")) for d in dirs)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Require the Level Zero loader library before reporting the SDK

On a Windows XPU host where ze_api.h is present but ze_loader.lib is absent—for example, a header-only SDK or a Level Zero include added to INCLUDE without its library added to LIB—_level_zero_sdk_found() returns true here. _prepare() then omits the only Level Zero warning even though the helper link described immediately above still fails, leaving chat/compile with an unexplained linker error; verify the import library in the SDK/library search paths as well as the header.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing this: the official level-zero-win-sdk zip ships include/level_zero/ze_api.h and lib/ze_loader.lib together, and Triton derives both paths from the same LEVEL_ZERO_V1_SDK_PATH root, so a header-only state is not a real install. This check only decides whether a log hint is printed.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5d699054bb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +157 to +159
if not _msvc_environment_loaded() and _load_msvc_environment():
log.info("Loaded the Visual Studio C++ environment for Intel XPU Triton")
cl = shutil.which(cxx or "cl")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Require a complete MSVC environment before accepting cl

When cl is already on PATH (or CXX points to it) but INCLUDE/LIB are not initialized, a failed or partial _load_msvc_environment() falls through here and line 159 still finds the executable; line 167 then advertises it to Triton even though this module's own _msvc_environment_loaded() predicate is still false. MSVC cannot compile and link the XPU helper without those CRT headers and libraries, so this changes the startup failure into a later JIT error; only accept cl after rechecking the loaded environment, and otherwise emit the Build Tools warning.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing this: reaching this line with cl found but no INCLUDE/LIB needs cl on PATH or in CXX while vswhere finds no Visual Studio with the C++ tools, which a Build Tools install does not produce. That state fails on main too (D8021 there), so the only difference would be one extra log line.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1bdfe6437d

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

import urllib.request # noqa: PLC0415
import zipfile # noqa: PLC0415

with urllib.request.urlopen(_L0_URL, timeout = 30) as response:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Honor offline mode before fetching the SDK

When a Windows XPU worker has cl but no cached Level Zero SDK and the operator has set UV_OFFLINE=1 (or a user-set HF_HUB_OFFLINE/TRANSFORMERS_OFFLINE), this unconditional call still reaches GitHub and can wait for the 30-second timeout. studio/backend/utils/utils.py:43-56 defines those switches as forbidding on-demand installs, so this path should return the existing manual-install warning without networking while still accepting a complete cached SDK.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 5628ac3: the fetch now follows package_installs_offline(), so UV_OFFLINE or an operator-set HF offline only uses a cached SDK and otherwise prints the manual-install hint.

target.write_bytes(archive.read(member))
# Another worker may have won the race; its copy is the same pinned archive.
try:
os.replace(staging, dest)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Replace an incomplete SDK cache before installing

When the versioned cache directory already exists but is incomplete—for example, antivirus or cache cleanup removed lib/ze_loader.lib—_provide_level_zero_sdk() calls this fetch, but on Windows os.replace(staging, dest) cannot replace the remaining non-empty directory. The exception path re-raises because that destination is still incomplete, so every later worker repeats the failure and XPU remains unusable until the user discovers and deletes the internal cache directory; quarantine or remove an incomplete destination before atomically installing the staging tree.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not changing this: the SDK is unpacked into a fresh staging directory and moved into place in one rename, so Studio never leaves a partial cache directory. A partial one needs a third party deleting files out of Studio's cache, which this does not try to repair.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5628ac328f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

if not os.path.isfile(vswhere):
return None
try:
done = subprocess.run(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3 Badge Hide the toolchain discovery subprocesses

When Studio starts an XPU inference, training, or export worker from the Windows desktop shell without an attached console, both new subprocess.run calls launch console-subsystem executables without the repository's windows_hidden_subprocess_kwargs; vswhere.exe and especially the cmd.exe/vcvars64.bat call can therefore create visible console windows on every worker start. Pass the helper's kwargs to both probes so toolchain discovery remains invisible in desktop mode.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 8a9b537: both vswhere and the vcvars cmd now run with windows_hidden_subprocess_kwargs(), so a Desktop worker start opens no console window.

@danielhanchen

Copy link
Copy Markdown
Member

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Hooray!

Reviewed commit: 8a9b5370ac

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen danielhanchen changed the title Studio: give Intel XPU Triton a usable C++ compiler on Windows Studio: give Intel XPU Triton a working C++ toolchain on Windows Oct 23, 2026
shimmyshimmer pushed a commit to shimmyshimmer/unsloth-staging-4 that referenced this pull request Oct 23, 2026
@danielhanchen
danielhanchen merged commit ff823ac into unslothai:main Oct 23, 2026
4 of 35 checks passed
danielhanchen added a commit that referenced this pull request Oct 23, 2026
Four checks fail on `main` (fd924a5), so every open PR inherits them.
Each was reproduced on a clean `main` worktree and attributed. A fifth,
`test_every_resolver_probe_forwards_the_trust_decision`, was fixed on
`main` by #14990 in the meantime, so this PR takes that version.

| Check | Introduced by | Wrong side | Fix |
|---|---|---|---|
|
`studio/backend/tests/test_text_io_encoding.py::test_text_io_names_its_encoding[_xpu_triton_toolchain.py]`
| #14814 | Product | The `vswhere` call used `text=True` with no
encoding, so it decoded with the locale code page. A Visual Studio
install path with non-ASCII characters would garble. It now passes
`-utf8` to vswhere and decodes as UTF-8 (`errors="replace"`). |
|
`tests/python/test_accelerator_skip_guards.py::test_no_skip_guard_reads_a_spoofable_accelerator_probe`
| 0947723 | Test | `test_packed_fp32_cuda_parity` skipped on
`torch.cuda.is_available()`, which the CPU CI spoof answers True. It is
now gated on `has_real_cuda()`, same as the sibling attention tests. |
|
`tests/studio/test_usage_examples_model_source_contract.py::test_examples_never_print_a_hardcoded_model_id`
| #14914 | Test | #14914 deliberately renders the example with a
`YOUR_MODEL` placeholder when nothing is servable, instead of hiding it.
The test still asserted the old "no snippet" shape. |
|
`tests/test_fp8_pre_sm89_byte_dequant.py::test_kill_switch_env_is_read_at_import`
| #14936 | Test | The subprocess imports `unsloth` in a fresh
interpreter that has no conftest spoof, so on CPU CI the import raised
"cannot find any torch accelerator" before the switch was read. With no
GPU, the subprocess now applies `tests/_zoo_aggressive_cuda_spoof.py`,
the same pattern `test_qat_phone_deployment.py` uses. It now checks both
`0` and `1`. |

For the third check, the invariant is unchanged: no hardcoded
`unsloth/...` id and no `MODEL_FALLBACK`. The test now also pins the new
contract:
- the placeholder constant
- the `model ?? MODEL_PLACEHOLDER` call as the only `buildSnippets` call
site
- the no-model note rendering exactly when the placeholder is shown

## Verification

- On `main` before #14990: 4 failed in `tests/` and 1 failed in
`studio/backend/tests`.
- On this branch: `test_accelerator_skip_guards.py`,
`test_usage_examples_model_source_contract.py` and
`test_attention_packed_fp32_routing.py` give 48 passed, 1 skipped. With
the two new files and `test_audio_load_routing.py` added, the run gives
94 passed, 133 skipped. `test_text_io_encoding.py` and
`test_xpu_triton_toolchain_14807.py` give 904 passed.
- Sabotage check: hardcoding `_HIP_FP8_BYTES_DEQUANT = True` fails the
kill-switch test.
- Sabotage check: both edits below make the updated contract test fail.
  - Changing the placeholder to `"unsloth/Qwen3-4B"`.
  - Dropping the `model ? null :` gate on the note.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Unsloth Studio Intel XPU LoRA inference mixes GNU and MSVC linker arguments on Windows

2 participants