Skip to content

Commit 73c93e0

Browse files
committed
Add some kernel layout checks
- Validate that the kernel does not have a file/directory `torch-ext/<mod_name>/<mod_name>` since we need that directory for the compat module. - Validate that the kernel has `torch-ext/<mod_name>/__init__.py`. This is also verified by get-kernel-check, but that only fires *after* the kernel is already compiled.
1 parent 029516f commit 73c93e0

File tree

6 files changed

+78
-35
lines changed

6 files changed

+78
-35
lines changed

lib/torch-extension/arch.nix

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
cuda_nvcc,
1414
get-kernel-check,
1515
kernel-abi-check,
16+
kernel-layout-check,
1617
ninja,
1718
python3,
1819
remove-bytecode-hook,
@@ -79,7 +80,12 @@ in
7980
stdenv.mkDerivation (prevAttrs: {
8081
name = "${extensionName}-torch-ext";
8182

82-
inherit doAbiCheck nvccThreads src;
83+
inherit
84+
doAbiCheck
85+
extensionName
86+
nvccThreads
87+
src
88+
;
8389

8490
# Generate build files.
8591
postPatch = ''
@@ -123,10 +129,11 @@ stdenv.mkDerivation (prevAttrs: {
123129
'';
124130

125131
nativeBuildInputs = [
126-
kernel-abi-check
127132
cmake
128133
ninja
129134
build2cmake
135+
kernel-abi-check
136+
kernel-layout-check
130137
remove-bytecode-hook
131138
]
132139
++ lib.optionals doGetKernelCheck [
@@ -248,8 +255,6 @@ stdenv.mkDerivation (prevAttrs: {
248255

249256
doInstallCheck = true;
250257

251-
getKernelCheck = extensionName;
252-
253258
# We need access to the host system on Darwin for the Metal compiler.
254259
__noChroot = metalSupport;
255260

lib/torch-extension/no-arch.nix

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
build2cmake,
66
get-kernel-check,
7+
kernel-layout-check,
78
torch,
89
}:
910

@@ -22,14 +23,15 @@
2223
stdenv.mkDerivation (prevAttrs: {
2324
name = "${extensionName}-torch-ext";
2425

25-
inherit src;
26+
inherit extensionName src;
2627

2728
# Add Torch as a dependency, so that devshells for universal kernels
2829
# also get torch as a build input.
2930
buildInputs = [ torch ];
3031

3132
nativeBuildInputs = [
3233
build2cmake
34+
kernel-layout-check
3335
]
3436
++ lib.optionals doGetKernelCheck [
3537
get-kernel-check
@@ -52,6 +54,4 @@ stdenv.mkDerivation (prevAttrs: {
5254
'';
5355

5456
doInstallCheck = true;
55-
56-
getKernelCheck = extensionName;
5757
})

overlay.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ final: prev: {
99

1010
kernel-abi-check = prev.callPackage ./pkgs/kernel-abi-check { };
1111

12+
kernel-layout-check = prev.callPackage ./pkgs/kernel-layout-check { };
13+
1214
rewrite-nix-paths-macho = prev.callPackage ./pkgs/rewrite-nix-paths-macho { };
1315

1416
remove-bytecode-hook = prev.callPackage ./pkgs/remove-bytecode-hook { };

pkgs/get-kernel-check/get-kernel-check-hook.sh

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,39 @@
33
echo "Sourcing get-kernel-check-hook.sh"
44

55
_getKernelCheckHook() {
6-
if [ ! -z "${getKernelCheck}" ]; then
7-
echo "Checking loading kernel with get_kernel"
8-
echo "Check whether the kernel can be loaded with get-kernel: ${getKernelCheck}"
9-
10-
# We strip the full library paths from the extension. Unfortunately,
11-
# in a Nix environment, the library dependencies cannot be found
12-
# anymore. So we have to add the Torch library directory to the
13-
# dynamic linker path to get it to pick it up.
14-
if [ $(uname -s) == "Darwin" ]; then
15-
TORCH_DIR=$(python -c "from pathlib import Path; import torch; print(Path(torch.__file__).parent)")
16-
export DYLD_LIBRARY_PATH="${TORCH_DIR}/lib:${DYLD_LIBRARY_PATH}"
17-
fi
18-
19-
TMPDIR=$(mktemp -d -t test.XXXXXX) || exit 1
20-
trap "rm -rf '$TMPDIR'" EXIT
21-
22-
# Some kernels want to write stuff (especially when they use Triton).
23-
HOME=$(mktemp -d -t test.XXXXXX) || exit 1
24-
trap "rm -rf '$HOME'" EXIT
25-
26-
# Emulate the bundle layout that kernels expects. This even works
27-
# for universal kernels, since kernels checks the non-universal
28-
# path first.
29-
BUILD_VARIANT=$(python -c "from kernels.utils import build_variant; print(build_variant())")
30-
mkdir -p "${TMPDIR}/build"
31-
ln -s "$out" "${TMPDIR}/build/${BUILD_VARIANT}"
32-
33-
python -c "from pathlib import Path; import kernels; kernels.get_local_kernel(Path('${TMPDIR}'), '${getKernelCheck}')"
6+
echo "Checking loading kernel with get_kernel"
7+
8+
if [ -z ${extensionName+x} ]; then
9+
echo "extensionName must be set in derivation"
10+
exit 1
11+
fi
12+
13+
echo "Check whether the kernel can be loaded with get-kernel: ${extensionName}"
14+
15+
# We strip the full library paths from the extension. Unfortunately,
16+
# in a Nix environment, the library dependencies cannot be found
17+
# anymore. So we have to add the Torch library directory to the
18+
# dynamic linker path to get it to pick it up.
19+
if [ $(uname -s) == "Darwin" ]; then
20+
TORCH_DIR=$(python -c "from pathlib import Path; import torch; print(Path(torch.__file__).parent)")
21+
export DYLD_LIBRARY_PATH="${TORCH_DIR}/lib:${DYLD_LIBRARY_PATH}"
3422
fi
23+
24+
TMPDIR=$(mktemp -d -t test.XXXXXX) || exit 1
25+
trap "rm -rf '$TMPDIR'" EXIT
26+
27+
# Some kernels want to write stuff (especially when they use Triton).
28+
HOME=$(mktemp -d -t test.XXXXXX) || exit 1
29+
trap "rm -rf '$HOME'" EXIT
30+
31+
# Emulate the bundle layout that kernels expects. This even works
32+
# for universal kernels, since kernels checks the non-universal
33+
# path first.
34+
BUILD_VARIANT=$(python -c "from kernels.utils import build_variant; print(build_variant())")
35+
mkdir -p "${TMPDIR}/build"
36+
ln -s "$out" "${TMPDIR}/build/${BUILD_VARIANT}"
37+
38+
python -c "from pathlib import Path; import kernels; kernels.get_local_kernel(Path('${TMPDIR}'), '${extensionName}')"
3539
}
3640

3741
postInstallCheckHooks+=(_getKernelCheckHook)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{ makeSetupHook, python3 }:
2+
3+
makeSetupHook {
4+
name = "kernel-layout-check-hook";
5+
} ./kernel-layout-check-hook.sh
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/sh
2+
3+
echo "Sourcing kernel-layout-check-hook.sh"
4+
5+
kernelLayoutCheckHook() {
6+
echo "Checking kernel layout"
7+
8+
if [ -z ${extensionName+x} ]; then
9+
echo "extensionName must be set in derivation"
10+
exit 1
11+
fi
12+
13+
if [ ! -f source/torch-ext/${extensionName}/__init__.py ]; then
14+
echo "Python module at source/torch-ext/${extensionName} must contain __init__.py"
15+
exit 1
16+
fi
17+
18+
# TODO: remove once the old location is removed from kernels.
19+
if [ -e source/torch-ext/${extensionName}/${extensionName} ]; then
20+
echo "Python module at source/torch-ext/${extensionName} must not have ${extensionName} file or directory."
21+
exit 1
22+
fi
23+
}
24+
25+
if [ -z "${dontCheckLayout-}" ]; then
26+
postUnpackHooks+=(kernelLayoutCheckHook)
27+
fi

0 commit comments

Comments
 (0)