Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ third_party/mthreads/python/*.egg-info
third_party/nvidia/backend/flagcx*
third_party/nvidia/backend/lib/libflagcx*

# Backends iluvatar
third_party/iluvatar/python/triton/FLAGTREE_BACKEND
third_party/iluvatar/python/*.egg-info
third_party/iluvatar/.triton/
third_party/iluvatar/bin/FileCheck
third_party/iluvatar/logs/

# Backends copied from submodules
python/triton/backends/*
!python/triton/backends/__init__.py
Expand Down
74 changes: 64 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ if(NOT FLAGTREE_BACKEND)
add_definitions(-D__AMD__)
elseif(FLAGTREE_BACKEND STREQUAL "iluvatar")
add_definitions(-D__ILUVATAR__)
remove_definitions(-D_GLIBCXX_USE_CXX11_ABI=1)
add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)
set(FLAGTREE_TLE OFF)
set(FLAGTREE_ILUVATAR_TLE ON)
add_definitions(-D__ILUVATAR_TLE__)
remove_definitions(-D__TLE__)
list(REMOVE_ITEM LLVM_TABLEGEN_FLAGS -D__TLE__)
elseif(FLAGTREE_BACKEND STREQUAL "mthreads")
set(ENV{PATH} "$ENV{LLVM_SYSPATH}/bin:$ENV{PATH}")
set(CMAKE_C_COMPILER clang)
Expand Down Expand Up @@ -235,6 +238,8 @@ if(NOT MSVC)
# Suppress visibility warnings in gluon_ir.cc (GCC 13+ -Wattributes on pybind11 hidden types)
# Also suppress -Wcomment for generated TritonGPUAttrDefs.h.inc (ASCII diagrams in TableGen output)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wno-attributes -Wno-comment")
elseif(FLAGTREE_BACKEND STREQUAL "iluvatar")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wno-covered-switch-default -Wno-deprecated-declarations -Wno-attributes")
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -Wno-covered-switch-default")
endif()
Expand Down Expand Up @@ -264,6 +269,25 @@ if (FLAGTREE_BACKEND MATCHES "^(cambricon|aipu|tsingmicro|enflame|rpu|thrive)$")
include_directories(${PROJECT_BINARY_DIR}/include) # Tablegen'd files
add_subdirectory(include)
add_subdirectory(lib)
elseif (FLAGTREE_BACKEND STREQUAL "iluvatar")
set(TRITON_CORE_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/third_party/iluvatar)
set(TRITON_CORE_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}/third_party/iluvatar)
option(TRITON_BUILD_GLUON "Build the Gluon IR Python bindings (gluon_ir.cc)" OFF)
include_directories(${TRITON_CORE_SOURCE_DIR}/include)
include_directories(${TRITON_CORE_BINARY_DIR}/include)
include_directories(${TRITON_CORE_SOURCE_DIR}/backend/include)
include_directories(${TRITON_CORE_BINARY_DIR}/backend/include)
if(FLAGTREE_ILUVATAR_TLE)
include_directories(${TRITON_CORE_SOURCE_DIR}/tle/include)
include_directories(${TRITON_CORE_BINARY_DIR}/tle/include)
endif()
add_subdirectory(${TRITON_CORE_SOURCE_DIR}/include ${TRITON_CORE_BINARY_DIR}/include)
add_subdirectory(${TRITON_CORE_SOURCE_DIR}/lib ${TRITON_CORE_BINARY_DIR}/lib)
elseif (FLAGTREE_BACKEND MATCHES "^(cambricon|aipu|tsingmicro|enflame|thrive)$")
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_BINARY_DIR}/include) # Tablegen'd files
add_subdirectory(include)
add_subdirectory(lib)
elseif(NOT FLAGTREE_BACKEND)
add_subdirectory(include)
add_subdirectory(lib)
Expand Down Expand Up @@ -323,6 +347,11 @@ if(TRITON_BUILD_PYTHON_MODULE)
elseif(FLAGTREE_BACKEND AND FLAGTREE_BACKEND STREQUAL "mthreads")
include_directories(${PROJECT_BINARY_DIR}/third_party/${FLAGTREE_BACKEND})
add_subdirectory(third_party/mthreads/proton/Dialect)
elseif(FLAGTREE_BACKEND AND FLAGTREE_BACKEND STREQUAL "iluvatar")
if (TRITON_BUILD_PROTON)
list(APPEND TRITON_PLUGIN_NAMES "proton")
add_subdirectory(third_party/proton/Dialect)
endif()
else()
list(APPEND TRITON_PLUGIN_NAMES "proton")
add_subdirectory(third_party/proton/Dialect)
Expand Down Expand Up @@ -488,14 +517,33 @@ if(TRITON_BUILD_PYTHON_MODULE)

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
${PYTHON_SRC_PATH}/ir.cc
${PYTHON_SRC_PATH}/gluon_ir.cc
${PYTHON_SRC_PATH}/linear_layout.cc
${PYTHON_SRC_PATH}/passes.cc
${PYTHON_SRC_PATH}/interpreter.cc
${PYTHON_SRC_PATH}/llvm.cc
${PYTHON_SRC_PATH}/specialize.cc)
if(FLAGTREE_BACKEND STREQUAL "iluvatar")
set(TRITON_ILU_PYTHON_SRCS
${PYTHON_SRC_PATH}/main.cc
${PYTHON_SRC_PATH}/ir.cc
${PYTHON_SRC_PATH}/linear_layout.cc
${PYTHON_SRC_PATH}/passes.cc
${PYTHON_SRC_PATH}/interpreter.cc
${PYTHON_SRC_PATH}/llvm.cc
${PYTHON_SRC_PATH}/specialize.cc)
if(TRITON_BUILD_GLUON)
list(APPEND TRITON_ILU_PYTHON_SRCS ${PYTHON_SRC_PATH}/gluon_ir.cc)
endif()
add_library(triton SHARED ${TRITON_ILU_PYTHON_SRCS})
if(TRITON_BUILD_GLUON)
target_compile_definitions(triton PRIVATE TRITON_BUILD_GLUON)
endif()
else()
add_library(triton SHARED ${PYTHON_SRC_PATH}/main.cc
${PYTHON_SRC_PATH}/ir.cc
${PYTHON_SRC_PATH}/gluon_ir.cc
${PYTHON_SRC_PATH}/linear_layout.cc
${PYTHON_SRC_PATH}/passes.cc
${PYTHON_SRC_PATH}/interpreter.cc
${PYTHON_SRC_PATH}/llvm.cc
${PYTHON_SRC_PATH}/specialize.cc)
target_compile_definitions(triton PRIVATE TRITON_BUILD_GLUON)
endif()

# Link triton with its dependencies
target_link_libraries(triton PRIVATE ${TRITON_LIBRARIES})
Expand Down Expand Up @@ -560,6 +608,12 @@ if(NOT FLAGTREE_BACKEND OR FLAGTREE_BACKEND MATCHES "^(aipu|tsingmicro|enflame|r
flagtree_add_tle_generated_header_dependencies()
endif()
add_subdirectory(test)
elif (FLAGTREE_BACKEND STREQUAL "iluvatar")

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

elseif(FLAGTREE_BACKEND STREQUAL "iluvatar")

option(FLAGTREE_ILUVATAR_BUILD_BIN "Build third_party/iluvatar/bin tools and lit tests" OFF)
if(FLAGTREE_ILUVATAR_BUILD_BIN)
add_subdirectory(${TRITON_CORE_SOURCE_DIR}/bin ${TRITON_CORE_BINARY_DIR}/bin)
add_subdirectory(test)
endif()
endif()

if(TRITON_BUILD_UT)
Expand Down
15 changes: 7 additions & 8 deletions python/setup_tools/setup_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,11 @@ def post_install():

def write_flagtree_backend_file(triton_pkg_dir=None):
if triton_pkg_dir is None:
triton_pkg_dir = Path(__file__).resolve().parents[1] / "triton"
repo_root = Path(__file__).resolve().parents[2]
if os.environ.get("FLAGTREE_BACKEND") == "iluvatar":
triton_pkg_dir = repo_root / "third_party" / "iluvatar" / "python" / "triton"
else:
triton_pkg_dir = repo_root / "python" / "triton"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

backend_value = os.environ.get("FLAGTREE_BACKEND", "")
dest_file = Path(triton_pkg_dir) / "FLAGTREE_BACKEND"
dest_file.write_text(backend_value)
Expand Down Expand Up @@ -418,18 +422,13 @@ def uninstall_triton():

# iluvatar
cache.store(
file="iluvatar-llvm18-x86_64",
file="iluvatar-llvm22-x86_64",
condition=("iluvatar" == flagtree_backend),
url="https://baai-cp-web.ks3-cn-beijing.ksyuncs.com/trans/iluvatar-llvm18-x86_64_v0.3.0.tar.gz",
url="https://baai-cp-web.ks3-cn-beijing.ksyuncs.com/trans/iluvatar-llvm22-x86_64_v0.6.0.tar.gz",
pre_hook=lambda: check_env('LLVM_SYSPATH'),
post_hook=set_llvm_env,
)

cache.store(
file="iluvatarTritonPlugin.so", condition=("iluvatar" == flagtree_backend) and (not configs.flagtree_plugin), url=
"https://baai-cp-web.ks3-cn-beijing.ksyuncs.com/trans/iluvatarTritonPlugin-cpython3.10-glibc2.30-glibcxx3.4.28-cxxabi1.3.12-ubuntu-x86_64_v0.3.0.tar.gz",
copy_dst_path=f"third_party/{flagtree_backend}", md5_digest="015b9af8")

# klx xpu
cache.store(
file="XTDK-llvm18-ubuntu2004_x86_64",
Expand Down
128 changes: 128 additions & 0 deletions python/setup_tools/utils/iluvatar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import inspect
import os
import shutil
import sys
from pathlib import Path

from setuptools import find_packages

FLAGTREE_BACKEND = os.environ.get("FLAGTREE_BACKEND", "iluvatar")
PYTHON_ROOT = f"third_party/{FLAGTREE_BACKEND}/python"
FLAGTREE_PYTHON_ROOT = "python"
TLE_PACKAGE = "triton.experimental.tle"
SKIP_BACKEND_PACKAGES_IN_PACKAGE_DIR = True


def _is_backend_package(package):
return package == "triton.backends" or package.startswith("triton.backends.")


def _is_language_extra_package(package):
return package == "triton.language.extra" or package.startswith("triton.language.extra.")


def _build_setup_hooks(backend, python_root, skip_backend_packages_in_package_dir=False):
patched_attr = f"_{backend}_python_root_patched"

def merge_packages(existing_packages):
packages = []
seen = set()

def add(package):
if package not in seen:
packages.append(package)
seen.add(package)

for package in find_packages(where=python_root, include=["triton", "triton.*"]):
add(package)

for package in find_packages(where=FLAGTREE_PYTHON_ROOT, include=[TLE_PACKAGE, f"{TLE_PACKAGE}.*"]):
add(package)

for package in existing_packages:
if (not package.startswith("triton.") or _is_backend_package(package)
or _is_language_extra_package(package) or package == "triton.profiler"
or package.startswith("triton.profiler.")):
add(package)

return packages

def merge_package_dir(existing_package_dir):
package_dir = dict(existing_package_dir or {})
package_dir[""] = python_root

for package in find_packages(where=python_root, include=["triton", "triton.*"]):
if skip_backend_packages_in_package_dir and package.startswith("triton.backends."):
continue
rel_package_path = package.replace(".", "/")
package_dir[package] = f"{python_root}/{rel_package_path}"

for package in find_packages(where=FLAGTREE_PYTHON_ROOT, include=[TLE_PACKAGE, f"{TLE_PACKAGE}.*"]):
rel_package_path = package.replace(".", "/")
package_dir[package] = f"{FLAGTREE_PYTHON_ROOT}/{rel_package_path}"

return package_dir

def patch_cmdclass(existing_cmdclass):
cmdclass = dict(existing_cmdclass or {})
original_build_py = cmdclass.get("build_py")
if original_build_py is None:
return cmdclass

class BackendBuildPy(original_build_py):

def run(self):
self.force = True
build_triton_dir = Path(self.build_lib) / "triton"
if build_triton_dir.exists():
shutil.rmtree(build_triton_dir)
return super().run()

cmdclass["build_py"] = BackendBuildPy
return cmdclass

def wrap_setup(original_setup):
if getattr(original_setup, patched_attr, False):
return original_setup

def setup_with_backend_python_root(*args, **kwargs):
kwargs["packages"] = merge_packages(kwargs.get("packages", []))
kwargs["package_dir"] = merge_package_dir(kwargs.get("package_dir", {}))
kwargs["cmdclass"] = patch_cmdclass(kwargs.get("cmdclass", {}))
return original_setup(*args, **kwargs)

setattr(setup_with_backend_python_root, patched_attr, True)
setup_with_backend_python_root._backend_python_root_original_setup = original_setup
return setup_with_backend_python_root

return wrap_setup


def _patch_setup(wrap_setup):
patched = False

frame = inspect.currentframe()
while frame is not None:
setup_func = frame.f_globals.get("setup")
if callable(setup_func):
frame.f_globals["setup"] = wrap_setup(setup_func)
patched = True
frame = frame.f_back

main_module = sys.modules.get("__main__")
if main_module is not None and hasattr(main_module, "setup"):
main_module.setup = wrap_setup(main_module.setup)
patched = True

if not patched:
raise RuntimeError(
f"{FLAGTREE_BACKEND} setup hook could not find setup() to patch "
f"(python root: {PYTHON_ROOT})")


_wrap_setup = _build_setup_hooks(
FLAGTREE_BACKEND,
PYTHON_ROOT,
skip_backend_packages_in_package_dir=SKIP_BACKEND_PACKAGES_IN_PACKAGE_DIR,
)
_patch_setup(_wrap_setup)
10 changes: 9 additions & 1 deletion python/triton/experimental/tle/language/gpu/core.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# flagtree tle
import builtins
import os
import triton.language.core as tl
from typing import Optional, Sequence
from enum import Enum
Expand All @@ -13,6 +14,13 @@
range,
)

try:
from triton._flagtree_backend import FLAGTREE_BACKEND
except ModuleNotFoundError:
FLAGTREE_BACKEND = os.environ.get("FLAGTREE_BACKEND", "")

iluvatar_enabled = FLAGTREE_BACKEND == "iluvatar"

# Address space 3 matches the shared-memory space used in TritonGPU lowering.
SHARED_MEMORY_ADDRESS_SPACE = 3

Expand Down Expand Up @@ -394,7 +402,7 @@ def normcopy(
try:
if direction == CopyDirection.GM_TO_LOCAL:
# None fills the FlagTree hints slot; TLE copy has no hints to pass.
load_extra_args = () if mthreads_enabled else (None, )
load_extra_args = () if (mthreads_enabled or iluvatar_enabled) else (None, )
tt_load = _semantic.load(src, mask, other, boundary_check, padding_option, cache_modifier,
eviction_policy, volatile, *load_extra_args)
local_ptrs = local_ptr(dst, _make_full_indices(dst, _semantic), _semantic=_semantic)
Expand Down
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,10 @@ def build_extension(self, ext):
if check_env_flag("TRITON_BUILD_PROTON", "ON"): # Default ON
cmake_args += self.get_proton_cmake_args()

if helper.flagtree_backend == "iluvatar":
gluon_flag = "ON" if check_env_flag("TRITON_ILU_BUILD_GLUON") else "OFF"
cmake_args += [f"-DTRITON_BUILD_GLUON={gluon_flag}"]

if is_offline_build():
# unit test builds fetch googletests from GitHub
cmake_args += ["-DTRITON_BUILD_UT=OFF"]
Expand Down
36 changes: 36 additions & 0 deletions third_party/iluvatar/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/backend/include)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/backend/include)
if(FLAGTREE_ILUVATAR_TLE)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/tle/include)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/tle/include)
add_subdirectory(tle)
endif()
add_subdirectory(backend/include)
add_subdirectory(backend/lib)
if(TRITON_BUILD_PYTHON_MODULE)
find_package(LLD REQUIRED CONFIG PATHS "${LLD_DIR}" NO_DEFAULT_PATH)
include_directories(${LLD_INCLUDE_DIRS})
message(STATUS "Found LLD distro-package @ ${LLD_DIR} and LLD include dirs @ ${LLD_INCLUDE_DIRS}")
if(FLAGTREE_ILUVATAR_TLE)
set(_ILUVATAR_TLE_PLUGIN_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/tle/triton_iluvatar_tle.cc)
set(_ILUVATAR_TLE_PLUGIN_LIBS IluvatarTleIR IluvatarTleTransforms)
set(_ILUVATAR_TLE_PLUGIN_DEPS IluvatarTleTableGen IluvatarTleTransformsIncGen)
else()
set(_ILUVATAR_TLE_PLUGIN_SOURCES "")
set(_ILUVATAR_TLE_PLUGIN_LIBS "")
set(_ILUVATAR_TLE_PLUGIN_DEPS "")
endif()
add_triton_plugin(TritonILUVATAR
${CMAKE_CURRENT_SOURCE_DIR}/triton_iluvatar.cc
${_ILUVATAR_TLE_PLUGIN_SOURCES}
LINK_LIBS TritonILUVATARGPUToLLVM ${_ILUVATAR_TLE_PLUGIN_LIBS})
if(FLAGTREE_ILUVATAR_TLE)
add_dependencies(TritonILUVATAR ${_ILUVATAR_TLE_PLUGIN_DEPS})
endif()
target_link_libraries(TritonILUVATAR PRIVATE Python3::Module pybind11::headers lldCommon lldELF)
endif()
if(TRITON_BUILD_UT)
add_subdirectory(unittest)
endif()
# add_subdirectory(test)
Empty file.
Loading
Loading