-
-
Notifications
You must be signed in to change notification settings - Fork 18.1k
WIP: python3Packages.openai-triton: make cudaSupport optional #224898
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
Draft
SomeoneSerge
wants to merge
13
commits into
NixOS:master
Choose a base branch
from
SomeoneSerge:triton-optional-cuda
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
3244a8d
python3Packages.torch: 1.13.1 -> 2.0.0
SomeoneSerge 36eba15
python3Packages.torch: add missing install_requires
SomeoneSerge 0b5b6a1
python3Packages.torchWithRocm: ignore config.cudaSupport
SomeoneSerge 1b67689
python3Packages.openai-triton: init at 2.0.0
SomeoneSerge b70e91a
python3Packages.torchWithCuda: avoid "unknown-warning" when building …
SomeoneSerge ab43bd9
python3Packages.torchinfo: 1.64 -> 1.7.2
SomeoneSerge 8214f1b
python3Packages.torchinfo: fix pythonImportsCheck
SomeoneSerge 0e25f1b
ocamlPackages.torch: patch for pytorch 2.0.0 compatibility
SomeoneSerge b5d94f4
python3Packages.openai-triton: inline bash comments
SomeoneSerge 87c87cd
python3Packages.openai-triton: justify the use of pkgsTargetTarget
SomeoneSerge 22a779a
cudaPackages.cudatoolkit: hotfix the libstdc++ mismatch caused by cud…
SomeoneSerge 2bae0a3
python3Packages.openai-triton: make cudaSupport optional (rocm be unf…
SomeoneSerge 6ef1fcc
python3Packages.openai-triton: alphabetize inputs
SomeoneSerge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
265 changes: 265 additions & 0 deletions
265
pkgs/development/python-modules/openai-triton/default.nix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,265 @@ | ||
| { addOpenGLRunpath | ||
| , buildPythonPackage | ||
| , cmake | ||
| , config | ||
| , cudaPackages ? { } | ||
| , cudaSupport ? config.cudaSupport or false | ||
| , fetchFromGitHub | ||
| , fetchpatch | ||
| , filelock | ||
| , gtest | ||
| , lib | ||
| , libxml2 | ||
| , lit | ||
| , llvmPackages | ||
| , ncurses | ||
| , pkgsTargetTarget | ||
| , pybind11 | ||
| , pytest | ||
| , pytestCheckHook | ||
| , python | ||
| , pythonRelaxDepsHook | ||
| , torchWithRocm | ||
| , zlib | ||
| }: | ||
|
|
||
| let | ||
| pname = "triton"; | ||
| version = "2.0.0"; | ||
|
|
||
| inherit (cudaPackages) cuda_cudart backendStdenv; | ||
|
|
||
| # A time may come we'll want to be cross-friendly | ||
| # | ||
| # Short explanation: we need pkgsTargetTarget, because we use string | ||
| # interpolation instead of buildInputs. | ||
| # | ||
| # Long explanation: OpenAI/triton downloads and vendors a copy of NVidia's | ||
| # ptxas compiler. We're not running this ptxas on the build machine, but on | ||
| # the user's machine, i.e. our Target platform. The second "Target" in | ||
| # pkgsTargetTarget maybe doesn't matter, because ptxas compiles programs to | ||
| # be executed on the GPU. | ||
| # Cf. https://nixos.org/manual/nixpkgs/unstable/#sec-cross-infra | ||
| ptxas = "${pkgsTargetTarget.cudaPackages.cuda_nvcc}/bin/ptxas"; | ||
|
|
||
| llvm = (llvmPackages.llvm.override { | ||
| llvmTargetsToBuild = [ "NATIVE" "NVPTX" ]; | ||
| # Upstream CI sets these too: | ||
| # targetProjects = [ "mlir" ]; | ||
| extraCMakeFlags = [ | ||
| "-DLLVM_INSTALL_UTILS=ON" | ||
| ]; | ||
| }); | ||
| in | ||
| buildPythonPackage { | ||
| inherit pname version; | ||
|
|
||
| format = "setuptools"; | ||
|
|
||
| src = fetchFromGitHub { | ||
| owner = "openai"; | ||
| repo = pname; | ||
| rev = "v${version}"; | ||
| hash = "sha256-9GZzugab+Pdt74Dj6zjlEzjj4BcJ69rzMJmqcVMxsKU="; | ||
| }; | ||
|
|
||
| patches = [ | ||
| # Prerequisite for llvm15 patch | ||
| (fetchpatch { | ||
| url = "https://github.com/openai/triton/commit/2aba985daaa70234823ea8f1161da938477d3e02.patch"; | ||
| hash = "sha256-LGv0+Ut2WYPC4Ksi4803Hwmhi3FyQOF9zElJc/JCobk="; | ||
| }) | ||
| (fetchpatch { | ||
| url = "https://github.com/openai/triton/commit/e3941f9d09cdd31529ba4a41018cfc0096aafea6.patch"; | ||
| hash = "sha256-A+Gor6qzFlGQhVVhiaaYOzqqx8yO2MdssnQS6TIfUWg="; | ||
| }) | ||
|
|
||
| # Source: https://github.com/openai/triton/commit/fc7a8e35819bda632bdcf1cf75fd9abe4d4e077a.patch | ||
| # The original patch adds ptxas binary, so we include our own clean copy | ||
| # Drop with the next update | ||
| ./llvm15.patch | ||
|
|
||
| # TODO: there have been commits upstream aimed at removing the "torch" | ||
| # circular dependency, but the patches fail to apply on the release | ||
| # revision. Keeping the link for future reference | ||
| # Also cf. https://github.com/openai/triton/issues/1374 | ||
|
|
||
| # (fetchpatch { | ||
| # url = "https://github.com/openai/triton/commit/fc7c0b0e437a191e421faa61494b2ff4870850f1.patch"; | ||
| # hash = "sha256-f0shIqHJkVvuil2Yku7vuqWFn7VCRKFSFjYRlwx25ig="; | ||
| # }) | ||
| ]; | ||
|
|
||
| postPatch = '' | ||
| substituteInPlace python/setup.py \ | ||
| --replace \ | ||
| '= get_thirdparty_packages(triton_cache_path)' \ | ||
| '= os.environ["cmakeFlags"].split()' | ||
| '' | ||
| # Wiring triton=2.0.0 with llcmPackages_rocm.llvm=5.4.3 | ||
| # Revisit when updating either triton or llvm | ||
| + '' | ||
| substituteInPlace CMakeLists.txt \ | ||
| --replace "nvptx" "NVPTX" \ | ||
| --replace "LLVM 11" "LLVM" | ||
| sed -i '/AddMLIR/a set(MLIR_TABLEGEN_EXE "${llvmPackages.mlir}/bin/mlir-tblgen")' CMakeLists.txt | ||
| sed -i '/AddMLIR/a set(MLIR_INCLUDE_DIR ''${MLIR_INCLUDE_DIRS})' CMakeLists.txt | ||
| find -iname '*.td' -exec \ | ||
| sed -i \ | ||
| -e '\|include "mlir/IR/OpBase.td"|a include "mlir/IR/AttrTypeBase.td"' \ | ||
| -e 's|include "mlir/Dialect/StandardOps/IR/Ops.td"|include "mlir/Dialect/Func/IR/FuncOps.td"|' \ | ||
| '{}' ';' | ||
| substituteInPlace unittest/CMakeLists.txt --replace "include(GoogleTest)" "find_package(GTest REQUIRED)" | ||
| sed -i 's/^include.*$//' unittest/CMakeLists.txt | ||
| sed -i '/LINK_LIBS/i NVPTXInfo' lib/Target/PTX/CMakeLists.txt | ||
| sed -i '/LINK_LIBS/i NVPTXCodeGen' lib/Target/PTX/CMakeLists.txt | ||
| '' | ||
| # TritonMLIRIR already links MLIRIR. Not transitive? | ||
| # + '' | ||
| # echo "target_link_libraries(TritonPTX PUBLIC MLIRIR)" >> lib/Target/PTX/CMakeLists.txt | ||
| # '' | ||
| # Already defined in llvm, when built with -DLLVM_INSTALL_UTILS | ||
| + '' | ||
| substituteInPlace bin/CMakeLists.txt \ | ||
| --replace "add_subdirectory(FileCheck)" "" | ||
|
|
||
| rm cmake/FindLLVM.cmake | ||
| '' | ||
| + | ||
| ( | ||
| let | ||
| # Bash was getting weird without linting, | ||
| # but basically upstream contains [cc, ..., "-lcuda", ...] | ||
| # and we replace it with [..., "-L/run/opengl-driver/lib", "-L$stubs", "-lcuda", ...] | ||
| old = [ "-lcuda" ]; | ||
| new = [ "-L${addOpenGLRunpath.driverLink}" ] ++ lib.optionals cudaSupport [ "-L${cuda_cudart}/lib/stubs/" "-lcuda" ]; | ||
|
|
||
| quote = x: ''"${x}"''; | ||
| oldStr = lib.concatMapStringsSep ", " quote old; | ||
| newStr = lib.concatMapStringsSep ", " quote new; | ||
| in | ||
| '' | ||
| substituteInPlace python/triton/compiler.py \ | ||
| --replace '${oldStr}' '${newStr}' | ||
| '' | ||
| ) | ||
| # Triton seems to be looking up cuda.h | ||
| + lib.optionalString cudaSupport '' | ||
| sed -i 's|cu_include_dir = os.path.join.*$|cu_include_dir = "${cuda_cudart}/include"|' python/triton/compiler.py | ||
| ''; | ||
|
|
||
| nativeBuildInputs = [ | ||
| cmake | ||
| pythonRelaxDepsHook | ||
|
|
||
| # Requires torch (circular dependency) and probably needs GPUs: | ||
| # pytestCheckHook | ||
|
|
||
| # Note for future: | ||
| # These *probably* should go in depsTargetTarget | ||
| # ...but we cannot test cross right now anyway | ||
| # because we only support cudaPackages on x86_64-linux atm | ||
| lit | ||
| llvm | ||
| llvmPackages.mlir | ||
| ]; | ||
|
|
||
| buildInputs = [ | ||
| gtest | ||
| libxml2.dev | ||
| ncurses | ||
| pybind11 | ||
| zlib | ||
| ]; | ||
|
|
||
| propagatedBuildInputs = [ | ||
| filelock | ||
| ]; | ||
|
|
||
| preConfigure = '' | ||
| # Upstream's setup.py tries to write cache somewhere in ~/ | ||
| export HOME=$TMPDIR | ||
|
|
||
| # Upstream's github actions patch setup.cfg to write base-dir. May be redundant | ||
| echo " | ||
| [build_ext] | ||
| base-dir=$PWD" >> python/setup.cfg | ||
|
|
||
| # The rest (including buildPhase) is relative to ./python/ | ||
| cd python/ | ||
|
|
||
| # Work around download_and_copy_ptxas() | ||
| dst_cuda="$PWD/triton/third_party/cuda/bin" | ||
| mkdir -p "$dst_cuda" | ||
| '' | ||
| + lib.optionalString (!cudaSupport) '' | ||
| touch $dst_cuda/ptxas | ||
| '' | ||
| + lib.optionalString cudaSupport '' | ||
| ln -s "${ptxas}" "$dst_cuda/ptxas" | ||
|
|
||
| # Avoid GLIBCXX mismatch with other cuda-enabled python packages | ||
| export CC="${backendStdenv.cc}/bin/cc"; | ||
| export CXX="${backendStdenv.cc}/bin/c++"; | ||
| ''; | ||
|
|
||
| # CMake is run by setup.py instead | ||
| dontUseCmakeConfigure = true; | ||
| cmakeFlags = [ | ||
| "-DMLIR_DIR=${llvmPackages.mlir}/lib/cmake/mlir" | ||
| ]; | ||
|
|
||
| postFixup = | ||
| let | ||
| ptxasDestination = "$out/${python.sitePackages}/triton/third_party/cuda/bin/ptxas"; | ||
| in | ||
| # Setuptools (?) strips runpath and +x flags. Let's just restore the symlink | ||
| '' | ||
| rm -f ${ptxasDestination} | ||
| '' + lib.optionalString cudaSupport '' | ||
| ln -s ${ptxas} ${ptxasDestination} | ||
| ''; | ||
|
|
||
| checkInputs = [ | ||
| cmake # ctest | ||
| ]; | ||
| dontUseSetuptoolsCheck = true; | ||
| preCheck = | ||
| # build/temp* refers to build_ext.build_temp (looked up in the build logs) | ||
| '' | ||
| (cd /build/source/python/build/temp* ; ctest) | ||
| '' # For pytestCheckHook | ||
| + '' | ||
| cd test/unit | ||
| ''; | ||
| pythonImportsCheck = [ | ||
| # Circular dependency on torch | ||
| # "triton" | ||
| # "triton.language" | ||
| ]; | ||
|
|
||
| passthru = { | ||
| inherit cudaSupport; | ||
|
|
||
| # Ultimately, torch is our test suite: | ||
| tests = { | ||
| inherit torchWithRocm; | ||
| }; | ||
| }; | ||
|
|
||
| pythonRemoveDeps = [ | ||
| # Circular dependency, cf. https://github.com/openai/triton/issues/1374 | ||
| "torch" | ||
|
|
||
| # CLI tools without dist-info | ||
| "cmake" | ||
| "lit" | ||
| ]; | ||
| meta = with lib; { | ||
| description = "Development repository for the Triton language and compiler"; | ||
| homepage = "https://github.com/openai/triton/"; | ||
| platforms = lib.platforms.unix; | ||
| license = licenses.mit; | ||
| maintainers = with maintainers; [ SomeoneSerge ]; | ||
| }; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you define this as a proper option in the file that describes the config attr? Then we also have a default false
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, so
configalready supports options?!!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking at
pkgs/top-level/config.nixfor the first time now. This is great, I really needed this, ourcudaCapabilitiesandcudaSupportare already a mess, and we still need more controls (virtual vs real architectures, default mkl support)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We'll have a separate PR for
config.nixintegrationThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does since ~4 years but people only really started using it a year ago. Glad that that helps you.