Skip to content

Commit 3137889

Browse files
authored
[Windows] misc fixes to enable Windows build (#9198)
Summary: ## Context Implement some small miscellaneous fixes to enable installing ExecuTorch on Windows. Currently, the optimized kernels cannot be built successfully but the installation works in custom ops are not built. ## Installation ```powershell cd executorch # Need to disable building custom kernels; there are a bunch of build errors $env:EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT=0 # For some reason need to explicitly enable tensor extension otherwise I see a linker error $env:CMAKE_ARGS="-DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON" python install_executorch.py # Unset if you want Remove-Item Env:\EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT Remove-Item Env:\CMAKE_ARGS ``` ## Testing I am able to follow the [Getting Started tutorial](https://pytorch.org/executorch/stable/getting-started-setup.html#run-your-program) successfully. ## Outstanding Build Issues Currently, with CMake I can successfully build with the following settings: ``` del -Recurse -Force cmake-out; ` cmake . ` -DCMAKE_INSTALL_PREFIX=cmake-out ` -DPYTHON_EXECUTABLE=C:\\Users\\ssjia\\AppData\\Local\\miniconda3\\python.exe ` -DCMAKE_PREFIX_PATH=C:\\Users\\ssjia\\AppData\\Local\\miniconda3\\Lib\\site-packages ` -DCMAKE_BUILD_TYPE=Release ` -DEXECUTORCH_BUILD_EXTENSION_TENSOR=ON ` -DEXECUTORCH_BUILD_PYBIND=ON ` -DEXECUTORCH_BUILD_XNNPACK=ON ` -DEXECUTORCH_BUILD_KERNELS_QUANTIZED_AOT=ON ` -DEXECUTORCH_BUILD_KERNELS_CUSTOM_AOT=ON ` -DEXECUTORCH_BUILD_KERNELS_CUSTOM=OFF ` -T ClangCL ` -Bcmake-out; ` cmake --build cmake-out -j64 --target install ``` If I switch `EXECUTORCH_BUILD_KERNELS_CUSTOM` to `ON`, then the build fails. The primary offenders appear to be the `gelu`. The implementation includes a header from ATen which is causing the majority of the errors.
1 parent d44d6cd commit 3137889

File tree

5 files changed

+41
-21
lines changed

5 files changed

+41
-21
lines changed

CMakeLists.txt

+7-2
Original file line numberDiff line numberDiff line change
@@ -506,13 +506,18 @@ if(EXECUTORCH_BUILD_FLATC)
506506
-DFLATBUFFERS_BUILD_FLATLIB=${FLATBUFFERS_BUILD_FLATLIB}
507507
-DFLATBUFFERS_BUILD_TESTS=${FLATBUFFERS_BUILD_TESTS}
508508
-DFLATBUFFERS_INSTALL=${FLATBUFFERS_INSTALL}
509-
-DCMAKE_BUILD_TYPE=Release
510509
-DCMAKE_CXX_FLAGS="-DFLATBUFFERS_MAX_ALIGNMENT=${FLATBUFFERS_MAX_ALIGNMENT}"
511510
INSTALL_COMMAND ""
512511
BUILD_BYPRODUCTS <BINARY_DIR>/flatc
513512
)
514513
ExternalProject_Get_Property(flatbuffers BINARY_DIR)
515-
set(FLATC_EXECUTABLE ${BINARY_DIR}/flatc)
514+
if(WIN32)
515+
# flatbuffers does not use CMAKE_BUILD_TYPE. Internally, the build forces Release
516+
# config, but from CMake's perspective the build type is always Debug.
517+
set(FLATC_EXECUTABLE ${BINARY_DIR}/$<CONFIG>/flatc.exe)
518+
else()
519+
set(FLATC_EXECUTABLE ${BINARY_DIR}/flatc)
520+
endif()
516521
set(FLATC_EXECUTABLE_BUILT_FROM_SOURCE YES)
517522
endif()
518523

backends/xnnpack/CMakeLists.txt

+11-3
Original file line numberDiff line numberDiff line change
@@ -69,35 +69,43 @@ foreach(fbs_file ${_xnnpack_schema__srcs})
6969
)
7070
endforeach()
7171

72+
if(WIN32)
73+
set(MV_COMMAND powershell -Command "Move-Item -Path ${_xnnpack_flatbuffer__outputs} -Destination ${_xnnpack_schema__outputs}")
74+
else()
75+
set(MV_COMMAND mv ${_xnnpack_flatbuffer__outputs} ${_xnnpack_schema__outputs})
76+
endif()
77+
7278
# Generate the headers from the .fbs files.
7379
add_custom_command(
7480
OUTPUT ${_xnnpack_schema__outputs}
7581
COMMAND
7682
${FLATC_EXECUTABLE} --cpp --cpp-std c++11 --scoped-enums -o
7783
"${_xnnpack_schema__include_dir}/executorch/backends/xnnpack/serialization"
7884
${_xnnpack_schema__srcs}
79-
COMMAND mv ${_xnnpack_flatbuffer__outputs} ${_xnnpack_schema__outputs}
85+
COMMAND ${MV_COMMAND}
8086
WORKING_DIRECTORY ${EXECUTORCH_ROOT}
8187
DEPENDS flatc
8288
COMMENT "Generating xnnpack_schema headers"
8389
VERBATIM
8490
)
8591

92+
unset(MV_COMMAND)
93+
8694
add_library(xnnpack_schema INTERFACE ${_xnnpack_schema__outputs})
8795
set_target_properties(xnnpack_schema PROPERTIES LINKER_LANGUAGE CXX)
8896
target_include_directories(
8997
xnnpack_schema INTERFACE ${_xnnpack_schema__include_dir}
9098
${EXECUTORCH_ROOT}/third-party/flatbuffers/include
9199
)
92100

93-
set(xnnpack_third_party pthreadpool cpuinfo)
101+
set(xnnpack_third_party pthreadpool extension_threadpool cpuinfo)
94102

95103
include(cmake/Dependencies.cmake)
96104

97105
list(TRANSFORM _xnnpack_backend__srcs PREPEND "${EXECUTORCH_ROOT}/")
98106
add_library(xnnpack_backend STATIC ${_xnnpack_backend__srcs})
99107
target_link_libraries(
100-
xnnpack_backend PRIVATE ${xnnpack_third_party} executorch_core xnnpack_schema
108+
xnnpack_backend PUBLIC ${xnnpack_third_party} executorch_core xnnpack_schema
101109
)
102110

103111
target_include_directories(

extension/llm/custom_ops/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64|armv7)$")
4949
list(APPEND _custom_ops__srcs
5050
"extension/llm/custom_ops/spinquant/third-party/FFHT/fht_neon.c"
5151
)
52-
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
52+
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|AMD64)")
5353
list(APPEND _custom_ops__srcs
5454
"extension/llm/custom_ops/spinquant/third-party/FFHT/fht_avx.c"
5555
)

install_executorch.bat

+2-8
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,8 @@ rem This batch file provides a basic functionality similar to the bash script.
77

88
cd /d "%~dp0"
99

10-
rem Find the names of the python tools to use (replace with your actual python installation)
11-
if "%PYTHON_EXECUTABLE%"=="" (
12-
if "%CONDA_DEFAULT_ENV%"=="" OR "%CONDA_DEFAULT_ENV%"=="base" OR NOT EXIST "python" (
13-
set PYTHON_EXECUTABLE=python3
14-
) else (
15-
set PYTHON_EXECUTABLE=python
16-
)
17-
)
10+
rem Under windows, it's always python
11+
set PYTHON_EXECUTABLE=python
1812

1913
"%PYTHON_EXECUTABLE%" install_executorch.py %*
2014

setup.py

+20-7
Original file line numberDiff line numberDiff line change
@@ -345,22 +345,30 @@ def inplace_dir(self, installer: "InstallerBuildExt") -> Path:
345345
class BuiltExtension(_BaseExtension):
346346
"""An extension that installs a python extension that was built by cmake."""
347347

348-
def __init__(self, src: str, modpath: str):
348+
def __init__(self, src: str, modpath: str, src_dir: Optional[str] = None):
349349
"""Initializes a BuiltExtension.
350350
351351
Args:
352-
src: The path to the file to install (typically a shared library),
353-
relative to the cmake-out directory. May be an fnmatch-style
354-
glob that matches exactly one file. If the path ends in `.so`,
355-
this class will also look for similarly-named `.dylib` files.
352+
src_dir: The directory of the file to install, relative to the cmake-out
353+
directory. A placeholder %BUILD_TYPE% will be replaced with the build
354+
type for multi-config generators (like Visual Studio) where the build
355+
output is in a subdirectory named after the build type. For single-
356+
config generators (like Makefile Generators or Ninja), this placeholder
357+
will be removed.
358+
src_name: The name of the file to install. If the path ends in `.so`,
356359
modpath: The dotted path of the python module that maps to the
357360
extension.
358361
"""
359362
assert (
360363
"/" not in modpath
361364
), f"modpath must be a dotted python module path: saw '{modpath}'"
365+
full_src = src
366+
if src_dir is None and platform.system() == "Windows":
367+
src_dir = "%BUILD_TYPE%/"
368+
if src_dir is not None:
369+
full_src = os.path.join(src_dir, src)
362370
# This is a real extension, so use the modpath as the name.
363-
super().__init__(src=f"%CMAKE_CACHE_DIR%/{src}", dst=modpath, name=modpath)
371+
super().__init__(src=f"%CMAKE_CACHE_DIR%/{full_src}", dst=modpath, name=modpath)
364372

365373
def src_path(self, installer: "InstallerBuildExt") -> Path:
366374
"""Returns the path to the source file, resolving globs.
@@ -780,7 +788,12 @@ def get_ext_modules() -> List[Extension]:
780788
# portable kernels, and a selection of backends. This lets users
781789
# load and execute .pte files from python.
782790
BuiltExtension(
783-
"_portable_lib.*", "executorch.extension.pybindings._portable_lib"
791+
(
792+
"_portable_lib.cp*"
793+
if platform.system() == "Windows"
794+
else "_portable_lib.*"
795+
),
796+
"executorch.extension.pybindings._portable_lib",
784797
)
785798
)
786799
if ShouldBuild.training():

0 commit comments

Comments
 (0)