Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUILD] Support building out of tree plugins #3007

Merged
merged 20 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ if(TRITON_BUILD_PYTHON_MODULE)
add_link_options(${Python3_LINK_OPTIONS})
endif()

if (DEFINED TRITON_EXTERNAL_CODEGEN_BACKEND_DIRS)
nhat-nguyen marked this conversation as resolved.
Show resolved Hide resolved
foreach(BACKEND_DIR ${TRITON_EXTERNAL_CODEGEN_BACKEND_DIRS})
cmake_path(APPEND BACKEND_DIR "build" OUTPUT_VARIABLE BACKEND_DIR_BUILD_OUTPUT)
nhat-nguyen marked this conversation as resolved.
Show resolved Hide resolved
add_subdirectory(${BACKEND_DIR} ${BACKEND_DIR_BUILD_OUTPUT})
endforeach()
endif()

foreach(CODEGEN_BACKEND ${TRITON_CODEGEN_BACKENDS})
add_subdirectory(third_party/${CODEGEN_BACKEND})
endforeach()
Expand Down Expand Up @@ -191,6 +198,12 @@ if(TRITON_BUILD_PYTHON_MODULE)

# Define triton library
string(JOIN "," TRITON_BACKENDS_TUPLE ${TRITON_CODEGEN_BACKENDS})

if (DEFINED TRITON_EXTERNAL_CODEGEN_BACKEND_DIRS)
string(JOIN "," TRITON_BACKENDS_TUPLE ${TRITON_BACKENDS_TUPLE} ${TRITON_EXTERNAL_CODEGEN_BACKENDS})
endif()


set(TRITON_BACKENDS_TUPLE "(${TRITON_BACKENDS_TUPLE})")
add_compile_definitions(TRITON_BACKENDS_TUPLE=${TRITON_BACKENDS_TUPLE})
add_library(triton SHARED ${PYTHON_SRC_PATH}/main.cc
Expand Down
44 changes: 41 additions & 3 deletions python/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Backend:
src_dir: str
backend_dir: str
install_dir: str
is_external: bool


def _copy_backends(active):
Expand All @@ -54,7 +55,42 @@ def _copy_backends(active):
package_data = [f"{os.path.relpath(p, backend_path)}/*" for p, _, _, in os.walk(backend_path)]
ret.append(
Backend(name=backend, package_data=package_data, src_dir=curr_path, backend_dir=backend_path,
install_dir=install_dir))
install_dir=install_dir, is_external=False))
return ret

def _copy_external_backends():
# TRITON_EXTERNAL_CODEGEN_BACKENDS is a semicolon-separated list of backend names
nhat-nguyen marked this conversation as resolved.
Show resolved Hide resolved
# TRITON_EXTERNAL_CODEGEN_BACKEND_DIRS is a semicolon-separated list of paths to the backends
# Note that the paths have to include the provided backend names as a subfolder
backends = os.getenv('TRITON_EXTERNAL_CODEGEN_BACKENDS')
backend_dirs = os.getenv('TRITON_EXTERNAL_CODEGEN_BACKEND_DIRS')

if backends is None or backend_dirs is None:
return []

backends = backends.strip().split(';')
backend_dirs = backend_dirs.strip().split(';')

if len(backends) != len(backend_dirs):
raise Exception(f"External backend names and directories mismatch")

ret = []

for backend, root_dir in zip(backends, backend_dirs):
curr_path = os.path.join(root_dir, backend)
backend_path = os.path.abspath(os.path.join(curr_path, "backend"))
install_dir = os.path.join(os.path.dirname(__file__), "triton", "backends", backend)

# check conditions
assert backend in os.listdir(root_dir), f"{backend} is requested for install but not present in {root_dir}"
nhat-nguyen marked this conversation as resolved.
Show resolved Hide resolved
assert os.path.exists(backend_path), f"{backend_path} does not exist!"
for file in ["compiler.py", "driver.py"]:
assert os.path.exists(os.path.join(backend_path, file))
# update
package_data = [f"{os.path.relpath(p, backend_path)}/*" for p, _, _, in os.walk(backend_path)]
ret.append(
Backend(name=backend, package_data=package_data, src_dir=curr_path, backend_dir=backend_path,
install_dir=install_dir, is_external=True))
return ret


Expand Down Expand Up @@ -297,7 +333,9 @@ def build_extension(self, ext):
"-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=" + extdir, "-DTRITON_BUILD_TUTORIALS=OFF",
"-DTRITON_BUILD_PYTHON_MODULE=ON", "-DPython3_EXECUTABLE:FILEPATH=" + sys.executable,
"-DCMAKE_VERBOSE_MAKEFILE:BOOL=ON", "-DPYTHON_INCLUDE_DIRS=" + python_include_dir,
"-DTRITON_CODEGEN_BACKENDS=" + ';'.join([b.name for b in backends])
"-DTRITON_CODEGEN_BACKENDS=" + ';'.join([b.name for b in backends if not b.is_external]),
"-DTRITON_EXTERNAL_CODEGEN_BACKENDS=" + ';'.join([b.name for b in backends if b.is_external]),
"-DTRITON_EXTERNAL_CODEGEN_BACKEND_DIRS=" + ';'.join([b.src_dir for b in backends if b.is_external])
]
if lit_dir is not None:
cmake_args.append("-DLLVM_EXTERNAL_LIT=" + lit_dir)
Expand Down Expand Up @@ -380,8 +418,8 @@ def build_extension(self, ext):
url_func=lambda arch, version:
f"https://anaconda.org/nvidia/cuda-nvdisasm/12.3.52/download/linux-{arch}/cuda-nvdisasm-{version}-0.tar.bz2",
)
backends = _copy_backends(["nvidia", "amd"])

backends = [*_copy_backends(["nvidia", "amd"]), *_copy_external_backends()]
nhat-nguyen marked this conversation as resolved.
Show resolved Hide resolved

def add_link_to_backends():
for backend in backends:
Expand Down
Loading