Skip to content

Commit f4fdd8f

Browse files
authored
Benchmark improvements (#2415)
I've picked up some useful changes from #1905 and pushed them here. Also organized python library build. So basically it is a clean up with some features added and get it ready for the 2025 release List of changes: - Add benchmark project artifacts to gitignore - Compile python library using dedicated cmake api: https://cmake.org/cmake/help/latest/module/FindPython3.html - Suppress old style warning of xetla library - Prevent cmake build on clean up commands (before that it was unconditional - If there is no ipex, library will be compiled in no ipex mode with user warning - More modular setup.py - Verbose output of cmake commands being run - CMakeLists.txt cleanup - Fix shadow import usage of cmake library Closes #1905
1 parent 4e41541 commit f4fdd8f

File tree

6 files changed

+110
-48
lines changed

6 files changed

+110
-48
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,19 @@ build-*/
66
python/build/
77
python/dist/
88
python/triton*.egg-info/
9+
python/*.whl
910

1011
python/triton/_C/*.pyd
1112
python/triton/_C/*.so
1213
python/triton/_C/*.dylib
1314

15+
benchmarks/dist
16+
benchmarks/*.egg-info/
17+
benchmarks/**/*.so
18+
19+
# Logs
20+
inductor_log/
21+
1422
# Backends copied from submodules
1523
python/triton/backends/
1624
!python/triton/backends/__init__.py

benchmarks/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ if(NOT WIN32)
1010
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
1111
endif()
1212

13-
find_package(Python3 COMPONENTS Interpreter)
13+
find_package(Python3 REQUIRED
14+
COMPONENTS Development.Module)
1415
find_package(Torch REQUIRED)
1516
find_library(TORCH_PYTHON_LIBRARY torch_python PATH "${TORCH_INSTALL_PREFIX}/lib")
17+
find_package(XeTLALibrary REQUIRED)
1618

1719
if(USE_IPEX)
1820
string(APPEND CMAKE_CXX_FLAGS " -DUSE_IPEX")

benchmarks/cmake/FindXeTLALibrary.cmake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
include(FetchContent)
44

55
if (NOT XeTLALibrary_FOUND)
6+
# TODO: switch ot FetchContent_MakeAvailable once XeTLA supports it
7+
cmake_policy(SET CMP0169 OLD)
68

79
set(XeTLALibrary_SOURCE_DIR
810
"${CMAKE_CURRENT_BINARY_DIR}/XeTLALibrary")
911
message(STATUS "XeTLALibrary is not specified. Will try to download
1012
XeTLA library from https://github.com/intel/xetla into
1113
${XeTLALibrary_SOURCE_DIR}")
12-
file(READ xetla-library.conf XeTLALibrary_TAG)
14+
file(READ xetla_kernel/xetla-library.conf XeTLALibrary_TAG)
1315
# Strip the potential trailing newline from tag
1416
string(STRIP "${XeTLALibrary_TAG}" XeTLALibrary_TAG)
1517
FetchContent_Declare(xetla-library

benchmarks/setup.py

Lines changed: 91 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,135 @@
11
import os
2-
import re
32
import shutil
43
import subprocess
5-
import sysconfig
64
import sys
75

8-
from setuptools import setup
6+
# TODO: update once there is replacement for clean:
7+
# https://github.com/pypa/setuptools/discussions/2838
8+
from distutils import log # pylint: disable=[deprecated-module]
9+
from distutils.dir_util import remove_tree # pylint: disable=[deprecated-module]
10+
from distutils.command.clean import clean as _clean # pylint: disable=[deprecated-module]
11+
12+
from setuptools import setup, Extension
13+
from setuptools.command.build_ext import build_ext as _build_ext
914

1015
import torch
1116

12-
ipex_cmake_prefix_path = ""
13-
USE_IPEX_OPTION = os.getenv("USE_IPEX", "1")
14-
if USE_IPEX_OPTION == "1":
15-
import intel_extension_for_pytorch
16-
ipex_cmake_prefix_path = f";{intel_extension_for_pytorch.cmake_prefix_path}"
17+
18+
class CMakeExtension(Extension):
19+
20+
def __init__(self, name):
21+
# don't invoke the original build_ext for this special extension
22+
super().__init__(name, sources=[])
1723

1824

1925
class CMakeBuild():
2026

21-
def __init__(self):
27+
def __init__(self, debug=False, dry_run=False):
2228
self.current_dir = os.path.abspath(os.path.dirname(__file__))
2329
self.build_temp = self.current_dir + "/build/temp"
2430
self.extdir = self.current_dir + "/triton_kernels_benchmark"
31+
self.build_type = self.get_build_type(debug)
32+
self.cmake_prefix_paths = [torch.utils.cmake_prefix_path]
33+
self.use_ipex = False
34+
self.dry_run = dry_run
35+
36+
def get_build_type(self, debug):
37+
DEBUG_OPTION = os.getenv("DEBUG", "0")
38+
return "Debug" if debug or (DEBUG_OPTION == "1") else "Release"
2539

2640
def run(self):
27-
try:
28-
out = subprocess.check_output(["cmake", "--version"])
29-
except OSError as error:
30-
raise RuntimeError("CMake must be installed") from error
41+
self.check_ipex()
42+
self.build_extension()
3143

32-
match = re.search(r"version\s*(?P<major>\d+)\.(?P<minor>\d+)([\d.]+)?", out.decode())
33-
cmake_major, cmake_minor = int(match.group("major")), int(match.group("minor"))
34-
if (cmake_major, cmake_minor) < (3, 18):
35-
raise RuntimeError("CMake >= 3.18.0 is required")
44+
def check_ipex(self):
45+
self.use_ipex = os.getenv("USE_IPEX", "1") == "1"
46+
if not self.use_ipex:
47+
return
48+
try:
49+
import intel_extension_for_pytorch
50+
except ImportError:
51+
log.warn("ipex is not installed trying to build without ipex")
52+
self.use_ipex = False
53+
return
54+
self.cmake_prefix_paths.append(intel_extension_for_pytorch.cmake_prefix_path)
3655

37-
self.build_extension()
56+
def check_call(self, *popenargs, **kwargs):
57+
log.info(" ".join(popenargs[0]))
58+
if not self.dry_run:
59+
subprocess.check_call(*popenargs, **kwargs)
3860

3961
def build_extension(self):
4062
ninja_dir = shutil.which("ninja")
4163
# create build directories
4264
if not os.path.exists(self.build_temp):
4365
os.makedirs(self.build_temp)
44-
# python directories
45-
python_include_dir = sysconfig.get_path("platinclude")
4666
cmake_args = [
4767
"-G",
4868
"Ninja", # Ninja is much faster than make
4969
"-DCMAKE_MAKE_PROGRAM=" +
5070
ninja_dir, # Pass explicit path to ninja otherwise cmake may cache a temporary path
51-
f"-DCMAKE_PREFIX_PATH={torch.utils.cmake_prefix_path}{ipex_cmake_prefix_path}",
52-
f"-DUSE_IPEX={USE_IPEX_OPTION}",
53-
"-DCMAKE_EXPORT_COMPILE_COMMANDS=ON",
54-
"-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY=" + self.extdir,
55-
"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=" + self.extdir,
56-
"-DPython3_EXECUTABLE:FILEPATH=" + sys.executable,
57-
"-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON",
58-
"-DPYTHON_INCLUDE_DIRS=" + python_include_dir,
71+
"-DCMAKE_PREFIX_PATH=" + ";".join(self.cmake_prefix_paths),
72+
f"-DUSE_IPEX={int(self.use_ipex)}",
73+
"-DCMAKE_INSTALL_PREFIX=" + self.extdir,
74+
"-DPython3_ROOT_DIR:FILEPATH=" + sys.exec_prefix,
75+
"-DCMAKE_VERBOSE_MAKEFILE=TRUE",
5976
"-DCMAKE_C_COMPILER=icx",
6077
"-DCMAKE_CXX_COMPILER=icpx",
78+
"-DCMAKE_BUILD_TYPE=" + self.build_type,
79+
"-S",
80+
self.current_dir,
81+
"-B",
82+
self.build_temp,
6183
]
6284

63-
# configuration
64-
build_type = "Debug"
65-
build_args = ["--config", build_type]
66-
cmake_args += ["-DCMAKE_BUILD_TYPE=" + build_type]
6785
max_jobs = os.getenv("MAX_JOBS", str(2 * os.cpu_count()))
68-
build_args += ["-j" + max_jobs]
86+
build_args = [
87+
"--build",
88+
self.build_temp,
89+
"-j" + max_jobs,
90+
]
91+
92+
install_args = [
93+
"--build",
94+
self.build_temp,
95+
"--target",
96+
"install",
97+
]
6998

7099
env = os.environ.copy()
71-
cmake_dir = self.build_temp
72-
subprocess.check_call(["cmake", self.current_dir] + cmake_args, cwd=cmake_dir, env=env)
73-
subprocess.check_call(["cmake", "--build", "."] + build_args, cwd=cmake_dir)
100+
self.check_call(["cmake"] + cmake_args, env=env)
101+
self.check_call(["cmake"] + build_args)
102+
self.check_call(["cmake"] + install_args)
103+
104+
def clean(self):
105+
if os.path.exists(self.build_temp):
106+
remove_tree(self.build_temp, dry_run=self.dry_run)
107+
else:
108+
log.warn("'%s' does not exist -- can't clean it", os.path.relpath(self.build_temp,
109+
os.path.dirname(__file__)))
110+
74111

112+
class build_ext(_build_ext):
113+
114+
def run(self):
115+
cmake = CMakeBuild(debug=self.debug, dry_run=self.dry_run)
116+
cmake.run()
117+
super().run()
118+
119+
120+
class clean(_clean):
121+
122+
def run(self):
123+
cmake = CMakeBuild(dry_run=self.dry_run)
124+
cmake.clean()
125+
super().run()
75126

76-
cmake = CMakeBuild()
77-
cmake.run()
78127

79128
setup(name="triton-kernels-benchmark", packages=[
80129
"triton_kernels_benchmark",
81130
], package_dir={
82131
"triton_kernels_benchmark": "triton_kernels_benchmark",
83-
}, package_data={"triton_kernels_benchmark": ["xetla_kernel.so"]})
132+
}, package_data={"triton_kernels_benchmark": ["xetla_kernel.cpython-*.so"]}, cmdclass={
133+
"build_ext": build_ext,
134+
"clean": clean,
135+
}, ext_modules=[CMakeExtension("triton_kernels_benchmark")])

benchmarks/xetla_kernel/CMakeLists.txt

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
# XeTLA library is required.
2-
find_package(XeTLALibrary REQUIRED)
3-
set(CMAKE_CXX_STANDARD 20)
4-
51
set(XETLA_KERNEL_FLAGS ${XETLA_KERNEL_FLAGS}
62
-fsycl
73
-fsycl-device-code-split=per_kernel
@@ -29,8 +25,7 @@ else()
2925
set(XETLA_KERNEL_FLAGS ${XETLA_KERNEL_FLAGS} "${XETLA_OFFLINE_OPTIONS}")
3026
endif()
3127

32-
add_library(xetla_kernel SHARED python_main.cpp)
33-
set_target_properties(xetla_kernel PROPERTIES PREFIX "")
28+
Python3_add_library(xetla_kernel MODULE WITH_SOABI python_main.cpp)
3429
target_compile_options(xetla_kernel PRIVATE "-fPIC")
3530
if(USE_IPEX)
3631
target_compile_options(xetla_kernel PRIVATE "-fsycl")
@@ -40,7 +35,6 @@ endif()
4035
target_compile_options(xetla_kernel PUBLIC "-DXETPP_NEW_XMAIN")
4136
target_link_options(xetla_kernel PRIVATE ${XETLA_KERNEL_FLAGS})
4237
target_link_libraries(xetla_kernel PUBLIC ${TORCH_LIBRARIES} ${TORCH_PYTHON_LIBRARY})
43-
target_include_directories(xetla_kernel PUBLIC "${PYTHON_INCLUDE_DIRS}")
4438
target_include_directories(xetla_kernel PUBLIC "${XeTLALibrary_INCLUDE_DIR}")
4539

4640
if(USE_IPEX)
@@ -52,3 +46,5 @@ add_subdirectory(softmax)
5246
add_subdirectory(gemm)
5347
add_subdirectory(stream_k_gemm)
5448
add_subdirectory(flash_attention)
49+
50+
install(TARGETS xetla_kernel LIBRARY DESTINATION .)

benchmarks/xetla_kernel/flash_attention/fmha_forward_v5.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#ifndef TRITONBENCHMARK_FMHA_FWD_V5_H
1717
#define TRITONBENCHMARK_FMHA_FWD_V5_H
1818

19+
#include <cmath>
20+
1921
#include "fmha_policy_v2.h"
2022
#include "fmha_utils.h"
2123
#include "xetla.hpp"

0 commit comments

Comments
 (0)