1+ # Generate a standardized build variant name following the pattern:
2+ # torch<VERSION>-<ABI>-<COMPUTE>-windows
3+ #
4+ # Arguments:
5+ # OUT_BUILD_NAME - Output variable name
6+ # TORCH_VERSION - PyTorch version (e.g., "2.7.1")
7+ # CXX11_ABI - Whether C++11 ABI is enabled (TRUE/FALSE)
8+ # COMPUTE_FRAMEWORK - One of: cuda, rocm, metal, xpu
9+ # COMPUTE_VERSION - Version of compute framework (e.g., "12.4" for CUDA, "6.0" for ROCm)
10+ # Example output: torch271-cxx11-cu124-x86_64-windows
11+ #
12+ function (generate_build_name OUT_BUILD_NAME TORCH_VERSION CXX11_ABI COMPUTE_FRAMEWORK COMPUTE_VERSION )
13+ # Flatten version by removing dots and padding to 2 components
14+ string (REPLACE "." ";" VERSION_LIST "${TORCH_VERSION} " )
15+ list (LENGTH VERSION_LIST VERSION_COMPONENTS)
16+
17+ # Pad to at least 2 components
18+ if (VERSION_COMPONENTS LESS 2)
19+ list (APPEND VERSION_LIST "0" )
20+ endif ()
21+
22+ # Take first 2 components and join without dots
23+ list (GET VERSION_LIST 0 MAJOR)
24+ list (GET VERSION_LIST 1 MINOR)
25+ set (FLATTENED_TORCH "${MAJOR}${MINOR} " )
26+
27+ # Generate compute string
28+ if (COMPUTE_FRAMEWORK STREQUAL "cuda" )
29+ # Flatten CUDA version (e.g., "12.4" -> "124")
30+ string (REPLACE "." ";" COMPUTE_VERSION_LIST "${COMPUTE_VERSION} " )
31+ list (LENGTH COMPUTE_VERSION_LIST COMPUTE_COMPONENTS)
32+ if (COMPUTE_COMPONENTS GREATER_EQUAL 2)
33+ list (GET COMPUTE_VERSION_LIST 0 COMPUTE_MAJOR)
34+ list (GET COMPUTE_VERSION_LIST 1 COMPUTE_MINOR)
35+ set (COMPUTE_STRING "cu${COMPUTE_MAJOR}${COMPUTE_MINOR} " )
36+ else ()
37+ list (GET COMPUTE_VERSION_LIST 0 COMPUTE_MAJOR)
38+ set (COMPUTE_STRING "cu${COMPUTE_MAJOR} 0" )
39+ endif ()
40+ elseif (COMPUTE_FRAMEWORK STREQUAL "rocm" )
41+ # Flatten ROCm version (e.g., "6.0" -> "60")
42+ string (REPLACE "." ";" COMPUTE_VERSION_LIST "${COMPUTE_VERSION} " )
43+ list (LENGTH COMPUTE_VERSION_LIST COMPUTE_COMPONENTS)
44+ if (COMPUTE_COMPONENTS GREATER_EQUAL 2)
45+ list (GET COMPUTE_VERSION_LIST 0 COMPUTE_MAJOR)
46+ list (GET COMPUTE_VERSION_LIST 1 COMPUTE_MINOR)
47+ set (COMPUTE_STRING "rocm${COMPUTE_MAJOR}${COMPUTE_MINOR} " )
48+ else ()
49+ list (GET COMPUTE_VERSION_LIST 0 COMPUTE_MAJOR)
50+ set (COMPUTE_STRING "rocm${COMPUTE_MAJOR} 0" )
51+ endif ()
52+ elseif (COMPUTE_FRAMEWORK STREQUAL "xpu" )
53+ # Flatten XPU version (e.g., "2025.2" -> "202552")
54+ string (REPLACE "." ";" COMPUTE_VERSION_LIST "${COMPUTE_VERSION} " )
55+ list (LENGTH COMPUTE_VERSION_LIST COMPUTE_COMPONENTS)
56+ if (COMPUTE_COMPONENTS GREATER_EQUAL 2)
57+ list (GET COMPUTE_VERSION_LIST 0 COMPUTE_MAJOR)
58+ list (GET COMPUTE_VERSION_LIST 1 COMPUTE_MINOR)
59+ set (COMPUTE_STRING "xpu${COMPUTE_MAJOR}${COMPUTE_MINOR} " )
60+ else ()
61+ list (GET COMPUTE_VERSION_LIST 0 COMPUTE_MAJOR)
62+ set (COMPUTE_STRING "xpu${COMPUTE_MAJOR} 0" )
63+ endif ()
64+ else ()
65+ message (FATAL_ERROR "Unknown compute framework: ${COMPUTE_FRAMEWORK} " )
66+ endif ()
67+
68+ # Assemble the final build name
69+ if (ABI_STRING STREQUAL "" )
70+ set (BUILD_NAME "torch${FLATTENED_TORCH} -${COMPUTE_STRING} -windows" )
71+ else ()
72+ set (BUILD_NAME "torch${FLATTENED_TORCH} -${ABI_STRING} -${COMPUTE_STRING} -windows" )
73+ endif ()
74+
75+ set (${OUT_BUILD_NAME} "${BUILD_NAME} " PARENT_SCOPE )
76+ message (STATUS "Generated build name: ${BUILD_NAME} " )
77+ endfunction ()
78+
79+ #
80+ # Create a custom install target for the huggingface/kernels library layout.
81+ # This installs the extension into a directory structure suitable for kernel hub discovery:
82+ # <PREFIX>/<BUILD_VARIANT_NAME>/<PACKAGE_NAME>/
83+ #
84+ # Arguments:
85+ # TARGET_NAME - Name of the target to create the install rule for
86+ # PACKAGE_NAME - Python package name (e.g., "activation")
87+ # BUILD_VARIANT_NAME - Build variant name (e.g., "torch271-cxx11-cu124-x86_64-linux")
88+ # INSTALL_PREFIX - Base installation directory (defaults to CMAKE_INSTALL_PREFIX)
89+ #
90+ function (add_kernels_install_target TARGET_NAME PACKAGE_NAME BUILD_VARIANT_NAME )
91+ set (oneValueArgs INSTALL_PREFIX )
92+ cmake_parse_arguments (ARG "" "${oneValueArgs} " "" ${ARGN} )
93+
94+ if (NOT ARG_INSTALL_PREFIX)
95+ set (ARG_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX} " )
96+ endif ()
97+
98+ # Create the kernels_install target if it doesn't exist
99+ if (NOT TARGET kernels_install)
100+ add_custom_target (kernels_install ALL
101+ COMMENT "Installing all kernels to hub-compatible layout"
102+ VERBATIM )
103+ endif ()
104+
105+ # Create a custom target for this specific kernel
106+ set (KERNEL_INSTALL_TARGET "${TARGET_NAME} _kernel_install" )
107+ set (KERNEL_INSTALL_DIR "${ARG_INSTALL_PREFIX} /${BUILD_VARIANT_NAME} /${PACKAGE_NAME} " )
108+
109+ add_custom_target (${KERNEL_INSTALL_TARGET} ALL
110+ COMMAND ${CMAKE_COMMAND} -E make_directory "${KERNEL_INSTALL_DIR} "
111+ COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_FILE :${TARGET_NAME} > "${KERNEL_INSTALL_DIR} /"
112+ COMMAND ${CMAKE_COMMAND} -E copy_directory
113+ "${CMAKE_SOURCE_DIR} /torch-ext/${PACKAGE_NAME} "
114+ "${KERNEL_INSTALL_DIR} /"
115+ DEPENDS ${TARGET_NAME}
116+ COMMENT "Installing ${TARGET_NAME} to ${KERNEL_INSTALL_DIR} "
117+ VERBATIM )
118+
119+ # Make kernels_install depend on this specific kernel's install
120+ add_dependencies (kernels_install ${KERNEL_INSTALL_TARGET} )
121+
122+ # Set folder for IDE organization
123+ if (MSVC OR XCODE)
124+ set_target_properties (${KERNEL_INSTALL_TARGET} PROPERTIES FOLDER "Install" )
125+ endif ()
126+
127+ message (STATUS "Added kernels_install target for ${TARGET_NAME} -> ${BUILD_VARIANT_NAME} /${PACKAGE_NAME} " )
128+ endfunction ()
129+
130+ #
131+ # Add install rules for local development with huggingface/kernels.
132+ # This installs the extension into the layout expected by get_local_kernel():
133+ # ${CMAKE_SOURCE_DIR}/build/<BUILD_VARIANT_NAME>/<PACKAGE_NAME>/
134+ #
135+ # This allows developers to use get_local_kernel() from the kernels library to load
136+ # locally built kernels without needing to publish to the hub.
137+ #
138+ # This uses the standard CMake install() command, so it works with the default
139+ # "install" target that is always available.
140+ #
141+ # Arguments:
142+ # TARGET_NAME - Name of the target to create the install rule for
143+ # PACKAGE_NAME - Python package name (e.g., "activation")
144+ # BUILD_VARIANT_NAME - Build variant name (e.g., "torch271-cxx11-cu124-x86_64-linux")
145+ #
146+ function (add_local_install_target TARGET_NAME PACKAGE_NAME BUILD_VARIANT_NAME )
147+ # Define your local, folder based, installation directory
148+ set (LOCAL_INSTALL_DIR "${CMAKE_SOURCE_DIR} /build/${BUILD_VARIANT_NAME} /${PACKAGE_NAME} " )
149+
150+ # Glob Python files at configure time
151+ file (GLOB PYTHON_FILES "${CMAKE_SOURCE_DIR} /torch-ext/${PACKAGE_NAME} /*.py" )
152+
153+ # Create a custom target for local installation
154+ add_custom_target (local_install
155+ COMMENT "Installing files to local directory..."
156+ )
157+
158+ # Add custom commands to copy files
159+ add_custom_command (TARGET local_install POST_BUILD
160+ # Copy the shared library
161+ COMMAND ${CMAKE_COMMAND} -E copy_if_different
162+ $<TARGET_FILE :${TARGET_NAME} >
163+ ${LOCAL_INSTALL_DIR} /
164+
165+ # Copy each Python file
166+ COMMAND ${CMAKE_COMMAND} -E copy_if_different
167+ ${PYTHON_FILES}
168+ ${LOCAL_INSTALL_DIR} /
169+
170+ COMMENT "Copying shared library and Python files to ${LOCAL_INSTALL_DIR} "
171+ COMMAND_EXPAND_LISTS
172+ )
173+
174+ file (MAKE_DIRECTORY ${LOCAL_INSTALL_DIR} )
175+ message (STATUS "Added install rules for ${TARGET_NAME} -> build/${BUILD_VARIANT_NAME} /${PACKAGE_NAME} " )
176+ endfunction ()
0 commit comments