-
Notifications
You must be signed in to change notification settings - Fork 91
nix-builder: switch to manylinux_2_28, dynamically link libstdc++ #558
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| # Nix Builder design | ||
|
|
||
| ## Introduction | ||
|
|
||
| kernel-builder uses a Nix-based builder that orchestrates the build. The Nix | ||
| builder provides: | ||
|
|
||
| - Reproducible evaluation. The same Nix builder version will always produce | ||
| the same derivations (build recipes). | ||
| - Largely reproducible builds by using a build sandbox that only has the | ||
| dependencies specified in a derivation. | ||
| - Seamless creation of different build environments (e.g. different Torch | ||
| and CUDA combinations). | ||
|
|
||
| ## Kernel build steps | ||
|
|
||
| A kernel derivation builds a kernel in the following steps: | ||
|
|
||
| 1. Generate CMake files for the kernel using | ||
| `kernel-builder create-pyproject`. | ||
| 2. Generate Ninja build files using CMake. | ||
| 3. Build the kernel using Ninja. | ||
| 4. Perform various checks on the compiled kernel, such as: | ||
| - Verify that the kernel only uses ABI3/`manylinux_2_28` symbols. | ||
| - Verify that the kernel can be loaded by the `kernels` Python package. | ||
| 5. Strip runpaths (ELF-embedded library directories) from kernel binaries | ||
| to make the kernel distribution-independent. | ||
|
|
||
| ## manylinux_2_28 compatibility | ||
|
|
||
| To achieve `manylinux_2_28` compatibility, kernels are built using a | ||
| toolchain similar to the `manylinux_2_28` Docker images. This toolchain | ||
| is based on the gcc toolsets from AlmaLinux 8. `manylinux_2_28` [uses | ||
| AlmaLinux 8 as its base](https://github.com/pypa/manylinux#manylinux_2_28-almalinux-8-based), | ||
| so we have to compile against the same glibc/libstdc++ versions to | ||
| ensure compatibility. | ||
|
|
||
| We repackage the AlmaLinux 8 toolsets and libstdc++ as Nix derivations (see | ||
| the `nix-builder/packages/manylinux_2_28` source directory). Then we merge | ||
| various toolset packages to an unwrapped gcc that resembles unwrapped gcc in | ||
| nixpkgs. Finally, we wrap binutils and gcc to combine them into a stdenv. | ||
|
|
||
| The stdenv does not reuse glibc from AlmaLinux, since its dynamic loader has | ||
| hardcoded FHS paths (`/lib64` etc.) that are not valid in Nix. Using this | ||
| dynamic loader results in linking errors, since the paths in the dynamic | ||
| loader are used as a last resort (to link glibc libraries). So, instead we | ||
| build our own glibc 2.28 package | ||
| (see `nix-builder/pkgs/manylinux_2_28/stdenv.nix`) and use that. | ||
|
|
||
| ## The package set pattern | ||
|
|
||
| We repackage various existing package sets as Nix derivations. For instance, | ||
|
danieldk marked this conversation as resolved.
|
||
| this is done for ROCm, XPU, and manylinux_2_28 packages. We do this because | ||
| we want these libraries to be as close as what the user would install. This | ||
| avoids compatibility issues between the kernels and the official vendor | ||
| packages. For instance, suppose that we built a ROCm library as a shared | ||
| library and ROCm provides the same library as a static library, then compiled | ||
| kernels could use symbols that cannot be resolved when installing the official | ||
| ROCm packages. Similarly, using the official packages allows us to test | ||
| against the official upstram packages. | ||
|
|
||
| These package sets all follow the same pattern: | ||
|
|
||
| ```nix | ||
| { | ||
| lib, | ||
| callPackage, | ||
| newScope, | ||
| pkgs, | ||
| }: | ||
|
|
||
| { | ||
| packageMetadata, | ||
| }: | ||
|
|
||
| let | ||
| inherit (lib.fixedPoints) extends composeManyExtensions; | ||
|
|
||
| fixedPoint = final: { | ||
| inherit lib; | ||
| }; | ||
| composed = lib.composeManyExtensions [ | ||
| # Base package set. | ||
| (import ./components.nix { inherit packageMetadata; }) | ||
|
|
||
| # Package-specific overrides. | ||
| (import ./overrides.nix) | ||
|
|
||
| # Additional overlays that extend the package set. | ||
| (import ./some-overlay.nix) | ||
| ]; | ||
| in | ||
| lib.makeScope newScope (lib.extends composed fixedPoint) | ||
| ``` | ||
|
|
||
| We use a fixed point to build up the package set as a list of | ||
| [overlays](https://nixos.org/manual/nixpkgs/stable/#sec-overlays-definition). | ||
| This has various benefits. For instance, it allows us to refine the | ||
| package set incrementally and we can refer to the final versions of | ||
| packages in intermediate overlays. | ||
|
|
||
| The package sets all use a similar list of overlays: | ||
|
|
||
| - An initial overlay (`components.nix`) that applies a generic builder | ||
| to the package set metadata. The metadata typically comes from a Yum/DNF | ||
| repository that contains RPM packages.The generic builder will extract the | ||
| RPMs and move binaries, libraries, and headers to the right location. This | ||
| results in a set of Nix derivations that may or may not build. | ||
| - The next overlay (`overrides.nix`) fixes up derivations generated by the | ||
| generic builder in the previous overlay that do not build. Fixing the | ||
| derivations typically consists of adding missing dependencies and changing | ||
| embedded FHS paths to Nix store paths. | ||
| - Additional overlays with derivations that combine outputs from previous | ||
| overlays. One typical example are derivations that construct a full compiler | ||
| toolchain (e.g. `nix-builder/pkgs/manylinux_2_28/gcc-unwrapped.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,16 @@ | ||
| [general] | ||
| name = "cpp20-symbols" | ||
| version = 1 | ||
| license = "Apache-2.0" | ||
| backends = ["cpu"] | ||
|
|
||
| [torch] | ||
| src = [ | ||
| "torch-ext/torch_binding.cpp", | ||
| "torch-ext/torch_binding.h", | ||
| ] | ||
|
|
||
| [kernel.cpp20_symbols_cpu] | ||
| backend = "cpu" | ||
| depends = ["torch"] | ||
| src = ["cpu/cpu.cpp"] |
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,18 @@ | ||
| #include <array> | ||
| #include <charconv> | ||
| #include <stdexcept> | ||
|
|
||
| #include <torch/all.h> | ||
|
|
||
| // std::to_chars(char*, char*, double) is a floating-point overload that | ||
| // requires GLIBCXX_3.4.29, introduced in GCC 11. We use this to verify | ||
| // that manylinux_2_28 kernels build correctly: the Red Hat toolset | ||
| // statically links the newer libstdc++ symbols that exceed the system | ||
| // GLIBCXX_3.4.25 ceiling of AlmaLinux 8 / RHEL 8. | ||
| torch::Tensor float_to_chars(torch::Tensor const &input) { | ||
| std::array<char, 32> buf; | ||
| auto [ptr, ec] = std::to_chars(buf.begin(), buf.end(), input.item<double>()); | ||
| if (ec != std::errc{}) | ||
| throw std::runtime_error("to_chars failed"); | ||
| return input; | ||
| } |
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,17 @@ | ||
| { | ||
| description = "Flake for invalid-cpp-manylinux-symbols kernel"; | ||
|
|
||
| inputs = { | ||
| kernel-builder.url = "path:../../.."; | ||
| }; | ||
|
|
||
| outputs = | ||
| { | ||
| self, | ||
| kernel-builder, | ||
| }: | ||
| kernel-builder.lib.genKernelFlakeOutputs { | ||
| inherit self; | ||
| path = ./.; | ||
| }; | ||
| } |
Empty file.
17 changes: 17 additions & 0 deletions
17
examples/kernels/cpp20-symbols/tests/test_cpp20_symbols.py
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,17 @@ | ||
| import cpp20_symbols | ||
| import pytest | ||
| import torch | ||
|
|
||
|
|
||
| @pytest.mark.kernels_ci | ||
| def test_float_to_chars_runs(): | ||
| x = torch.tensor([3.14], dtype=torch.float64) | ||
| out = cpp20_symbols.float_to_chars(x) | ||
| torch.testing.assert_close(out, x) | ||
|
|
||
|
|
||
| @pytest.mark.kernels_ci | ||
| def test_float_to_chars_float32(): | ||
| x = torch.tensor([2.71828], dtype=torch.float32) | ||
| out = cpp20_symbols.float_to_chars(x) | ||
| torch.testing.assert_close(out, x) |
10 changes: 10 additions & 0 deletions
10
examples/kernels/cpp20-symbols/torch-ext/cpp20_symbols/__init__.py
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,10 @@ | ||
| import torch | ||
|
|
||
| from ._ops import ops | ||
|
|
||
|
|
||
| def float_to_chars(input: torch.Tensor) -> torch.Tensor: | ||
| return ops.float_to_chars(input) | ||
|
|
||
|
|
||
| __all__ = ["float_to_chars"] |
13 changes: 13 additions & 0 deletions
13
examples/kernels/cpp20-symbols/torch-ext/torch_binding.cpp
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,13 @@ | ||
| #include <torch/library.h> | ||
|
|
||
| #include "registration.h" | ||
| #include "torch_binding.h" | ||
|
|
||
| TORCH_LIBRARY_EXPAND(TORCH_EXTENSION_NAME, ops) { | ||
| ops.def("float_to_chars(Tensor input) -> Tensor"); | ||
| #if defined(CPU_KERNEL) | ||
| ops.impl("float_to_chars", torch::kCPU, &float_to_chars); | ||
| #endif | ||
| } | ||
|
|
||
| REGISTER_EXTENSION(TORCH_EXTENSION_NAME) |
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,10 @@ | ||
| #pragma once | ||
|
|
||
| #include <torch/torch.h> | ||
|
|
||
| // Uses std::to_chars for floating-point, which requires GLIBCXX_3.4.29 | ||
| // (introduced in GCC 11). We use this to verify that manylinux_2_28 | ||
| // kernels build correctly: the Red Hat toolset statically links the newer | ||
| // libstdc++ symbols that exceed the system GLIBCXX_3.4.25 ceiling of | ||
| // AlmaLinux 8 / RHEL 8. | ||
| torch::Tensor float_to_chars(torch::Tensor const &input); |
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.