Skip to content
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions pkgs/by-name/tr/triton-llvm/package.nix
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{ lib
, stdenv
, fetchFromGitHub
, fetchpatch
, pkgsBuildBuild
, pkg-config
, cmake
Expand All @@ -11,6 +12,7 @@
, libedit
, libffi
, libpfm
, lit
, mpfr
, zlib
, ncurses
Expand Down Expand Up @@ -45,7 +47,7 @@ let
isNative = stdenv.hostPlatform == stdenv.buildPlatform;
in stdenv.mkDerivation (finalAttrs: {
pname = "triton-llvm";
version = "17.0.0-c5dede880d17";
version = "19.1.0-rc1"; # One of the tags at https://github.com/llvm/llvm-project/commit/10dc3a8e916d73291269e5e2b82dd22681489aa1

outputs = [
"out"
Expand All @@ -60,9 +62,18 @@ in stdenv.mkDerivation (finalAttrs: {
src = fetchFromGitHub {
owner = "llvm";
repo = "llvm-project";
rev = "c5dede880d175f7229c9b2923f4753e12702305d";
hash = "sha256-v4r3+7XVFK+Dzxt/rErZNJ9REqFO3JmGN4X4vZ+77ew=";
rev = "10dc3a8e916d73291269e5e2b82dd22681489aa1";
hash = "sha256-9DPvcFmhzw6MipQeCQnr35LktW0uxtEL8axMMPXIfWw=";
};
patches = [
# glibc-2.40 support
# [llvm-exegesis] Use correct rseq struct size #100804
# https://github.com/llvm/llvm-project/issues/100791
(fetchpatch {
url = "https://github.com/llvm/llvm-project//commit/84837e3cc1cf17ed71580e3ea38299ed2bfaa5f6.patch";
hash = "sha256-QKa+kyXjjGXwTQTEpmKZx5yYjOyBX8A8NQoIYUaGcIw=";
})
];

nativeBuildInputs = [
pkg-config
Expand All @@ -74,6 +85,7 @@ in stdenv.mkDerivation (finalAttrs: {
doxygen
sphinx
python3Packages.recommonmark
python3Packages.myst-parser
];

buildInputs = [
Expand All @@ -90,7 +102,9 @@ in stdenv.mkDerivation (finalAttrs: {
ncurses
];

sourceRoot = "${finalAttrs.src.name}/llvm";
preConfigure = ''
cd llvm
'';

cmakeFlags = [
(lib.cmakeFeature "LLVM_TARGETS_TO_BUILD" (lib.concatStringsSep ";" llvmTargetsToBuild'))
Expand Down Expand Up @@ -140,23 +154,25 @@ in stdenv.mkDerivation (finalAttrs: {

postPatch = ''
# `CMake Error: cannot write to file "/build/source/llvm/build/lib/cmake/mlir/MLIRTargets.cmake": Permission denied`
chmod +w -R ../mlir
patchShebangs ../mlir/test/mlir-reduce
chmod +w -R ./mlir
patchShebangs ./mlir/test/mlir-reduce

# FileSystem permissions tests fail with various special bits
rm test/tools/llvm-objcopy/ELF/mirror-permissions-unix.test
rm unittests/Support/Path.cpp
rm llvm/test/tools/llvm-objcopy/ELF/mirror-permissions-unix.test
rm llvm/unittests/Support/Path.cpp

substituteInPlace unittests/Support/CMakeLists.txt \
substituteInPlace llvm/unittests/Support/CMakeLists.txt \
--replace "Path.cpp" ""
'' + lib.optionalString stdenv.hostPlatform.isAarch64 ''
# Not sure why this fails
rm test/tools/llvm-exegesis/AArch64/latency-by-opcode-name.s
rm llvm/test/tools/llvm-exegesis/AArch64/latency-by-opcode-name.s
'';

postInstall = lib.optionalString (!isNative) ''
postInstall = ''
cp ${lib.getExe lit} $out/bin/llvm-lit
'' + (lib.optionalString (!isNative) ''
cp -a NATIVE/bin/llvm-config $out/bin/llvm-config-native
'';
'');

doCheck = buildTests;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
From c5d4087519eae6f41c80bbd8ffbcc9390db44c7f Mon Sep 17 00:00:00 2001
From: SomeoneSerge <else+aalto@someonex.net>
Date: Thu, 10 Oct 2024 19:19:18 +0000
Subject: [PATCH] cmake.py: propagate cmakeFlags from environment

---
tools/setup_helpers/cmake.py | 6 ++++++
1 file changed, 6 insertions(+)

diff --git a/tools/setup_helpers/cmake.py b/tools/setup_helpers/cmake.py
index 4b605fe5975..ea1d6a1ef46 100644
--- a/tools/setup_helpers/cmake.py
+++ b/tools/setup_helpers/cmake.py
@@ -332,6 +332,12 @@ class CMake:
file=sys.stderr,
)
print(e, file=sys.stderr)
+
+ # Nixpkgs compat:
+ if "cmakeFlags" in os.environ:
+ import shlex
+ args.extend(shlex.split(os.environ["cmakeFlags"]))
+
# According to the CMake manual, we should pass the arguments first,
# and put the directory as the last element. Otherwise, these flags
# may not be passed correctly.
--
2.46.0

56 changes: 46 additions & 10 deletions pkgs/development/python-modules/torch/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,8 @@
removeReferencesTo,

# Build inputs
darwin,
numactl,
Accelerate,
CoreServices,
libobjc,

# Propagated build inputs
astunparse,
Expand All @@ -56,6 +54,17 @@
tritonSupport ? (!stdenv.hostPlatform.isDarwin),
triton,

# TODO: 1. callPackage needs to learn to distinguish between the task
# of "asking for an attribute from the parent scope" and
# the task of "exposing a formal parameter in .override".
# TODO: 2. We should probably abandon attributes such as `torchWithCuda` (etc.)
# as they routinely end up consuming the wrong arguments\
# (dependencies without cuda support).
# Instead we should rely on overlays and nixpkgsFun.
# (@SomeoneSerge)
_tritonEffective ? if cudaSupport then triton-cuda else triton,
triton-cuda,

# Unit tests
hypothesis,
psutil,
Expand Down Expand Up @@ -95,6 +104,8 @@ let
;
inherit (cudaPackages) cudaFlags cudnn nccl;

triton = throw "python3Packages.torch: use _tritonEffective instead of triton to avoid divergence";

rocmPackages = rocmPackages_5;

setBool = v: if v then "1" else "0";
Expand Down Expand Up @@ -240,6 +251,7 @@ buildPythonPackage rec {
# Allow setting PYTHON_LIB_REL_PATH with an environment variable.
# https://github.com/pytorch/pytorch/pull/128419
./passthrough-python-lib-rel-path.patch
./0001-cmake.py-propagate-cmakeFlags-from-environment.patch
]
++ lib.optionals cudaSupport [ ./fix-cmake-cuda-toolkit.patch ]
++ lib.optionals (stdenv.hostPlatform.isDarwin && stdenv.hostPlatform.isx86_64) [
Expand All @@ -257,7 +269,18 @@ buildPythonPackage rec {
];

postPatch =
lib.optionalString rocmSupport ''
''
substituteInPlace cmake/public/cuda.cmake \
--replace-fail \
'message(FATAL_ERROR "Found two conflicting CUDA' \
'message(WARNING "Found two conflicting CUDA' \
--replace-warn \
"set(CUDAToolkit_ROOT" \
"# Upstream: set(CUDAToolkit_ROOT"
substituteInPlace third_party/gloo/cmake/Cuda.cmake \
--replace-warn "find_package(CUDAToolkit 7.0" "find_package(CUDAToolkit"
''
+ lib.optionalString rocmSupport ''
# https://github.com/facebookincubator/gloo/pull/297
substituteInPlace third_party/gloo/cmake/Hipify.cmake \
--replace "\''${HIPIFY_COMMAND}" "python \''${HIPIFY_COMMAND}"
Expand Down Expand Up @@ -351,6 +374,17 @@ buildPythonPackage rec {
# NB technical debt: building without NNPACK as workaround for missing `six`
USE_NNPACK = 0;

cmakeFlags =
[
# (lib.cmakeBool "CMAKE_FIND_DEBUG_MODE" true)
(lib.cmakeFeature "CUDAToolkit_VERSION" cudaPackages.cudaVersion)
]
++ lib.optionals cudaSupport [
# Unbreaks version discovery in enable_language(CUDA) when wrapping nvcc with ccache
# Cf. https://gitlab.kitware.com/cmake/cmake/-/issues/26363
(lib.cmakeFeature "CMAKE_CUDA_COMPILER_TOOLKIT_VERSION" cudaPackages.cudaVersion)
];

preBuild = ''
export MAX_JOBS=$NIX_BUILD_CORES
${python.pythonOnBuildForHost.interpreter} setup.py build --cmake-only
Expand Down Expand Up @@ -495,11 +529,11 @@ buildPythonPackage rec {
++ lib.optionals (cudaSupport || rocmSupport) [ effectiveMagma ]
++ lib.optionals stdenv.hostPlatform.isLinux [ numactl ]
++ lib.optionals stdenv.hostPlatform.isDarwin [
Accelerate
CoreServices
libobjc
darwin.apple_sdk.frameworks.Accelerate
darwin.apple_sdk.frameworks.CoreServices
darwin.libobjc
]
++ lib.optionals tritonSupport [ triton ]
++ lib.optionals tritonSupport [ _tritonEffective ]
++ lib.optionals MPISupport [ mpi ]
++ lib.optionals rocmSupport [ rocmtoolkit_joined ];

Expand Down Expand Up @@ -527,7 +561,7 @@ buildPythonPackage rec {

# torch/csrc requires `pybind11` at runtime
pybind11
] ++ lib.optionals tritonSupport [ triton ];
] ++ lib.optionals tritonSupport [ _tritonEffective ];

propagatedCxxBuildInputs =
[ ] ++ lib.optionals MPISupport [ mpi ] ++ lib.optionals rocmSupport [ rocmtoolkit_joined ];
Expand Down Expand Up @@ -662,7 +696,9 @@ buildPythonPackage rec {
thoughtpolice
tscholak
]; # tscholak esp. for darwin-related builds
platforms = with lib.platforms; linux ++ lib.optionals (!cudaSupport && !rocmSupport) darwin;
platforms =
lib.platforms.linux
++ lib.optionals (!cudaSupport && !rocmSupport) lib.platforms.darwin;
broken = builtins.any trivial.id (builtins.attrValues brokenConditions);
};
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
From 2751c5de5c61c90b56e3e392a41847f4c47258fd Mon Sep 17 00:00:00 2001
From: SomeoneSerge <else+aalto@someonex.net>
Date: Sun, 13 Oct 2024 14:16:48 +0000
Subject: [PATCH 1/3] _build: allow extra cc flags

---
python/triton/runtime/build.py | 10 +++++++++-
1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/python/triton/runtime/build.py b/python/triton/runtime/build.py
index d7baeb286..d334dce77 100644
--- a/python/triton/runtime/build.py
+++ b/python/triton/runtime/build.py
@@ -42,9 +42,17 @@ def _build(name, src, srcdir, library_dirs, include_dirs, libraries):
py_include_dir = sysconfig.get_paths(scheme=scheme)["include"]
include_dirs = include_dirs + [srcdir, py_include_dir]
cc_cmd = [cc, src, "-O3", "-shared", "-fPIC", "-o", so]
+
+ # Nixpkgs support branch
+ # Allows passing e.g. extra -Wl,-rpath
+ cc_cmd_extra_flags = "@ccCmdExtraFlags@"
+ if cc_cmd_extra_flags != ("@" + "ccCmdExtraFlags@"): # substituteAll hack
+ import shlex
+ cc_cmd.extend(shlex.split(cc_cmd_extra_flags))
+
cc_cmd += [f'-l{lib}' for lib in libraries]
cc_cmd += [f"-L{dir}" for dir in library_dirs]
- cc_cmd += [f"-I{dir}" for dir in include_dirs]
+ cc_cmd += [f"-I{dir}" for dir in include_dirs if dir is not None]
ret = subprocess.check_call(cc_cmd)
if ret == 0:
return so
--
2.46.0

This file was deleted.

Copy link
Contributor

Choose a reason for hiding this comment

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

As I proposed before, instead of manually deleting all the download* calls we should maintain a smaller/more generic patch and we should try to uptream what we can. Here's a PoC, but I didn't verify the build yet, building llvm now

Copy link
Contributor

Choose a reason for hiding this comment

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

Seeing if this is upstreamable: triton-lang/triton#4414

Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
From 587d1f3428eca63544238802f19e0be670d03244 Mon Sep 17 00:00:00 2001
From: SomeoneSerge <else@someonex.net>
Date: Mon, 29 Jul 2024 14:31:11 +0000
Subject: [PATCH] setup.py: introduce TRITON_OFFLINE_BUILD

To prevent any vendoring whatsoever
---
python/setup.py | 26 ++++++++++++++++++++++++--
1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/python/setup.py b/python/setup.py
index 73800ec40..4e5b04de4 100644
--- a/python/setup.py
+++ b/python/setup.py
@@ -112,6 +112,20 @@ def get_env_with_keys(key: list):
return os.environ[k]
return ""

+def is_offline_build() -> bool:
+ """
+ Downstream projects and distributions which bootstrap their own dependencies from scratch
+ and run builds in offline sandboxes
+ may set `TRITON_OFFLINE_BUILD` in the build environment to prevent any attempts at downloading
+ pinned dependencies from the internet or at using dependencies vendored in-tree.
+
+ Dependencies must be defined using respective search paths (cf. `syspath_var_name` in `Package`).
+ Missing dependencies lead to an early abortion.
+ Dependencies' compatibility is not verified.
+
+ Note that this flag isn't tested by the CI and does not provide any guarantees.
+ """
+ return os.environ.get("TRITON_OFFLINE_BUILD", "") != ""

# --- third party packages -----

@@ -220,8 +234,14 @@ def get_thirdparty_packages(packages: list):
if os.environ.get(p.syspath_var_name):
package_dir = os.environ[p.syspath_var_name]
version_file_path = os.path.join(package_dir, "version.txt")
- if p.syspath_var_name not in os.environ and\
- (not os.path.exists(version_file_path) or Path(version_file_path).read_text() != p.url):
+
+ input_defined = p.syspath_var_name not in os.environ
+ input_exists = input_defined and os.path.exists(version_file_path)
+ input_compatible = input_exists and Path(version_file_path).read_text() == p.url
+
+ if is_offline_build() and not input_defined:
+ raise RuntimeError(f"Requested an offline build but {p.syspath_var_name} is not set")
+ if not is_offline_build() and not input_compatible:
with contextlib.suppress(Exception):
shutil.rmtree(package_root_dir)
os.makedirs(package_root_dir, exist_ok=True)
@@ -245,6 +265,8 @@ def get_thirdparty_packages(packages: list):


def download_and_copy(name, src_path, variable, version, url_func):
+ if is_offline_build():
+ return
Comment on lines 56 to 58
Copy link
Contributor

Choose a reason for hiding this comment

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

I also only now realized that the way I implemented triton-lang/triton#4414 this makes triton's setup.py simply ignore TRITON_PTXAS_PATH and friends: the default branch was to (download and) copy it into the wheel as package_data, but now we exit too early. Recovering the package_data branch I noticed that setuptools actually messes up the binary (e.g. DT_RUNPATHs were different) to the point that it segfaults at runtime, so instead of fixing this I added another substiteAll-based patch, grep for @nixpkgsExtraBinaryPaths@

This needs to be improved, and improvements need to be upstreamed, but right now our triton support is lagging toooooooooo far behind so I think merging these hacky bits is warranted...

triton_cache_path = get_triton_cache_path()
if variable in os.environ:
return
--
2.45.1

Loading