From 76751164df8239f481fbb439029c6b8b667d5430 Mon Sep 17 00:00:00 2001 From: "Liu, Kaixuan" Date: Tue, 18 Nov 2025 13:47:00 +0800 Subject: [PATCH 1/7] add Intel XPU platform support on windows Signed-off-by: Liu, Kaixuan --- scripts/windows/builder.ps1 | 141 +++++++++++++++++++++++++++++++++--- 1 file changed, 130 insertions(+), 11 deletions(-) diff --git a/scripts/windows/builder.ps1 b/scripts/windows/builder.ps1 index 0747ee3..6df2c51 100644 --- a/scripts/windows/builder.ps1 +++ b/scripts/windows/builder.ps1 @@ -145,6 +145,69 @@ param( Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' +#region Environment Initialization + +function Initialize-XPUEnvironment { + <# + .SYNOPSIS + Initializes Intel oneAPI environment for XPU builds on Windows + #> + + if ($env:OS -ne 'Windows_NT') { + return # Only needed on Windows + } + + # Check if already initialized + if (Get-Command icx -ErrorAction SilentlyContinue) { + Write-Status "Intel oneAPI environment already initialized" -Type Info + return + } + + Write-Status "Initializing Intel oneAPI environment for XPU build..." -Type Info + + # Intel oneAPI 2025.2 paths + $oneAPICompilerBin = "C:\Program Files (x86)\Intel\oneAPI\compiler\2025.2\bin" + $oneAPICompilerBinCompiler = "C:\Program Files (x86)\Intel\oneAPI\compiler\2025.2\bin\compiler" + $oneAPICompilerLib = "C:\Program Files (x86)\Intel\oneAPI\compiler\2025.2\lib" + $oneAPIOCLOC = "C:\Program Files (x86)\Intel\oneAPI\ocloc\2025.2\bin" + $oneAPIDNNL = "C:\Program Files (x86)\Intel\oneAPI\dnnl\2025.1\lib" + + # Add to PATH + if (Test-Path $oneAPICompilerBin) { + $env:PATH = "$oneAPICompilerBin;$oneAPICompilerBinCompiler;$oneAPIOCLOC;$env:PATH" + Write-Status "Added Intel oneAPI compiler to PATH" -Type Info + } else { + Write-Status "Intel oneAPI compiler not found at: $oneAPICompilerBin" -Type Warning + Write-Status "Please install Intel oneAPI Base Toolkit 2025.2" -Type Warning + return + } + + # Add to LIB + if (Test-Path $oneAPICompilerLib) { + $env:LIB = "$oneAPICompilerLib;$oneAPIDNNL;$env:LIB" + Write-Status "Added Intel oneAPI libraries to LIB environment" -Type Info + } + + # Check if a conda environment is active + if ($env:CONDA_DEFAULT_ENV) { + Write-Status "Using conda environment: $env:CONDA_DEFAULT_ENV" -Type Info + } else { + Write-Status "No conda environment detected. Using system Python." -Type Info + Write-Status "For best results, activate a conda environment with PyTorch XPU before running this script." -Type Warning + } + + # Verify installation + $icx = Get-Command icx -ErrorAction SilentlyContinue + if ($icx) { + Write-Status "Intel oneAPI environment initialized successfully" -Type Success + Write-Status "Using icx compiler: $($icx.Source)" -Type Info + } else { + Write-Status "Warning: icx compiler still not found after initialization" -Type Warning + } +} + +#endregion + #region Helper Functions function Write-Status { @@ -319,16 +382,35 @@ function Get-CMakeConfigureArgs { #> param( [bool]$ShouldInstall, - [string]$InstallPrefix + [string]$InstallPrefix, + [string]$Backend ) - # Detect platform architecture - $arch = [System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture - $vsArch = if ($arch -eq 'Arm64') { 'ARM64' } else { 'x64' } + # For XPU backend, use Ninja generator with Intel compilers + if ($Backend -and $Backend.ToLower() -eq 'xpu') { + Write-Status "Using Ninja generator for XPU backend with Intel SYCL compilers" -Type Info + + $kwargs = @("..", "-G", "Ninja", "-DCMAKE_BUILD_TYPE=Release") + + # Verify Intel compilers are available (CMakeLists.txt will set them correctly) + $icx = Get-Command icx -ErrorAction SilentlyContinue + + if ($icx) { + Write-Status "Found Intel compiler: $($icx.Source)" -Type Info + Write-Status "CMakeLists.txt will configure icx for Windows (MSVC-compatible mode)" -Type Info + } else { + Write-Status "Intel compilers not found in PATH. Make sure oneAPI environment is initialized." -Type Warning + Write-Status "Run: & `"C:\Program Files (x86)\Intel\oneAPI\setvars.bat`"" -Type Warning + } + } else { + # Use Visual Studio generator for other backends + $arch = [System.Runtime.InteropServices.RuntimeInformation]::ProcessArchitecture + $vsArch = if ($arch -eq 'Arm64') { 'ARM64' } else { 'x64' } - Write-Status "Detected platform: $arch, using Visual Studio architecture: $vsArch" -Type Info + Write-Status "Detected platform: $arch, using Visual Studio architecture: $vsArch" -Type Info - $kwargs = @("..", "-G", "Visual Studio 17 2022", "-A", $vsArch) + $kwargs = @("..", "-G", "Visual Studio 17 2022", "-A", $vsArch) + } # Detect Python from current environment $pythonExe = (Get-Command python -ErrorAction SilentlyContinue).Source @@ -382,13 +464,14 @@ function Invoke-CMakeBuild { [string]$BuildConfig, [bool]$RunLocalInstall = $false, [bool]$RunKernelsInstall = $false, - [string]$InstallPrefix = $null + [string]$InstallPrefix = $null, + [string]$Backend = $null ) Write-Status "Building project with CMake..." -Type Info Write-Status "Configuration: $BuildConfig" -Type Info - # Ensure VS environment is initialized + # Ensure VS environment is initialized (needed for Ninja and MSVC) Initialize-VSEnvironment # Create build directory @@ -402,7 +485,7 @@ function Invoke-CMakeBuild { Write-Status "Configuring CMake project..." -Type Info Push-Location $buildDir try { - $configureArgs = Get-CMakeConfigureArgs -ShouldInstall ($RunKernelsInstall -or $RunLocalInstall) -InstallPrefix $InstallPrefix + $configureArgs = Get-CMakeConfigureArgs -ShouldInstall ($RunKernelsInstall -or $RunLocalInstall) -InstallPrefix $InstallPrefix -Backend $Backend cmake @configureArgs @@ -412,7 +495,13 @@ function Invoke-CMakeBuild { # Build with CMake Write-Status "Building project..." -Type Info - cmake --build . --config $BuildConfig + + # For Ninja generator, don't specify --config + if ($Backend -and $Backend.ToLower() -eq 'xpu') { + cmake --build . + } else { + cmake --build . --config $BuildConfig + } if ($LASTEXITCODE -ne 0) { throw "CMake build failed with exit code $LASTEXITCODE" @@ -462,6 +551,30 @@ function Invoke-Backend { if ($Backend -and $Backend -ne 'universal') { $kwargs += '--backend', $Backend } Invoke-Build2Cmake -Build2CmakeExe $Build2CmakeExe -Arguments $kwargs + + # Post-process CMakeLists.txt for XPU on Windows: use icx instead of icpx + if ($Backend -and $Backend.ToLower() -eq 'xpu' -and $env:OS -eq 'Windows_NT') { + $targetPath = if ($Target) { $Target } else { Split-Path $BuildToml -Parent } + $cmakeListsPath = Join-Path $targetPath 'CMakeLists.txt' + + if (Test-Path $cmakeListsPath) { + Write-Status "Patching CMakeLists.txt for XPU on Windows (using icx instead of icpx)..." -Type Info + + $content = Get-Content $cmakeListsPath -Raw + + # Replace the CMAKE_CXX_COMPILER line to use icx on Windows + $oldLine = ' set(CMAKE_CXX_COMPILER ${ICPX_COMPILER})' + $newLine = ' set(CMAKE_CXX_COMPILER ${ICX_COMPILER}) # Using icx (MSVC-compatible) on Windows' + + if ($content.Contains($oldLine)) { + $content = $content.Replace($oldLine, $newLine) + Set-Content $cmakeListsPath -Value $content -NoNewline + Write-Status "Applied Windows XPU fix: CMAKE_CXX_COMPILER=icx" -Type Success + } else { + Write-Status "Could not apply automatic patch. Manually edit CMakeLists.txt if build fails." -Type Warning + } + } + } } function Set-BackendArchitecture { @@ -530,6 +643,11 @@ try { if ($Backend -and $Backend.ToLower() -eq 'metal') { throw "Metal backend is not supported on Windows. Metal is only available on macOS." } + + # Initialize Intel oneAPI environment for XPU backend on Windows + if ($Backend -and $Backend.ToLower() -eq 'xpu') { + Initialize-XPUEnvironment + } $options = @{ Force = $Force.IsPresent @@ -573,7 +691,8 @@ try { -BuildConfig $BuildConfig ` -RunLocalInstall $LocalInstall.IsPresent ` -RunKernelsInstall $KernelsInstall.IsPresent ` - -InstallPrefix $InstallPrefix + -InstallPrefix $InstallPrefix ` + -Backend $Backend } } From 3019920ef2060f3088b70d04eee3d96054ce802d Mon Sep 17 00:00:00 2001 From: "Liu, Kaixuan" Date: Thu, 20 Nov 2025 16:04:46 +0800 Subject: [PATCH 2/7] use setvars.bat for oneAPI Signed-off-by: Liu, Kaixuan --- scripts/windows/builder.ps1 | 52 ++++++++++++++----------------------- 1 file changed, 20 insertions(+), 32 deletions(-) diff --git a/scripts/windows/builder.ps1 b/scripts/windows/builder.ps1 index 6df2c51..2963c5c 100644 --- a/scripts/windows/builder.ps1 +++ b/scripts/windows/builder.ps1 @@ -165,45 +165,33 @@ function Initialize-XPUEnvironment { Write-Status "Initializing Intel oneAPI environment for XPU build..." -Type Info - # Intel oneAPI 2025.2 paths - $oneAPICompilerBin = "C:\Program Files (x86)\Intel\oneAPI\compiler\2025.2\bin" - $oneAPICompilerBinCompiler = "C:\Program Files (x86)\Intel\oneAPI\compiler\2025.2\bin\compiler" - $oneAPICompilerLib = "C:\Program Files (x86)\Intel\oneAPI\compiler\2025.2\lib" - $oneAPIOCLOC = "C:\Program Files (x86)\Intel\oneAPI\ocloc\2025.2\bin" - $oneAPIDNNL = "C:\Program Files (x86)\Intel\oneAPI\dnnl\2025.1\lib" + # Path to Intel oneAPI setvars.bat + $setvarsPath = "C:\Program Files (x86)\Intel\oneAPI\setvars.bat" - # Add to PATH - if (Test-Path $oneAPICompilerBin) { - $env:PATH = "$oneAPICompilerBin;$oneAPICompilerBinCompiler;$oneAPIOCLOC;$env:PATH" - Write-Status "Added Intel oneAPI compiler to PATH" -Type Info - } else { - Write-Status "Intel oneAPI compiler not found at: $oneAPICompilerBin" -Type Warning - Write-Status "Please install Intel oneAPI Base Toolkit 2025.2" -Type Warning + if (!(Test-Path $setvarsPath)) { + Write-Status "Intel oneAPI setvars.bat not found at: $setvarsPath" -Type Warning + Write-Status "Please install Intel oneAPI Base Toolkit" -Type Warning return } - # Add to LIB - if (Test-Path $oneAPICompilerLib) { - $env:LIB = "$oneAPICompilerLib;$oneAPIDNNL;$env:LIB" - Write-Status "Added Intel oneAPI libraries to LIB environment" -Type Info - } + # Execute setvars.bat and capture environment variables + $tempFile = [System.IO.Path]::GetTempFileName() - # Check if a conda environment is active - if ($env:CONDA_DEFAULT_ENV) { - Write-Status "Using conda environment: $env:CONDA_DEFAULT_ENV" -Type Info - } else { - Write-Status "No conda environment detected. Using system Python." -Type Info - Write-Status "For best results, activate a conda environment with PyTorch XPU before running this script." -Type Warning - } + Write-Status "Running Intel oneAPI setvars.bat..." -Type Info - # Verify installation - $icx = Get-Command icx -ErrorAction SilentlyContinue - if ($icx) { - Write-Status "Intel oneAPI environment initialized successfully" -Type Success - Write-Status "Using icx compiler: $($icx.Source)" -Type Info - } else { - Write-Status "Warning: icx compiler still not found after initialization" -Type Warning + # Run setvars.bat and export environment to temp file + cmd /c "`"$setvarsPath`" && set > `"$tempFile`"" + + if ($LASTEXITCODE -ne 0) { + Remove-Item $tempFile -ErrorAction SilentlyContinue + throw "Failed to initialize Intel oneAPI environment. Please ensure Intel oneAPI Base Toolkit is properly installed." } + + # Parse and apply environment variables + Import-EnvironmentVariables -FilePath $tempFile + Remove-Item $tempFile -ErrorAction SilentlyContinue + + Write-Status "Intel oneAPI environment initialized successfully" -Type Success } #endregion From b5be45d1e95baa32c5d67f710d0a56fe8bbbe1d8 Mon Sep 17 00:00:00 2001 From: "Liu, Kaixuan" Date: Wed, 26 Nov 2025 12:46:40 +0800 Subject: [PATCH 3/7] refine code Signed-off-by: Liu, Kaixuan --- scripts/windows/builder.ps1 | 39 ++++++++----------------------------- 1 file changed, 8 insertions(+), 31 deletions(-) diff --git a/scripts/windows/builder.ps1 b/scripts/windows/builder.ps1 index 2963c5c..b0ade68 100644 --- a/scripts/windows/builder.ps1 +++ b/scripts/windows/builder.ps1 @@ -483,13 +483,7 @@ function Invoke-CMakeBuild { # Build with CMake Write-Status "Building project..." -Type Info - - # For Ninja generator, don't specify --config - if ($Backend -and $Backend.ToLower() -eq 'xpu') { - cmake --build . - } else { - cmake --build . --config $BuildConfig - } + cmake --build . --config $BuildConfig if ($LASTEXITCODE -ne 0) { throw "CMake build failed with exit code $LASTEXITCODE" @@ -539,30 +533,6 @@ function Invoke-Backend { if ($Backend -and $Backend -ne 'universal') { $kwargs += '--backend', $Backend } Invoke-Build2Cmake -Build2CmakeExe $Build2CmakeExe -Arguments $kwargs - - # Post-process CMakeLists.txt for XPU on Windows: use icx instead of icpx - if ($Backend -and $Backend.ToLower() -eq 'xpu' -and $env:OS -eq 'Windows_NT') { - $targetPath = if ($Target) { $Target } else { Split-Path $BuildToml -Parent } - $cmakeListsPath = Join-Path $targetPath 'CMakeLists.txt' - - if (Test-Path $cmakeListsPath) { - Write-Status "Patching CMakeLists.txt for XPU on Windows (using icx instead of icpx)..." -Type Info - - $content = Get-Content $cmakeListsPath -Raw - - # Replace the CMAKE_CXX_COMPILER line to use icx on Windows - $oldLine = ' set(CMAKE_CXX_COMPILER ${ICPX_COMPILER})' - $newLine = ' set(CMAKE_CXX_COMPILER ${ICX_COMPILER}) # Using icx (MSVC-compatible) on Windows' - - if ($content.Contains($oldLine)) { - $content = $content.Replace($oldLine, $newLine) - Set-Content $cmakeListsPath -Value $content -NoNewline - Write-Status "Applied Windows XPU fix: CMAKE_CXX_COMPILER=icx" -Type Success - } else { - Write-Status "Could not apply automatic patch. Manually edit CMakeLists.txt if build fails." -Type Warning - } - } - } } function Set-BackendArchitecture { @@ -635,6 +605,13 @@ try { # Initialize Intel oneAPI environment for XPU backend on Windows if ($Backend -and $Backend.ToLower() -eq 'xpu') { Initialize-XPUEnvironment + + # Set Intel compilers for Windows XPU builds (icx is MSVC-compatible) + if ($env:OS -eq 'Windows_NT') { + $env:CXX = 'icx' + $env:CC = 'icx' + Write-Status "Set CXX=icx and CC=icx for Windows XPU build" -Type Info + } } $options = @{ From b3e9a73acf08d0019be235bf87247e917e5939b6 Mon Sep 17 00:00:00 2001 From: "Liu, Kaixuan" Date: Wed, 26 Nov 2025 13:08:53 +0800 Subject: [PATCH 4/7] remove oneAPI init part, will put this into github action workflow yaml file Signed-off-by: Liu, Kaixuan --- scripts/windows/builder.ps1 | 55 +------------------------------------ 1 file changed, 1 insertion(+), 54 deletions(-) diff --git a/scripts/windows/builder.ps1 b/scripts/windows/builder.ps1 index b0ade68..cfba76c 100644 --- a/scripts/windows/builder.ps1 +++ b/scripts/windows/builder.ps1 @@ -145,56 +145,6 @@ param( Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' -#region Environment Initialization - -function Initialize-XPUEnvironment { - <# - .SYNOPSIS - Initializes Intel oneAPI environment for XPU builds on Windows - #> - - if ($env:OS -ne 'Windows_NT') { - return # Only needed on Windows - } - - # Check if already initialized - if (Get-Command icx -ErrorAction SilentlyContinue) { - Write-Status "Intel oneAPI environment already initialized" -Type Info - return - } - - Write-Status "Initializing Intel oneAPI environment for XPU build..." -Type Info - - # Path to Intel oneAPI setvars.bat - $setvarsPath = "C:\Program Files (x86)\Intel\oneAPI\setvars.bat" - - if (!(Test-Path $setvarsPath)) { - Write-Status "Intel oneAPI setvars.bat not found at: $setvarsPath" -Type Warning - Write-Status "Please install Intel oneAPI Base Toolkit" -Type Warning - return - } - - # Execute setvars.bat and capture environment variables - $tempFile = [System.IO.Path]::GetTempFileName() - - Write-Status "Running Intel oneAPI setvars.bat..." -Type Info - - # Run setvars.bat and export environment to temp file - cmd /c "`"$setvarsPath`" && set > `"$tempFile`"" - - if ($LASTEXITCODE -ne 0) { - Remove-Item $tempFile -ErrorAction SilentlyContinue - throw "Failed to initialize Intel oneAPI environment. Please ensure Intel oneAPI Base Toolkit is properly installed." - } - - # Parse and apply environment variables - Import-EnvironmentVariables -FilePath $tempFile - Remove-Item $tempFile -ErrorAction SilentlyContinue - - Write-Status "Intel oneAPI environment initialized successfully" -Type Success -} - -#endregion #region Helper Functions @@ -601,11 +551,8 @@ try { if ($Backend -and $Backend.ToLower() -eq 'metal') { throw "Metal backend is not supported on Windows. Metal is only available on macOS." } - - # Initialize Intel oneAPI environment for XPU backend on Windows + if ($Backend -and $Backend.ToLower() -eq 'xpu') { - Initialize-XPUEnvironment - # Set Intel compilers for Windows XPU builds (icx is MSVC-compatible) if ($env:OS -eq 'Windows_NT') { $env:CXX = 'icx' From 6cf598e949e41fe576297ce088039a2380a61191 Mon Sep 17 00:00:00 2001 From: "Liu, Kaixuan" Date: Wed, 26 Nov 2025 13:11:09 +0800 Subject: [PATCH 5/7] update Signed-off-by: Liu, Kaixuan --- scripts/windows/builder.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/windows/builder.ps1 b/scripts/windows/builder.ps1 index cfba76c..9464803 100644 --- a/scripts/windows/builder.ps1 +++ b/scripts/windows/builder.ps1 @@ -145,7 +145,6 @@ param( Set-StrictMode -Version Latest $ErrorActionPreference = 'Stop' - #region Helper Functions function Write-Status { From bb9a0f92dc8c771750cc86982459835dbf548863 Mon Sep 17 00:00:00 2001 From: "Liu, Kaixuan" Date: Wed, 26 Nov 2025 16:15:45 +0800 Subject: [PATCH 6/7] update Signed-off-by: Liu, Kaixuan --- build2cmake/src/templates/xpu/preamble.cmake | 12 ++++++++++-- scripts/windows/builder.ps1 | 9 --------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/build2cmake/src/templates/xpu/preamble.cmake b/build2cmake/src/templates/xpu/preamble.cmake index d171d10..c05afcf 100644 --- a/build2cmake/src/templates/xpu/preamble.cmake +++ b/build2cmake/src/templates/xpu/preamble.cmake @@ -12,8 +12,16 @@ if(ICX_COMPILER AND ICPX_COMPILER) string(REGEX MATCH "[0-9]+\\.[0-9]+" DPCPP_VERSION "${ICPX_VERSION_OUTPUT}") set(DPCPP_VERSION "${DPCPP_VERSION}" CACHE STRING "DPCPP major.minor version") set(CMAKE_C_COMPILER ${ICX_COMPILER}) - set(CMAKE_CXX_COMPILER ${ICPX_COMPILER}) - message(STATUS "Using Intel SYCL C++ compiler: ${ICPX_COMPILER} and C compiler: ${ICX_COMPILER} Version: ${DPCPP_VERSION}") + + # On Windows, use icx (MSVC-compatible) for C++ to work with Ninja generator + # On Linux, use icpx (GNU-compatible) for C++ + if(WIN32) + set(CMAKE_CXX_COMPILER ${ICX_COMPILER}) + message(STATUS "Using Intel SYCL C++ compiler: ${ICX_COMPILER} and C compiler: ${ICX_COMPILER} Version: ${DPCPP_VERSION} (Windows MSVC-compatible mode)") + else() + set(CMAKE_CXX_COMPILER ${ICPX_COMPILER}) + message(STATUS "Using Intel SYCL C++ compiler: ${ICPX_COMPILER} and C compiler: ${ICX_COMPILER} Version: ${DPCPP_VERSION}") + endif() else() message(FATAL_ERROR "Intel SYCL C++ compiler (icpx) and/or C compiler (icx) not found. Please install Intel oneAPI toolkit.") endif() diff --git a/scripts/windows/builder.ps1 b/scripts/windows/builder.ps1 index 9464803..1361d78 100644 --- a/scripts/windows/builder.ps1 +++ b/scripts/windows/builder.ps1 @@ -551,15 +551,6 @@ try { throw "Metal backend is not supported on Windows. Metal is only available on macOS." } - if ($Backend -and $Backend.ToLower() -eq 'xpu') { - # Set Intel compilers for Windows XPU builds (icx is MSVC-compatible) - if ($env:OS -eq 'Windows_NT') { - $env:CXX = 'icx' - $env:CC = 'icx' - Write-Status "Set CXX=icx and CC=icx for Windows XPU build" -Type Info - } - } - $options = @{ Force = $Force.IsPresent OpsId = $OpsId From 6e4b2236a31c4d6ca4af262b7b5ea6b32808fd61 Mon Sep 17 00:00:00 2001 From: "Liu, Kaixuan" Date: Thu, 27 Nov 2025 09:45:00 +0800 Subject: [PATCH 7/7] add early exit Signed-off-by: Liu, Kaixuan --- scripts/windows/builder.ps1 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scripts/windows/builder.ps1 b/scripts/windows/builder.ps1 index 1361d78..6f917e2 100644 --- a/scripts/windows/builder.ps1 +++ b/scripts/windows/builder.ps1 @@ -336,8 +336,9 @@ function Get-CMakeConfigureArgs { Write-Status "Found Intel compiler: $($icx.Source)" -Type Info Write-Status "CMakeLists.txt will configure icx for Windows (MSVC-compatible mode)" -Type Info } else { - Write-Status "Intel compilers not found in PATH. Make sure oneAPI environment is initialized." -Type Warning - Write-Status "Run: & `"C:\Program Files (x86)\Intel\oneAPI\setvars.bat`"" -Type Warning + Write-Status "Intel compilers not found in PATH. Make sure oneAPI environment is initialized." -Type Error + Write-Status "Run: & `"C:\Program Files (x86)\Intel\oneAPI\setvars.bat`"" -Type Error + throw "Intel compilers (icx) are required for XPU backend but were not found in PATH. Please initialize oneAPI environment." } } else { # Use Visual Studio generator for other backends